ovs-thread: Use fair (but nonrecursive) rwlocks on glibc.
[sliver-openvswitch.git] / lib / ovs-thread.h
1 /*
2  * Copyright (c) 2013, 2014 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 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
41 #define OVS_ADAPTIVE_MUTEX_INITIALIZER                  \
42     { PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, NULL }
43 #else
44 #define OVS_ADAPTIVE_MUTEX_INITIALIZER OVS_MUTEX_INITIALIZER
45 #endif
46
47 /* ovs_mutex functions analogous to pthread_mutex_*() functions.
48  *
49  * Most of these functions abort the process with an error message on any
50  * error.  ovs_mutex_trylock() is an exception: it passes through a 0 or EBUSY
51  * return value to the caller and aborts on any other error. */
52 void ovs_mutex_init(const struct ovs_mutex *);
53 void ovs_mutex_init_recursive(const struct ovs_mutex *);
54 void ovs_mutex_init_adaptive(const struct ovs_mutex *);
55 void ovs_mutex_destroy(const struct ovs_mutex *);
56 void ovs_mutex_unlock(const struct ovs_mutex *mutex) OVS_RELEASES(mutex);
57 void ovs_mutex_lock_at(const struct ovs_mutex *mutex, const char *where)
58     OVS_ACQUIRES(mutex);
59 #define ovs_mutex_lock(mutex) \
60         ovs_mutex_lock_at(mutex, SOURCE_LOCATOR)
61
62 int ovs_mutex_trylock_at(const struct ovs_mutex *mutex, const char *where)
63     OVS_TRY_LOCK(0, mutex);
64 #define ovs_mutex_trylock(mutex) \
65         ovs_mutex_trylock_at(mutex, SOURCE_LOCATOR)
66
67 void ovs_mutex_cond_wait(pthread_cond_t *, const struct ovs_mutex *);
68
69 /* Wrappers for pthread_mutex_*() that abort the process on any error.
70  * This is still needed when ovs-atomic-pthreads.h is used. */
71 void xpthread_mutex_lock(pthread_mutex_t *mutex);
72 void xpthread_mutex_unlock(pthread_mutex_t *mutex);
73
74 /* Wrappers for pthread_mutexattr_*() that abort the process on any error. */
75 void xpthread_mutexattr_init(pthread_mutexattr_t *);
76 void xpthread_mutexattr_destroy(pthread_mutexattr_t *);
77 void xpthread_mutexattr_settype(pthread_mutexattr_t *, int type);
78 void xpthread_mutexattr_gettype(pthread_mutexattr_t *, int *typep);
79
80 /* Read-write lock.
81  *
82  * An ovs_rwlock does not support recursive readers, because POSIX allows
83  * taking the reader lock recursively to deadlock when a thread is waiting on
84  * the write-lock.  (NetBSD does deadlock.)  glibc rwlocks in their default
85  * configuration do not deadlock, but ovs_rwlock_init() initializes rwlocks as
86  * non-recursive (which will deadlock) for two reasons:
87  *
88  *     - glibc only provides fairness to writers in this mode.
89  *
90  *     - It's better to find bugs in the primary Open vSwitch target rather
91  *       than exposing them only to porters. */
92 struct OVS_LOCKABLE ovs_rwlock {
93     pthread_rwlock_t lock;
94     const char *where;
95 };
96
97 /* Initializer. */
98 #ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
99 #define OVS_RWLOCK_INITIALIZER \
100         { PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP, NULL }
101 #else
102 #define OVS_RWLOCK_INITIALIZER { PTHREAD_RWLOCK_INITIALIZER, NULL }
103 #endif
104
105 /* ovs_rwlock functions analogous to pthread_rwlock_*() functions.
106  *
107  * Most of these functions abort the process with an error message on any
108  * error.  The "trylock" functions are exception: they pass through a 0 or
109  * EBUSY return value to the caller and abort on any other error. */
110 void ovs_rwlock_init(const struct ovs_rwlock *);
111 void ovs_rwlock_destroy(const struct ovs_rwlock *);
112 void ovs_rwlock_unlock(const struct ovs_rwlock *rwlock) OVS_RELEASES(rwlock);
113
114 /* Wrappers for pthread_rwlockattr_*() that abort the process on any error. */
115 void xpthread_rwlockattr_init(pthread_rwlockattr_t *);
116 void xpthread_rwlockattr_destroy(pthread_rwlockattr_t *);
117 #ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
118 void xpthread_rwlockattr_setkind_np(pthread_rwlockattr_t *, int kind);
119 #endif
120
121 void ovs_rwlock_wrlock_at(const struct ovs_rwlock *rwlock, const char *where)
122     OVS_ACQ_WRLOCK(rwlock);
123 #define ovs_rwlock_wrlock(rwlock) \
124         ovs_rwlock_wrlock_at(rwlock, SOURCE_LOCATOR)
125
126 int ovs_rwlock_trywrlock_at(const struct ovs_rwlock *rwlock, const char *where)
127     OVS_TRY_WRLOCK(0, rwlock);
128 #define ovs_rwlock_trywrlock(rwlock) \
129     ovs_rwlock_trywrlock_at(rwlock, SOURCE_LOCATOR)
130
131 void ovs_rwlock_rdlock_at(const struct ovs_rwlock *rwlock, const char *where)
132     OVS_ACQ_RDLOCK(rwlock);
133 #define ovs_rwlock_rdlock(rwlock) \
134         ovs_rwlock_rdlock_at(rwlock, SOURCE_LOCATOR)
135
136 int ovs_rwlock_tryrdlock_at(const struct ovs_rwlock *rwlock, const char *where)
137     OVS_TRY_RDLOCK(0, rwlock);
138 #define ovs_rwlock_tryrdlock(rwlock) \
139         ovs_rwlock_tryrdlock_at(rwlock, SOURCE_LOCATOR)
140
141 /* Wrappers for xpthread_cond_*() that abort the process on any error.
142  *
143  * Use ovs_mutex_cond_wait() to wait for a condition. */
144 void xpthread_cond_init(pthread_cond_t *, pthread_condattr_t *);
145 void xpthread_cond_destroy(pthread_cond_t *);
146 void xpthread_cond_signal(pthread_cond_t *);
147 void xpthread_cond_broadcast(pthread_cond_t *);
148
149 void xpthread_key_create(pthread_key_t *, void (*destructor)(void *));
150 void xpthread_key_delete(pthread_key_t);
151 void xpthread_setspecific(pthread_key_t, const void *);
152
153 void xpthread_create(pthread_t *, pthread_attr_t *, void *(*)(void *), void *);
154 void xpthread_join(pthread_t, void **);
155 \f
156 /* Per-thread data.
157  *
158  *
159  * Standard Forms
160  * ==============
161  *
162  * Multiple forms of standard per-thread data exist, each with its own pluses
163  * and minuses.  In general, if one of these forms is appropriate, then it's a
164  * good idea to use it:
165  *
166  *     - POSIX per-thread data via pthread_key_t is portable to any pthreads
167  *       implementation, and allows a destructor function to be defined.  It
168  *       only (directly) supports per-thread pointers, which are always
169  *       initialized to NULL.  It requires once-only allocation of a
170  *       pthread_key_t value.  It is relatively slow.  Typically few
171  *       "pthread_key_t"s are available (POSIX requires only at least 128,
172  *       glibc supplies only 1024).
173  *
174  *     - The thread_local feature newly defined in C11 <threads.h> works with
175  *       any data type and initializer, and it is fast.  thread_local does not
176  *       require once-only initialization like pthread_key_t.  C11 does not
177  *       define what happens if one attempts to access a thread_local object
178  *       from a thread other than the one to which that object belongs.  There
179  *       is no provision to call a user-specified destructor when a thread
180  *       ends.  Typical implementations allow for an arbitrary amount of
181  *       thread_local storage, but statically allocated only.
182  *
183  *     - The __thread keyword is a GCC extension similar to thread_local but
184  *       with a longer history.  __thread is not portable to every GCC version
185  *       or environment.  __thread does not restrict the use of a thread-local
186  *       object outside its own thread.
187  *
188  * Here's a handy summary:
189  *
190  *                     pthread_key_t     thread_local       __thread
191  *                     -------------     ------------     -------------
192  * portability             high               low             medium
193  * speed                    low              high               high
194  * supports destructors?    yes                no                 no
195  * needs key allocation?    yes                no                 no
196  * arbitrary initializer?    no               yes                yes
197  * cross-thread access?     yes                no                yes
198  * amount available?        few            arbitrary         arbitrary
199  * dynamically allocated?   yes                no                 no
200  *
201  *
202  * Extensions
203  * ==========
204  *
205  * OVS provides some extensions and wrappers:
206  *
207  *     - In a situation where the performance of thread_local or __thread is
208  *       desirable, but portability is required, DEFINE_STATIC_PER_THREAD_DATA
209  *       and DECLARE_EXTERN_PER_THREAD_DATA/DEFINE_EXTERN_PER_THREAD_DATA may
210  *       be appropriate (see below).
211  *
212  *     - DEFINE_PER_THREAD_MALLOCED_DATA can be convenient for simple
213  *       per-thread malloc()'d buffers.
214  *
215  *     - struct ovs_tsd provides an alternative to pthread_key_t that isn't
216  *       limited to a small number of keys.
217  */
218
219 /* For static data, use this macro in a source file:
220  *
221  *    DEFINE_STATIC_PER_THREAD_DATA(TYPE, NAME, INITIALIZER).
222  *
223  * For global data, "declare" the data in the header and "define" it in
224  * the source file, with:
225  *
226  *    DECLARE_EXTERN_PER_THREAD_DATA(TYPE, NAME).
227  *    DEFINE_EXTERN_PER_THREAD_DATA(NAME, INITIALIZER).
228  *
229  * One should prefer to use POSIX per-thread data, via pthread_key_t, when its
230  * performance is acceptable, because of its portability (see the table above).
231  * This macro is an alternatives that takes advantage of thread_local (and
232  * __thread), for its performance, when it is available, and falls back to
233  * POSIX per-thread data otherwise.
234  *
235  * Defines per-thread variable NAME with the given TYPE, initialized to
236  * INITIALIZER (which must be valid as an initializer for a variable with
237  * static lifetime).
238  *
239  * The public interface to the variable is:
240  *
241  *    TYPE *NAME_get(void)
242  *    TYPE *NAME_get_unsafe(void)
243  *
244  *       Returns the address of this thread's instance of NAME.
245  *
246  *       Use NAME_get() in a context where this might be the first use of the
247  *       per-thread variable in the program.  Use NAME_get_unsafe(), which
248  *       avoids a conditional test and is thus slightly faster, in a context
249  *       where one knows that NAME_get() has already been called previously.
250  *
251  * There is no "NAME_set()" (or "NAME_set_unsafe()") function.  To set the
252  * value of the per-thread variable, dereference the pointer returned by
253  * TYPE_get() or TYPE_get_unsafe(), e.g. *TYPE_get() = 0.
254  */
255 #if HAVE_THREAD_LOCAL || HAVE___THREAD
256
257 #if HAVE_THREAD_LOCAL
258 #include <threads.h>
259 #elif HAVE___THREAD
260 #define thread_local __thread
261 #else
262 #error
263 #endif
264
265 #define DEFINE_STATIC_PER_THREAD_DATA(TYPE, NAME, ...)                  \
266     typedef TYPE NAME##_type;                                           \
267                                                                         \
268     static NAME##_type *                                                \
269     NAME##_get_unsafe(void)                                             \
270     {                                                                   \
271         static thread_local NAME##_type var = __VA_ARGS__;              \
272         return &var;                                                    \
273     }                                                                   \
274                                                                         \
275     static NAME##_type *                                                \
276     NAME##_get(void)                                                    \
277     {                                                                   \
278         return NAME##_get_unsafe();                                     \
279     }
280 #define DECLARE_EXTERN_PER_THREAD_DATA(TYPE, NAME)                      \
281     typedef TYPE NAME##_type;                                           \
282     extern thread_local NAME##_type NAME##_var;                         \
283                                                                         \
284     static inline NAME##_type *                                         \
285     NAME##_get_unsafe(void)                                             \
286     {                                                                   \
287         return &NAME##_var;                                             \
288     }                                                                   \
289                                                                         \
290     static inline NAME##_type *                                         \
291     NAME##_get(void)                                                    \
292     {                                                                   \
293         return NAME##_get_unsafe();                                     \
294     }
295 #define DEFINE_EXTERN_PER_THREAD_DATA(NAME, ...)         \
296     thread_local NAME##_type NAME##_var = __VA_ARGS__;
297 #else  /* no C implementation support for thread-local storage  */
298 #define DEFINE_STATIC_PER_THREAD_DATA(TYPE, NAME, ...)                  \
299     typedef TYPE NAME##_type;                                           \
300     static pthread_key_t NAME##_key;                                    \
301                                                                         \
302     static NAME##_type *                                                \
303     NAME##_get_unsafe(void)                                             \
304     {                                                                   \
305         return pthread_getspecific(NAME##_key);                         \
306     }                                                                   \
307                                                                         \
308     static void                                                         \
309     NAME##_once_init(void)                                              \
310     {                                                                   \
311         if (pthread_key_create(&NAME##_key, free)) {                    \
312             abort();                                                    \
313         }                                                               \
314     }                                                                   \
315                                                                         \
316     static NAME##_type *                                                \
317     NAME##_get(void)                                                    \
318     {                                                                   \
319         static pthread_once_t once = PTHREAD_ONCE_INIT;                 \
320         NAME##_type *value;                                             \
321                                                                         \
322         pthread_once(&once, NAME##_once_init);                          \
323         value = NAME##_get_unsafe();                                    \
324         if (!value) {                                                   \
325             static const NAME##_type initial_value = __VA_ARGS__;       \
326                                                                         \
327             value = malloc(sizeof *value);                              \
328             if (value == NULL) {                                        \
329                 out_of_memory();                                        \
330             }                                                           \
331             *value = initial_value;                                     \
332             xpthread_setspecific(NAME##_key, value);                    \
333         }                                                               \
334         return value;                                                   \
335     }
336 #define DECLARE_EXTERN_PER_THREAD_DATA(TYPE, NAME)                      \
337     typedef TYPE NAME##_type;                                           \
338     static pthread_key_t NAME##_key;                                    \
339                                                                         \
340     static inline NAME##_type *                                         \
341     NAME##_get_unsafe(void)                                             \
342     {                                                                   \
343         return pthread_getspecific(NAME##_key);                         \
344     }                                                                   \
345                                                                         \
346     NAME##_type *NAME##_get(void);
347 #define DEFINE_EXTERN_PER_THREAD_DATA(NAME, ...)                        \
348     static void                                                         \
349     NAME##_once_init(void)                                              \
350     {                                                                   \
351         if (pthread_key_create(&NAME##_key, free)) {                    \
352             abort();                                                    \
353         }                                                               \
354     }                                                                   \
355                                                                         \
356     NAME##_type *                                                       \
357     NAME##_get(void)                                                    \
358     {                                                                   \
359         static pthread_once_t once = PTHREAD_ONCE_INIT;                 \
360         NAME##_type *value;                                             \
361                                                                         \
362         pthread_once(&once, NAME##_once_init);                          \
363         value = NAME##_get_unsafe();                                    \
364         if (!value) {                                                   \
365             static const NAME##_type initial_value = __VA_ARGS__;       \
366                                                                         \
367             value = malloc(sizeof *value);                              \
368             if (value == NULL) {                                        \
369                 out_of_memory();                                        \
370             }                                                           \
371             *value = initial_value;                                     \
372             xpthread_setspecific(NAME##_key, value);                    \
373         }                                                               \
374         return value;                                                   \
375     }
376 #endif
377
378 /* DEFINE_PER_THREAD_MALLOCED_DATA(TYPE, NAME).
379  *
380  * This is a simple wrapper around POSIX per-thread data primitives.  It
381  * defines per-thread variable NAME with the given TYPE, which must be a
382  * pointer type.  In each thread, the per-thread variable is initialized to
383  * NULL.  When a thread terminates, the variable is freed with free().
384  *
385  * The public interface to the variable is:
386  *
387  *    TYPE NAME_get(void)
388  *    TYPE NAME_get_unsafe(void)
389  *
390  *       Returns the value of per-thread variable NAME in this thread.
391  *
392  *       Use NAME_get() in a context where this might be the first use of the
393  *       per-thread variable in the program.  Use NAME_get_unsafe(), which
394  *       avoids a conditional test and is thus slightly faster, in a context
395  *       where one knows that NAME_get() has already been called previously.
396  *
397  *    TYPE NAME_set(TYPE new_value)
398  *    TYPE NAME_set_unsafe(TYPE new_value)
399  *
400  *       Sets the value of per-thread variable NAME to 'new_value' in this
401  *       thread, and returns its previous value.
402  *
403  *       Use NAME_set() in a context where this might be the first use of the
404  *       per-thread variable in the program.  Use NAME_set_unsafe(), which
405  *       avoids a conditional test and is thus slightly faster, in a context
406  *       where one knows that NAME_set() has already been called previously.
407  */
408 #define DEFINE_PER_THREAD_MALLOCED_DATA(TYPE, NAME)     \
409     static pthread_key_t NAME##_key;                    \
410                                                         \
411     static void                                         \
412     NAME##_once_init(void)                              \
413     {                                                   \
414         if (pthread_key_create(&NAME##_key, free)) {    \
415             abort();                                    \
416         }                                               \
417     }                                                   \
418                                                         \
419     static void                                         \
420     NAME##_init(void)                                   \
421     {                                                   \
422         static pthread_once_t once = PTHREAD_ONCE_INIT; \
423         pthread_once(&once, NAME##_once_init);          \
424     }                                                   \
425                                                         \
426     static TYPE                                         \
427     NAME##_get_unsafe(void)                             \
428     {                                                   \
429         return pthread_getspecific(NAME##_key);         \
430     }                                                   \
431                                                         \
432     static OVS_UNUSED TYPE                              \
433     NAME##_get(void)                                    \
434     {                                                   \
435         NAME##_init();                                  \
436         return NAME##_get_unsafe();                     \
437     }                                                   \
438                                                         \
439     static TYPE                                         \
440     NAME##_set_unsafe(TYPE value)                       \
441     {                                                   \
442         TYPE old_value = NAME##_get_unsafe();           \
443         xpthread_setspecific(NAME##_key, value);        \
444         return old_value;                               \
445     }                                                   \
446                                                         \
447     static OVS_UNUSED TYPE                              \
448     NAME##_set(TYPE value)                              \
449     {                                                   \
450         NAME##_init();                                  \
451         return NAME##_set_unsafe(value);                \
452     }
453
454 /* Dynamically allocated thread-specific data with lots of slots.
455  *
456  * pthread_key_t can provide as few as 128 pieces of thread-specific data (even
457  * glibc is limited to 1,024).  Thus, one must be careful to allocate only a
458  * few keys globally.  One cannot, for example, allocate a key for every
459  * instance of a data structure if there might be an arbitrary number of those
460  * data structures.
461  *
462  * This API is similar to the pthread one (simply search and replace pthread_
463  * by ovsthread_) but it a much larger limit that can be raised if necessary
464  * (by recompiling).  Thus, one may more freely use this form of
465  * thread-specific data.
466  *
467  * ovsthread_key_t also differs from pthread_key_t in the following ways:
468  *
469  *    - Destructors must not access thread-specific data (via ovsthread_key).
470  *
471  *    - The pthread_key_t API allows concurrently exiting threads to start
472  *      executing the destructor after pthread_key_delete() returns.  The
473  *      ovsthread_key_t API guarantees that, when ovsthread_key_delete()
474  *      returns, all destructors have returned and no new ones will start
475  *      execution.
476  */
477 typedef struct ovsthread_key *ovsthread_key_t;
478
479 void ovsthread_key_create(ovsthread_key_t *, void (*destructor)(void *));
480 void ovsthread_key_delete(ovsthread_key_t);
481
482 void ovsthread_setspecific(ovsthread_key_t, const void *);
483 void *ovsthread_getspecific(ovsthread_key_t);
484 \f
485 /* Convenient once-only execution.
486  *
487  *
488  * Problem
489  * =======
490  *
491  * POSIX provides pthread_once_t and pthread_once() as primitives for running a
492  * set of code only once per process execution.  They are used like this:
493  *
494  *     static void run_once(void) { ...initialization... }
495  *     static pthread_once_t once = PTHREAD_ONCE_INIT;
496  * ...
497  *     pthread_once(&once, run_once);
498  *
499  * pthread_once() does not allow passing any parameters to the initialization
500  * function, which is often inconvenient, because it means that the function
501  * can only access data declared at file scope.
502  *
503  *
504  * Solution
505  * ========
506  *
507  * Use ovsthread_once, like this, instead:
508  *
509  *     static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
510  *
511  *     if (ovsthread_once_start(&once)) {
512  *         ...initialization...
513  *         ovsthread_once_done(&once);
514  *     }
515  */
516
517 struct ovsthread_once {
518     atomic_bool done;
519     struct ovs_mutex mutex;
520 };
521
522 #define OVSTHREAD_ONCE_INITIALIZER              \
523     {                                           \
524         ATOMIC_VAR_INIT(false),                 \
525         OVS_MUTEX_INITIALIZER,                  \
526     }
527
528 static inline bool ovsthread_once_start(struct ovsthread_once *once)
529     OVS_TRY_LOCK(true, once->mutex);
530 void ovsthread_once_done(struct ovsthread_once *once)
531     OVS_RELEASES(once->mutex);
532
533 bool ovsthread_once_start__(struct ovsthread_once *once)
534     OVS_TRY_LOCK(false, once->mutex);
535
536 static inline bool
537 ovsthread_once_is_done__(struct ovsthread_once *once)
538 {
539     bool done;
540
541     atomic_read_explicit(&once->done, &done, memory_order_relaxed);
542     return done;
543 }
544
545 /* Returns true if this is the first call to ovsthread_once_start() for
546  * 'once'.  In this case, the caller should perform whatever initialization
547  * actions it needs to do, then call ovsthread_once_done() for 'once'.
548  *
549  * Returns false if this is not the first call to ovsthread_once_start() for
550  * 'once'.  In this case, the call will not return until after
551  * ovsthread_once_done() has been called. */
552 static inline bool
553 ovsthread_once_start(struct ovsthread_once *once)
554 {
555     return OVS_UNLIKELY(!ovsthread_once_is_done__(once)
556                         && !ovsthread_once_start__(once));
557 }
558 \f
559 /* Thread ID.
560  *
561  * pthread_t isn't so nice for some purposes.  Its size and representation are
562  * implementation dependent, which means that there is no way to hash it.
563  * This thread ID avoids the problem.
564  */
565
566 DECLARE_EXTERN_PER_THREAD_DATA(unsigned int, ovsthread_id);
567
568 /* Returns a per-thread identifier unique within the lifetime of the
569  * process. */
570 static inline unsigned int
571 ovsthread_id_self(void)
572 {
573     return *ovsthread_id_get();
574 }
575 \f
576 /* Simulated global counter.
577  *
578  * Incrementing such a counter is meant to be cheaper than incrementing a
579  * global counter protected by a lock.  It is probably more expensive than
580  * incrementing a truly thread-local variable, but such a variable has no
581  * straightforward way to get the sum.
582  *
583  *
584  * Thread-safety
585  * =============
586  *
587  * Fully thread-safe. */
588
589 struct ovsthread_counter *ovsthread_counter_create(void);
590 void ovsthread_counter_destroy(struct ovsthread_counter *);
591 void ovsthread_counter_inc(struct ovsthread_counter *, unsigned long long int);
592 unsigned long long int ovsthread_counter_read(
593     const struct ovsthread_counter *);
594 \f
595 void assert_single_threaded_at(const char *where);
596 #define assert_single_threaded() assert_single_threaded_at(SOURCE_LOCATOR)
597
598 pid_t xfork_at(const char *where);
599 #define xfork() xfork_at(SOURCE_LOCATOR)
600
601 void forbid_forking(const char *reason);
602 bool may_fork(void);
603 \f
604 /* Useful functions related to threading. */
605
606 int count_cpu_cores(void);
607
608 #endif /* ovs-thread.h */