aboutsummaryrefslogtreecommitdiff
path: root/lib/ovs-thread.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2013-06-19 11:21:47 -0700
committerBen Pfaff <blp@nicira.com>2013-06-28 16:09:36 -0700
commit1514b275558304c63bc3838c2da5c4be0c1cbef0 (patch)
treea460f9a18cf7ee9a4c70a99cf51676733d0c0720 /lib/ovs-thread.c
parent31a3fc6e3e9ce68d8bfebf65150d9455b9334dda (diff)
ovs-thread: Add support for convenient once-only initializers.
pthread_once() is portable but it 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. This commit introduces an alternative with a more convenient interface. Signed-off-by: Ben Pfaff <blp@nicira.com> Acked-by: Ethan Jackson <ethan@nicira.com>
Diffstat (limited to 'lib/ovs-thread.c')
-rw-r--r--lib/ovs-thread.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/lib/ovs-thread.c b/lib/ovs-thread.c
index 23224323..e1e72891 100644
--- a/lib/ovs-thread.c
+++ b/lib/ovs-thread.c
@@ -88,4 +88,22 @@ xpthread_create(pthread_t *threadp, pthread_attr_t *attr,
ovs_abort(error, "pthread_create failed");
}
}
+
+bool
+ovsthread_once_start__(struct ovsthread_once *once)
+{
+ xpthread_mutex_lock(&once->mutex);
+ if (!ovsthread_once_is_done__(once)) {
+ return false;
+ }
+ xpthread_mutex_unlock(&once->mutex);
+ return true;
+}
+
+void OVS_RELEASES(once)
+ovsthread_once_done(struct ovsthread_once *once)
+{
+ atomic_store(&once->done, true);
+ xpthread_mutex_unlock(&once->mutex);
+}
#endif