qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Paolo Bonzini <pbonzini@redhat.com>
To: qemu-devel@nongnu.org
Subject: [Qemu-devel] [RFC PATCH 04/13] qemu-threads: add QemuOnce
Date: Mon, 15 Aug 2011 14:08:31 -0700	[thread overview]
Message-ID: <1313442520-12062-5-git-send-email-pbonzini@redhat.com> (raw)
In-Reply-To: <1313442520-12062-1-git-send-email-pbonzini@redhat.com>

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(-)

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);
-- 
1.7.6

  parent reply	other threads:[~2011-08-15 21:09 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-15 21:08 [Qemu-devel] [RFC PATCH 00/13] RCU implementation for QEMU Paolo Bonzini
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 01/13] add smp_mb() Paolo Bonzini
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 02/13] rename qemu_event_{init,read} Paolo Bonzini
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 03/13] qemu-threads: add QemuEvent Paolo Bonzini
2011-08-17 17:09   ` Blue Swirl
2011-08-15 21:08 ` Paolo Bonzini [this message]
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 05/13] add rcu library Paolo Bonzini
2011-08-17 17:04   ` Blue Swirl
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 06/13] rcu: add rcutorture Paolo Bonzini
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 07/13] osdep: add qemu_msleep Paolo Bonzini
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 08/13] add call_rcu support Paolo Bonzini
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 09/13] rcu: avoid repeated system calls Paolo Bonzini
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 10/13] rcu: report quiescent states Paolo Bonzini
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 11/13] rcuify iohandlers Paolo Bonzini
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 12/13] split MRU ram list Paolo Bonzini
2011-08-15 21:08 ` [Qemu-devel] [RFC PATCH 13/13] RCUify ram_list Paolo Bonzini

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1313442520-12062-5-git-send-email-pbonzini@redhat.com \
    --to=pbonzini@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).