2 * Copyright (c) 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define OVS_THREAD_H 1
22 #include <sys/types.h>
23 #include "ovs-atomic.h"
28 struct OVS_LOCKABLE ovs_mutex {
33 /* "struct ovs_mutex" initializer. */
34 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
35 #define OVS_MUTEX_INITIALIZER { PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, NULL }
37 #define OVS_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, NULL }
40 /* ovs_mutex functions analogous to pthread_mutex_*() functions.
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)
51 #define ovs_mutex_lock(mutex) \
52 ovs_mutex_lock_at(mutex, SOURCE_LOCATOR)
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)
59 void ovs_mutex_cond_wait(pthread_cond_t *, const struct ovs_mutex *);
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);
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);
72 /* Read-write lock. */
73 struct OVS_LOCKABLE ovs_rwlock {
74 pthread_rwlock_t lock;
79 #define OVS_RWLOCK_INITIALIZER { PTHREAD_RWLOCK_INITIALIZER, NULL }
81 /* ovs_rwlock functions analogous to pthread_rwlock_*() functions.
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);
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)
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)
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)
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)
110 /* Wrappers for xpthread_cond_*() that abort the process on any error.
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 *);
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
129 void xpthread_key_create(pthread_key_t *, void (*destructor)(void *));
130 void xpthread_key_delete(pthread_key_t);
131 void xpthread_setspecific(pthread_key_t, const void *);
133 void xpthread_create(pthread_t *, pthread_attr_t *, void *(*)(void *), void *);
134 void xpthread_join(pthread_t, void **);
142 * Multiple forms of standard per-thread data exist, each with its own pluses
143 * and minuses. In general, if one of these forms is appropriate, then it's a
144 * good idea to use it:
146 * - POSIX per-thread data via pthread_key_t is portable to any pthreads
147 * implementation, and allows a destructor function to be defined. It
148 * only (directly) supports per-thread pointers, which are always
149 * initialized to NULL. It requires once-only allocation of a
150 * pthread_key_t value. It is relatively slow. Typically few
151 * "pthread_key_t"s are available (POSIX requires only at least 128,
152 * glibc supplies only 1024).
154 * - The thread_local feature newly defined in C11 <threads.h> works with
155 * any data type and initializer, and it is fast. thread_local does not
156 * require once-only initialization like pthread_key_t. C11 does not
157 * define what happens if one attempts to access a thread_local object
158 * from a thread other than the one to which that object belongs. There
159 * is no provision to call a user-specified destructor when a thread
160 * ends. Typical implementations allow for an arbitrary amount of
161 * thread_local storage, but statically allocated only.
163 * - The __thread keyword is a GCC extension similar to thread_local but
164 * with a longer history. __thread is not portable to every GCC version
165 * or environment. __thread does not restrict the use of a thread-local
166 * object outside its own thread.
168 * Here's a handy summary:
170 * pthread_key_t thread_local __thread
171 * ------------- ------------ -------------
172 * portability high low medium
173 * speed low high high
174 * supports destructors? yes no no
175 * needs key allocation? yes no no
176 * arbitrary initializer? no yes yes
177 * cross-thread access? yes no yes
178 * amount available? few arbitrary arbitrary
179 * dynamically allocated? yes no no
185 * OVS provides some extensions and wrappers:
187 * - In a situation where the performance of thread_local or __thread is
188 * desirable, but portability is required, DEFINE_STATIC_PER_THREAD_DATA
189 * and DECLARE_EXTERN_PER_THREAD_DATA/DEFINE_EXTERN_PER_THREAD_DATA may
190 * be appropriate (see below).
192 * - DEFINE_PER_THREAD_MALLOCED_DATA can be convenient for simple
193 * per-thread malloc()'d buffers.
195 * - struct ovs_tsd provides an alternative to pthread_key_t that isn't
196 * limited to a small number of keys.
199 /* For static data, use this macro in a source file:
201 * DEFINE_STATIC_PER_THREAD_DATA(TYPE, NAME, INITIALIZER).
203 * For global data, "declare" the data in the header and "define" it in
204 * the source file, with:
206 * DECLARE_EXTERN_PER_THREAD_DATA(TYPE, NAME).
207 * DEFINE_EXTERN_PER_THREAD_DATA(NAME, INITIALIZER).
209 * One should prefer to use POSIX per-thread data, via pthread_key_t, when its
210 * performance is acceptable, because of its portability (see the table above).
211 * This macro is an alternatives that takes advantage of thread_local (and
212 * __thread), for its performance, when it is available, and falls back to
213 * POSIX per-thread data otherwise.
215 * Defines per-thread variable NAME with the given TYPE, initialized to
216 * INITIALIZER (which must be valid as an initializer for a variable with
219 * The public interface to the variable is:
221 * TYPE *NAME_get(void)
222 * TYPE *NAME_get_unsafe(void)
224 * Returns the address of this thread's instance of NAME.
226 * Use NAME_get() in a context where this might be the first use of the
227 * per-thread variable in the program. Use NAME_get_unsafe(), which
228 * avoids a conditional test and is thus slightly faster, in a context
229 * where one knows that NAME_get() has already been called previously.
231 * There is no "NAME_set()" (or "NAME_set_unsafe()") function. To set the
232 * value of the per-thread variable, dereference the pointer returned by
233 * TYPE_get() or TYPE_get_unsafe(), e.g. *TYPE_get() = 0.
235 #if HAVE_THREAD_LOCAL || HAVE___THREAD
237 #if HAVE_THREAD_LOCAL
240 #define thread_local __thread
245 #define DEFINE_STATIC_PER_THREAD_DATA(TYPE, NAME, ...) \
246 typedef TYPE NAME##_type; \
248 static NAME##_type * \
249 NAME##_get_unsafe(void) \
251 static thread_local NAME##_type var = __VA_ARGS__; \
255 static NAME##_type * \
258 return NAME##_get_unsafe(); \
260 #define DECLARE_EXTERN_PER_THREAD_DATA(TYPE, NAME) \
261 typedef TYPE NAME##_type; \
262 extern thread_local NAME##_type NAME##_var; \
264 static inline NAME##_type * \
265 NAME##_get_unsafe(void) \
267 return &NAME##_var; \
270 static inline NAME##_type * \
273 return NAME##_get_unsafe(); \
275 #define DEFINE_EXTERN_PER_THREAD_DATA(NAME, ...) \
276 thread_local NAME##_type NAME##_var = __VA_ARGS__;
277 #else /* no C implementation support for thread-local storage */
278 #define DEFINE_STATIC_PER_THREAD_DATA(TYPE, NAME, ...) \
279 typedef TYPE NAME##_type; \
280 static pthread_key_t NAME##_key; \
282 static NAME##_type * \
283 NAME##_get_unsafe(void) \
285 return pthread_getspecific(NAME##_key); \
289 NAME##_once_init(void) \
291 if (pthread_key_create(&NAME##_key, free)) { \
296 static NAME##_type * \
299 static pthread_once_t once = PTHREAD_ONCE_INIT; \
300 NAME##_type *value; \
302 pthread_once(&once, NAME##_once_init); \
303 value = NAME##_get_unsafe(); \
305 static const NAME##_type initial_value = __VA_ARGS__; \
307 value = malloc(sizeof *value); \
308 if (value == NULL) { \
311 *value = initial_value; \
312 xpthread_setspecific(NAME##_key, value); \
316 #define DECLARE_EXTERN_PER_THREAD_DATA(TYPE, NAME) \
317 typedef TYPE NAME##_type; \
318 static pthread_key_t NAME##_key; \
320 static inline NAME##_type * \
321 NAME##_get_unsafe(void) \
323 return pthread_getspecific(NAME##_key); \
326 NAME##_type *NAME##_get(void);
327 #define DEFINE_EXTERN_PER_THREAD_DATA(NAME, ...) \
329 NAME##_once_init(void) \
331 if (pthread_key_create(&NAME##_key, free)) { \
339 static pthread_once_t once = PTHREAD_ONCE_INIT; \
340 NAME##_type *value; \
342 pthread_once(&once, NAME##_once_init); \
343 value = NAME##_get_unsafe(); \
345 static const NAME##_type initial_value = __VA_ARGS__; \
347 value = malloc(sizeof *value); \
348 if (value == NULL) { \
351 *value = initial_value; \
352 xpthread_setspecific(NAME##_key, value); \
358 /* DEFINE_PER_THREAD_MALLOCED_DATA(TYPE, NAME).
360 * This is a simple wrapper around POSIX per-thread data primitives. It
361 * defines per-thread variable NAME with the given TYPE, which must be a
362 * pointer type. In each thread, the per-thread variable is initialized to
363 * NULL. When a thread terminates, the variable is freed with free().
365 * The public interface to the variable is:
367 * TYPE NAME_get(void)
368 * TYPE NAME_get_unsafe(void)
370 * Returns the value of per-thread variable NAME in this thread.
372 * Use NAME_get() in a context where this might be the first use of the
373 * per-thread variable in the program. Use NAME_get_unsafe(), which
374 * avoids a conditional test and is thus slightly faster, in a context
375 * where one knows that NAME_get() has already been called previously.
377 * TYPE NAME_set(TYPE new_value)
378 * TYPE NAME_set_unsafe(TYPE new_value)
380 * Sets the value of per-thread variable NAME to 'new_value' in this
381 * thread, and returns its previous value.
383 * Use NAME_set() in a context where this might be the first use of the
384 * per-thread variable in the program. Use NAME_set_unsafe(), which
385 * avoids a conditional test and is thus slightly faster, in a context
386 * where one knows that NAME_set() has already been called previously.
388 #define DEFINE_PER_THREAD_MALLOCED_DATA(TYPE, NAME) \
389 static pthread_key_t NAME##_key; \
392 NAME##_once_init(void) \
394 if (pthread_key_create(&NAME##_key, free)) { \
402 static pthread_once_t once = PTHREAD_ONCE_INIT; \
403 pthread_once(&once, NAME##_once_init); \
407 NAME##_get_unsafe(void) \
409 return pthread_getspecific(NAME##_key); \
412 static OVS_UNUSED TYPE \
416 return NAME##_get_unsafe(); \
420 NAME##_set_unsafe(TYPE value) \
422 TYPE old_value = NAME##_get_unsafe(); \
423 xpthread_setspecific(NAME##_key, value); \
427 static OVS_UNUSED TYPE \
428 NAME##_set(TYPE value) \
431 return NAME##_set_unsafe(value); \
434 /* Dynamically allocated thread-specific data with lots of slots.
436 * pthread_key_t can provide as few as 128 pieces of thread-specific data (even
437 * glibc is limited to 1,024). Thus, one must be careful to allocate only a
438 * few keys globally. One cannot, for example, allocate a key for every
439 * instance of a data structure if there might be an arbitrary number of those
442 * This API is similar to the pthread one (simply search and replace pthread_
443 * by ovsthread_) but it a much larger limit that can be raised if necessary
444 * (by recompiling). Thus, one may more freely use this form of
445 * thread-specific data.
447 * Compared to pthread_key_t, ovsthread_key_t has the follow limitations:
449 * - Destructors must not access thread-specific data (via ovsthread_key).
451 typedef struct ovsthread_key *ovsthread_key_t;
453 void ovsthread_key_create(ovsthread_key_t *, void (*destructor)(void *));
454 void ovsthread_key_delete(ovsthread_key_t);
456 void ovsthread_setspecific(ovsthread_key_t, const void *);
457 void *ovsthread_getspecific(ovsthread_key_t);
459 /* Convenient once-only execution.
465 * POSIX provides pthread_once_t and pthread_once() as primitives for running a
466 * set of code only once per process execution. They are used like this:
468 * static void run_once(void) { ...initialization... }
469 * static pthread_once_t once = PTHREAD_ONCE_INIT;
471 * pthread_once(&once, run_once);
473 * pthread_once() does not allow passing any parameters to the initialization
474 * function, which is often inconvenient, because it means that the function
475 * can only access data declared at file scope.
481 * Use ovsthread_once, like this, instead:
483 * static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
485 * if (ovsthread_once_start(&once)) {
486 * ...initialization...
487 * ovsthread_once_done(&once);
491 struct ovsthread_once {
493 struct ovs_mutex mutex;
496 #define OVSTHREAD_ONCE_INITIALIZER \
498 ATOMIC_VAR_INIT(false), \
499 OVS_MUTEX_INITIALIZER, \
502 static inline bool ovsthread_once_start(struct ovsthread_once *once)
503 OVS_TRY_LOCK(true, once->mutex);
504 void ovsthread_once_done(struct ovsthread_once *once)
505 OVS_RELEASES(once->mutex);
507 bool ovsthread_once_start__(struct ovsthread_once *once)
508 OVS_TRY_LOCK(false, once->mutex);
511 ovsthread_once_is_done__(struct ovsthread_once *once)
515 atomic_read_explicit(&once->done, &done, memory_order_relaxed);
519 /* Returns true if this is the first call to ovsthread_once_start() for
520 * 'once'. In this case, the caller should perform whatever initialization
521 * actions it needs to do, then call ovsthread_once_done() for 'once'.
523 * Returns false if this is not the first call to ovsthread_once_start() for
524 * 'once'. In this case, the call will not return until after
525 * ovsthread_once_done() has been called. */
527 ovsthread_once_start(struct ovsthread_once *once)
529 return OVS_UNLIKELY(!ovsthread_once_is_done__(once)
530 && !ovsthread_once_start__(once));
535 * pthread_t isn't so nice for some purposes. Its size and representation are
536 * implementation dependent, which means that there is no way to hash it.
537 * This thread ID avoids the problem.
540 DECLARE_EXTERN_PER_THREAD_DATA(unsigned int, ovsthread_id);
542 /* Returns a per-thread identifier unique within the lifetime of the
544 static inline unsigned int
545 ovsthread_id_self(void)
547 return *ovsthread_id_get();
550 /* Simulated global counter.
552 * Incrementing such a counter is meant to be cheaper than incrementing a
553 * global counter protected by a lock. It is probably more expensive than
554 * incrementing a truly thread-local variable, but such a variable has no
555 * straightforward way to get the sum.
561 * Fully thread-safe. */
563 struct ovsthread_counter *ovsthread_counter_create(void);
564 void ovsthread_counter_destroy(struct ovsthread_counter *);
565 void ovsthread_counter_inc(struct ovsthread_counter *, unsigned long long int);
566 unsigned long long int ovsthread_counter_read(
567 const struct ovsthread_counter *);
569 void assert_single_threaded_at(const char *where);
570 #define assert_single_threaded() assert_single_threaded_at(SOURCE_LOCATOR)
572 pid_t xfork_at(const char *where);
573 #define xfork() xfork_at(SOURCE_LOCATOR)
575 void forbid_forking(const char *reason);
578 /* Useful functions related to threading. */
580 int count_cpu_cores(void);
582 #endif /* ovs-thread.h */