epicsAtomic.h

OS independent interface to perform atomic operations.

This is an operating system and compiler independent interface to an operating system and or compiler dependent implementation of several atomic primitives.

These primitives can be safely used in a multithreaded programs on symmetric multiprocessing (SMP) systems. Where possible the primitives are implemented with compiler intrinsic wrappers for architecture specific instructions. Otherwise they are implemeted with OS specific functions and otherwise, when lacking a sufficently capable OS specific interface, then in some rare situations a mutual exclusion primitive is used for synchronization.

In operating systems environments which allow C code to run at interrupt level the implementation must use interrupt level invokable CPU instruction primitives.

All C++ functions are implemented in the namespace atomics which is nested inside of namespace epics.

Defines

EPICS_ATOMIC_INLINE

Typedefs

typedef void *EpicsAtomicPtrT

Argument type for atomic operations on pointers

Functions

static void epicsAtomicReadMemoryBarrier(void)

load target into cache

load target into cache

static void epicsAtomicWriteMemoryBarrier(void)

push cache version of target into target

push cache version of target into target

static size_t epicsAtomicIncrSizeT(size_t *pTarget)

atomic increment on size_t value

Lock out other smp processors from accessing the target, load target into cache, add one to target, flush cache to target, allow other smp processors to access the target, return new value of target as modified by this operation

Parameters:

pTarget – pointer to target

Returns:

New value of target

static int epicsAtomicIncrIntT(int *pTarget)

atomic increment on int value

Lock out other smp processors from accessing the target, load target into cache, add one to target, flush cache to target, allow other smp processors to access the target, return new value of target as modified by this operation

Parameters:

pTarget – pointer to target

Returns:

New value of target

static size_t epicsAtomicDecrSizeT(size_t *pTarget)

atomic decrement on size_t value

Lock out other smp processors from accessing the target, load target into cache, subtract one from target, flush cache to target, allow out other smp processors to access the target, return new value of target as modified by this operation

Parameters:

pTarget – pointer to target

Returns:

New value of target

static int epicsAtomicDecrIntT(int *pTarget)

atomic decrement on int value

Lock out other smp processors from accessing the target, load target into cache, subtract one from target, flush cache to target, allow out other smp processors to access the target, return new value of target as modified by this operation

Parameters:

pTarget – pointer to target

Returns:

New value of target

static size_t epicsAtomicAddSizeT(size_t *pTarget, size_t delta)

atomic addition on size_t value

Lock out other smp processors from accessing the target, load target into cache, add delta to target, flush cache to target, allow other smp processors to access the target, return new value of target as modified by this operation

Parameters:
  • pTarget – pointer to target

  • delta – value to add to target

Returns:

New value of target

static size_t epicsAtomicSubSizeT(size_t *pTarget, size_t delta)

atomic subtraction on size_t value

Lock out other smp processors from accessing the target, load target into cache, subtract delta from target, flush cache to target, allow other smp processors to access the target, return new value of target as modified by this operation

Parameters:
  • pTarget – pointer to target

  • delta – value to subtract from target

Returns:

New value of target

static int epicsAtomicAddIntT(int *pTarget, int delta)

atomic addition on int value

Lock out other smp processors from accessing the target, load target into cache, add delta to target, flush cache to target, allow other smp processors to access the target, return new value of target as modified by this operation

Parameters:
  • pTarget – pointer to target

  • delta – value to add to target

Returns:

New value of target

static void epicsAtomicSetSizeT(size_t *pTarget, size_t newValue)

atomically assign size_t value to variable

set cache version of target to new value, flush cache to target

Parameters:
  • pTarget – pointer to target

  • newValue – desired value of target

static void epicsAtomicSetIntT(int *pTarget, int newValue)

atomically assign int value to variable

set cache version of target to new value, flush cache to target

Parameters:
  • pTarget – pointer to target

  • newValue – desired value of target

static void epicsAtomicSetPtrT(EpicsAtomicPtrT *pTarget, EpicsAtomicPtrT newValue)

atomically assign pointer value to variable

set cache version of target to new value, flush cache to target

Parameters:
  • pTarget – pointer to target

  • newValue – desired value of target

static size_t epicsAtomicGetSizeT(const size_t *pTarget)

atomically load and return size_t value

fetch target into cache, return new value of target

Parameters:

pTarget – pointer to target

Returns:

value of target

static int epicsAtomicGetIntT(const int *pTarget)

atomically load and return int value

fetch target into cache, return new value of target

Parameters:

pTarget – pointer to target

Returns:

value of target

static EpicsAtomicPtrT epicsAtomicGetPtrT(const EpicsAtomicPtrT *pTarget)

atomically load and return pointer value

fetch target into cache, return new value of target

Parameters:

pTarget – pointer to target

Returns:

value of target

static size_t epicsAtomicCmpAndSwapSizeT(size_t *pTarget, size_t oldVal, size_t newVal)

atomically compare size_t value with expected and if equal swap with new value

lock out other smp processors from accessing the target, load target into cache. If target is equal to oldVal, set target to newVal, flush cache to target, allow other smp processors to access the target

Parameters:
  • pTarget – pointer to target

  • oldVal – value that will be compared with target

  • newVal – value that will be set to target if oldVal == target

Returns:

the original value stored in the target

static int epicsAtomicCmpAndSwapIntT(int *pTarget, int oldVal, int newVal)

atomically compare int value with expected and if equal swap with new value

lock out other smp processors from accessing the target, load target into cache. If target is equal to oldVal, set target to newVal, flush cache to target, allow other smp processors to access the target

Parameters:
  • pTarget – pointer to target

  • oldVal – value that will be compared with target

  • newVal – value that will be set to target if oldVal == target

Returns:

the original value stored in the target

static EpicsAtomicPtrT epicsAtomicCmpAndSwapPtrT(EpicsAtomicPtrT *pTarget, EpicsAtomicPtrT oldVal, EpicsAtomicPtrT newVal)

atomically compare int value with expected and if equal swap with new value

lock out other smp processors from accessing the target, load target into cache. If target is equal to oldVal, set target to newVal, flush cache to target, allow other smp processors to access the target

Parameters:
  • pTarget – pointer to target

  • oldVal – value that will be compared with target

  • newVal – value that will be set to target if oldVal == target

Returns:

the original value stored in the target

namespace epics
namespace atomic

Functions

static size_t increment(size_t &v)

C++ API for atomic size_t increment.

C++ API for atomic size_t increment.

Parameters:

v – variable to increment

Returns:

new value

static int increment(int &v)

C++ API for atomic int increment.

C++ API for atomic int increment.

Parameters:

v – variable to increment

Returns:

new value

static size_t decrement(size_t &v)

C++ API for atomic size_t decrement.

C++ API for atomic size_t decrement

Parameters:

v – variable to decrement

Returns:

new value

static int decrement(int &v)

C++ API for atomic int decrement.

C++ API for atomic int decrement

Parameters:

v – variable to decrement

Returns:

new value

static size_t add(size_t &v, size_t delta)

C++ API for atomic size_t addition.

C++ API for atomic size_t addition

Parameters:
  • v – variable to add to

  • delta – value to add to v

Returns:

new value

static int add(int &v, int delta)

C++ API for atomic int addition.

C++ API for atomic int addition

Parameters:
  • v – variable to add to

  • delta – value to add to v

Returns:

new value

static size_t subtract(size_t &v, size_t delta)

C++ API for atomic size_t subtraction.

C++ API for atomic size_t subtraction

Parameters:
  • v – variable to subtract from

  • delta – value to subtract from v

Returns:

new value

static int subtract(int &v, int delta)

C++ API for atomic int subtraction.

C++ API for atomic int subtraction

Parameters:
  • v – variable to subtract from

  • delta – value to subtract from v

Returns:

new value

static void set(size_t &v, size_t newValue)

C++ API for atomic size_t assignment.

C++ API for atomic size_t assignment

Parameters:
  • v – variable to assign to

  • newValue – new value for v

static void set(int &v, int newValue)

C++ API for atomic int assignment.

C++ API for atomic int assignment

Parameters:
  • v – variable to assign to

  • newValue – new value for v

static void set(EpicsAtomicPtrT &v, EpicsAtomicPtrT newValue)

C++ API for atomic pointer assignment.

C++ API for atomic pointer assignment

Parameters:
  • v – variable to assign to

  • newValue – new value for v

static size_t get(const size_t &v)

C++ API for atomic size_t load value.

C++ API for atomic size_t load value

Parameters:

v – variable to load

Returns:

value of v

static int get(const int &v)

C++ API for atomic int load value.

C++ API for atomic int load value

Parameters:

v – variable to load

Returns:

value of v

static EpicsAtomicPtrT get(const EpicsAtomicPtrT &v)

C++ API for atomic pointer load value.

C++ API for atomic pointer load value

Parameters:

v – variable to load

Returns:

value of v

static size_t compareAndSwap(size_t &v, size_t oldVal, size_t newVal)

C++ API for atomic size_t compare-and-swap.

C++ API for atomic size_t compare-and-swap. Atomic operation that compares v with oldVal and if v == \v oldVal, sets v to \v newVal

Parameters:
  • v – variable to compare and swap

  • oldVal – value to compare to \v

  • newVal – value to set to v

Returns:

original value stored in v

static int compareAndSwap(int &v, int oldVal, int newVal)

C++ API for atomic int compare-and-swap.

C++ API for atomic size_t compare-and-swap. Atomic operation that compares v with oldVal and if v == \v oldVal, sets v to \v newVal

Parameters:
  • v – variable to compare and swap

  • oldVal – value to compare to \v

  • newVal – value to set to v

Returns:

original value stored in v

static EpicsAtomicPtrT compareAndSwap(EpicsAtomicPtrT &v, EpicsAtomicPtrT oldVal, EpicsAtomicPtrT newVal)

C++ API for atomic pointer compare-and-swap.

C++ API for atomic size_t compare-and-swap. Atomic operation that compares v with oldVal and if v == \v oldVal, sets v to \v newVal

Parameters:
  • v – variable to compare and swap

  • oldVal – value to compare to \v

  • newVal – value to set to v

Returns:

original value stored in v