tags:

views:

45

answers:

1

So I was reading http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html, and came across this:

type __sync_and_and_fetch (type *ptr, type value, ...)
type __sync_xor_and_fetch (type *ptr, type value, ...)
type __sync_nand_and_fetch (type *ptr, type value, ...)
These builtins perform the operation suggested by the name, and return the new value. That is,
      { *ptr op= value; return *ptr; }
      { *ptr = ~*ptr & value; return *ptr; }   // nand

Is this code literal? or is it just to explain what gcc is doing atomically using c-like syntax? And if this is the direct translation, can someone explain how it is atomic?

+1  A: 

No. The code is just there to illustrate how the functions operate.

The atomic functions are not written in c, but rather in each architecture's assembly language. Since some architectures don't have the necessary instructions to implement the atomic operations, not all operations are valid on every architecture.

JayM