Subject: Re: Atomic ops API
To: Jason Thorpe <thorpej@shagadelic.org>
From: Bang Jun-Young <moguabjy@gmail.com>
List: tech-kern
Date: 03/14/2007 09:52:27
On 3/14/07, Bang Jun-Young <junyoung@netbsd.org> wrote:
> On 3/13/07, Jason Thorpe <thorpej@shagadelic.org> wrote:
> > The API is borrowed from Solaris.  There is one major difference,
> > however.  Solaris defines "cas" as COMPARE-AND-SWAP, returning the
> > previous value of the memory cell.  I have defined it as COMPARE-AND-
> > STORE, returning true if the ops succeeds, or false otherwise.  The
> > reason for this is because some platforms will have extreme difficulty
> > implementing COMPARE-AND-SWAP, whereas COMPARE-AND-STORE is somewhat
> > easier for these platforms.
>
> uint32_t
> atomic_compare_and_swap_32(volatile uint32_t *target, uint32_t cmp,
> uint32_t new)
> {
>        uint32_t old = *target;
>        (void) atomic_compare_and_store_32(target, cmp, new);
>        return old;
> }
>
> I think atomic_compare_and_swap_*() might not be so difficult to implement or
> expensive as you expected. :-)

Forgot to quote a code snippet from Solaris ZFS:

	while (i > (max = arc.hash_chain_max) &&
	    max != atomic_cas_32(&arc.hash_chain_max, max, i)) {
		continue;
	}

I guess this kind of code would not be so expensive on UP-only architectures,
would it?

Jun-Young