| # Atomic Operations
Includes: | "APSCommonServices.h"
## Introduction Provides lock-free atomic operations like such as incrementing an integer, compare-and-swap, etc. ## Groups ### Atomic Operations Provides lock-free atomic operations like such as incrementing an integer, compare-and-swap, etc. ##### Discussion AtomicUtils 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: `atomic_add_and_fetch_32` `atomic_bool_compare_and_swap_32` `atomic_fetch_and_and_32` `atomic_fetch_and_or_32` `atomic_read_write_barrier` `atomic_sub_and_fetch_32` `atomic_val_compare_and_swap_32` `atomic_yield` ## Functions `atomic_add_and_fetch_32` `atomic_bool_compare_and_swap_32` `atomic_fetch_and_and_32` `atomic_fetch_and_or_32` `atomic_read_write_barrier` `atomic_sub_and_fetch_32` `atomic_val_compare_and_swap_32` `atomic_yield` ### atomic_add_and_fetch_32
    int32_t atomic_add_and_fetch_32(
        int32_t *inPtr,
        int32_t inVal );
##### Discussion This 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_32
    Boolean atomic_bool_compare_and_swap_32(
        int32_t *inPtr,
        int32_t inOldValue,
        int32_t inNewValue );
##### Discussion This 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_32
    int32_t atomic_fetch_and_and_32(
        int32_t *inPtr,
        int32_t inVal );
##### Discussion This 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_32
    int32_t atomic_fetch_and_or_32(
        int32_t *inPtr,
        int32_t inVal );
##### Discussion This 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_barrier
    void atomic_read_write_barrier(
        void );
##### Discussion This function ensures reads and writes before this line have completed. ### atomic_sub_and_fetch_32
    int32_t atomic_sub_and_fetch_32(
        int32_t *inPtr,
        int32_t inVal );
##### Discussion This 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_32
    int32_t atomic_val_compare_and_swap_32(
        int32_t *inPtr,
        int32_t inOldValue,
        int32_t inNewValue );
##### Discussion This 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_yield
    void atomic_yield(
        void );
##### Discussion This function yields the current thread to allow other threads to execute. Last Updated: Friday, August 29, 2014