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