From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/9118 Path: news.gmane.org!not-for-mail From: Rich Felker Newsgroups: gmane.linux.lib.musl.general Subject: Re: atomic primitives documentation Date: Thu, 14 Jan 2016 18:28:56 -0500 Message-ID: <20160114232856.GZ238@brightrain.aerifal.cx> References: Reply-To: musl@lists.openwall.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1452814157 16617 80.91.229.3 (14 Jan 2016 23:29:17 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 14 Jan 2016 23:29:17 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-9131-gllmg-musl=m.gmane.org@lists.openwall.com Fri Jan 15 00:29:12 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 1aJrKI-0005uP-Td for gllmg-musl@m.gmane.org; Fri, 15 Jan 2016 00:29:10 +0100 Original-Received: (qmail 22179 invoked by uid 550); 14 Jan 2016 23:29:09 -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 22161 invoked from network); 14 Jan 2016 23:29:08 -0000 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) Original-Sender: Rich Felker Xref: news.gmane.org gmane.linux.lib.musl.general:9118 Archived-At: 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