mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Timo Teras <timo.teras@iki.fi>
To: Rich Felker <dalias@libc.org>
Cc: musl@lists.openwall.com
Subject: Re: Refactoring atomics as llsc?
Date: Wed, 20 May 2015 08:33:23 +0300	[thread overview]
Message-ID: <20150520083323.2340cd1b@vostro> (raw)
In-Reply-To: <20150520051108.GA28347@brightrain.aerifal.cx>

On Wed, 20 May 2015 01:11:08 -0400
Rich Felker <dalias@libc.org> wrote:

> Of course the big outlier is x86, which is not llsc based but has
> actual atomic primitives at the instruction level. If we defined the
> sc() primitive to take 3 args instead of 2 (address, old value from
> ll, new value to conditionally store; most archs would ignore the old
> value argument) then we could model x86 with ll being a plain load and
> sc being cmpxchg to allow any new custom primitives to work using
> cmpxchg. Then we would just continue providing custom versions of all
> the old a_* ops (a_cas, a_fetch_add, a_inc, a_dec, a_and, a_or,
> a_swap) to take advantage of the x86 instructions. These versions
> could probably be shared by all x86 variants (i386, x86_64, x32) since
> they're operating on 32-bit values and the asm should be the same.

I wonder if calling that kind of emulation ll()/sc() would be
misleading. load-linked store-conditional has stronger guarantees. sc
will fail if the cache-line was invalidated in-between, thread was
pre-empted etc.

Using cmpxchg can be used to emulate it only when the user is aware of
ABA problem (some other thread may have changed the value behind us
multiple times). Such emulation is of course ok for a_fetch_add, etc.
But one needs to be more careful if using pointers (and trying to make
sure the same pointer was not first removed and later re-added).

And if you want to optimize the above mentioned cases, one really needs
to know if it's true ll+sc, or write the synchronization differently.
In these cases the algorithm is often implemented twice with the
different available atomics.

/Timo


  reply	other threads:[~2015-05-20  5:33 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-20  5:11 Rich Felker
2015-05-20  5:33 ` Timo Teras [this message]
2015-05-20  6:19   ` Rich Felker
2015-05-20  6:36   ` Rich Felker
2015-05-21  4:12 ` Rich Felker
2015-05-21 12:06   ` Szabolcs Nagy
2015-05-21 12:21     ` Alexander Monakov
2015-05-21 17:08       ` Rich Felker
2015-05-21 17:07     ` Rich Felker

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=20150520083323.2340cd1b@vostro \
    --to=timo.teras@iki.fi \
    --cc=dalias@libc.org \
    --cc=musl@lists.openwall.com \
    /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.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).