Subject: Re: Atomic ops API
To: Bang Jun-Young <junyoung@netbsd.org>
From: Johnny Billquist <bqt@softjar.se>
List: tech-kern
Date: 03/15/2007 17:23:17
Bang Jun-Young wrote:
> I'd say nobody would write an atomic op in pure C. :-) The first one was
> just
> a conceptual implementation with an if omitted (yes, it was a mistake).
Yes, nobody would probably write it in C, for various reasons.
But it wasn't an if that was missing in the concept, it was a lock.
> The
> difference is, Jason undestood what I tried to explain, and you didn't,
> maybe
> because you didn't read Solaris sources before.
If Jason understood you, then I guess we should all be happy. The reason
I didn't understand you was because your conceptual implementation was
broken. :-)
If we were to describe conceptually, without worrying about the fact
that it wouldn't work, then I'd write it like this:
uint32_t compare_and_swap(uint32_t *target, uint32_t cmp, uint32_t new)
{
uint32_t old = *target;
if (*target == cmp) *target = new;
return old;
}
But that also does away with the whole issue about choosing between
compare_and_swap() and compare_and_store() since that is about wether
one would be easier in general to implement on machines. Obviously, from
this conceptual description, either would be just as easy.
But I'm happy to leave it at that. Just call me stupid. :-)
Johnny