SDL
2.0
|
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... | |
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.
#define SDL_AtomicDecRef | ( | a | ) | (SDL_AtomicAdd(a, -1) == 1) |
Decrement an atomic variable used as a reference count.
Definition at line 244 of file SDL_atomic.h.
Referenced by DequeueEvent_LockFree(), EnqueueEvent_LockFree(), FIFO_Watcher(), RunBasicTest(), and SDL_free().
#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().
#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().
#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().
#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 int SDL_SpinLock |
Definition at line 89 of file SDL_atomic.h.
int SDL_AtomicAdd | ( | SDL_atomic_t * | a, |
int | v | ||
) |
Add to an atomic variable.
Definition at line 237 of file SDL_atomic.c.
References SDL_AtomicCAS(), and SDL_atomic_t::value.
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 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().
Set a pointer to a new value if it is currently an old value.
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().
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.
Get the value of a pointer atomically.
Definition at line 278 of file SDL_atomic.c.
References SDL_AtomicCASPtr().
void SDL_AtomicLock | ( | SDL_SpinLock * | lock | ) |
Lock a spin lock by setting it to a non-zero value.
lock | Points to the lock. |
Definition at line 120 of file SDL_spinlock.c.
References SDL_AtomicTryLock(), and SDL_Delay.
int SDL_AtomicSet | ( | SDL_atomic_t * | a, |
int | v | ||
) |
Set an atomic variable to a value.
Definition at line 193 of file SDL_atomic.c.
References SDL_AtomicCAS(), and SDL_atomic_t::value.
Set a pointer to a value atomically.
Definition at line 215 of file SDL_atomic.c.
References SDL_AtomicCASPtr().
SDL_bool SDL_AtomicTryLock | ( | SDL_SpinLock * | lock | ) |
Try to lock a spin lock by setting it to a non-zero value.
lock | Points to the lock. |
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().
void SDL_AtomicUnlock | ( | SDL_SpinLock * | lock | ) |
Unlock a spin lock by setting it to 0. Always returns immediately.
lock | Points to the lock. |
Definition at line 129 of file SDL_spinlock.c.
References SDL_CompilerBarrier.
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.