summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/src/thread_service_v1.cpp
blob: ee81ba31018133fcd94472aeba6d48c04a3cdfdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
 * Copyright (C) 2019-2020 Codership Oy <info@codership.com>
 *
 * This file is part of wsrep-lib.
 *
 * Wsrep-lib is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * Wsrep-lib is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with wsrep-lib.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "thread_service_v1.hpp"
#include "service_helpers.hpp"

#include "wsrep/thread_service.hpp"
#include "wsrep/logger.hpp"
#include "v26/wsrep_thread_service.h"

#include <cassert>
#include <dlfcn.h>
#include <cerrno>

namespace wsrep_thread_service_v1
{
    //
    // Thread service callbacks
    //

    // Pointer to thread service implementation provided by
    // the application.
    static wsrep::thread_service* thread_service_impl{ 0 };
    static std::atomic<size_t> use_count;

    static const wsrep_thread_key_t* thread_key_create_cb(const char* name)
    {
        assert(thread_service_impl);
        return reinterpret_cast<const wsrep_thread_key_t*>(
            thread_service_impl->create_thread_key(name));
        ;
    }

    static int thread_create_cb(const wsrep_thread_key_t* key,
                                wsrep_thread_t** thread,
                                void* (*fn)(void*), void* args)
    {
        assert(thread_service_impl);
        return thread_service_impl->create_thread(
            reinterpret_cast<const wsrep::thread_service::thread_key*>(key),
            reinterpret_cast<wsrep::thread_service::thread**>(thread), fn,
            args);
    }

    int thread_detach_cb(wsrep_thread_t* thread)
    {
        assert(thread_service_impl);
        return thread_service_impl->detach(
            reinterpret_cast<wsrep::thread_service::thread*>(thread));
    }

    int thread_equal_cb(wsrep_thread_t* thread_1, wsrep_thread_t* thread_2)
    {
        assert(thread_service_impl);
        return thread_service_impl->equal(
            reinterpret_cast<wsrep::thread_service::thread*>(thread_1),
            reinterpret_cast<wsrep::thread_service::thread*>(thread_2));
    }

    __attribute__((noreturn))
    void thread_exit_cb(wsrep_thread_t* thread, void* retval)
    {
        assert(thread_service_impl);
        thread_service_impl->exit(
            reinterpret_cast<wsrep::thread_service::thread*>(thread), retval);
        throw; // Implementation broke the contract and returned.
    }

    int thread_join_cb(wsrep_thread_t* thread, void** retval)
    {
        assert(thread_service_impl);
        return thread_service_impl->join(
            reinterpret_cast<wsrep::thread_service::thread*>(thread), retval);
    }

    wsrep_thread_t* thread_self_cb(void)
    {
        assert(thread_service_impl);
        return reinterpret_cast<wsrep_thread_t*>(thread_service_impl->self());
    }

    int thread_setschedparam_cb(wsrep_thread_t* thread, int policy,
                                const struct sched_param* sp)
    {
        assert(thread_service_impl);
        return thread_service_impl->setschedparam(
            reinterpret_cast<wsrep::thread_service::thread*>(thread),
            policy, sp);
    }

    int thread_getschedparam_cb(wsrep_thread_t* thread, int* policy,
                                struct sched_param* sp)
    {
        assert(thread_service_impl);
        return thread_service_impl->getschedparam(
            reinterpret_cast<wsrep::thread_service::thread*>(thread), policy,
            sp);
    }

    const wsrep_mutex_key_t* mutex_key_create_cb(const char* name)
    {
        assert(thread_service_impl);
        return reinterpret_cast<const wsrep_mutex_key_t*>(
            thread_service_impl->create_mutex_key(name));
    }

    wsrep_mutex_t* mutex_init_cb(const wsrep_mutex_key_t* key, void* memblock,
                                 size_t memblock_size)
    {
        assert(thread_service_impl);
        return reinterpret_cast<wsrep_mutex_t*>(
            thread_service_impl->init_mutex(
                reinterpret_cast<const wsrep::thread_service::mutex_key*>(key),
                memblock, memblock_size));
    }

    int mutex_destroy_cb(wsrep_mutex_t* mutex)
    {
        assert(thread_service_impl);
        return thread_service_impl->destroy(
            reinterpret_cast<wsrep::thread_service::mutex*>(mutex));
    }
    int mutex_lock_cb(wsrep_mutex_t* mutex)
    {
        assert(thread_service_impl);
        return thread_service_impl->lock(
            reinterpret_cast<wsrep::thread_service::mutex*>(mutex));
    }

    int mutex_trylock_cb(wsrep_mutex_t* mutex)
    {
        assert(thread_service_impl);
        return thread_service_impl->trylock(
            reinterpret_cast<wsrep::thread_service::mutex*>(mutex));
    }

    int mutex_unlock_cb(wsrep_mutex_t* mutex)
    {
        assert(thread_service_impl);
        return thread_service_impl->unlock(
            reinterpret_cast<wsrep::thread_service::mutex*>(mutex));
    }

    const wsrep_cond_key_t* cond_key_create_cb(const char* name)
    {
        assert(thread_service_impl);
        return reinterpret_cast<const wsrep_cond_key_t*>(
            thread_service_impl->create_cond_key(name));
    }

    wsrep_cond_t* cond_init_cb(const wsrep_cond_key_t* key, void* memblock,
                               size_t memblock_size)
    {
        assert(thread_service_impl);
        return reinterpret_cast<wsrep_cond_t*>(thread_service_impl->init_cond(
            reinterpret_cast<const wsrep::thread_service::cond_key*>(key),
            memblock, memblock_size));
    }

    int cond_destroy_cb(wsrep_cond_t* cond)
    {
        assert(thread_service_impl);
        return thread_service_impl->destroy(
            reinterpret_cast<wsrep::thread_service::cond*>(cond));
    }

    int cond_wait_cb(wsrep_cond_t* cond, wsrep_mutex_t* mutex)
    {
        assert(thread_service_impl);
        return thread_service_impl->wait(
            reinterpret_cast<wsrep::thread_service::cond*>(cond),
            reinterpret_cast<wsrep::thread_service::mutex*>(mutex));
    }

    int cond_timedwait_cb(wsrep_cond_t* cond, wsrep_mutex_t* mutex,
                          const struct timespec* ts)
    {
        assert(thread_service_impl);
        return thread_service_impl->timedwait(
            reinterpret_cast<wsrep::thread_service::cond*>(cond),
            reinterpret_cast<wsrep::thread_service::mutex*>(mutex), ts);
    }

    int cond_signal_cb(wsrep_cond_t* cond)
    {
        assert(thread_service_impl);
        return thread_service_impl->signal(
            reinterpret_cast<wsrep::thread_service::cond*>(cond));
    }

    int cond_broadcast_cb(wsrep_cond_t* cond)
    {
        assert(thread_service_impl);
        return thread_service_impl->broadcast(
            reinterpret_cast<wsrep::thread_service::cond*>(cond));
    }

    static wsrep_thread_service_v1_t thread_service_callbacks
        = { thread_key_create_cb,
            thread_create_cb,
            thread_detach_cb,
            thread_equal_cb,
            thread_exit_cb,
            thread_join_cb,
            thread_self_cb,
            thread_setschedparam_cb,
            thread_getschedparam_cb,
            mutex_key_create_cb,
            mutex_init_cb,
            mutex_destroy_cb,
            mutex_lock_cb,
            mutex_trylock_cb,
            mutex_unlock_cb,
            cond_key_create_cb,
            cond_init_cb,
            cond_destroy_cb,
            cond_wait_cb,
            cond_timedwait_cb,
            cond_signal_cb,
            cond_broadcast_cb };
}

int wsrep::thread_service_v1_probe(void* dlh)
{
    typedef int (*init_fn)(wsrep_thread_service_v1_t*);
    typedef void (*deinit_fn)();
    if (wsrep_impl::service_probe<init_fn>(
            dlh, WSREP_THREAD_SERVICE_INIT_FUNC_V1, "thread service v1") ||
        wsrep_impl::service_probe<deinit_fn>(
            dlh, WSREP_THREAD_SERVICE_DEINIT_FUNC_V1, "thread service v1"))
    {
        wsrep::log_warning() << "Provider does not support thread service v1";
        return 1;
    }
    return 0;
}

int wsrep::thread_service_v1_init(void* dlh,
                                  wsrep::thread_service* thread_service)
{
    if (not (dlh && thread_service)) return EINVAL;
    typedef int (*init_fn)(wsrep_thread_service_v1_t*);
    wsrep_thread_service_v1::thread_service_impl = thread_service;
    int ret(0);
    if ((ret = wsrep_impl::service_init<init_fn>(
             dlh, WSREP_THREAD_SERVICE_INIT_FUNC_V1,
             &wsrep_thread_service_v1::thread_service_callbacks,
             "thread service v1")))
    {
        wsrep_thread_service_v1::thread_service_impl = 0;
    }
    else
    {
        ++wsrep_thread_service_v1::use_count;
    }
    return ret;
}

void wsrep::thread_service_v1_deinit(void* dlh)
{
    typedef int (*deinit_fn)();
    wsrep_impl::service_deinit<deinit_fn>(
        dlh, WSREP_THREAD_SERVICE_DEINIT_FUNC_V1, "thread service v1");
    --wsrep_thread_service_v1::use_count;
    if (wsrep_thread_service_v1::use_count == 0)
    {
        wsrep_thread_service_v1::thread_service_impl = 0;
    }
}