SDL_atomic.c File Reference
`#include "../SDL_internal.h"`
`#include "SDL_atomic.h"`
Include dependency graph for SDL_atomic.c:

Go to the source code of this file.

## Macros

#define | EMULATE_CAS 1

## Functions

static SDL_INLINE void | enterLock (void *a)
static SDL_INLINE void | leaveLock (void *a)
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...
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...
int | SDL_AtomicSet (SDL_atomic_t *a, int v)
| Set an atomic variable to a value. More...
void * | SDL_AtomicSetPtr (void **a, void *v)
| Set a pointer to a value atomically. More...
int | SDL_AtomicAdd (SDL_atomic_t *a, int v)
| Add to an atomic variable. More...
int | SDL_AtomicGet (SDL_atomic_t *a)
| Get the value of an atomic variable. More...
void * | SDL_AtomicGetPtr (void **a)
| Get the value of a pointer atomically. More...

## Variables

static SDL_SpinLock | locks [32]

## Macro Definition Documentation

#define EMULATE_CAS 1

Definition at line 62 of file SDL_atomic.c.

## Function Documentation

static SDL_INLINE void enterLock | ( | void * | a | ) |

static

Definition at line 69 of file SDL_atomic.c.

References locks, SDL_AtomicLock, and SDL_INLINE.

Referenced by SDL_AtomicCAS(), and SDL_AtomicCASPtr().

{
uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
SDL_AtomicLock(&locks[index]);
}
SDL_AtomicLock
#define SDL_AtomicLock
Definition: SDL_dynapi_overrides.h:70
uintptr_t
unsigned int uintptr_t
Definition: SDL_config_windows.h:69
index
GLuint index
Definition: SDL_opengl_glext.h:660
locks
static SDL_SpinLock locks[32]
Definition: SDL_atomic.c:66
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109

static SDL_INLINE void leaveLock | ( | void * | a | ) |

static

Definition at line 77 of file SDL_atomic.c.

References locks, and SDL_AtomicUnlock.

Referenced by SDL_AtomicCAS(), and SDL_AtomicCASPtr().

{
uintptr_t index = ((((uintptr_t)a) >> 3) & 0x1f);
SDL_AtomicUnlock(&locks[index]);
}
SDL_AtomicUnlock
#define SDL_AtomicUnlock
Definition: SDL_dynapi_overrides.h:71
uintptr_t
unsigned int uintptr_t
Definition: SDL_config_windows.h:69
index
GLuint index
Definition: SDL_opengl_glext.h:660
locks
static SDL_SpinLock locks[32]
Definition: SDL_atomic.c:66
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109

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 187 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_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
}
uint64_t
unsigned long long uint64_t
Definition: SDL_config_windows.h:64
v
const GLdouble * v
Definition: SDL_opengl.h:2057
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
SDL_atomic_t::value
int value
Definition: SDL_atomic.h:189
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:62
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:87

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 87 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(__MACOSX__) /* !!! FIXME: should we favor gcc atomics? */
return (SDL_bool) OSAtomicCompareAndSwap32Barrier(oldval, newval, &a->value);
#elif defined(HAVE_GCC_ATOMICS)
return (SDL_bool) __sync_bool_compare_and_swap(&a->value, oldval, newval);
#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
}
uint64_t
unsigned long long uint64_t
Definition: SDL_config_windows.h:64
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:126
retval
SDL_bool retval
Definition: testgamecontroller.c:65
leaveLock
static SDL_INLINE void leaveLock(void *a)
Definition: SDL_atomic.c:77
enterLock
static SDL_INLINE void enterLock(void *a)
Definition: SDL_atomic.c:69
SDL_atomic_t::value
int value
Definition: SDL_atomic.h:189
SDL_TRUE
Definition: SDL_stdinc.h:129
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:62
SDL_FALSE
Definition: SDL_stdinc.h:128

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 116 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(__MACOSX__) && defined(__LP64__) /* !!! FIXME: should we favor gcc atomics? */
return (SDL_bool) OSAtomicCompareAndSwap64Barrier((int64_t)oldval, (int64_t)newval, (int64_t*) a);
#elif defined(__MACOSX__) && !defined(__LP64__) /* !!! FIXME: should we favor gcc atomics? */
return (SDL_bool) OSAtomicCompareAndSwap32Barrier((int32_t)oldval, (int32_t)newval, (int32_t*) a);
#elif defined(HAVE_GCC_ATOMICS)
return __sync_bool_compare_and_swap(a, oldval, newval);
#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
}
int32_t
signed int int32_t
Definition: SDL_config_windows.h:61
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:126
retval
SDL_bool retval
Definition: testgamecontroller.c:65
leaveLock
static SDL_INLINE void leaveLock(void *a)
Definition: SDL_atomic.c:77
enterLock
static SDL_INLINE void enterLock(void *a)
Definition: SDL_atomic.c:69
SDL_TRUE
Definition: SDL_stdinc.h:129
SDL_FALSE
Definition: SDL_stdinc.h:128
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109
int64_t
signed long long int64_t
Definition: SDL_config_windows.h:63

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

Get the value of an atomic variable.

Definition at line 212 of file SDL_atomic.c.

References SDL_AtomicCAS(), and SDL_atomic_t::value.

{
int value;
do {
value = a->value;
} while (!SDL_AtomicCAS(a, value, value));
return value;
}
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
SDL_atomic_t::value
int value
Definition: SDL_atomic.h:189
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:87

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

Get the value of a pointer atomically.

Definition at line 222 of file SDL_atomic.c.

References SDL_AtomicCASPtr().

{
void *value;
do {
value = *a;
} while (!SDL_AtomicCASPtr(a, value, value));
return value;
}
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:116
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109

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 147 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_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
}
uint64_t
unsigned long long uint64_t
Definition: SDL_config_windows.h:64
v
const GLdouble * v
Definition: SDL_opengl.h:2057
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
SDL_atomic_t::value
int value
Definition: SDL_atomic.h:189
uint32_t
unsigned int uint32_t
Definition: SDL_config_windows.h:62
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:87

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

Set a pointer to a value atomically.

Returns
  The previous value of the pointer.

Definition at line 167 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_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
}
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:116
v
const GLdouble * v
Definition: SDL_opengl.h:2057
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
a
GLboolean GLboolean GLboolean GLboolean a
Definition: SDL_opengl_glext.h:1109

## Variable Documentation

SDL_SpinLock locks[32]

static

Definition at line 66 of file SDL_atomic.c.

Referenced by enterLock(), and leaveLock().