public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: "H.J. Lu" <hjl.tools@gmail.com>
To: Arjan van de Ven <arjan@linux.intel.com>
Cc: Andreas Schwab <schwab@linux-m68k.org>,
	 Arjan van de Ven via Libc-alpha <libc-alpha@sourceware.org>,
	Oleh Derevenko <oleh.derevenko@gmail.com>
Subject: Re: [PATCH] x86: Optimize atomic_compare_and_exchange_[val|bool]_acq [BZ #28537]
Date: Wed, 3 Nov 2021 12:48:39 -0700	[thread overview]
Message-ID: <CAMe9rOqDJb7mHCotRbCGBpJGXw0u3b3-SwqNCrG6pPyrHndNpg@mail.gmail.com> (raw)
In-Reply-To: <f51d7521-7ee9-9978-6219-6f87afa54b38@linux.intel.com>

On Wed, Nov 3, 2021 at 12:21 PM Arjan van de Ven <arjan@linux.intel.com> wrote:
>
> On 11/3/2021 10:17 AM, Andreas Schwab wrote:
> > On Nov 03 2021, Arjan van de Ven via Libc-alpha wrote:
> >
> >> What the patch does is check non-atomic first if the actual atomic operation has
> >> a chance of working. if it has a chance, the actual normal atomic operation is done as
> >> before. But if non-atomic read already tells you the cmpxchg has no chance to succeed, it errors
> >> out early.
> >
> > But if the compiler keeps reusing the same cached value it may conclude
> > that the CAS _never_ has a chance to succeed.
>
> that's a good point. Now there SHOULD be a (compiler) barrier around this somewhere due
> to inserting the pause instruction for such a loop. But putting some explicit compiler barrier in this
> makes it for sure safer.

Like this?

#define atomic_compare_and_exchange_val_acq(mem, newval, oldval) \
  ({ __typeof (*(mem)) oldmem = *(mem), ret;                            \
     atomic_read_barrier ();                                            \
     ret = (oldmem == (oldval)                                          \
            ? __sync_val_compare_and_swap (mem, oldval, newval)         \
            : oldmem);                                                  \
     ret; })
#define atomic_compare_and_exchange_bool_acq(mem, newval, oldval) \
  ({ __typeof (*(mem)) oldmem = *(mem);                                 \
     atomic_read_barrier ();                                            \
     int ret;                                                           \
     ret = (oldmem == (oldval)                                          \
            ? !__sync_bool_compare_and_swap (mem, oldval, newval)       \
            : 1);                                                       \
     ret; })


-- 
H.J.

  reply	other threads:[~2021-11-03 19:49 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-03 15:04 H.J. Lu
2021-11-03 15:14 ` Andreas Schwab
2021-11-03 15:50 ` Oleh Derevenko
2021-11-03 16:59   ` Arjan van de Ven
2021-11-03 17:17     ` Andreas Schwab
2021-11-03 19:21       ` Arjan van de Ven
2021-11-03 19:48         ` H.J. Lu [this message]
2021-11-03 20:38       ` Oleh Derevenko
2021-11-03 22:12         ` H.J. Lu
2021-11-04  8:58           ` Oleh Derevenko
2021-11-04  9:44             ` Oleh Derevenko
2021-11-03 17:26     ` Oleh Derevenko
2021-11-03 17:30       ` Arjan van de Ven
2021-11-03 17:55         ` Oleh Derevenko
2021-11-03 19:22           ` Arjan van de Ven
2021-11-04 11:42     ` Oleh Derevenko
2021-11-04 14:15       ` Arjan van de Ven
2021-11-03 16:35 ` Florian Weimer
2021-11-03 19:13   ` H.J. Lu
2021-11-04 10:15     ` Florian Weimer
2021-11-04 14:31       ` H.J. Lu
2021-11-04 14:59         ` H.J. Lu
2021-11-03 17:25 ` Noah Goldstein

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAMe9rOqDJb7mHCotRbCGBpJGXw0u3b3-SwqNCrG6pPyrHndNpg@mail.gmail.com \
    --to=hjl.tools@gmail.com \
    --cc=arjan@linux.intel.com \
    --cc=libc-alpha@sourceware.org \
    --cc=oleh.derevenko@gmail.com \
    --cc=schwab@linux-m68k.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).