From patchwork Sat Apr 30 03:33:09 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emilio Cota X-Patchwork-Id: 8986601 Return-Path: X-Original-To: patchwork-qemu-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 5CE079F46D for ; Sat, 30 Apr 2016 05:08:30 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9BA06201CE for ; Sat, 30 Apr 2016 05:08:29 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id B8B08201B9 for ; Sat, 30 Apr 2016 05:08:28 +0000 (UTC) Received: from localhost ([::1]:57204 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1awLkR-00057i-HZ for patchwork-qemu-devel@patchwork.kernel.org; Fri, 29 Apr 2016 23:39:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59214) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1awLfM-0003y4-9j for qemu-devel@nongnu.org; Fri, 29 Apr 2016 23:34:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1awLf7-00005c-FV for qemu-devel@nongnu.org; Fri, 29 Apr 2016 23:33:54 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:38108) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1awLf6-0008TV-6N for qemu-devel@nongnu.org; Fri, 29 Apr 2016 23:33:45 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id B3D7720A6F for ; Fri, 29 Apr 2016 23:33:20 -0400 (EDT) Received: from frontend1 ([10.202.2.160]) by compute4.internal (MEProxy); Fri, 29 Apr 2016 23:33:20 -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=v1NdiLNPWe3oO2BY7QkbS4tuOYs =; b=I/YGiUm0diqAcKhjezYwpeQJhuCZe53mrr74KHGqM0q3nNfZubvRcJtYG1w nx6mHjNumkPQ5sGzHzmMtk8TjlrO+o8LxhpIX8cdkNld7KnqqVeQYz5tHosvpiNj 2aiG3xFbluHy8dgoi8qyCXLY+yNmG8ym4MuawCRUP8luddPw= 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=v1Nd iLNPWe3oO2BY7QkbS4tuOYs=; b=rIEO+OmxpcBFzK5hQZepKgFzV8S1fL932L4H RJMctVYwbcxgf8Z4g0zY9BYzPtBjOksKH9kinx8S9+26e5s9+3eQvEdYQwonyzLG r4eBZodAbYxTNvvN4NUgjbm08FQNCtCYzFdz9leBNSnFrI1pUcW2jXF/4YryxePz tO6zZcg= X-Sasl-enc: 4GbrbqJ42F3hJVM2oLqSqaZpqDw4l1XFL9N1MIfvdFwL 1461987200 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 78C41C00012; Fri, 29 Apr 2016 23:33:20 -0400 (EDT) From: "Emilio G. Cota" To: QEMU Developers , MTTCG Devel Date: Fri, 29 Apr 2016 23:33:09 -0400 Message-Id: <1461987197-31264-7-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1461987197-31264-1-git-send-email-cota@braap.org> References: <1461987197-31264-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.29 Subject: [Qemu-devel] [PATCH v4 06/14] 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: Paolo Bonzini , Sergey Fedorov , Richard Henderson , =?UTF-8?q?Alex=20Benn=C3=A9e?= , Peter Crosthwaite Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Spam-Status: No, score=-6.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Guillaume Delbergue Signed-off-by: Guillaume Delbergue [Rewritten. - Paolo] Signed-off-by: Paolo Bonzini [Emilio's additions: use atomic_test_and_set instead of atomic_xchg; call cpu_relax() while spinning; optimize for uncontended locks by acquiring the lock with TAS instead of TATAS.] Signed-off-by: Emilio G. Cota Reviewed-by: Richard Henderson --- include/qemu/thread.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index bdae6df..39ff1ac 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_test_and_set(&spin->value)) { + while (atomic_read(&spin->value)) { + cpu_relax(); + } + } +} + +static inline int qemu_spin_trylock(QemuSpin *spin) +{ + if (atomic_test_and_set(&spin->value)) { + return -EBUSY; + } + return 0; +} + +static inline void qemu_spin_unlock(QemuSpin *spin) +{ + atomic_mb_set(&spin->value, 0); +} + #endif