mailing list of musl libc
 help / color / mirror / code / Atom feed
* atomic primitives documentation
@ 2016-01-14 21:46 Max Ruttenberg
  2016-01-14 23:28 ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: Max Ruttenberg @ 2016-01-14 21:46 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 469 bytes --]

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.

Thanks,
Max

[-- Attachment #2: Type: text/html, Size: 716 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: atomic primitives documentation
  2016-01-14 21:46 atomic primitives documentation Max Ruttenberg
@ 2016-01-14 23:28 ` Rich Felker
  2016-01-14 23:36   ` Max Ruttenberg
  0 siblings, 1 reply; 3+ messages in thread
From: Rich Felker @ 2016-01-14 23:28 UTC (permalink / raw)
  To: musl

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: atomic primitives documentation
  2016-01-14 23:28 ` Rich Felker
@ 2016-01-14 23:36   ` Max Ruttenberg
  0 siblings, 0 replies; 3+ messages in thread
From: Max Ruttenberg @ 2016-01-14 23:36 UTC (permalink / raw)
  To: musl

[-- Attachment #1: Type: text/plain, Size: 3269 bytes --]

>
> 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).


Yeah, I noticed that one didn't touch memory so I couldn't see what was
atomic about it.


> 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.


The architecture I'm porting to has good variety of atomic operations, most
of these looked like they can be implemented with just one instruction.

Thank you! This email was very helpful and clearly took some time to write,
so I appreciate you taking the time.

Cheers,
Max

On Thu, Jan 14, 2016 at 6:28 PM, Rich Felker <dalias@libc.org> wrote:

> 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
>

[-- Attachment #2: Type: text/html, Size: 4704 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-01-14 23:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-14 21:46 atomic primitives documentation Max Ruttenberg
2016-01-14 23:28 ` Rich Felker
2016-01-14 23:36   ` Max Ruttenberg

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).