From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48309) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoExJ-0000yW-3V for qemu-devel@nongnu.org; Thu, 07 Apr 2016 14:47:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoEts-0007yn-MQ for qemu-devel@nongnu.org; Thu, 07 Apr 2016 14:43:31 -0400 Received: from lists.gnu.org ([2001:4830:134:3::11]:45176) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoEts-0007yi-IA for qemu-devel@nongnu.org; Thu, 07 Apr 2016 14:43:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56304) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoEAN-0002gq-IN for qemu-devel@nongnu.org; Thu, 07 Apr 2016 13:56:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoDnR-0003Wd-HF for qemu-devel@nongnu.org; Thu, 07 Apr 2016 13:32:48 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:40502) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoDnR-0003Vz-Dc for qemu-devel@nongnu.org; Thu, 07 Apr 2016 13:32:45 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 6C91C2111A for ; Thu, 7 Apr 2016 13:32:44 -0400 (EDT) From: "Emilio G. Cota" Date: Thu, 7 Apr 2016 13:32:31 -0400 Message-Id: <1460050358-25025-7-git-send-email-cota@braap.org> In-Reply-To: <1460050358-25025-1-git-send-email-cota@braap.org> References: <1460050358-25025-1-git-send-email-cota@braap.org> Subject: [Qemu-devel] [PATCH v2 06/13] qemu-thread: add simple test-and-set spinlock List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: QEMU Developers , MTTCG Devel Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Paolo Bonzini , Peter Crosthwaite , Richard Henderson , Peter Maydell , Sergey Fedorov From: Guillaume Delbergue Signed-off-by: Guillaume Delbergue [Rewritten. - Paolo] Signed-off-by: Paolo Bonzini --- include/qemu/thread.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index bdae6df..1aa843b 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -1,6 +1,8 @@ #ifndef __QEMU_THREAD_H #define __QEMU_THREAD_H 1 +#include +#include "qemu/atomic.h" typedef struct QemuMutex QemuMutex; typedef struct QemuCond QemuCond; @@ -60,4 +62,33 @@ struct Notifier; void qemu_thread_atexit_add(struct Notifier *notifier); void qemu_thread_atexit_remove(struct Notifier *notifier); +typedef struct QemuSpin { + int value; +} QemuSpin; + +static inline void qemu_spin_init(QemuSpin *spin) +{ + spin->value = 0; +} + +static inline void qemu_spin_lock(QemuSpin *spin) +{ + do { + while (atomic_read(&spin->value)); + } while (atomic_xchg(&spin->value, true)); +} + +static inline int qemu_spin_trylock(QemuSpin *spin) +{ + if (atomic_read(&spin->value) || atomic_xchg(&spin->value, true)) { + return -EBUSY; + } + return 0; +} + +static inline void qemu_spin_unlock(QemuSpin *spin) +{ + atomic_mb_set(&spin->value, 0); +} + #endif -- 2.5.0