mihkhub / gtoolchain-in-action

GNU toolchain in action
0 stars 0 forks source link

Legacy sync Built-in Functions for Atomic Memory Access #5

Closed mihkhub closed 6 years ago

mihkhub commented 6 years ago

GCC allows any integral scalar or pointer type that is 1, 2, 4 or 8 bytes in length.

mihkhub commented 6 years ago
bool __sync_bool_compare_and_swap (type *ptr, type oldval, type newval, ...);
type __sync_val_compare_and_swap (type *ptr, type oldval, type newval, ...);

These built-in functions perform an atomic compare and swap. That is, if the current value of *ptr is oldval, then write newval into *ptr. The “bool” version returns true if the comparison is successful and newval is written. The “val” version returns the contents of *ptr before the operation.

mihkhub commented 6 years ago
type __sync_fetch_and_and (type *ptr, type value, ...);

These built-in functions perform the operation suggested by the name, and returns the value that had previously been in memory. That is,

{ tmp = *ptr; *ptr op= value; return tmp; };
{ tmp = *ptr; *ptr = ~(tmp & value); return tmp; };  // nand
mihkhub commented 6 years ago

These built-in functions 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