From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49568) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoW3B-0002G0-Qs for qemu-devel@nongnu.org; Fri, 08 Apr 2016 09:02:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoW36-0004iC-3B for qemu-devel@nongnu.org; Fri, 08 Apr 2016 09:02:13 -0400 Received: from mail-wm0-x231.google.com ([2a00:1450:400c:c09::231]:35830) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoW35-0004hB-Tr for qemu-devel@nongnu.org; Fri, 08 Apr 2016 09:02:08 -0400 Received: by mail-wm0-x231.google.com with SMTP id 191so17620809wmq.0 for ; Fri, 08 Apr 2016 06:02:07 -0700 (PDT) References: <1460050358-25025-1-git-send-email-cota@braap.org> <1460050358-25025-7-git-send-email-cota@braap.org> From: Alex =?utf-8?Q?Benn=C3=A9e?= In-reply-to: <1460050358-25025-7-git-send-email-cota@braap.org> Date: Fri, 08 Apr 2016 14:02:11 +0100 Message-ID: <87bn5kb6ek.fsf@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Subject: Re: [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: "Emilio G. Cota" Cc: QEMU Developers , MTTCG Devel , Paolo Bonzini , Peter Crosthwaite , Richard Henderson , Peter Maydell , Sergey Fedorov Emilio G. Cota writes: > 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; If we are throwing true and false around as the only two values can we use bool here and be consistent when setting/clearing. > +} 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 -- Alex Bennée