On 3/15/07, Johnny Billquist <bqt@softjar.se> wrote:
The return value _is_ very important, indeed. It's used to determine if
*target has been overwritten by another thread before the cas operation was
peformed. If it has, you should load the new value to *target and call
atomic_cas() again. This time if no other thread overwrites *target, the
call succeeds (and returns with the expected 'old' value).
Actually, it's just not mine. :-) FreeBSD, Solaris, Windows, Linux, and all the
other OS'es making use of fine-grained locking have the almost same atomic
cas functions on i386 architecture.
A lot. :-)
For another example, atomic_cas() is used to implement spinlock:
acquire_lock(lock)
{
while (1)
if (atomic_cas(lock, 0, 1) == 0)
break;
/*
* Lock is acquired.
*/
}
Now isn't it clear why the return value is important? :-)
Jun-Young