summaryrefslogtreecommitdiffstats
path: root/wsrep-lib/wsrep-API/v26/wsrep_thread_service.h
blob: 956751aa1f87eee968f8cd7e4f0a755be6f4bf72 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*
 * Copyright (C) 2019 Codership Oy <info@codership.com>
 *
 * This file is part of wsrep-API.
 *
 * Wsrep-API 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-API 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-API.  If not, see <https://www.gnu.org/licenses/>.
 */

/** @file wsrep_thread_service.h
 *
 * Service interface for threads, mutexes and condition variables.
 * The application which may provide callbacks to routines which will
 * be used to manage lifetime and use threads and sycnronization primitives.
 *
 * The type tags and interface methods are loosely modeled after POSIX
 * threading interface.
 *
 * The application must either none or all of the callbacks defined in
 * wsrep_thread_service structure which is defined below.
 *
 * The error codes returned by the callbacks are generally assumed to
 * the system error numbers defined in errno.h unless stated otherwise.
 *
 * The provider must implement and export the following functions
 * to provide initialization point for the service implementation:
 *
 * Version 1:
 *   int wsrep_init_thread_service_v1(wsrep_thread_service_v1_t*)
 *   void wsrep_deinit_thread_service_v1().
 *
 * The application defined implementation must be initialized before
 * calling the provider initialization function via wsrep->init(). The
 * deinitialization must be done via deinit function after the
 * provider side resources have been released via wsrep->free().
 */

#ifndef WSREP_THREAD_SERVICE_H
#define WSREP_THREAD_SERVICE_H

#include <stddef.h> /* size_t */

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

    /* Forward declarations */
    struct timespec;
    struct sched_param;

    /** Thread type tags */
    typedef struct wsrep_thread_key_st wsrep_thread_key_t;
    typedef struct wsrep_thread_st wsrep_thread_t;
    /** Mutex type tags */
    typedef struct wsrep_mutex_key_st wsrep_mutex_key_t;
    typedef struct wsrep_mutex_st wsrep_mutex_t;
    /** Condition variable tags */
    typedef struct wsrep_cond_key_st wsrep_cond_key_t;
    typedef struct wsrep_cond_st wsrep_cond_t;

    /**
     * Create key for a thread with a name. This key object will be passed
     * to thread creation and destrunction notification callbacks.
     *
     * @param name Name of the thread.
     */
    typedef const wsrep_thread_key_t* (*wsrep_thread_key_create_cb_t)(
        const char* name);

    /**
     * Create a new thread.
     *
     * @param[out] thread Newly allocated thread.
     * @param key Key created by wsrep_thread_key_create_cb_t
     * @param start_fn Pointer to start routine
     * @param arg Argument for start_fn
     *
     * @return Zero in case of success, non-zero error code in case of failure.
     */
    typedef int (*wsrep_thread_create_cb_t)(const wsrep_thread_key_t* key,
                                            wsrep_thread_t** thread,
                                            void* (*start_fn)(void*),
                                            void* arg);

    /**
     * Detach a thread.
     *
     * @param thread Thread to be detached.
     *
     * @return Zero in case of error, non-zero error code in case of failure.
     */
    typedef int (*wsrep_thread_detach_cb_t)(wsrep_thread_t* thread);

    /**
     * Compare two threads for equality.
     *
     * @params t1, t2 Threads to be compared.
     *
     * @return Non-zero value if threads are equal, zero otherwise.
     */
    typedef int (*wsrep_thread_equal_cb_t)(wsrep_thread_t* t1,
                                           wsrep_thread_t* t2);

    /**
     * Terminate the calling thread.
     *
     * @param thread Pointer to thread.
     * @param retval Pointer to return value.
     *
     * This function does not return.
     */
    typedef void __attribute__((noreturn)) (*wsrep_thread_exit_cb_t)(
        wsrep_thread_t* thread, void* retval);

    /**
     * Join a thread. Trying to join detached thread may cause undefined
     * behavior.
     *
     * @param thread Thread to be joined.
     * @param[out] retval Return value from the thread wthat was joined.
     *
     * @return Zero in case of success, non-zero error code in case of error.
     */
    typedef int (*wsrep_thread_join_cb_t)(wsrep_thread_t* thread,
                                          void** retval);

    /**
     * Return a pointer to the wsrep_thread_t of the calling thread.
     *
     * @return Pointer to wsrep_thread_t associated with current thread.
     */
    typedef wsrep_thread_t* (*wsrep_thread_self_cb_t)(void);

    /**
     * Set the scheduling policy for the thread.
     *
     * @param thread Thread for which sceduing policy should be changed.
     * @param policy New scheduling policy for the thread.
     * @param param New scheduling parameters for the thread.
     */
    typedef int (*wsrep_thread_setschedparam_cb_t)(
        wsrep_thread_t* thread, int policy, const struct sched_param* param);

    /**
     * Get the current scheduling policy for the thread.
     *
     * @param thread Thread.
     * @param policy Pointer to location where the scheduling policy will
     *               will be stored in.
     * @param Param Pointer to location where the current scheduling
     *              parameters will be stored.
     */
    typedef int (*wsrep_thread_getschedparam_cb_t)(wsrep_thread_t* thread,
                                                   int* policy,
                                                   struct sched_param* param);
    /**
     * Create key for a mutex with a name. This key object must be passed
     * to mutex creation callback.
     *
     * @param name Name of the mutex.
     *
     * @return Const pointer to mutex key.
     */
    typedef const wsrep_mutex_key_t* (*wsrep_mutex_key_create_cb_t)(
        const char* name);

    /**
     * Create a mutex.
     *
     * @param key Mutex key obtained via wsrep_mutex_key_create_cb call.
     * @param memblock Optional memory block allocated by the provider
     *                 which can be used by the implementation to store
     *                 the mutex.
     * @param memblock_size Size of the optional memory block.
     *
     * @return Pointer to wsrep_mutex_t object or NULL in case of failure.
     */
    typedef wsrep_mutex_t* (*wsrep_mutex_init_cb_t)(
        const wsrep_mutex_key_t* key, void* memblock, size_t memblock_size);

    /**
     * Destroy a mutex. This call must consume the mutex object.
     *
     * @param mutex Mutex to be destroyed.
     */
    typedef int (*wsrep_mutex_destroy_cb_t)(wsrep_mutex_t* mutex);

    /**
     * Lock a mutex.
     *
     * @param mutex Mutex to be locked.
     *
     * @return Zero on success, non-zero error code on error.
     */
    typedef int (*wsrep_mutex_lock_cb_t)(wsrep_mutex_t* mutex);

    /**
     * Try to lock a mutex.
     *
     * @param Mutex to be locked.
     *
     * @return Zero if mutex was successfully locked.
     * @return EBUSY if the mutex could not be acquired because it was already
     *         locked.
     * @return Non-zero error code on any other error.
     */
    typedef int (*wsrep_mutex_trylock_cb_t)(wsrep_mutex_t* mutex);

    /**
     * Unlock a mutex.
     *
     * @param mutex Mutex to be unlocked.
     *
     * @return Zero on success, non-zero on error.
     */
    typedef int (*wsrep_mutex_unlock_cb_t)(wsrep_mutex_t* mutex);

    /**
     * Create key for a condition variable with a name. This key
     * must be passed to wsrep_cond_create_cb when creating a new
     * condition variable.
     *
     * @param name Name of the condition variable.
     *
     * @return Allocated key object.
     */
    typedef const wsrep_cond_key_t* (*wsrep_cond_key_create_cb_t)(
        const char* name);

    /**
     * Create a new condition variable.
     *
     * @param key Const pointer to key object created by
     * wsrep_cond_key_create_cb.
     * @param memblock Optional memory block allocated by the provider
     *                 which can be used by the implementation to store
     *                 the mutex.
     * @param memblock_size Size of the optional memory block.
     *
     * @return Pointer to new condition variable.
     */
    typedef wsrep_cond_t* (*wsrep_cond_init_cb_t)(const wsrep_cond_key_t* key,
                                                  void* memblock,
                                                  size_t memblock_size);

    /**
     * Destroy a condition variable. This call must consume the condition
     * variable object.
     *
     * @param cond Condition variable to be destroyed.
     *
     * @return Zero on success, non-zero on error.
     */
    typedef int (*wsrep_cond_destroy_cb_t)(wsrep_cond_t* cond);

    /**
     * Wait for condition.
     *
     * @param cond Condition variable to wait for.
     * @param mutex Mutex associated to the condition variable. The mutex
     *              may be unlocked for the duration of the wait.
     *
     * @return Zero on success, non-zero on error.
     */
    typedef int (*wsrep_cond_wait_cb_t)(wsrep_cond_t* cond,
                                        wsrep_mutex_t* mutex);

    /**
     * Perform timed wait on condition.
     *
     * @param cond Condition to wait for.
     * @param mutex Mutex associated to the condition variable. The mutex
     *              may be unlocked for the duration of the wait.
     * @param wait_until System time to wait until before returning from the
     *                   the timed wait.
     *
     * @return Zero on success.
     * @return ETIMEDOUT if the time specified by wait_until has passed.
     * @return Non-zero error code on other error.
     */
    typedef int (*wsrep_cond_timedwait_cb_t)(wsrep_cond_t* cond,
                                             wsrep_mutex_t* mutex,
                                             const struct timespec* wait_until);

    /**
     * Signal a condition variable. This will wake up at least one of
     * the threads which is waiting for the condition.
     *
     * @param cond Condition variable to signal.
     *
     * @return Zero on success, non-zero on failure.
     */
    typedef int (*wsrep_cond_signal_cb_t)(wsrep_cond_t* cond);

    /**
     * Broadcast a signal to condition variable. This will wake up
     * all the threads which are currently waiting on condition variable.
     *
     * @param cond Condition variable to broadcast the signal to.
     *
     * @return Zero on success, non-zero on failure.
     */
    typedef int (*wsrep_cond_broadcast_cb_t)(wsrep_cond_t* cond);

    typedef struct wsrep_thread_service_v1_st
    {
        /* Threads */
        wsrep_thread_key_create_cb_t thread_key_create_cb;
        wsrep_thread_create_cb_t thread_create_cb;
        wsrep_thread_detach_cb_t thread_detach_cb;
        wsrep_thread_equal_cb_t thread_equal_cb;
        wsrep_thread_exit_cb_t thread_exit_cb;
        wsrep_thread_join_cb_t thread_join_cb;
        wsrep_thread_self_cb_t thread_self_cb;
        wsrep_thread_setschedparam_cb_t thread_setschedparam_cb;
        wsrep_thread_getschedparam_cb_t thread_getschedparam_cb;
        /* Mutexes */
        wsrep_mutex_key_create_cb_t mutex_key_create_cb;
        wsrep_mutex_init_cb_t mutex_init_cb;
        wsrep_mutex_destroy_cb_t mutex_destroy_cb;
        wsrep_mutex_lock_cb_t mutex_lock_cb;
        wsrep_mutex_trylock_cb_t mutex_trylock_cb;
        wsrep_mutex_unlock_cb_t mutex_unlock_cb;
        /* Condition variables */
        wsrep_cond_key_create_cb_t cond_key_create_cb;
        wsrep_cond_init_cb_t cond_init_cb;
        wsrep_cond_destroy_cb_t cond_destroy_cb;
        wsrep_cond_wait_cb_t cond_wait_cb;
        wsrep_cond_timedwait_cb_t cond_timedwait_cb;
        wsrep_cond_signal_cb_t cond_signal_cb;
        wsrep_cond_broadcast_cb_t cond_broadcast_cb;
    } wsrep_thread_service_v1_t;

#ifdef __cplusplus
}

#define WSREP_THREAD_SERVICE_INIT_FUNC_V1 "wsrep_init_thread_service_v1"
#define WSREP_THREAD_SERVICE_DEINIT_FUNC_V1 "wsrep_deinit_thread_service_v1"

/* For backwards compatibility. */
#define WSREP_THREAD_SERVICE_INIT_FUNC WSREP_THREAD_SERVICE_INIT_FUNC_V1

#endif /* __cplusplus */
#endif /* WSREP_THREAD_SERVICE_H */