From patchwork Fri May 12 10:20:40 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Hildenbrand X-Patchwork-Id: 9723855 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4E45F600CB for ; Fri, 12 May 2017 10:20:56 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 43962287EE for ; Fri, 12 May 2017 10:20:56 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 38723287FB; Fri, 12 May 2017 10:20:56 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CEF0F287EE for ; Fri, 12 May 2017 10:20:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756802AbdELKUx (ORCPT ); Fri, 12 May 2017 06:20:53 -0400 Received: from mx1.redhat.com ([209.132.183.28]:45958 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756428AbdELKUx (ORCPT ); Fri, 12 May 2017 06:20:53 -0400 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 830C380C0C; Fri, 12 May 2017 10:20:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 830C380C0C Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx02.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=david@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 830C380C0C Received: from t460s.redhat.com (ovpn-116-140.ams2.redhat.com [10.36.116.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id B2E408B766; Fri, 12 May 2017 10:20:49 +0000 (UTC) From: David Hildenbrand To: kvm@vger.kernel.org Cc: Paolo Bonzini , =?UTF-8?q?Radim=20Kr=C4=8Dm=C3=A1=C5=99?= , Thomas Huth , david@redhat.com, Laurent Vivier , kvm-ppc@vger.kernel.org Subject: [kvm-unit-tests PATCH v1 1/3] lib: provide generic spinlock Date: Fri, 12 May 2017 12:20:40 +0200 Message-Id: <20170512102042.4956-2-david@redhat.com> In-Reply-To: <20170512102042.4956-1-david@redhat.com> References: <20170512102042.4956-1-david@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Fri, 12 May 2017 10:20:52 +0000 (UTC) Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Let's provide a basic lock implementation that should work on most architectures. Signed-off-by: David Hildenbrand Reviewed-by: Thomas Huth --- 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); +} + #endif