From patchwork Wed May 18 16:59:19 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Emilio Cota X-Patchwork-Id: 9121461 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 B7D2D9F1D3 for ; Wed, 18 May 2016 16:59:58 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 9B2E9202AE for ; Wed, 18 May 2016 16:59:57 +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 B338E20221 for ; Wed, 18 May 2016 16:59:52 +0000 (UTC) Received: from localhost ([::1]:46160 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b34p5-00034d-QE for patchwork-qemu-devel@patchwork.kernel.org; Wed, 18 May 2016 12:59:51 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:50676) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b34oq-00034F-8H for qemu-devel@nongnu.org; Wed, 18 May 2016 12:59:37 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b34om-00066z-30 for qemu-devel@nongnu.org; Wed, 18 May 2016 12:59:35 -0400 Received: from out5-smtp.messagingengine.com ([66.111.4.29]:58517) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b34oj-00063f-JR for qemu-devel@nongnu.org; Wed, 18 May 2016 12:59:32 -0400 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 1038A20D0D for ; Wed, 18 May 2016 12:59:20 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute3.internal (MEProxy); Wed, 18 May 2016 12:59:20 -0400 DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d=braap.org; h=cc :content-type:date:from:in-reply-to:message-id:mime-version :references:subject:to:x-sasl-enc:x-sasl-enc; s=mesmtp; bh=39C4H vpMgpteF+QQoW4+gm3i/yY=; b=W3IHY9kZjI5nqXpXGcbRdPU88Ouw9I4MjLLzD RGqZPTKmIMfonCJNdZLVK/CgrSjIl+oL02xy5WGrZJOvt8oi4mTMdL/ayi3qBF8B IZe4ZnBfPXbY2SFLG5mqFljqqIL788j70WP6Eqk8eP4/OTt+hvtNALyMj0wIvTqo 8yfOGg= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-sasl-enc :x-sasl-enc; s=smtpout; bh=39C4HvpMgpteF+QQoW4+gm3i/yY=; b=IFuZF N5hJCrtFGlsvf0wfU9KpnCLcFMDdirB4kAgpl2z/Aq/l0c8yyKRsF3GVHPaZVWOo GihKH/fiPt8QQiJbt+/4mIAbRjnRtShYxXtJ7y9VGO+0gXGRN4qltBLf1OvO7agZ mGUT5s2D27UXUZhQKQd+OJbI7tIcutAXrB+RJM= X-Sasl-enc: 5N3ib1XKIiPt03wzKFh2HwrfGjOi1Y9skoRfSzd5m6Z6 1463590759 Received: from localhost (flamenco.cs.columbia.edu [128.59.20.216]) by mail.messagingengine.com (Postfix) with ESMTPA id B9A8D6801DF; Wed, 18 May 2016 12:59:19 -0400 (EDT) Date: Wed, 18 May 2016 12:59:19 -0400 From: "Emilio G. Cota" To: Paolo Bonzini Message-ID: <20160518165919.GA26903@flamenco> References: <573B5134.8060104@gmail.com> <66d14198-dab0-c72e-fe17-d022cff3feff@twiddle.net> <20160517200415.GD30174@flamenco> <573B7CFB.30002@gmail.com> <20160518002814.GA25803@flamenco> <573C79CA.3010703@gmail.com> <573C807D.7050505@gmail.com> <0da2faf1-4d51-3bda-cc2b-81dfac485b80@redhat.com> <573C84A7.6000707@gmail.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.23 (2014-03-12) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 66.111.4.29 Subject: Re: [Qemu-devel] [PATCH v5 07/18] 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: MTTCG Devel , Peter Crosthwaite , QEMU Developers , Sergey Fedorov , Alex =?iso-8859-1?Q?Benn=E9e?= , 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 On Wed, May 18, 2016 at 17:09:48 +0200, Paolo Bonzini wrote: > But honestly I think it would be even better to just use > __sync_lock_test_and_set in the spinlock implementation and not add this > to atomics.h. There's already enough issues with the current subset of > atomics, I am not really happy to add non-SC read-modify-write > operations to the mix. I can drop the two patches that touch atomic.h, and have the spinlock patch as appended. OK with this? Thanks, Emilio commit 99c8f1049a1508edc9de30e3cf27898888aa7f68 Author: Guillaume Delbergue Date: Sun Oct 18 09:44:02 2015 +0200 qemu-thread: add simple test-and-set spinlock Signed-off-by: Guillaume Delbergue [Rewritten. - Paolo] Signed-off-by: Paolo Bonzini [Emilio's additions: use TAS instead of atomic_xchg; emit acquire/release barriers; call cpu_relax() while spinning; optimize for uncontended locks by acquiring the lock with TAS instead of TATAS; add qemu_spin_locked().] Signed-off-by: Emilio G. Cota diff --git a/include/qemu/thread.h b/include/qemu/thread.h index bdae6df..2d225ff 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,40 @@ 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) +{ + __sync_lock_release(&spin->value); +} + +static inline void qemu_spin_lock(QemuSpin *spin) +{ + while (__sync_lock_test_and_set(&spin->value, true)) { + while (atomic_read(&spin->value)) { + cpu_relax(); + } + } +} + +static inline int qemu_spin_trylock(QemuSpin *spin) +{ + if (__sync_lock_test_and_set(&spin->value, true)) { + return -EBUSY; + } + return 0; +} + +static inline bool qemu_spin_locked(QemuSpin *spin) +{ + return atomic_read(&spin->value); +} + +static inline void qemu_spin_unlock(QemuSpin *spin) +{ + __sync_lock_release(&spin->value); +} + #endif