From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39241) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b7lCy-0001iR-1f for qemu-devel@nongnu.org; Tue, 31 May 2016 11:03:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1b7lCw-0005l7-VQ for qemu-devel@nongnu.org; Tue, 31 May 2016 11:03:51 -0400 Received: from mail-it0-x242.google.com ([2607:f8b0:4001:c0b::242]:33894) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1b7lCw-0005kh-Qd for qemu-devel@nongnu.org; Tue, 31 May 2016 11:03:50 -0400 Received: by mail-it0-x242.google.com with SMTP id k76so10967557ita.1 for ; Tue, 31 May 2016 08:03:50 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <20160526163549.3276-2-a.rigo@virtualopensystems.com> References: <20160526163549.3276-1-a.rigo@virtualopensystems.com> <20160526163549.3276-2-a.rigo@virtualopensystems.com> From: Pranith Kumar Date: Tue, 31 May 2016 11:03:20 -0400 Message-ID: Content-Type: text/plain; charset=UTF-8 Subject: Re: [Qemu-devel] [RFC 01/10] exec: Introduce tcg_exclusive_{lock, unlock}() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Alvise Rigo Cc: MTTCG Devel , =?UTF-8?B?QWxleCBCZW5uw6ll?= , qemu-devel , jani.kokkonen@huawei.com, claudio.fontana@huawei.com, tech@virtualopensystems.com, =?UTF-8?B?S09OUkFEIEZyw6lkw6lyaWM=?= , Paolo Bonzini , Richard Henderson , Sergey Fedorov , "Emilio G. Cota" , Peter Maydell Hi Alvise, On Thu, May 26, 2016 at 12:35 PM, Alvise Rigo wrote: > Add tcg_exclusive_{lock,unlock}() functions that will be used for making > the emulation of LL and SC instructions thread safe. > > Signed-off-by: Alvise Rigo > +__thread bool cpu_have_exclusive_lock; > +QemuSpin cpu_exclusive_lock; > +inline void tcg_exclusive_lock(void) > +{ > + if (!cpu_have_exclusive_lock) { > + qemu_spin_lock(&cpu_exclusive_lock); > + cpu_have_exclusive_lock = true; > + } > +} > + > +inline void tcg_exclusive_unlock(void) > +{ > + if (cpu_have_exclusive_lock) { > + cpu_have_exclusive_lock = false; > + qemu_spin_unlock(&cpu_exclusive_lock); > + } > +} I think the unlock() here should have an assert if cpu_have_exclusive_lock is false. From what I can see, a thread should either take the exclusive lock or wait spinning for it in lock(). So unlock() should always see cpu_have_exclusive_lock as true. It is a good place to find locking bugs. -- Pranith