Patchwork [RFC,04/13] qemu-threads: add QemuOnce

login
register
mail settings
Submitter Paolo Bonzini
Date Aug. 15, 2011, 9:08 p.m.
Message ID <1313442520-12062-5-git-send-email-pbonzini@redhat.com>
Download mbox | patch
Permalink /patch/110098/
State New
Headers show

Comments

Paolo Bonzini - Aug. 15, 2011, 9:08 p.m.
Add Windows and POSIX versions, as usual.

Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
 compiler.h          |    2 ++
 qemu-thread-posix.c |    5 +++++
 qemu-thread-posix.h |    5 +++++
 qemu-thread-win32.c |   19 +++++++++++++++++++
 qemu-thread-win32.h |    5 +++++
 qemu-thread.h       |    3 +++
 6 files changed, 39 insertions(+), 0 deletions(-)

Patch

diff --git a/compiler.h b/compiler.h
index 9af5dc6..c87dd96 100644
--- a/compiler.h
+++ b/compiler.h
@@ -31,4 +31,6 @@ 
 #define GCC_FMT_ATTR(n, m)
 #endif
 
+#define ACCESS_ONCE(x)      (*(volatile typeof(x) *)&(x))
+
 #endif /* COMPILER_H */
diff --git a/qemu-thread-posix.c b/qemu-thread-posix.c
index 50e7421..894134c 100644
--- a/qemu-thread-posix.c
+++ b/qemu-thread-posix.c
@@ -239,6 +239,11 @@  void qemu_event_wait(QemuEvent *ev)
     }
 }
 
+void qemu_once(QemuOnce *o, void (*func)(void))
+{
+    pthread_once(&o->once, func);
+}
+
 void qemu_thread_create(QemuThread *thread,
                        void *(*start_routine)(void*),
                        void *arg)
diff --git a/qemu-thread-posix.h b/qemu-thread-posix.h
index 2f5b63d..d781ca6 100644
--- a/qemu-thread-posix.h
+++ b/qemu-thread-posix.h
@@ -18,6 +18,11 @@  struct QemuEvent {
     unsigned value;
 };
 
+#define QEMU_ONCE_INIT { .once = PTHREAD_ONCE_INIT }
+struct QemuOnce {
+    pthread_once_t once;
+};
+
 struct QemuThread {
     pthread_t thread;
 };
diff --git a/qemu-thread-win32.c b/qemu-thread-win32.c
index 9bdbb48..7b1c407 100644
--- a/qemu-thread-win32.c
+++ b/qemu-thread-win32.c
@@ -12,6 +12,7 @@ 
  */
 #include "qemu-common.h"
 #include "qemu-thread.h"
+#include "qemu-barrier.h"
 #include <process.h>
 #include <assert.h>
 #include <limits.h>
@@ -218,6 +219,24 @@  void qemu_event_wait(QemuEvent *ev)
     WaitForSingleObject(ev->event, INFINITE);
 }
 
+void qemu_once(QemuOnce *o, void (*func)(void))
+{
+    int old;
+    if (once->state != 2) {
+        old = __sync_val_compare_and_swap (&once->state, 0, 1);
+        if (old == 0) {
+            func();
+            once->state = 2;
+            smp_mb();
+            return;
+        }
+        /* Busy wait until the first thread gives us a green flag.  */
+        while (ACCESS_ONCE(once->state) == 1) {
+            Sleep(0);
+        }
+    }
+}
+
 struct QemuThreadData {
     QemuThread *thread;
     void *(*start_routine)(void *);
diff --git a/qemu-thread-win32.h b/qemu-thread-win32.h
index ddd6d0f..fbee4d9 100644
--- a/qemu-thread-win32.h
+++ b/qemu-thread-win32.h
@@ -17,6 +17,11 @@  struct QemuEvent {
     HANDLE event;
 };
 
+#define QEMU_ONCE_INIT = { .state = 0 }
+struct QemuOnce {
+    int state;
+}
+
 struct QemuThread {
     HANDLE thread;
     void *ret;
diff --git a/qemu-thread.h b/qemu-thread.h
index 8353e3d..ae75638 100644
--- a/qemu-thread.h
+++ b/qemu-thread.h
@@ -7,6 +7,7 @@ 
 typedef struct QemuMutex QemuMutex;
 typedef struct QemuCond QemuCond;
 typedef struct QemuEvent QemuEvent;
+typedef struct QemuOnce QemuOnce;
 typedef struct QemuThread QemuThread;
 
 #ifdef _WIN32
@@ -39,6 +40,8 @@  void qemu_event_reset(QemuEvent *ev);
 void qemu_event_wait(QemuEvent *ev);
 void qemu_event_destroy(QemuEvent *ev);
 
+void qemu_once(QemuOnce *o, void (*func)(void));
+
 void qemu_thread_create(QemuThread *thread,
                        void *(*start_routine)(void*),
                        void *arg);