mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Rich Felker <dalias@libc.org>
To: musl@lists.openwall.com
Subject: Re: atomic primitives documentation
Date: Thu, 14 Jan 2016 18:28:56 -0500	[thread overview]
Message-ID: <20160114232856.GZ238@brightrain.aerifal.cx> (raw)
In-Reply-To: <CAD+Cw=eNuZ1dNQOVGrceNuB3GJjG7YijGbNPsHQTgZZvP9mK_g@mail.gmail.com>

On Thu, Jan 14, 2016 at 04:46:03PM -0500, Max Ruttenberg wrote:
> Hi all,
> 
> I'm trying to implement the atomic primitives but am finding the lack of
> documentation (i.e. specifying what each one is supposed to do, which
> argument is which, etc. ) a challenging obstacle. I've been googling around
> and haven't found anything helpful. I could look at how it's used in the
> source (that's what I've been doing so far) but it's cumbersome and I was
> wondering if there was documentation somewhere that I just happen to be
> missing.

For all atomic primitives, the pointer argument (the first argument
slot) is the address to operate on and the subsequent arguments are
the values to work with. Unless mentioned otherwise, they all operate
on 32-bit ints; only the _p (pointer), _l (long), and _64 (64-bit)
versions operate on other types. All atomic ops are strong memory
barriers.

a_cas is an atomic compare-and-swap which compares against the value
passed as the second ("test") argument and, if it matches, replaces
with the value passed as the third ("set") argument. The return value
is the old value read, which will be equal to the second argument on
success, and equal to some other value that was actually observed in
the case of failure.

a_fetch_add atomically adds the value argument to the pointed-to
object and returns the _old_ value.

a_inc and a_dec are like a_fetch_add with values 1/-1, but they're not
required to return the old value which makes them faster/simpler on
some archs.

a_and_64 and a_or_64 are misnamed; they're actually only usable as
atomic bitset/bitclear since they're not necessarily atomic on the
whole 64-bit unit.

a_barrier is a memory barrier by itself with no actual atomic
operation.

a_spin is a cpu-relaxation & barrier operation to be used when
spinning on an atomic.

A few functions have nothing to do with "atomics" and are just in this
file for no good reason except that they're arch-specific and usually
written in asm: a_ctz_l/_64 (count trailing zeros) and a_crash
(generate SIGSEGV or SIGILL termination as immediately as possible).

If you want to do this the easy way, just implement a_cas and do
everything else in terms of a_cas or with generic C code; see some of
the existing archs for examples.

Rich


  reply	other threads:[~2016-01-14 23:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-01-14 21:46 Max Ruttenberg
2016-01-14 23:28 ` Rich Felker [this message]
2016-01-14 23:36   ` Max Ruttenberg

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=20160114232856.GZ238@brightrain.aerifal.cx \
    --to=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).