From patchwork Thu Apr 7 17:32:31 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emilio Cota X-Patchwork-Id: 8777671 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 2A1329F3D1 for ; Thu, 7 Apr 2016 21:50:04 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 52DC9201EC for ; Thu, 7 Apr 2016 21:50:03 +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 6F06D201CD for ; Thu, 7 Apr 2016 21:50:02 +0000 (UTC) Received: from localhost ([::1]:52177 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoHoP-0002mO-Kb for patchwork-qemu-devel@patchwork.kernel.org; Thu, 07 Apr 2016 17:50:01 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:48309) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoExJ-0000yW-3V for qemu-devel@nongnu.org; Thu, 07 Apr 2016 14:47:03 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoEts-0007yn-MQ for qemu-devel@nongnu.org; Thu, 07 Apr 2016 14:43:31 -0400 Received: from lists.gnu.org ([2001:4830:134:3::11]:45176) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoEts-0007yi-IA for qemu-devel@nongnu.org; Thu, 07 Apr 2016 14:43:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:56304) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoEAN-0002gq-IN for qemu-devel@nongnu.org; Thu, 07 Apr 2016 13:56:28 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aoDnR-0003Wd-HF for qemu-devel@nongnu.org; Thu, 07 Apr 2016 13:32:48 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:40502) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aoDnR-0003Vz-Dc for qemu-devel@nongnu.org; Thu, 07 Apr 2016 13:32:45 -0400 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 6C91C2111A for ; Thu, 7 Apr 2016 13:32:44 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute4.internal (MEProxy); Thu, 07 Apr 2016 13:32:44 -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=vsPQfq54jdQI8EMT9AtbL6ehJ4s =; b=kmC1+TFG/DvIAgiScGoUkEl9nb65ZV2FbfWIg+h8JpR4wIwk4uv3hK5G2xr OIJH3+8GjnNtJl6ruGPct6IGRDpUygtnlPpbRaPGHkvcJwl9QThiM/JfUNDJTKg6 N2yCrIErAR3MdMOEiJaKfOQNcqgpLmseCA5FSbxFfPjsthJU= 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=vsPQ fq54jdQI8EMT9AtbL6ehJ4s=; b=pHtUzzIdmiF1s1RAfvy1NxNCGb5nYduW7Mcx jEkfBQ7NKumI9Imgejqx/vXpr4t2ymveNRy9mdBy6KE7QxS01/vAxxMsOrOJd/+l DhUrJDK0SgJOUpdhO8CwlpF/wmyODx+IyzaEQ8E/nbyD5DJUZpx8KwkILcUrRfpt O595kKY= X-Sasl-enc: gc2adK8112Vw3aEc9m7CRx2pmA7IvCzgM4Lwa/gjT3aq 1460050364 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id 33F8E6800F7; Thu, 7 Apr 2016 13:32:44 -0400 (EDT) From: "Emilio G. Cota" To: QEMU Developers , MTTCG Devel Date: Thu, 7 Apr 2016 13:32:31 -0400 Message-Id: <1460050358-25025-7-git-send-email-cota@braap.org> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1460050358-25025-1-git-send-email-cota@braap.org> References: <1460050358-25025-1-git-send-email-cota@braap.org> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2001:4830:134:3::11 Subject: [Qemu-devel] [PATCH v2 06/13] 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+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=ham 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 --- include/qemu/thread.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/include/qemu/thread.h b/include/qemu/thread.h index bdae6df..1aa843b 100644 --- a/include/qemu/thread.h +++ b/include/qemu/thread.h @@ -1,6 +1,8 @@ #ifndef __QEMU_THREAD_H #define __QEMU_THREAD_H 1 +#include +#include "qemu/atomic.h" typedef struct QemuMutex QemuMutex; typedef struct QemuCond QemuCond; @@ -60,4 +62,33 @@ 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) +{ + do { + while (atomic_read(&spin->value)); + } while (atomic_xchg(&spin->value, true)); +} + +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