#include "ovs-atomic.h"
#include "util.h"
+struct seq;
/* Mutex. */
struct OVS_LOCKABLE ovs_mutex {
pthread_mutex_t lock;
- const char *where;
+ const char *where; /* NULL if and only if uninitialized. */
+};
+
+/* Poll-block()-able barrier similar to pthread_barrier_t. */
+struct ovs_barrier {
+ uint32_t size; /* Number of threads to wait. */
+ atomic_uint32_t count; /* Number of threads already hit the barrier. */
+ struct seq *seq;
};
/* "struct ovs_mutex" initializer. */
#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
-#define OVS_MUTEX_INITIALIZER { PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, NULL }
+#define OVS_MUTEX_INITIALIZER { PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP, \
+ "<unlocked>" }
#else
-#define OVS_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, NULL }
+#define OVS_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, "<unlocked>" }
#endif
#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
#define OVS_ADAPTIVE_MUTEX_INITIALIZER \
- { PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, NULL }
+ { PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP, "<unlocked>" }
#else
#define OVS_ADAPTIVE_MUTEX_INITIALIZER OVS_MUTEX_INITIALIZER
#endif
* than exposing them only to porters. */
struct OVS_LOCKABLE ovs_rwlock {
pthread_rwlock_t lock;
- const char *where;
+ const char *where; /* NULL if and only if uninitialized. */
};
/* Initializer. */
#ifdef PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
#define OVS_RWLOCK_INITIALIZER \
- { PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP, NULL }
+ { PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP, "<unlocked>" }
#else
-#define OVS_RWLOCK_INITIALIZER { PTHREAD_RWLOCK_INITIALIZER, NULL }
+#define OVS_RWLOCK_INITIALIZER { PTHREAD_RWLOCK_INITIALIZER, "<unlocked>" }
#endif
/* ovs_rwlock functions analogous to pthread_rwlock_*() functions.
#define ovs_rwlock_tryrdlock(rwlock) \
ovs_rwlock_tryrdlock_at(rwlock, SOURCE_LOCATOR)
+/* ovs_barrier functions analogous to pthread_barrier_*() functions. */
+void ovs_barrier_init(struct ovs_barrier *, uint32_t count);
+void ovs_barrier_destroy(struct ovs_barrier *);
+void ovs_barrier_block(struct ovs_barrier *);
+
/* Wrappers for xpthread_cond_*() that abort the process on any error.
*
* Use ovs_mutex_cond_wait() to wait for a condition. */
void xpthread_cond_signal(pthread_cond_t *);
void xpthread_cond_broadcast(pthread_cond_t *);
-/* Wrappers for pthread_barrier_*() that abort the process on any error. */
-void xpthread_barrier_init(pthread_barrier_t *, pthread_barrierattr_t *,
- unsigned int count);
-int xpthread_barrier_wait(pthread_barrier_t *);
-void xpthread_barrier_destroy(pthread_barrier_t *);
-
void xpthread_key_create(pthread_key_t *, void (*destructor)(void *));
void xpthread_key_delete(pthread_key_t);
void xpthread_setspecific(pthread_key_t, const void *);
-void xpthread_create(pthread_t *, pthread_attr_t *, void *(*)(void *), void *);
+#ifndef _WIN32
+void xpthread_sigmask(int, const sigset_t *, sigset_t *);
+#endif
+
+pthread_t ovs_thread_create(const char *name, void *(*)(void *), void *);
void xpthread_join(pthread_t, void **);
\f
/* Per-thread data.
*
* 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 *);
+struct ovsthread_stats {
+ struct ovs_mutex mutex;
+ void *volatile buckets[16];
+};
+
+void ovsthread_stats_init(struct ovsthread_stats *);
+void ovsthread_stats_destroy(struct ovsthread_stats *);
+
+void *ovsthread_stats_bucket_get(struct ovsthread_stats *,
+ void *(*new_bucket)(void));
+
+#define OVSTHREAD_STATS_FOR_EACH_BUCKET(BUCKET, IDX, STATS) \
+ for ((IDX) = ovs_thread_stats_next_bucket(STATS, 0); \
+ ((IDX) < ARRAY_SIZE((STATS)->buckets) \
+ ? ((BUCKET) = (STATS)->buckets[IDX], true) \
+ : false); \
+ (IDX) = ovs_thread_stats_next_bucket(STATS, (IDX) + 1))
+size_t ovs_thread_stats_next_bucket(const struct ovsthread_stats *, size_t);
\f
+bool single_threaded(void);
+
void assert_single_threaded_at(const char *where);
#define assert_single_threaded() assert_single_threaded_at(SOURCE_LOCATOR)
+#ifndef _WIN32
pid_t xfork_at(const char *where);
#define xfork() xfork_at(SOURCE_LOCATOR)
+#endif
void forbid_forking(const char *reason);
bool may_fork(void);