From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34257) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1asxya-0000KF-Je for qemu-devel@nongnu.org; Wed, 20 Apr 2016 15:39:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1asxyW-0003J2-5e for qemu-devel@nongnu.org; Wed, 20 Apr 2016 15:39:52 -0400 Received: from mail-qg0-x233.google.com ([2607:f8b0:400d:c04::233]:35215) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1asxyW-0003Is-26 for qemu-devel@nongnu.org; Wed, 20 Apr 2016 15:39:48 -0400 Received: by mail-qg0-x233.google.com with SMTP id f74so31660665qge.2 for ; Wed, 20 Apr 2016 12:39:47 -0700 (PDT) Sender: Richard Henderson References: <1461107270-19234-1-git-send-email-cota@braap.org> <1461107270-19234-6-git-send-email-cota@braap.org> <57179DE0.5080701@twiddle.net> <20160420171734.GA1124@flamenco> <5717C2A1.9010805@twiddle.net> <20160420181132.GA24862@flamenco> From: Richard Henderson Message-ID: <5717DB01.5080802@twiddle.net> Date: Wed, 20 Apr 2016 12:39:45 -0700 MIME-Version: 1.0 In-Reply-To: <20160420181132.GA24862@flamenco> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v3 05/11] qemu-thread: add simple test-and-set spinlock List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: "Emilio G. Cota" Cc: QEMU Developers , MTTCG Devel , =?UTF-8?Q?Alex_Benn=c3=a9e?= , Paolo Bonzini , Peter Crosthwaite , Peter Maydell , Sergey Fedorov On 04/20/2016 11:11 AM, Emilio G. Cota wrote: > On Wed, Apr 20, 2016 at 10:55:45 -0700, Richard Henderson wrote: >> On 04/20/2016 10:17 AM, Emilio G. Cota wrote: >>> I've tried to find a GCC intrinsic for test-and-set, and I've only found >>> lock_test_and_set, which is what we use for atomic_xchg (except on ppc) >>> because it really is an atomic exchange: >>> "This builtin, as described by Intel, is not a traditional test-and-set >>> operation, but rather an atomic exchange operation." >>> https://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html >> >> Please read the entire documentation, not just the first sentence. > > I did read it and I'm aware of the limitations of using xchg. > > My comment was related to this: > >> [...] do note that there are compiler primitives for test-and-set that >> (can be) simpler for a cpu to implement than xchg. > > What compiler (I assume gcc) primitives are these? I couldn't find them. __sync_lock_test_and_set and __atomic_test_and_set. Both expand to ldstub on sparcv7, tas on coldfire, tas.b on sh. None of these are xchg operations. I had forgotten that there wasn't a __sync_exchange builtin, so __sync_lock_test_and_set plays double-duty as both xchg and test-and-set. But when __atomic builtins are available, __atomic_exchange does not fall back; you must use __atomic_test_and_set for less capable hosts. But of course none of this is really relevant for normal hosts. r~