Subject: Re: Atomic ops API
To: Jason Thorpe <thorpej@shagadelic.org>
From: Bang Jun-Young <junyoung@netbsd.org>
List: tech-kern
Date: 03/14/2007 09:42:34
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. :-)

Jun-Young