]> git.proxmox.com Git - mirror_ovs.git/blob - lib/ovs-thread.h
Use "error-checking" mutexes in place of other kinds wherever possible.
[mirror_ovs.git] / lib / ovs-thread.h
1 /*
2 * Copyright (c) 2013 Nicira, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef OVS_THREAD_H
18 #define OVS_THREAD_H 1
19
20 #include <pthread.h>
21 #include <stddef.h>
22 #include <sys/types.h>
23 #include "ovs-atomic.h"
24 #include "util.h"
25
26
27 /* Mutex. */
28 struct OVS_LOCKABLE ovs_mutex {
29 pthread_mutex_t lock;
30 const char *where;
31 };
32
33 /* "struct ovs_mutex" initializer. */
34 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
35 #define OVS_MUTEX_INITIALIZER { PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, NULL }
36 #else
37 #define OVS_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, NULL }
38 #endif
39
40 /* ovs_mutex functions analogous to pthread_mutex_*() functions.
41 *
42 * Most of these functions abort the process with an error message on any
43 * error. ovs_mutex_trylock() is an exception: it passes through a 0 or EBUSY
44 * return value to the caller and aborts on any other error. */
45 void ovs_mutex_init(const struct ovs_mutex *);
46 void ovs_mutex_init_recursive(const struct ovs_mutex *);
47 void ovs_mutex_destroy(const struct ovs_mutex *);
48 void ovs_mutex_unlock(const struct ovs_mutex *mutex) OVS_RELEASES(mutex);
49 void ovs_mutex_lock_at(const struct ovs_mutex *mutex, const char *where)
50 OVS_ACQUIRES(mutex);
51 #define ovs_mutex_lock(mutex) \
52 ovs_mutex_lock_at(mutex, SOURCE_LOCATOR)
53
54 int ovs_mutex_trylock_at(const struct ovs_mutex *mutex, const char *where)
55 OVS_TRY_LOCK(0, mutex);
56 #define ovs_mutex_trylock(mutex) \
57 ovs_mutex_trylock_at(mutex, SOURCE_LOCATOR)
58
59 void ovs_mutex_cond_wait(pthread_cond_t *, const struct ovs_mutex *);
60
61 /* Wrappers for pthread_mutex_*() that abort the process on any error.
62 * This is still needed when ovs-atomic-pthreads.h is used. */
63 void xpthread_mutex_lock(pthread_mutex_t *mutex);
64 void xpthread_mutex_unlock(pthread_mutex_t *mutex);
65
66 /* Wrappers for pthread_mutexattr_*() that abort the process on any error. */
67 void xpthread_mutexattr_init(pthread_mutexattr_t *);
68 void xpthread_mutexattr_destroy(pthread_mutexattr_t *);
69 void xpthread_mutexattr_settype(pthread_mutexattr_t *, int type);
70 void xpthread_mutexattr_gettype(pthread_mutexattr_t *, int *typep);
71
72 /* Read-write lock. */
73 struct OVS_LOCKABLE ovs_rwlock {
74 pthread_rwlock_t lock;
75 const char *where;
76 };
77
78 /* Initializer. */
79 #define OVS_RWLOCK_INITIALIZER { PTHREAD_RWLOCK_INITIALIZER, NULL }
80
81 /* ovs_rwlock functions analogous to pthread_rwlock_*() functions.
82 *
83 * Most of these functions abort the process with an error message on any
84 * error. The "trylock" functions are exception: they pass through a 0 or
85 * EBUSY return value to the caller and abort on any other error. */
86 void ovs_rwlock_init(const struct ovs_rwlock *);
87 void ovs_rwlock_destroy(const struct ovs_rwlock *);
88 void ovs_rwlock_unlock(const struct ovs_rwlock *rwlock) OVS_RELEASES(rwlock);
89
90 void ovs_rwlock_wrlock_at(const struct ovs_rwlock *rwlock, const char *where)
91 OVS_ACQ_WRLOCK(rwlock);
92 #define ovs_rwlock_wrlock(rwlock) \
93 ovs_rwlock_wrlock_at(rwlock, SOURCE_LOCATOR)
94
95 int ovs_rwlock_trywrlock_at(const struct ovs_rwlock *rwlock, const char *where)
96 OVS_TRY_WRLOCK(0, rwlock);
97 #define ovs_rwlock_trywrlock(rwlock) \
98 ovs_rwlock_trywrlock_at(rwlock, SOURCE_LOCATOR)
99
100 void ovs_rwlock_rdlock_at(const struct ovs_rwlock *rwlock, const char *where)
101 OVS_ACQ_RDLOCK(rwlock);
102 #define ovs_rwlock_rdlock(rwlock) \
103 ovs_rwlock_rdlock_at(rwlock, SOURCE_LOCATOR)
104
105 int ovs_rwlock_tryrdlock_at(const struct ovs_rwlock *rwlock, const char *where)
106 OVS_TRY_RDLOCK(0, rwlock);
107 #define ovs_rwlock_tryrdlock(rwlock) \
108 ovs_rwlock_tryrdlock_at(rwlock, SOURCE_LOCATOR)
109
110 /* Wrappers for xpthread_cond_*() that abort the process on any error.
111 *
112 * Use ovs_mutex_cond_wait() to wait for a condition. */
113 void xpthread_cond_init(pthread_cond_t *, pthread_condattr_t *);
114 void xpthread_cond_destroy(pthread_cond_t *);
115 void xpthread_cond_signal(pthread_cond_t *);
116 void xpthread_cond_broadcast(pthread_cond_t *);
117
118 #ifdef __CHECKER__
119 /* Replace these functions by the macros already defined in the <pthread.h>
120 * annotations, because the macro definitions have correct semantics for the
121 * conditional acquisition that can't be captured in a function annotation.
122 * The difference in semantics from pthread_*() to xpthread_*() does not matter
123 * because sparse is not a compiler. */
124 #define xpthread_mutex_trylock pthread_mutex_trylock
125 #define xpthread_rwlock_tryrdlock pthread_rwlock_tryrdlock
126 #define xpthread_rwlock_trywrlock pthread_rwlock_trywrlock
127 #endif
128
129 void xpthread_key_create(pthread_key_t *, void (*destructor)(void *));
130 void xpthread_setspecific(pthread_key_t, const void *);
131
132 void xpthread_create(pthread_t *, pthread_attr_t *, void *(*)(void *), void *);
133 void xpthread_join(pthread_t, void **);
134 \f
135 /* Per-thread data.
136 *
137 * Multiple forms of per-thread data exist, each with its own pluses and
138 * minuses:
139 *
140 * - POSIX per-thread data via pthread_key_t is portable to any pthreads
141 * implementation, and allows a destructor function to be defined. It
142 * only (directly) supports per-thread pointers, which are always
143 * initialized to NULL. It requires once-only allocation of a
144 * pthread_key_t value. It is relatively slow.
145 *
146 * - The thread_local feature newly defined in C11 <threads.h> works with
147 * any data type and initializer, and it is fast. thread_local does not
148 * require once-only initialization like pthread_key_t. C11 does not
149 * define what happens if one attempts to access a thread_local object
150 * from a thread other than the one to which that object belongs. There
151 * is no provision to call a user-specified destructor when a thread
152 * ends.
153 *
154 * - The __thread keyword is a GCC extension similar to thread_local but
155 * with a longer history. __thread is not portable to every GCC version
156 * or environment. __thread does not restrict the use of a thread-local
157 * object outside its own thread.
158 *
159 * Here's a handy summary:
160 *
161 * pthread_key_t thread_local __thread
162 * ------------- ------------ -------------
163 * portability high low medium
164 * speed low high high
165 * supports destructors? yes no no
166 * needs key allocation? yes no no
167 * arbitrary initializer? no yes yes
168 * cross-thread access? yes no yes
169 */
170
171 /* For static data, use this macro in a source file:
172 *
173 * DEFINE_STATIC_PER_THREAD_DATA(TYPE, NAME, INITIALIZER).
174 *
175 * For global data, "declare" the data in the header and "define" it in
176 * the source file, with:
177 *
178 * DECLARE_EXTERN_PER_THREAD_DATA(TYPE, NAME).
179 * DEFINE_EXTERN_PER_THREAD_DATA(NAME, INITIALIZER).
180 *
181 * One should prefer to use POSIX per-thread data, via pthread_key_t, when its
182 * performance is acceptable, because of its portability (see the table above).
183 * This macro is an alternatives that takes advantage of thread_local (and
184 * __thread), for its performance, when it is available, and falls back to
185 * POSIX per-thread data otherwise.
186 *
187 * Defines per-thread variable NAME with the given TYPE, initialized to
188 * INITIALIZER (which must be valid as an initializer for a variable with
189 * static lifetime).
190 *
191 * The public interface to the variable is:
192 *
193 * TYPE *NAME_get(void)
194 * TYPE *NAME_get_unsafe(void)
195 *
196 * Returns the address of this thread's instance of NAME.
197 *
198 * Use NAME_get() in a context where this might be the first use of the
199 * per-thread variable in the program. Use NAME_get_unsafe(), which
200 * avoids a conditional test and is thus slightly faster, in a context
201 * where one knows that NAME_get() has already been called previously.
202 *
203 * There is no "NAME_set()" (or "NAME_set_unsafe()") function. To set the
204 * value of the per-thread variable, dereference the pointer returned by
205 * TYPE_get() or TYPE_get_unsafe(), e.g. *TYPE_get() = 0.
206 */
207 #if HAVE_THREAD_LOCAL || HAVE___THREAD
208
209 #if HAVE_THREAD_LOCAL
210 #include <threads.h>
211 #elif HAVE___THREAD
212 #define thread_local __thread
213 #else
214 #error
215 #endif
216
217 #define DEFINE_STATIC_PER_THREAD_DATA(TYPE, NAME, ...) \
218 typedef TYPE NAME##_type; \
219 \
220 static NAME##_type * \
221 NAME##_get_unsafe(void) \
222 { \
223 static thread_local NAME##_type var = __VA_ARGS__; \
224 return &var; \
225 } \
226 \
227 static NAME##_type * \
228 NAME##_get(void) \
229 { \
230 return NAME##_get_unsafe(); \
231 }
232 #define DECLARE_EXTERN_PER_THREAD_DATA(TYPE, NAME) \
233 typedef TYPE NAME##_type; \
234 extern thread_local NAME##_type NAME##_var; \
235 \
236 static inline NAME##_type * \
237 NAME##_get_unsafe(void) \
238 { \
239 return &NAME##_var; \
240 } \
241 \
242 static inline NAME##_type * \
243 NAME##_get(void) \
244 { \
245 return NAME##_get_unsafe(); \
246 }
247 #define DEFINE_EXTERN_PER_THREAD_DATA(NAME, ...) \
248 thread_local NAME##_type NAME##_var = __VA_ARGS__;
249 #else /* no C implementation support for thread-local storage */
250 #define DEFINE_STATIC_PER_THREAD_DATA(TYPE, NAME, ...) \
251 typedef TYPE NAME##_type; \
252 static pthread_key_t NAME##_key; \
253 \
254 static NAME##_type * \
255 NAME##_get_unsafe(void) \
256 { \
257 return pthread_getspecific(NAME##_key); \
258 } \
259 \
260 static void \
261 NAME##_once_init(void) \
262 { \
263 if (pthread_key_create(&NAME##_key, free)) { \
264 abort(); \
265 } \
266 } \
267 \
268 static NAME##_type * \
269 NAME##_get(void) \
270 { \
271 static pthread_once_t once = PTHREAD_ONCE_INIT; \
272 NAME##_type *value; \
273 \
274 pthread_once(&once, NAME##_once_init); \
275 value = NAME##_get_unsafe(); \
276 if (!value) { \
277 static const NAME##_type initial_value = __VA_ARGS__; \
278 \
279 value = xmalloc(sizeof *value); \
280 *value = initial_value; \
281 xpthread_setspecific(NAME##_key, value); \
282 } \
283 return value; \
284 }
285 #define DECLARE_EXTERN_PER_THREAD_DATA(TYPE, NAME) \
286 typedef TYPE NAME##_type; \
287 static pthread_key_t NAME##_key; \
288 \
289 static inline NAME##_type * \
290 NAME##_get_unsafe(void) \
291 { \
292 return pthread_getspecific(NAME##_key); \
293 } \
294 \
295 NAME##_type *NAME##_get(void);
296 #define DEFINE_EXTERN_PER_THREAD_DATA(NAME, ...) \
297 static void \
298 NAME##_once_init(void) \
299 { \
300 if (pthread_key_create(&NAME##_key, free)) { \
301 abort(); \
302 } \
303 } \
304 \
305 NAME##_type * \
306 NAME##_get(void) \
307 { \
308 static pthread_once_t once = PTHREAD_ONCE_INIT; \
309 NAME##_type *value; \
310 \
311 pthread_once(&once, NAME##_once_init); \
312 value = NAME##_get_unsafe(); \
313 if (!value) { \
314 static const NAME##_type initial_value = __VA_ARGS__; \
315 \
316 value = xmalloc(sizeof *value); \
317 *value = initial_value; \
318 xpthread_setspecific(NAME##_key, value); \
319 } \
320 return value; \
321 }
322 #endif
323
324 /* DEFINE_PER_THREAD_MALLOCED_DATA(TYPE, NAME).
325 *
326 * This is a simple wrapper around POSIX per-thread data primitives. It
327 * defines per-thread variable NAME with the given TYPE, which must be a
328 * pointer type. In each thread, the per-thread variable is initialized to
329 * NULL. When a thread terminates, the variable is freed with free().
330 *
331 * The public interface to the variable is:
332 *
333 * TYPE NAME_get(void)
334 * TYPE NAME_get_unsafe(void)
335 *
336 * Returns the value of per-thread variable NAME in this thread.
337 *
338 * Use NAME_get() in a context where this might be the first use of the
339 * per-thread variable in the program. Use NAME_get_unsafe(), which
340 * avoids a conditional test and is thus slightly faster, in a context
341 * where one knows that NAME_get() has already been called previously.
342 *
343 * TYPE NAME_set(TYPE new_value)
344 * TYPE NAME_set_unsafe(TYPE new_value)
345 *
346 * Sets the value of per-thread variable NAME to 'new_value' in this
347 * thread, and returns its previous value.
348 *
349 * Use NAME_set() in a context where this might be the first use of the
350 * per-thread variable in the program. Use NAME_set_unsafe(), which
351 * avoids a conditional test and is thus slightly faster, in a context
352 * where one knows that NAME_set() has already been called previously.
353 */
354 #define DEFINE_PER_THREAD_MALLOCED_DATA(TYPE, NAME) \
355 static pthread_key_t NAME##_key; \
356 \
357 static void \
358 NAME##_once_init(void) \
359 { \
360 if (pthread_key_create(&NAME##_key, free)) { \
361 abort(); \
362 } \
363 } \
364 \
365 static void \
366 NAME##_init(void) \
367 { \
368 static pthread_once_t once = PTHREAD_ONCE_INIT; \
369 pthread_once(&once, NAME##_once_init); \
370 } \
371 \
372 static TYPE \
373 NAME##_get_unsafe(void) \
374 { \
375 return pthread_getspecific(NAME##_key); \
376 } \
377 \
378 static OVS_UNUSED TYPE \
379 NAME##_get(void) \
380 { \
381 NAME##_init(); \
382 return NAME##_get_unsafe(); \
383 } \
384 \
385 static TYPE \
386 NAME##_set_unsafe(TYPE value) \
387 { \
388 TYPE old_value = NAME##_get_unsafe(); \
389 xpthread_setspecific(NAME##_key, value); \
390 return old_value; \
391 } \
392 \
393 static OVS_UNUSED TYPE \
394 NAME##_set(TYPE value) \
395 { \
396 NAME##_init(); \
397 return NAME##_set_unsafe(value); \
398 }
399 \f
400 /* Convenient once-only execution.
401 *
402 *
403 * Problem
404 * =======
405 *
406 * POSIX provides pthread_once_t and pthread_once() as primitives for running a
407 * set of code only once per process execution. They are used like this:
408 *
409 * static void run_once(void) { ...initialization... }
410 * static pthread_once_t once = PTHREAD_ONCE_INIT;
411 * ...
412 * pthread_once(&once, run_once);
413 *
414 * pthread_once() does not allow passing any parameters to the initialization
415 * function, which is often inconvenient, because it means that the function
416 * can only access data declared at file scope.
417 *
418 *
419 * Solution
420 * ========
421 *
422 * Use ovsthread_once, like this, instead:
423 *
424 * static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
425 *
426 * if (ovsthread_once_start(&once)) {
427 * ...initialization...
428 * ovsthread_once_done(&once);
429 * }
430 */
431
432 struct ovsthread_once {
433 atomic_bool done;
434 struct ovs_mutex mutex;
435 };
436
437 #define OVSTHREAD_ONCE_INITIALIZER \
438 { \
439 ATOMIC_VAR_INIT(false), \
440 OVS_MUTEX_INITIALIZER, \
441 }
442
443 static inline bool ovsthread_once_start(struct ovsthread_once *once)
444 OVS_TRY_LOCK(true, once->mutex);
445 void ovsthread_once_done(struct ovsthread_once *once)
446 OVS_RELEASES(once->mutex);
447
448 bool ovsthread_once_start__(struct ovsthread_once *once)
449 OVS_TRY_LOCK(false, once->mutex);
450
451 static inline bool
452 ovsthread_once_is_done__(const struct ovsthread_once *once)
453 {
454 bool done;
455
456 atomic_read_explicit(&once->done, &done, memory_order_relaxed);
457 return done;
458 }
459
460 /* Returns true if this is the first call to ovsthread_once_start() for
461 * 'once'. In this case, the caller should perform whatever initialization
462 * actions it needs to do, then call ovsthread_once_done() for 'once'.
463 *
464 * Returns false if this is not the first call to ovsthread_once_start() for
465 * 'once'. In this case, the call will not return until after
466 * ovsthread_once_done() has been called. */
467 static inline bool
468 ovsthread_once_start(struct ovsthread_once *once)
469 {
470 return OVS_UNLIKELY(!ovsthread_once_is_done__(once)
471 && !ovsthread_once_start__(once));
472 }
473 \f
474 /* Thread ID.
475 *
476 * pthread_t isn't so nice for some purposes. Its size and representation are
477 * implementation dependent, which means that there is no way to hash it.
478 * This thread ID avoids the problem.
479 */
480
481 DECLARE_EXTERN_PER_THREAD_DATA(unsigned int, ovsthread_id);
482
483 /* Returns a per-thread identifier unique within the lifetime of the
484 * process. */
485 static inline unsigned int
486 ovsthread_id_self(void)
487 {
488 return *ovsthread_id_get();
489 }
490 \f
491 void assert_single_threaded_at(const char *where);
492 #define assert_single_threaded() assert_single_threaded_at(SOURCE_LOCATOR)
493
494 pid_t xfork_at(const char *where);
495 #define xfork() xfork_at(SOURCE_LOCATOR)
496
497 void forbid_forking(const char *reason);
498 bool may_fork(void);
499
500 #endif /* ovs-thread.h */