From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:46889) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b1QM3-0002s2-5N for qemu-devel@nongnu.org; Fri, 13 May 2016 23:35:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b1QLy-0005Ko-Vj for qemu-devel@nongnu.org; Fri, 13 May 2016 23:35:02 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:43816) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b1QLw-0005JU-NI for qemu-devel@nongnu.org; Fri, 13 May 2016 23:34:58 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id F3BC920B8E for ; Fri, 13 May 2016 23:34:41 -0400 (EDT) From: "Emilio G. Cota" Date: Fri, 13 May 2016 23:34:22 -0400 Message-Id: <1463196873-17737-8-git-send-email-cota@braap.org> In-Reply-To: <1463196873-17737-1-git-send-email-cota@braap.org> References: <1463196873-17737-1-git-send-email-cota@braap.org> Subject: [Qemu-devel] [PATCH v5 07/18] 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 , Sergey Fedorov From: Guillaume Delbergue Signed-off-by: Guillaume Delbergue [Rewritten. - Paolo] Signed-off-by: Paolo Bonzini [Emilio's additions: use TAS instead of atomic_xchg; emit acquire/release barriers; call cpu_relax() while spinning; optimize for uncontended locks by acquiring the lock with TAS instead of TATAS; add qemu_spin_locked().] Signed-off-by: Emilio G. Cota --- include/qemu/thread.h | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index bdae6df..4b74ee5 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -1,6 +1,9 @@ #ifndef __QEMU_THREAD_H #define __QEMU_THREAD_H 1 +#include +#include "qemu/processor.h" +#include "qemu/atomic.h" typedef struct QemuMutex QemuMutex; typedef struct QemuCond QemuCond; @@ -60,4 +63,40 @@ 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) +{ + atomic_set_release(&spin->value, 0); +} + +static inline void qemu_spin_lock(QemuSpin *spin) +{ + while (atomic_test_and_set_acquire(&spin->value)) { + while (atomic_read(&spin->value)) { + cpu_relax(); + } + } +} + +static inline int qemu_spin_trylock(QemuSpin *spin) +{ + if (atomic_test_and_set_acquire(&spin->value)) { + return -EBUSY; + } + return 0; +} + +static inline bool qemu_spin_locked(QemuSpin *spin) +{ + return atomic_read_acquire(&spin->value); +} + +static inline void qemu_spin_unlock(QemuSpin *spin) +{ + atomic_set_release(&spin->value, 0); +} + #endif -- 2.5.0