SDL_atomic.h File Reference
`#include "SDL_stdinc.h"`
`#include "SDL_platform.h"`
`#include "begin_code.h"`
`#include "close_code.h"`
Include dependency graph for SDL_atomic.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Data Structures

struct | SDL_atomic_t
| A type representing an atomic integer value. It is a struct so people don't accidentally use numeric operations on it. More...

## Macros

#define | SDL_CompilerBarrier() { SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); }
#define | SDL_MemoryBarrierRelease() SDL_CompilerBarrier()
#define | SDL_MemoryBarrierAcquire() SDL_CompilerBarrier()
#define | SDL_AtomicIncRef(a) SDL_AtomicAdd(a, 1)
| Increment an atomic variable used as a reference count. More...
#define | SDL_AtomicDecRef(a) (SDL_AtomicAdd(a, -1) == 1)
| Decrement an atomic variable used as a reference count. More...

## Functions

void | SDL_MemoryBarrierReleaseFunction (void)
void | SDL_MemoryBarrierAcquireFunction (void)
SDL_bool | SDL_AtomicCAS (SDL_atomic_t *a, int oldval, int newval)
| Set an atomic variable to a new value if it is currently an old value. More...
int | SDL_AtomicSet (SDL_atomic_t *a, int v)
| Set an atomic variable to a value. More...
int | SDL_AtomicGet (SDL_atomic_t *a)
| Get the value of an atomic variable. More...
int | SDL_AtomicAdd (SDL_atomic_t *a, int v)
| Add to an atomic variable. More...
SDL_bool | SDL_AtomicCASPtr (void **a, void *oldval, void *newval)
| Set a pointer to a new value if it is currently an old value. More...
void * | SDL_AtomicSetPtr (void **a, void *v)
| Set a pointer to a value atomically. More...
void * | SDL_AtomicGetPtr (void **a)
| Get the value of a pointer atomically. More...

## SDL AtomicLock

The atomic locks are efficient spinlocks using CPU instructions, but are vulnerable to starvation and can spin forever if a thread holding a lock has been terminated. For this reason you should minimize the code executed inside an atomic lock and never do expensive things like API or system calls while holding them.

The atomic locks are not safe to lock recursively.

Porting Note: The spin lock functions and type are required and can not be emulated because they are used in the atomic emulation code.

typedef int | SDL_SpinLock
SDL_bool | SDL_AtomicTryLock (SDL_SpinLock *lock)
| Try to lock a spin lock by setting it to a non-zero value. More...
void | SDL_AtomicLock (SDL_SpinLock *lock)
| Lock a spin lock by setting it to a non-zero value. More...
void | SDL_AtomicUnlock (SDL_SpinLock *lock)
| Unlock a spin lock by setting it to 0. Always returns immediately. More...

## Detailed Description

Atomic operations.

IMPORTANT: If you are not an expert in concurrent lockless programming, you should only be using the atomic lock and reference counting functions in this file. In all other cases you should be protecting your data structures with full mutexes.

The list of "safe" functions to use are: SDL_AtomicLock() SDL_AtomicUnlock() SDL_AtomicIncRef() SDL_AtomicDecRef()

Seriously, here be dragons! ^^^^^^^^^^^^^^^^^^^^^^^^^^^

You can find out a little more about lockless programming and the subtle issues that can arise here: http://msdn.microsoft.com/en-us/library/ee418650%28v=vs.85%29.aspx

There's also lots of good information here: http://www.1024cores.net/home/lock-free-algorithms http://preshing.com/

These operations may or may not actually be implemented using processor specific atomic operations. When possible they are implemented as true processor specific atomic operations. When that is not possible the are implemented using locks that do use the available atomic operations.

All of the atomic operations that modify memory are full memory barriers.

Definition in file SDL_atomic.h.

## Macro Definition Documentation

## SDL_AtomicDecRef

#define SDL_AtomicDecRef | ( |  | a | ) | (SDL_AtomicAdd(a, -1) == 1)

Decrement an atomic variable used as a reference count.

Returns
  SDL_TRUE if the variable reached zero after decrementing, SDL_FALSE otherwise

Definition at line 244 of file SDL_atomic.h.

Referenced by DequeueEvent_LockFree(), EnqueueEvent_LockFree(), FIFO_Watcher(), RunBasicTest(), and SDL_free().

## SDL_AtomicIncRef

#define SDL_AtomicIncRef | ( |  | a | ) | SDL_AtomicAdd(a, 1)

Increment an atomic variable used as a reference count.

Definition at line 234 of file SDL_atomic.h.

Referenced by DequeueEvent_LockFree(), EnqueueEvent_LockFree(), FIFO_Watcher(), RunBasicTest(), SDL_AddTimer(), SDL_calloc(), SDL_malloc(), SDL_realloc(), and SDL_TLSCreate().

## SDL_CompilerBarrier

#define SDL_CompilerBarrier | ( |  | ) | { SDL_SpinLock _tmp = 0; SDL_AtomicLock(&_tmp); SDL_AtomicUnlock(&_tmp); }

The compiler barrier prevents the compiler from reordering reads and writes to globally visible variables across the call.

Definition at line 132 of file SDL_atomic.h.

Referenced by SDL_AtomicUnlock().

## SDL_MemoryBarrierAcquire

#define SDL_MemoryBarrierAcquire | ( |  | ) | SDL_CompilerBarrier()

Definition at line 190 of file SDL_atomic.h.

Referenced by SDL_Generic_GetTLSData(), SDL_GetErrBuf(), SDL_MemoryBarrierAcquireFunction(), and SDL_SYS_GetTLSData().

## SDL_MemoryBarrierRelease

#define SDL_MemoryBarrierRelease | ( |  | ) | SDL_CompilerBarrier()

Definition at line 189 of file SDL_atomic.h.

Referenced by SDL_Generic_GetTLSData(), SDL_GetErrBuf(), SDL_MemoryBarrierReleaseFunction(), and SDL_SYS_GetTLSData().

## Typedef Documentation

## SDL_SpinLock

typedef int SDL_SpinLock

Definition at line 89 of file SDL_atomic.h.

## Function Documentation

## SDL_AtomicAdd()

int SDL_AtomicAdd | ( | SDL_atomic_t * | a,
|  | int | v
| ) |  |

Add to an atomic variable.

Returns
  The previous value of the atomic variable.

Note
  This same style can be used for any number operation

Definition at line 237 of file SDL_atomic.c.

References SDL_AtomicCAS(), and SDL_atomic_t::value.

{
#ifdef HAVE_MSC_ATOMICS
return _InterlockedExchangeAdd((long*)&a->value, v);
#elif defined(HAVE_WATCOM_ATOMICS)
return _SDL_xadd_watcom(&a->value, v);
#elif defined(HAVE_GCC_ATOMICS)
return __sync_fetch_and_add(&a->value, v);
#elif defined(__SOLARIS__)
int pv = a->value;
membar_consumer();
#if defined(_LP64)
atomic_add_64((volatile uint64_t*)&a->value, v);
#elif !defined(_LP64)
atomic_add_32((volatile uint32_t*)&a->value, v);
#endif
return pv;
#else
int value;
do {
value = a->value;
} while (!SDL_AtomicCAS(a, value, (value + v)));
return value;
#endif
}
v
const GLdouble * v
Definition: SDL_opengl.h:2064
uint64_t
unsigned long long uint64_t
Definition: SDL_config_windows.h:65
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
SDL_atomic_t::value
int value
Definition: SDL_atomic.h:198
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:63
SDL_AtomicCAS
SDL_bool SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
Set an atomic variable to a new value if it is currently an old value.
Definition: SDL_atomic.c:129

## SDL_AtomicCAS()

SDL_bool SDL_AtomicCAS | ( | SDL_atomic_t * | a,
|  | int | oldval,
|  | int | newval
| ) |  |

Set an atomic variable to a new value if it is currently an old value.

Returns
  SDL_TRUE if the atomic variable was set, SDL_FALSE otherwise.

Note
  If you don't know what this function is for, you shouldn't use it!

Definition at line 129 of file SDL_atomic.c.

References enterLock(), leaveLock(), retval, SDL_FALSE, SDL_TRUE, and SDL_atomic_t::value.

Referenced by SDL_AtomicAdd(), SDL_AtomicGet(), and SDL_AtomicSet().

{
#ifdef HAVE_MSC_ATOMICS
return (_InterlockedCompareExchange((long*)&a->value, (long)newval, (long)oldval) == (long)oldval);
#elif defined(HAVE_WATCOM_ATOMICS)
return (SDL_bool) _SDL_cmpxchg_watcom(&a->value, newval, oldval);
#elif defined(HAVE_GCC_ATOMICS)
return (SDL_bool) __sync_bool_compare_and_swap(&a->value, oldval, newval);
#elif defined(__MACOSX__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */
return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
#elif defined(__SOLARIS__) && defined(_LP64)
return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)&a->value, (uint64_t)oldval, (uint64_t)newval) == oldval);
#elif defined(__SOLARIS__) && !defined(_LP64)
return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)&a->value, (uint32_t)oldval, (uint32_t)newval) == oldval);
#elif EMULATE_CAS
SDL_bool retval = SDL_FALSE;
enterLock(a);
if (a->value == oldval) {
a->value = newval;
retval = SDL_TRUE;
}
leaveLock(a);
return retval;
#else
#error Please define your platform.
#endif
}
SDL_TRUE
Definition: SDL_stdinc.h:142
SDL_FALSE
Definition: SDL_stdinc.h:141
uint64_t
unsigned long long uint64_t
Definition: SDL_config_windows.h:65
retval
SDL_bool retval
Definition: testgamecontroller.c:65
leaveLock
static SDL_INLINE void leaveLock(void *a)
Definition: SDL_atomic.c:119
enterLock
static SDL_INLINE void enterLock(void *a)
Definition: SDL_atomic.c:111
SDL_atomic_t::value
int value
Definition: SDL_atomic.h:198
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:139
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:63

## SDL_AtomicCASPtr()

SDL_bool SDL_AtomicCASPtr | ( | void ** | a,
|  | void * | oldval,
|  | void * | newval
| ) |  |

Set a pointer to a new value if it is currently an old value.

Returns
  SDL_TRUE if the pointer was set, SDL_FALSE otherwise.

Note
  If you don't know what this function is for, you shouldn't use it!

Definition at line 160 of file SDL_atomic.c.

References enterLock(), leaveLock(), retval, SDL_FALSE, and SDL_TRUE.

Referenced by SDL_AtomicGetPtr(), and SDL_AtomicSetPtr().

{
#if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
return (_InterlockedCompareExchange((long*)a, (long)newval, (long)oldval) == (long)oldval);
#elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
return (_InterlockedCompareExchangePointer(a, newval, oldval) == oldval);
#elif defined(HAVE_WATCOM_ATOMICS)
return (SDL_bool) _SDL_cmpxchg_watcom((int *)a, (long)newval, (long)oldval);
#elif defined(HAVE_GCC_ATOMICS)
return __sync_bool_compare_and_swap(a, oldval, newval);
#elif defined(__MACOSX__) && defined(__LP64__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */
return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
#elif defined(__MACOSX__) && !defined(__LP64__) /* this is deprecated in 10.12 sdk; favor gcc atomics. */
return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a);
#elif defined(__SOLARIS__)
return (SDL_bool) (atomic_cas_ptr(a, oldval, newval) == oldval);
#elif EMULATE_CAS
SDL_bool retval = SDL_FALSE;
enterLock(a);
if (*a == oldval) {
*a = newval;
retval = SDL_TRUE;
}
leaveLock(a);
return retval;
#else
#error Please define your platform.
#endif
}
SDL_TRUE
Definition: SDL_stdinc.h:142
int32_t
signed int int32_t
Definition: SDL_config_windows.h:62
SDL_FALSE
Definition: SDL_stdinc.h:141
retval
SDL_bool retval
Definition: testgamecontroller.c:65
leaveLock
static SDL_INLINE void leaveLock(void *a)
Definition: SDL_atomic.c:119
enterLock
static SDL_INLINE void enterLock(void *a)
Definition: SDL_atomic.c:111
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:139
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109
int64_t
signed long long int64_t
Definition: SDL_config_windows.h:64

## SDL_AtomicGet()

int SDL_AtomicGet | ( | SDL_atomic_t * | a | ) |

Get the value of an atomic variable.

Definition at line 264 of file SDL_atomic.c.

References SDL_AtomicCAS(), and SDL_atomic_t::value.

{
#ifdef HAVE_ATOMIC_LOAD_N
return __atomic_load_n(&a->value, __ATOMIC_SEQ_CST);
#else
int value;
do {
value = a->value;
} while (!SDL_AtomicCAS(a, value, value));
return value;
#endif
}
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
SDL_atomic_t::value
int value
Definition: SDL_atomic.h:198
SDL_AtomicCAS
SDL_bool SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
Set an atomic variable to a new value if it is currently an old value.
Definition: SDL_atomic.c:129

## SDL_AtomicGetPtr()

void* SDL_AtomicGetPtr | ( | void ** | a | ) |

Get the value of a pointer atomically.

Definition at line 278 of file SDL_atomic.c.

References SDL_AtomicCASPtr().

{
#ifdef HAVE_ATOMIC_LOAD_N
return __atomic_load_n(a, __ATOMIC_SEQ_CST);
#else
void *value;
do {
value = *a;
} while (!SDL_AtomicCASPtr(a, value, value));
return value;
#endif
}
SDL_AtomicCASPtr
SDL_bool SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
Set a pointer to a new value if it is currently an old value.
Definition: SDL_atomic.c:160
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109

## SDL_AtomicLock()

void SDL_AtomicLock | ( | SDL_SpinLock * | lock | ) |

Lock a spin lock by setting it to a non-zero value.

Parameters

  lock | Points to the lock.

Definition at line 120 of file SDL_spinlock.c.

References SDL_AtomicTryLock(), and SDL_Delay.

{
/* FIXME: Should we have an eventual timeout? */
while (!SDL_AtomicTryLock(lock)) {
SDL_Delay(0);
}
}
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
lock
SDL_mutex * lock
Definition: SDL_events.c:78
SDL_AtomicTryLock
SDL_bool SDL_AtomicTryLock(SDL_SpinLock *lock)
Try to lock a spin lock by setting it to a non-zero value.
Definition: SDL_spinlock.c:47

## SDL_AtomicSet()

int SDL_AtomicSet | ( | SDL_atomic_t * | a,
|  | int | v
| ) |  |

Set an atomic variable to a value.

Returns
  The previous value of the atomic variable.

Definition at line 193 of file SDL_atomic.c.

References SDL_AtomicCAS(), and SDL_atomic_t::value.

{
#ifdef HAVE_MSC_ATOMICS
return _InterlockedExchange((long*)&a->value, v);
#elif defined(HAVE_WATCOM_ATOMICS)
return _SDL_xchg_watcom(&a->value, v);
#elif defined(HAVE_GCC_ATOMICS)
return __sync_lock_test_and_set(&a->value, v);
#elif defined(__SOLARIS__) && defined(_LP64)
return (int) atomic_swap_64((volatile uint64_t*)&a->value, (uint64_t)v);
#elif defined(__SOLARIS__) && !defined(_LP64)
return (int) atomic_swap_32((volatile uint32_t*)&a->value, (uint32_t)v);
#else
int value;
do {
value = a->value;
} while (!SDL_AtomicCAS(a, value, v));
return value;
#endif
}
v
const GLdouble * v
Definition: SDL_opengl.h:2064
uint64_t
unsigned long long uint64_t
Definition: SDL_config_windows.h:65
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
SDL_atomic_t::value
int value
Definition: SDL_atomic.h:198
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:63
SDL_AtomicCAS
SDL_bool SDL_AtomicCAS(SDL_atomic_t *a, int oldval, int newval)
Set an atomic variable to a new value if it is currently an old value.
Definition: SDL_atomic.c:129

## SDL_AtomicSetPtr()

void* SDL_AtomicSetPtr | ( | void ** | a,
|  | void * | v
| ) |  |

Set a pointer to a value atomically.

Returns
  The previous value of the pointer.

Definition at line 215 of file SDL_atomic.c.

References SDL_AtomicCASPtr().

{
#if defined(HAVE_MSC_ATOMICS) && (_M_IX86)
return (void *) _InterlockedExchange((long *)a, (long) v);
#elif defined(HAVE_MSC_ATOMICS) && (!_M_IX86)
return _InterlockedExchangePointer(a, v);
#elif defined(HAVE_WATCOM_ATOMICS)
return (void *) _SDL_xchg_watcom((int *)a, (long)v);
#elif defined(HAVE_GCC_ATOMICS)
return __sync_lock_test_and_set(a, v);
#elif defined(__SOLARIS__)
return atomic_swap_ptr(a, v);
#else
void *value;
do {
value = *a;
} while (!SDL_AtomicCASPtr(a, value, v));
return value;
#endif
}
v
const GLdouble * v
Definition: SDL_opengl.h:2064
SDL_AtomicCASPtr
SDL_bool SDL_AtomicCASPtr(void **a, void *oldval, void *newval)
Set a pointer to a new value if it is currently an old value.
Definition: SDL_atomic.c:160
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109

## SDL_AtomicTryLock()

SDL_bool SDL_AtomicTryLock | ( | SDL_SpinLock * | lock | ) |

Try to lock a spin lock by setting it to a non-zero value.

Parameters

  lock | Points to the lock.

Returns
  SDL_TRUE if the lock succeeded, SDL_FALSE if the lock is already held.

Definition at line 47 of file SDL_spinlock.c.

References lock, SDL_COMPILE_TIME_ASSERT, SDL_CreateMutex, SDL_FALSE, SDL_LockMutex, SDL_TRUE, and SDL_UnlockMutex.

Referenced by SDL_AtomicLock().

{
#if SDL_ATOMIC_DISABLED
/* Terrible terrible damage */
static SDL_mutex *_spinlock_mutex;
if (!_spinlock_mutex) {
/* Race condition on first lock... */
_spinlock_mutex = SDL_CreateMutex();
}
SDL_LockMutex(_spinlock_mutex);
if (*lock == 0) {
*lock = 1;
SDL_UnlockMutex(_spinlock_mutex);
return SDL_TRUE;
} else {
SDL_UnlockMutex(_spinlock_mutex);
return SDL_FALSE;
}
#elif defined(_MSC_VER)
SDL_COMPILE_TIME_ASSERT(locksize, sizeof(*lock) == sizeof(long));
return (InterlockedExchange((long*)lock, 1) == 0);
#elif defined(__WATCOMC__) && defined(__386__)
return _SDL_xchg_watcom(lock, 1) == 0;
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
return (__sync_lock_test_and_set(lock, 1) == 0);
#elif defined(__GNUC__) && defined(__arm__) && \
(defined(__ARM_ARCH_4__) || defined(__ARM_ARCH_4T__) || \
defined(__ARM_ARCH_5__) || defined(__ARM_ARCH_5TE__) || \
defined(__ARM_ARCH_5TEJ__))
int result;
__asm__ __volatile__ (
"swp %0, %1, [%2]\n"
: "=&r,&r" (result) : "r,0" (1), "r,r" (lock) : "memory");
return (result == 0);
#elif defined(__GNUC__) && defined(__arm__)
int result;
__asm__ __volatile__ (
"ldrex %0, [%2]\nteq %0, #0\nstrexeq %0, %1, [%2]"
: "=&r" (result) : "r" (1), "r" (lock) : "cc", "memory");
return (result == 0);
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
int result;
__asm__ __volatile__(
"lock ; xchgl %0, (%1)\n"
: "=r" (result) : "r" (lock), "0" (1) : "cc", "memory");
return (result == 0);
#elif defined(__MACOSX__) || defined(__IPHONEOS__)
/* Maybe used for PowerPC, but the Intel asm or gcc atomics are favored. */
return OSAtomicCompareAndSwap32Barrier(0, 1, lock);
#elif defined(__SOLARIS__) && defined(_LP64)
/* Used for Solaris with non-gcc compilers. */
return (SDL_bool) ((int) atomic_cas_64((volatile uint64_t*)lock, 0, 1) == 0);
#elif defined(__SOLARIS__) && !defined(_LP64)
/* Used for Solaris with non-gcc compilers. */
return (SDL_bool) ((int) atomic_cas_32((volatile uint32_t*)lock, 0, 1) == 0);
#else
#error Please implement for your platform.
return SDL_FALSE;
#endif
}
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:260
SDL_TRUE
Definition: SDL_stdinc.h:142
result
GLuint64EXT * result
Definition: SDL_opengl_glext.h:9432
SDL_FALSE
Definition: SDL_stdinc.h:141
SDL_CreateMutex
#define SDL_CreateMutex
Definition: SDL_dynapi_overrides.h:259
uint64_t
unsigned long long uint64_t
Definition: SDL_config_windows.h:65
SDL_mutex
Definition: SDL_sysmutex.c:29
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:139
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:63
lock
SDL_mutex * lock
Definition: SDL_events.c:78
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:262
SDL_COMPILE_TIME_ASSERT
#define SDL_COMPILE_TIME_ASSERT(name, x)
Definition: SDL_stdinc.h:290

## SDL_AtomicUnlock()

void SDL_AtomicUnlock | ( | SDL_SpinLock * | lock | ) |

Unlock a spin lock by setting it to 0. Always returns immediately.

Parameters

  lock | Points to the lock.

Definition at line 129 of file SDL_spinlock.c.

References SDL_CompilerBarrier.

{
#if defined(_MSC_VER)
_ReadWriteBarrier();
*lock = 0;
#elif defined(__WATCOMC__) && defined(__386__)
SDL_CompilerBarrier ();
*lock = 0;
#elif HAVE_GCC_ATOMICS || HAVE_GCC_SYNC_LOCK_TEST_AND_SET
__sync_lock_release(lock);
#elif defined(__SOLARIS__)
/* Used for Solaris when not using gcc. */
*lock = 0;
membar_producer();
#else
*lock = 0;
#endif
}
SDL_CompilerBarrier
#define SDL_CompilerBarrier()
Definition: SDL_atomic.h:132
lock
SDL_mutex * lock
Definition: SDL_events.c:78

## SDL_MemoryBarrierAcquireFunction()

void SDL_MemoryBarrierAcquireFunction | ( | void |  | ) |

Definition at line 298 of file SDL_atomic.c.

References SDL_MemoryBarrierAcquire.

{
SDL_MemoryBarrierAcquire();
}
SDL_MemoryBarrierAcquire
#define SDL_MemoryBarrierAcquire()
Definition: SDL_atomic.h:190

## SDL_MemoryBarrierReleaseFunction()

void SDL_MemoryBarrierReleaseFunction | ( | void |  | ) |

Memory barriers are designed to prevent reads and writes from being reordered by the compiler and being seen out of order on multi-core CPUs.

A typical pattern would be for thread A to write some data and a flag, and for thread B to read the flag and get the data. In this case you would insert a release barrier between writing the data and the flag, guaranteeing that the data write completes no later than the flag is written, and you would insert an acquire barrier between reading the flag and reading the data, to ensure that all the reads associated with the flag have completed.

In this pattern you should always see a release barrier paired with an acquire barrier and you should gate the data reads/writes with a single flag variable.

For more information on these semantics, take a look at the blog post: http://preshing.com/20120913/acquire-and-release-semantics

Definition at line 292 of file SDL_atomic.c.

References SDL_MemoryBarrierRelease.

{
SDL_MemoryBarrierRelease();
}
SDL_MemoryBarrierRelease
#define SDL_MemoryBarrierRelease()
Definition: SDL_atomic.h:189