SDL standard library
SDL_atomic.h File Reference
Atomic operations. More...
Go to the source code of this file.
Data Structures | |
struct | SDL_atomic_t |
A type representing an atomic integer value. More... | |
Functions | |
void | _ReadWriteBarrier (void) |
The compiler barrier prevents the compiler from reordering reads and writes to globally visible variables across the call. | |
DECLSPEC SDL_bool SDLCALL | 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. | |
DECLSPEC int SDLCALL | SDL_AtomicSet (SDL_atomic_t *a, int v) |
Set an atomic variable to a value. | |
DECLSPEC int SDLCALL | SDL_AtomicGet (SDL_atomic_t *a) |
Get the value of an atomic variable. | |
DECLSPEC int SDLCALL | SDL_AtomicAdd (SDL_atomic_t *a, int v) |
Add to an atomic variable. | |
DECLSPEC SDL_bool SDLCALL | SDL_AtomicCASPtr (void **a, void *oldval, void *newval) |
Set a pointer to a new value if it is currently an old value. | |
DECLSPEC void *SDLCALL | SDL_AtomicSetPtr (void **a, void *v) |
Set a pointer to a value atomically. | |
DECLSPEC void *SDLCALL | SDL_AtomicGetPtr (void **a) |
Get the value of a pointer atomically. | |
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 |
DECLSPEC SDL_bool SDLCALL | SDL_AtomicTryLock (SDL_SpinLock *lock) |
Try to lock a spin lock by setting it to a non-zero value. | |
DECLSPEC void SDLCALL | SDL_AtomicLock (SDL_SpinLock *lock) |
Lock a spin lock by setting it to a non-zero value. | |
DECLSPEC void SDLCALL | SDL_AtomicUnlock (SDL_SpinLock *lock) |
Unlock a spin lock by setting it to 0. |
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.
Function Documentation
void _ReadWriteBarrier | ( | void | ) |
The compiler barrier prevents the compiler from reordering reads and writes to globally visible variables across the call.
DECLSPEC int SDLCALL 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
DECLSPEC SDL_bool SDLCALL 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!
DECLSPEC SDL_bool SDLCALL 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!
DECLSPEC int SDLCALL SDL_AtomicGet | ( | SDL_atomic_t * | a ) |
Get the value of an atomic variable.
DECLSPEC void* SDLCALL SDL_AtomicGetPtr | ( | void ** | a ) |
Get the value of a pointer atomically.
DECLSPEC void SDLCALL SDL_AtomicLock | ( | SDL_SpinLock * | lock ) |
Lock a spin lock by setting it to a non-zero value.
- Parameters:
-
lock Points to the lock.
DECLSPEC int SDLCALL SDL_AtomicSet | ( | SDL_atomic_t * | a, |
int | v | ||
) |
Set an atomic variable to a value.
- Returns:
- The previous value of the atomic variable.
DECLSPEC void* SDLCALL SDL_AtomicSetPtr | ( | void ** | a, |
void * | v | ||
) |
Set a pointer to a value atomically.
- Returns:
- The previous value of the pointer.
DECLSPEC SDL_bool SDLCALL 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.
DECLSPEC void SDLCALL SDL_AtomicUnlock | ( | SDL_SpinLock * | lock ) |
Unlock a spin lock by setting it to 0.
Always returns immediately
- Parameters:
-
lock Points to the lock.
Generated on Tue Jul 12 2022 13:56:25 by 1.7.2