From patchwork Tue Apr 19 23:07:44 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emilio Cota X-Patchwork-Id: 612406 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qqLKZ327Sz9t4c for ; Wed, 20 Apr 2016 09:08:34 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=braap.org header.i=@braap.org header.b=Yf0hPq8c; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=messagingengine.com header.i=@messagingengine.com header.b=pCxAzJFO; dkim-atps=neutral Received: from localhost ([::1]:37918 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aseky-0003HF-8H for incoming@patchwork.ozlabs.org; Tue, 19 Apr 2016 19:08:32 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47952) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1asekP-00025K-Qm for qemu-devel@nongnu.org; Tue, 19 Apr 2016 19:07:59 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1asekN-0000hK-9D for qemu-devel@nongnu.org; Tue, 19 Apr 2016 19:07:57 -0400 Received: from out2-smtp.messagingengine.com ([66.111.4.26]:59822) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1asekN-0000gz-5y for qemu-devel@nongnu.org; Tue, 19 Apr 2016 19:07:55 -0400 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 2CF9F20AC0 for ; Tue, 19 Apr 2016 19:07:55 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute6.internal (MEProxy); Tue, 19 Apr 2016 19:07:55 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=braap.org; h=cc :date:from:in-reply-to:message-id:references:subject:to :x-sasl-enc:x-sasl-enc; s=mesmtp; bh=lyVjqsQgqDzWhzizTheJQ8kvaEY =; b=Yf0hPq8cnW50mHcR9VvLIZ9bGtIcBJaeZbObLJgjxW5mj1B1Yno4aZ/yuUg gbbJLeoGNKVyFRlObEuSaPcCv6eprolU0GgKNQAcLu93WinpMZLRYvSW7M43KEsd AMhz0U5uUai8eC5B2ZK3ZsefnLA15QleBW5aRAw08dLtIHq0= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:date:from:in-reply-to:message-id :references:subject:to:x-sasl-enc:x-sasl-enc; s=smtpout; bh=lyVj qsQgqDzWhzizTheJQ8kvaEY=; b=pCxAzJFO0tJxMcs2DRJfAWSJTxPrLGaoxWWV MsOBYghTemwPhHo1J0lrTMNbR5NqpIwOwvLFghWScB1gCWhxx6F8HQ0hnIDZ+jID MAvMFBSpb2fZHo5urQJgohiuUryxYC39nRlmR/h4JFI11OplhVIJkMIy0vbSJVGn u2wc72w= X-Sasl-enc: vt7SXUMGOqbHC2Vqgu13bhTvIlWQxJO2HT5OzkFMHpGV 1461107274 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id DD6DD680264; Tue, 19 Apr 2016 19:07:54 -0400 (EDT) From: "Emilio G. Cota" To: QEMU Developers , MTTCG Devel Date: Tue, 19 Apr 2016 19:07:44 -0400 Message-Id: <1461107270-19234-6-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1461107270-19234-1-git-send-email-cota@braap.org> References: <1461107270-19234-1-git-send-email-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.26 Subject: [Qemu-devel] [PATCH v3 05/11] qemu-thread: add simple test-and-set spinlock X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Peter Maydell , Peter Crosthwaite , Sergey Fedorov , Paolo Bonzini , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Richard Henderson Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 xchg+test instead of test+xchg+test.] 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..e2af57c 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_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