Loading...
Searching...
No Matches
atomic.h File Reference

Atomic operations for fast SMP synchronization. More...

#include <stdio.h>
#include "visibility.h"
#include "derived.h"

Classes

struct  PLMD::thread_mpi::tMPI_Atomic
 Atomic operations datatype. More...
 
struct  PLMD::thread_mpi::tMPI_Atomic_ptr
 Atomic pointer type equivalent to tMPI_Atomic_t. More...
 

Namespaces

namespace  PLMD
 Fast (possibly busy-wait-based) barrier type.
 
namespace  PLMD::thread_mpi
 

Macros

#define TMPI_ATOMIC_HAVE_NATIVE_ADD_RETURN
 
#define TMPI_ATOMIC_HAVE_NATIVE_FETCH_ADD
 
#define TMPI_ATOMIC_HAVE_NATIVE_SPINLOCK
 
#define TMPI_ATOMIC_HAVE_NATIVE_SWAP
 
#define tMPI_Atomic_memory_barrier()
 Memory barrier operation.
 
#define tMPI_Atomic_memory_barrier_acq()
 Memory barrier operation with acquire semantics.
 
#define tMPI_Atomic_memory_barrier_rel()
 Memory barrier operation with release semantics.
 
#define TMPI_HAVE_ACQ_REL_BARRIERS
 
#define TMPI_NO_ATOMICS
 Indicates that no support for atomic operations is present.
 
#define TMPI_SPINLOCK_INITIALIZER
 Spinlock static initializer.
 

Typedefs

typedef struct PLMD::thread_mpi::tMPI_Atomic_ptr PLMD::thread_mpi::tMPI_Atomic_ptr_t
 Atomic pointer type equivalent to tMPI_Atomic_t.
 
typedef struct PLMD::thread_mpi::tMPI_Atomic PLMD::thread_mpi::tMPI_Atomic_t
 Atomic operations datatype.
 
typedef struct tMPI_Spinlock * PLMD::thread_mpi::tMPI_Spinlock_t
 Spinlock.
 

Functions

TMPI_EXPORT int PLMD::thread_mpi::tMPI_Atomic_add_return (tMPI_Atomic_t *a, int i)
 Add integer to atomic variable.
 
TMPI_EXPORT int PLMD::thread_mpi::tMPI_Atomic_cas (tMPI_Atomic_t *a, int old_val, int new_val)
 Atomic compare-and-swap operation.
 
TMPI_EXPORT int PLMD::thread_mpi::tMPI_Atomic_fetch_add (tMPI_Atomic_t *a, int i)
 Add to variable, return the old value.
 
TMPI_EXPORT int PLMD::thread_mpi::tMPI_Atomic_get (const tMPI_Atomic_t *a)
 Return value of an atomic integer.
 
TMPI_EXPORT int PLMD::thread_mpi::tMPI_Atomic_ptr_cas (tMPI_Atomic_ptr_t *a, void *old_val, void *new_val)
 Atomic pointer compare-and-swap operation.
 
TMPI_EXPORT void * PLMD::thread_mpi::tMPI_Atomic_ptr_get (const tMPI_Atomic_ptr_t *a)
 Return value of an atomic pointer.
 
TMPI_EXPORT void PLMD::thread_mpi::tMPI_Atomic_ptr_set (tMPI_Atomic_ptr_t *a, void *p)
 Write value to an atomic pointer.
 
TMPI_EXPORT void * PLMD::thread_mpi::tMPI_Atomic_ptr_swap (tMPI_Atomic_ptr_t *a, void *b)
 Atomic swap pointer operation.
 
TMPI_EXPORT void PLMD::thread_mpi::tMPI_Atomic_set (tMPI_Atomic_t *a, int i)
 Write value to an atomic integer.
 
TMPI_EXPORT int PLMD::thread_mpi::tMPI_Atomic_swap (tMPI_Atomic_t *a, int b)
 Atomic swap operation.
 
TMPI_EXPORT void PLMD::thread_mpi::tMPI_Spinlock_init (tMPI_Spinlock_t *x)
 Initialize spinlock.
 
TMPI_EXPORT int PLMD::thread_mpi::tMPI_Spinlock_islocked (tMPI_Spinlock_t *x)
 Check if spinlock is locked.
 
TMPI_EXPORT void PLMD::thread_mpi::tMPI_Spinlock_lock (tMPI_Spinlock_t *x)
 Acquire spinlock.
 
TMPI_EXPORT int PLMD::thread_mpi::tMPI_Spinlock_trylock (tMPI_Spinlock_t *x)
 Attempt to acquire spinlock.
 
TMPI_EXPORT void PLMD::thread_mpi::tMPI_Spinlock_unlock (tMPI_Spinlock_t *x)
 Release spinlock.
 
TMPI_EXPORT void PLMD::thread_mpi::tMPI_Spinlock_wait (tMPI_Spinlock_t *x)
 Wait for a spinlock to become available.
 

Detailed Description

Atomic operations for fast SMP synchronization.

This file defines atomic integer operations and spinlocks for fast synchronization in performance-critical regions.

In general, the best option is to use functions without explicit locking, e.g. tMPI_Atomic_fetch_add() or tMPI_Atomic_cas().

Depending on the architecture/compiler, these operations may either be provided as functions or macros; be aware that those macros may reference their arguments repeatedly, possibly leading to multiply evaluated code with side effects: be careful with what you use as arguments.

Not all architectures support atomic operations though inline assembly, and even if they do it might not be implemented here. In that case we use a fallback mutex implementation, so you can always count on the function interfaces working.

Don't use spinlocks in non-performance-critical regions like file I/O. Since they always spin busy they would waste CPU cycles instead of properly yielding to a computation thread while waiting for the disk.

Finally, note that all our spinlock operations are defined to return 0 if initialization or locking completes successfully. This is the opposite of some other implementations, but the same standard as used for pthread mutexes. So, if e.g. are trying to lock a spinlock, you will have gotten the lock if the return value is 0.

tMPI_Spinlock_islocked(x) obviously still returns 1 if the lock is locked, and 0 if it is available, though...

Macro Definition Documentation

◆ TMPI_ATOMIC_HAVE_NATIVE_ADD_RETURN

#define TMPI_ATOMIC_HAVE_NATIVE_ADD_RETURN

◆ TMPI_ATOMIC_HAVE_NATIVE_FETCH_ADD

#define TMPI_ATOMIC_HAVE_NATIVE_FETCH_ADD

◆ TMPI_ATOMIC_HAVE_NATIVE_SPINLOCK

#define TMPI_ATOMIC_HAVE_NATIVE_SPINLOCK

◆ TMPI_ATOMIC_HAVE_NATIVE_SWAP

#define TMPI_ATOMIC_HAVE_NATIVE_SWAP

◆ tMPI_Atomic_memory_barrier

#define tMPI_Atomic_memory_barrier ( )

Memory barrier operation.

Modern CPUs rely heavily on out-of-order execution, and one common feature is that load/stores might be reordered. Also, when using inline assembly the compiler might already have loaded the variable we are changing into a register, so any update to memory won't be visible.

This command creates a memory barrier, i.e. all memory results before it in the code should be visible to all memory operations after it - the CPU cannot propagate load/stores across it.

This barrier is a full barrier: all load and store operations of instructions before it are completed, while all load and store operations that are in instructions after it won't be done before this barrier.

◆ tMPI_Atomic_memory_barrier_acq

#define tMPI_Atomic_memory_barrier_acq ( )

Memory barrier operation with acquire semantics.

This barrier is a barrier with acquire semantics: the terminology comes from its common use after acquiring a lock: all load/store instructions after this barrier may not be re-ordered to happen before this barrier.

◆ tMPI_Atomic_memory_barrier_rel

#define tMPI_Atomic_memory_barrier_rel ( )

Memory barrier operation with release semantics.

This barrier is a barrier with release semantics: the terminology comes from its common use before releasing a lock: all load/store instructions before this barrier may not be re-ordered to happen after this barrier.

◆ TMPI_HAVE_ACQ_REL_BARRIERS

#define TMPI_HAVE_ACQ_REL_BARRIERS

◆ TMPI_NO_ATOMICS

#define TMPI_NO_ATOMICS

Indicates that no support for atomic operations is present.

◆ TMPI_SPINLOCK_INITIALIZER

#define TMPI_SPINLOCK_INITIALIZER

Spinlock static initializer.

This is used for static spinlock initialization, and has the same properties as TMPI_THREAD_MUTEX_INITIALIZER has for mutexes. This is only for inlining in the tMPI_Thread.h header file. Whether it is 0, 1, or something else when unlocked depends on the platform. Don't assume anything about it. It might even be a mutex when using the fallback implementation!