From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47862) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar7Br-0001MY-LA for qemu-devel@nongnu.org; Fri, 15 Apr 2016 13:05:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ar7Bo-0005hz-7v for qemu-devel@nongnu.org; Fri, 15 Apr 2016 13:05:55 -0400 Received: from mail-wm0-x234.google.com ([2a00:1450:400c:c09::234]:36108) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ar7Bn-0005hr-Pc for qemu-devel@nongnu.org; Fri, 15 Apr 2016 13:05:52 -0400 Received: by mail-wm0-x234.google.com with SMTP id v188so39744023wme.1 for ; Fri, 15 Apr 2016 10:05:51 -0700 (PDT) References: <1460730231-1184-1-git-send-email-alex.bennee@linaro.org> <1460730231-1184-7-git-send-email-alex.bennee@linaro.org> <57111559.2010508@twiddle.net> From: Alex =?utf-8?Q?Benn=C3=A9e?= In-reply-to: <57111559.2010508@twiddle.net> Date: Fri, 15 Apr 2016 18:06:04 +0100 Message-ID: <87oa9ahkeb.fsf@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Subject: Re: [Qemu-devel] [RFC v1 05/12] atomic: introduce cmpxchg_bool List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Richard Henderson Cc: mttcg@greensocs.com, fred.konrad@greensocs.com, a.rigo@virtualopensystems.com, serge.fdrv@gmail.com, cota@braap.org, qemu-devel@nongnu.org, mark.burton@greensocs.com, pbonzini@redhat.com, jan.kiszka@siemens.com, peter.maydell@linaro.org, claudio.fontana@huawei.com Richard Henderson writes: > On 04/15/2016 07:23 AM, Alex Bennée wrote: >> +#define atomic_bool_cmpxchg(ptr, old, new) \ >> + ({ \ >> + typeof(*ptr) _old = (old), _new = (new); \ >> + bool r; \ >> + r = __atomic_compare_exchange(ptr, &_old, &_new, false, \ >> + __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \ >> + r; \ >> + }) > > How are you thinking this will be used? If a loop like > > do { > old = atomic_read (ptr); > new = f(old); > } while (!atomic_bool_cmpxchg(ptr, old, new)); > > then it's usually helpful to use a weak compare_exchange (s/false/true/ above). > This will produce one loop for ll/sc architectures instead of two. I used it to make Fred's STREX code a little neater: if (len == 1 << size) { oldval = (uint8_t)env->exclusive_val; result = atomic_bool_cmpxchg(p, oldval, (uint8_t)newval); } address_space_unmap(cs->as, p, len, true, result ? len : 0); Instead of: if (len == 1 << size) { oldval = (uint8_t)env->exclusive_val; result = (atomic_cmpxchg(p, oldval, (uint8_t)newval) == oldval); } address_space_unmap(cs->as, p, len, true, result ? len : 0); But now I'm wondering if there is a race in there. I'll have to look closer. > > > r~ -- Alex Bennée