+/* Convenient once-only execution.
+ *
+ *
+ * Problem
+ * =======
+ *
+ * POSIX provides pthread_once_t and pthread_once() as primitives for running a
+ * set of code only once per process execution. They are used like this:
+ *
+ * static void run_once(void) { ...initialization... }
+ * static pthread_once_t once = PTHREAD_ONCE_INIT;
+ * ...
+ * pthread_once(&once, run_once);
+ *
+ * pthread_once() does not allow passing any parameters to the initialization
+ * function, which is often inconvenient, because it means that the function
+ * can only access data declared at file scope.
+ *
+ *
+ * Solution
+ * ========
+ *
+ * Use ovsthread_once, like this, instead:
+ *
+ * static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
+ *
+ * if (ovsthread_once_start(&once)) {
+ * ...initialization...
+ * ovsthread_once_done(&once);
+ * }
+ */
+
+struct ovsthread_once {
+ atomic_bool done;
+ struct ovs_mutex mutex;
+};
+
+#define OVSTHREAD_ONCE_INITIALIZER \
+ { \
+ ATOMIC_VAR_INIT(false), \
+ OVS_MUTEX_INITIALIZER, \
+ }
+
+static inline bool ovsthread_once_start(struct ovsthread_once *once)
+ OVS_TRY_LOCK(true, once->mutex);
+void ovsthread_once_done(struct ovsthread_once *once)
+ OVS_RELEASES(once->mutex);
+
+bool ovsthread_once_start__(struct ovsthread_once *once)
+ OVS_TRY_LOCK(false, once->mutex);
+
+static inline bool
+ovsthread_once_is_done__(struct ovsthread_once *once)
+{
+ bool done;
+
+ atomic_read_explicit(&once->done, &done, memory_order_relaxed);
+ return done;
+}
+
+/* Returns true if this is the first call to ovsthread_once_start() for
+ * 'once'. In this case, the caller should perform whatever initialization
+ * actions it needs to do, then call ovsthread_once_done() for 'once'.
+ *
+ * Returns false if this is not the first call to ovsthread_once_start() for
+ * 'once'. In this case, the call will not return until after
+ * ovsthread_once_done() has been called. */
+static inline bool
+ovsthread_once_start(struct ovsthread_once *once)
+{
+ return OVS_UNLIKELY(!ovsthread_once_is_done__(once)
+ && !ovsthread_once_start__(once));
+}
+\f
+/* Thread ID.
+ *
+ * pthread_t isn't so nice for some purposes. Its size and representation are
+ * implementation dependent, which means that there is no way to hash it.
+ * This thread ID avoids the problem.
+ */
+
+DECLARE_EXTERN_PER_THREAD_DATA(unsigned int, ovsthread_id);
+
+/* Returns a per-thread identifier unique within the lifetime of the
+ * process. */
+static inline unsigned int
+ovsthread_id_self(void)
+{
+ return *ovsthread_id_get();
+}
+\f
+/* Simulated global counter.
+ *
+ * Incrementing such a counter is meant to be cheaper than incrementing a
+ * global counter protected by a lock. It is probably more expensive than
+ * incrementing a truly thread-local variable, but such a variable has no
+ * straightforward way to get the sum.
+ *
+ *
+ * Thread-safety
+ * =============
+ *
+ * Fully thread-safe. */
+
+struct ovsthread_counter *ovsthread_counter_create(void);
+void ovsthread_counter_destroy(struct ovsthread_counter *);
+void ovsthread_counter_inc(struct ovsthread_counter *, unsigned long long int);
+unsigned long long int ovsthread_counter_read(
+ const struct ovsthread_counter *);
+\f
+void assert_single_threaded_at(const char *where);
+#define assert_single_threaded() assert_single_threaded_at(SOURCE_LOCATOR)
+
+pid_t xfork_at(const char *where);
+#define xfork() xfork_at(SOURCE_LOCATOR)
+
+void forbid_forking(const char *reason);
+bool may_fork(void);
+\f
+/* Useful functions related to threading. */
+
+int count_cpu_cores(void);