From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:60454) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1asw2h-0006gg-Sg for qemu-devel@nongnu.org; Wed, 20 Apr 2016 13:36:00 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1asw2d-0003Ut-QU for qemu-devel@nongnu.org; Wed, 20 Apr 2016 13:35:59 -0400 Received: from out2-smtp.messagingengine.com ([66.111.4.26]:36381) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1asw2d-0003Up-MZ for qemu-devel@nongnu.org; Wed, 20 Apr 2016 13:35:55 -0400 Received: from compute2.internal (compute2.nyi.internal [10.202.2.42]) by mailout.nyi.internal (Postfix) with ESMTP id 84385216CA for ; Wed, 20 Apr 2016 13:35:55 -0400 (EDT) From: "Emilio G. Cota" Date: Wed, 20 Apr 2016 13:35:55 -0400 Message-Id: <1461173755-22035-1-git-send-email-cota@braap.org> In-Reply-To: <1461107270-19234-6-git-send-email-cota@braap.org> References: <1461107270-19234-6-git-send-email-cota@braap.org> Subject: [Qemu-devel] [UPDATED v3 05/11] 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 [Emilio's additions: call cpu_relax() while spinning; optimize for uncontended locks by acquiring the lock with test-and-set instead of test-and-test-and-set.] Signed-off-by: Emilio G. Cota --- include/qemu/thread.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index bdae6df..a216941 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,35 @@ 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) +{ + while (atomic_xchg(&spin->value, true)) { + while (atomic_read(&spin->value)) { + cpu_relax(); + } + } +} + +static inline int qemu_spin_trylock(QemuSpin *spin) +{ + if (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