From patchwork Mon Aug 15 21:08:31 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 110098 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [140.186.70.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id B3447B6F77 for ; Tue, 16 Aug 2011 07:39:56 +1000 (EST) Received: from localhost ([::1]:51385 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qt4QL-0006D0-8h for incoming@patchwork.ozlabs.org; Mon, 15 Aug 2011 17:10:17 -0400 Received: from eggs.gnu.org ([140.186.70.92]:49340) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qt4Pl-0004rq-TA for qemu-devel@nongnu.org; Mon, 15 Aug 2011 17:09:44 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Qt4Pk-0000Gx-QV for qemu-devel@nongnu.org; Mon, 15 Aug 2011 17:09:41 -0400 Received: from mail-pz0-f42.google.com ([209.85.210.42]:56686) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Qt4Pk-0000G7-A1 for qemu-devel@nongnu.org; Mon, 15 Aug 2011 17:09:40 -0400 Received: by mail-pz0-f42.google.com with SMTP id 37so9398728pzk.29 for ; Mon, 15 Aug 2011 14:09:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=sender:from:to:subject:date:message-id:x-mailer:in-reply-to :references; bh=BFBfcxwMaSRi1s3dND81Sw3/VNjFjdKxt4fwR+7v8rk=; b=Udthr8OUjIlU5cjPgIxR3MYwIWie3I2WKQnFtN1mO9WXdTReKFJ1RqkYpNUTdIHFiH y0MNdtv/TOE+06+6jIHWMDJoo3zyYf939to1zITOA4Aph0s+O4QUmqao95Hifgjx09Qi uWS7hyYICzs59QFwSiNqDp7kOqC0nKVlfR39Q= Received: by 10.142.156.20 with SMTP id d20mr2047170wfe.119.1313442579975; Mon, 15 Aug 2011 14:09:39 -0700 (PDT) Received: from localhost.localdomain ([216.123.155.199]) by mx.google.com with ESMTPS id i8sm1064587pbi.92.2011.08.15.14.09.38 (version=TLSv1/SSLv3 cipher=OTHER); Mon, 15 Aug 2011 14:09:39 -0700 (PDT) From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Mon, 15 Aug 2011 14:08:31 -0700 Message-Id: <1313442520-12062-5-git-send-email-pbonzini@redhat.com> X-Mailer: git-send-email 1.7.6 In-Reply-To: <1313442520-12062-1-git-send-email-pbonzini@redhat.com> References: <1313442520-12062-1-git-send-email-pbonzini@redhat.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 209.85.210.42 Subject: [Qemu-devel] [RFC PATCH 04/13] qemu-threads: add QemuOnce X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Add Windows and POSIX versions, as usual. Signed-off-by: Paolo Bonzini --- 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 #include #include @@ -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);