From mboxrd@z Thu Jan 1 00:00:00 1970 From: Radim =?utf-8?B?S3LEjW3DocWZ?= Subject: Re: [kvm-unit-tests PATCH v1 1/3] lib: provide generic spinlock Date: Fri, 12 May 2017 18:43:13 +0200 Message-ID: <20170512164313.GA12549@potion> References: <20170512102042.4956-1-david@redhat.com> <20170512102042.4956-2-david@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Cc: kvm@vger.kernel.org, Paolo Bonzini , Thomas Huth , Laurent Vivier , kvm-ppc@vger.kernel.org To: David Hildenbrand Return-path: Received: from mx1.redhat.com ([209.132.183.28]:42962 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933093AbdELQnV (ORCPT ); Fri, 12 May 2017 12:43:21 -0400 Content-Disposition: inline In-Reply-To: <20170512102042.4956-2-david@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: 2017-05-12 12:20+0200, David Hildenbrand: > Let's provide a basic lock implementation that should work on most > architectures. > > Signed-off-by: David Hildenbrand > --- > lib/asm-generic/spinlock.h | 16 +++++++++++++++- > 1 file changed, 15 insertions(+), 1 deletion(-) > > diff --git a/lib/asm-generic/spinlock.h b/lib/asm-generic/spinlock.h > index 3141744..e8c3a58 100644 > --- a/lib/asm-generic/spinlock.h > +++ b/lib/asm-generic/spinlock.h > @@ -1,4 +1,18 @@ > #ifndef _ASM_GENERIC_SPINLOCK_H_ > #define _ASM_GENERIC_SPINLOCK_H_ > -#error need architecture specific asm/spinlock.h > + > +struct spinlock { > + unsigned int v; > +}; > + > +static inline void spin_lock(struct spinlock *lock) > +{ > + while (!__sync_bool_compare_and_swap(&lock->v, 0, 1)); > +} > + > +static inline void spin_unlock(struct spinlock *lock) > +{ > + __sync_bool_compare_and_swap(&lock->v, 1, 0); > +} x86 would be better with __sync_lock_test_and_set() and __sync_lock_release() as they generate the same code we have now, instead of two locked cmpxchgs. GCC mentions that some targets might have problems with that, but they seem to fall back to boolean value and compare-and-swap. Any reason to avoid "while(__sync_lock_test_and_set(&lock->v, 1));"? Thanks.