From patchwork Fri Apr 15 14:23:42 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Alex_Benn=C3=A9e?= X-Patchwork-Id: 65929 Delivered-To: patch@linaro.org Received: by 10.140.93.198 with SMTP id d64csp1213213qge; Fri, 15 Apr 2016 07:26:42 -0700 (PDT) X-Received: by 10.140.42.68 with SMTP id b62mr25386497qga.12.1460730402319; Fri, 15 Apr 2016 07:26:42 -0700 (PDT) Return-Path: Received: from lists.gnu.org (lists.gnu.org. [2001:4830:134:3::11]) by mx.google.com with ESMTPS id d142si15349675qkc.27.2016.04.15.07.26.42 for (version=TLS1 cipher=AES128-SHA bits=128/128); Fri, 15 Apr 2016 07:26:42 -0700 (PDT) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) client-ip=2001:4830:134:3::11; Authentication-Results: mx.google.com; dkim=fail header.i=@linaro.org; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 2001:4830:134:3::11 as permitted sender) smtp.mailfrom=qemu-devel-bounces+patch=linaro.org@nongnu.org; dmarc=fail (p=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1]:35149 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar4hl-0005lh-O8 for patch@linaro.org; Fri, 15 Apr 2016 10:26:41 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:49361) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar4f6-0000NL-Or for qemu-devel@nongnu.org; Fri, 15 Apr 2016 10:23:57 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ar4f5-0007d5-MT for qemu-devel@nongnu.org; Fri, 15 Apr 2016 10:23:56 -0400 Received: from mail-wm0-x234.google.com ([2a00:1450:400c:c09::234]:35858) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar4f5-0007cs-Cf for qemu-devel@nongnu.org; Fri, 15 Apr 2016 10:23:55 -0400 Received: by mail-wm0-x234.google.com with SMTP id v188so33555185wme.1 for ; Fri, 15 Apr 2016 07:23:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=mTDwsJZdxhWT1fJhhxNVrggvUf6nAUTAwm0Mo4xHzZA=; b=iV9Pnk/sOFZ61q/V9JJ1sdCb+pWp40bK7cCP+xDoAqeRioGA3MK2qL6NQzwM4kttip qepcP7oK1w1f8Xo8VIzao5UD9b8+nk5K7ZxRua2YMDyueWxALIQ16+5o6lNN5hSrdxuY IK5/gEfKrqg0ZjBJeQqLIfW90CgPmVup4hFDs= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=mTDwsJZdxhWT1fJhhxNVrggvUf6nAUTAwm0Mo4xHzZA=; b=i1K+FNYNaoPMYQiVCYiFg0yOe8dVnEVLN25gUaxp2ZHAGX9kxbvaqluz3H7W4Qr4Xw O9ZmkoFOMU/8YvcSaun8dck0Q3htitOY3h4Ya+NkrxEpk9jOk/Clxg0uKxSU1PaKQJ7d 5sd1sO/ns5Z/sl/Q7FtWMh45FcBEfDg0gXLrVAYtHVgHuaJt5bXEo4RRG5EYszr6bMUl 93F+fN015dSTbgqUJrRQRgLTWaUYLhZmTvxgG+yT98/1MOgHjbn07tvAOKk7VvK0P1GX kBRw34fBAKMbUMDv0BCCZQvN7aULasaYkU7IHuzwZ5mdYnUTqY7vlolONgKNH4J1cr8h Gekg== X-Gm-Message-State: AOPr4FWIRi1k2ajqOVNQvYbRGzhYRCX/okU6Jo7lwK7lS9OCkegL3XZRfjwMTL6T4n8bfCk9 X-Received: by 10.28.220.67 with SMTP id t64mr4589868wmg.57.1460730234741; Fri, 15 Apr 2016 07:23:54 -0700 (PDT) Received: from zen.linaro.local ([81.128.185.34]) by smtp.gmail.com with ESMTPSA id v6sm38594644wmv.16.2016.04.15.07.23.49 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 15 Apr 2016 07:23:52 -0700 (PDT) Received: from zen.linaroharston (localhost [127.0.0.1]) by zen.linaro.local (Postfix) with ESMTP id 930A33E0556; Fri, 15 Apr 2016 15:24:04 +0100 (BST) From: =?UTF-8?q?Alex=20Benn=C3=A9e?= To: mttcg@listserver.greensocs.com, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, serge.fdrv@gmail.com, cota@braap.org Date: Fri, 15 Apr 2016 15:23:42 +0100 Message-Id: <1460730231-1184-5-git-send-email-alex.bennee@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1460730231-1184-1-git-send-email-alex.bennee@linaro.org> References: <1460730231-1184-1-git-send-email-alex.bennee@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::234 Subject: [Qemu-devel] [RFC v1 03/12] 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@linaro.org, claudio.fontana@huawei.com, jan.kiszka@siemens.com, mark.burton@greensocs.com, qemu-devel@nongnu.org, Guillaume Delbergue , pbonzini@redhat.com, =?UTF-8?q?Alex=20Benn=C3=A9e?= , rth@twiddle.net Errors-To: qemu-devel-bounces+patch=linaro.org@nongnu.org Sender: "Qemu-devel" This is a temporary squashed patch of the following patches from Emilio's qht series. This includes: - include/processor.h: define cpu_relax() - qemu-thread: add simple test-and-set spinlock - qemu-thread: call cpu_relax() while spinning - qemu-thread: optimize spin_lock for uncontended locks The sign-offs where from: Signed-off-by: Emilio G. Cota Signed-off-by: Guillaume Delbergue [Rewritten. - Paolo] Signed-off-by: Paolo Bonzini Signed-off-by: Alex Bennée --- include/qemu/processor.h | 28 ++++++++++++++++++++++++++++ include/qemu/thread.h | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 include/qemu/processor.h -- 2.7.4 diff --git a/include/qemu/processor.h b/include/qemu/processor.h new file mode 100644 index 0000000..675a00a --- /dev/null +++ b/include/qemu/processor.h @@ -0,0 +1,28 @@ +#ifndef QEMU_PROCESSOR_H +#define QEMU_PROCESSOR_H + +#include "qemu/atomic.h" + +#if defined(__i386__) || defined(__x86_64__) +#define cpu_relax() asm volatile("rep; nop" ::: "memory") +#endif + +#ifdef __ia64__ +#define cpu_relax() asm volatile("hint @pause" ::: "memory") +#endif + +#ifdef __aarch64__ +#define cpu_relax() asm volatile("yield" ::: "memory") +#endif + +#if defined(__powerpc64__) +/* set Hardware Multi-Threading (HMT) priority to low; then back to medium */ +#define cpu_relax() asm volatile("or 1, 1, 1;" + "or 2, 2, 2;" ::: "memory") +#endif + +#ifndef cpu_relax +#define cpu_relax() barrier() +#endif + +#endif /* QEMU_PROCESSOR_H */ 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