From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/9120 Path: news.gmane.org!not-for-mail From: Max Ruttenberg Newsgroups: gmane.linux.lib.musl.general Subject: Re: atomic primitives documentation Date: Thu, 14 Jan 2016 18:36:08 -0500 Message-ID: References: <20160114232856.GZ238@brightrain.aerifal.cx> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=e89a8ff1cd404f990f052953c0e5 X-Trace: ger.gmane.org 1452814588 23934 80.91.229.3 (14 Jan 2016 23:36:28 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 14 Jan 2016 23:36:28 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-9133-gllmg-musl=m.gmane.org@lists.openwall.com Fri Jan 15 00:36:24 2016 Return-path: Envelope-to: gllmg-musl@m.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by plane.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1aJrRH-0002yJ-KX for gllmg-musl@m.gmane.org; Fri, 15 Jan 2016 00:36:23 +0100 Original-Received: (qmail 27791 invoked by uid 550); 14 Jan 2016 23:36:21 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Original-Received: (qmail 27770 invoked from network); 14 Jan 2016 23:36:20 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emutechnology-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=KasXNeBcDHqcqfumAQkXY+wEoidSQQ5IWNjnx8QOvSA=; b=IAPiLBd1p3K3KjRVXy6EnPNU2P63Gmua/mTEP9d3WuMchEefMJY/anQiHTiyJiaCrw 6O4jYUSnkeW7FLHg6Z8rpedEcumlTZ5O1gx5l+Pbr5FqrdTtA/NEoUHYm7tquBpxd3RK Gk4pv9sy4WUhvNSeT/UuO9S/E49Z3y99niNsqrELsZizeCClrK9N3i9cQFKL4A3lpZSQ RD3yjekGVNt1u4KCo86QtbQw5ayP2MSB4QsINa/hvpG1N1xW0tGOd1dU7ePCdxmewzOv a5+X7Eobbym56OqmafaBKdlWf6WblRS9JLZ/3kjaJPSvwiq5EWlK3+Z6icBauxVlI6Ec CdNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=KasXNeBcDHqcqfumAQkXY+wEoidSQQ5IWNjnx8QOvSA=; b=LO5TdGzmi17z1JqQAyI2Mz3GcsvDNOy3lGX1R8lvlHHzmrhVkkKuCFR11d8kuvEDSY mlz9gE5RcN2OeTCVTC/6kDTxZEyTM22ypw0XiHxtU8lQJiF2cDFplaIdn/FhRrAhISvr 29gyEp7ujGuQ3L3urY8CCTV/0gvKRAeZD36mMoIKvn7X4jwErRZ/CizJKLJNkyHFWTuL Ka2B0Xp9IioUXF95fQg5jg7iTU6JJNc3Eawd8UMmXF9xML6D6EuCl3Pp1sKHOayQ+eUz pZ+D3R2Bv6yphmFsTzEJcgM+D6YsYnpRu+ygD8+QRGlKUm+AEFzyuId7cnuyTuKqYdF9 BBjg== X-Gm-Message-State: ALoCoQkxYjv1xiatpxJRlElFopm8XgG0JMclRyGvKEDklFV4sStK0oEesaEhTlJzWygRlOFZ+ZzP/YE9fzfBb7AQaOtLbYW1NQ== X-Received: by 10.182.166.1 with SMTP id zc1mr6039099obb.23.1452814569013; Thu, 14 Jan 2016 15:36:09 -0800 (PST) In-Reply-To: <20160114232856.GZ238@brightrain.aerifal.cx> Xref: news.gmane.org gmane.linux.lib.musl.general:9120 Archived-At: --e89a8ff1cd404f990f052953c0e5 Content-Type: text/plain; charset=UTF-8 > > 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 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 > --e89a8ff1cd404f990f052953c0e5 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
A few fu= nctions have nothing to do with "atomics" and are just in thisfile for no good reason except tha= t they're arch-specific and usually
written in asm: a_ctz_l/_64 (count trailing zeros) and a_crash
<= /span>(generate SIGSEGV or SIGILL terminat= ion as immediately as possible).

Yea= h, I noticed that one didn't touch memory so I couldn't see what wa= s atomic about it.
=C2=A0
If you want to do this the easy way, just implement a= _cas and do
everything else in t= erms of a_cas or with generic C code; see some of
the existing archs for examples.=C2=A0

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

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

Cheers,
Max

On Thu, Jan 14, 2016 a= t 6:28 PM, Rich Felker <dalias@libc.org> wrote:
On Thu, Jan 14, 2016 at 04:46:03PM -05= 00, Max Ruttenberg wrote:
> Hi all,
>
> I'm trying to implement the atomic primitives but am finding the l= ack of
> documentation (i.e. specifying what each one is supposed to do, which<= br> > argument is which, etc. ) a challenging obstacle. I've been googli= ng around
> and haven't found anything helpful. I could look at how it's u= sed in the
> source (that's what I've been doing so far) but it's cumbe= rsome and I was
> wondering if there was documentation somewhere that I just happen to b= e
> missing.

For all atomic primitives, the pointer argument (the first argument<= br> 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, replac= es
with the value passed as the third ("set") argument. The return v= alue
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<= br> 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



--e89a8ff1cd404f990f052953c0e5--