Atomic Operations
IntroductionProvides lock-free atomic operations like such as incrementing an integer, compare-and-swap, etc. GroupsAtomic OperationsProvides lock-free atomic operations like such as incrementing an integer, compare-and-swap, etc. DiscussionAtomicUtils provides lock-free atomic operations, such as incrementing an integer, compare-and-swap, etc. For many platforms, this porting has already been done either by implementing hardware/OS-specific code for it or by relying on features of the compiler to do it (e.g. __sync* builtins from clang/GCC, RAS on NetBSD). If porting is required, the following functions need to be implemented in an atomic, and ideally lock-free, manner. NOTE: GCC based implementation of these functions are provided. Group members:
Functions
atomic_add_and_fetch_32int32_t atomic_add_and_fetch_32( int32_t *inPtr, int32_t inVal ); DiscussionThis function atomically adds the value of inVal to the variable that inPtr points to. The result is stored in the address that is specified by inPtr. i.e. { *inPtr += inVal; return *inPtr; } NOTE: GCC based implementation of this function is provided. If porting is required, the following functions need to be implemented in an atomic, and ideally lock-free, manner. atomic_bool_compare_and_swap_32Boolean atomic_bool_compare_and_swap_32( int32_t *inPtr, int32_t inOldValue, int32_t inNewValue ); DiscussionThis function compares the value of inOldValue with the value of the variable that inPtr points to. If they are equal, the value of inNewValue is stored in the address that is specified by inPtr; otherwise, no operation is performed. If the value of inOldValue and the value of the variable that inPtr points to are equal, the function returns true, otherwise it returns false. i.e. { if( *inPtr == inOldValue ) { *inPtr = inNewValue; return 1; } else return 0; } NOTE: GCC based implementation of this function is provided. If porting is required, the following functions need to be implemented in an atomic, and ideally lock-free, manner. atomic_fetch_and_and_32int32_t atomic_fetch_and_and_32( int32_t *inPtr, int32_t inVal ); DiscussionThis function performs an atomic bitwise OR operation on the variable inVal with the variable that inPtr points to. The result is stored in the address that is specified by inPtr. The function returns the initial value of the variable that inPtr points to. i.e. { tmp = *inPtr; *inPtr &= inVal; return tmp; } NOTE: GCC based implementation of this function is provided. If porting is required, the following functions need to be implemented in an atomic, and ideally lock-free, manner. atomic_fetch_and_or_32int32_t atomic_fetch_and_or_32( int32_t *inPtr, int32_t inVal ); DiscussionThis function atomically adds the value of inVal to the variable that inPtr points to. The result is stored in the address that is specified by inPtr. The function returns the initial value of the variable that inPtr points to. i.e. { tmp = *inPtr; *inPtr |= inVal; return tmp; } NOTE: GCC based implementation of this function is provided. If porting is required, the following functions need to be implemented in an atomic, and ideally lock-free, manner. atomic_read_write_barriervoid atomic_read_write_barrier( void ); DiscussionThis function ensures reads and writes before this line have completed. atomic_sub_and_fetch_32int32_t atomic_sub_and_fetch_32( int32_t *inPtr, int32_t inVal ); DiscussionThis function atomically subtracts the value of inVal from the variable that inPtr points to. The result is stored in the address that is specified by inPtr. The function returns the new value of the variable that inPtr points to. i.e. { *inPtr -= inVal; return *inPtr; } NOTE: GCC based implementation of this function is provided. If porting is required, the following functions need to be implemented in an atomic, and ideally lock-free, manner. atomic_val_compare_and_swap_32int32_t atomic_val_compare_and_swap_32( int32_t *inPtr, int32_t inOldValue, int32_t inNewValue ); DiscussionThis function compares the value of inOldValue to the value of the variable that inPtr points to. If they are equal, the value of inNewValue is stored in the address that is specified by inPtr, otherwise, no operation is performed. The function returns the initial value of the variable that inPtr points to. i.e. { if( *inPtr == inOldValue ) { *inPtr = inNewValue; return inOldValue; } else return *inPtr; } NOTE: GCC based implementation of this function is provided. If porting is required, the following functions need to be implemented in an atomic, and ideally lock-free, manner. atomic_yieldvoid atomic_yield( void ); DiscussionThis function yields the current thread to allow other threads to execute. |