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.
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