SDL_syssem.c File Reference
`#include "../../SDL_internal.h"`
`#include "SDL_timer.h"`
`#include "SDL_thread.h"`
`#include "SDL_systhread_c.h"`
Include dependency graph for SDL_syssem.c:

Go to the source code of this file.

## Data Structures

struct | SDL_sem

## Functions

SDL_sem * | SDL_CreateSemaphore (Uint32 initial_value)
void | SDL_DestroySemaphore (SDL_sem *sem)
int | SDL_SemTryWait (SDL_sem *sem)
int | SDL_SemWaitTimeout (SDL_sem *sem, Uint32 timeout)
int | SDL_SemWait (SDL_sem *sem)
Uint32 | SDL_SemValue (SDL_sem *sem)
int | SDL_SemPost (SDL_sem *sem)

## Function Documentation

SDL_sem* SDL_CreateSemaphore | ( | Uint32 | initial_value | ) |

Create a semaphore, initialized with value, returns NULL on failure.

Definition at line 85 of file SDL_syssem.c.

References NULL, SDL_CreateCond, SDL_CreateMutex, SDL_DestroySemaphore(), SDL_malloc, SDL_OutOfMemory, and SDL_sem::sem.

{
SDL_sem *sem;
sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
if (!sem) {
SDL_OutOfMemory();
return NULL;
}
sem->count = initial_value;
sem->waiters_count = 0;
sem->count_lock = SDL_CreateMutex();
sem->count_nonzero = SDL_CreateCond();
if (!sem->count_lock || !sem->count_nonzero) {
SDL_DestroySemaphore(sem);
return NULL;
}
return sem;
}
SDL_CreateMutex
#define SDL_CreateMutex
Definition: SDL_dynapi_overrides.h:265
sem
static SDL_sem * sem
Definition: testsem.c:23
SDL_CreateCond
#define SDL_CreateCond
Definition: SDL_dynapi_overrides.h:277
NULL
#define NULL
Definition: begin_code.h:143
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:380
SDL_DestroySemaphore
void SDL_DestroySemaphore(SDL_sem *sem)
Definition: SDL_syssem.c:111

void SDL_DestroySemaphore | ( | SDL_sem * | sem | ) |

Destroy a semaphore.

Definition at line 111 of file SDL_syssem.c.

References SDL_CondSignal, SDL_Delay, SDL_DestroyCond, SDL_DestroyMutex, SDL_free(), SDL_LockMutex, and SDL_UnlockMutex.

Referenced by SDL_CreateSemaphore().

{
if (sem) {
sem->count = 0xFFFFFFFF;
while (sem->waiters_count > 0) {
SDL_CondSignal(sem->count_nonzero);
SDL_Delay(10);
}
SDL_DestroyCond(sem->count_nonzero);
if (sem->count_lock) {
SDL_LockMutex(sem->count_lock);
SDL_UnlockMutex(sem->count_lock);
SDL_DestroyMutex(sem->count_lock);
}
SDL_free(sem);
}
}
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:266
SDL_DestroyCond
#define SDL_DestroyCond
Definition: SDL_dynapi_overrides.h:278
SDL_CondSignal
#define SDL_CondSignal
Definition: SDL_dynapi_overrides.h:279
sem
static SDL_sem * sem
Definition: testsem.c:23
SDL_free
void SDL_free(void *mem)
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:490
SDL_DestroyMutex
#define SDL_DestroyMutex
Definition: SDL_dynapi_overrides.h:269
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:268

int SDL_SemPost | ( | SDL_sem * | sem | ) |

Atomically increases the semaphore's count (not blocking).

Returns
  0, or -1 on error.

Definition at line 200 of file SDL_syssem.c.

References SDL_CondSignal, SDL_LockMutex, SDL_SetError, and SDL_UnlockMutex.

{
if (!sem) {
return SDL_SetError("Passed a NULL semaphore");
}
SDL_LockMutex(sem->count_lock);
if (sem->waiters_count > 0) {
SDL_CondSignal(sem->count_nonzero);
}
++sem->count;
SDL_UnlockMutex(sem->count_lock);
return 0;
}
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:266
SDL_CondSignal
#define SDL_CondSignal
Definition: SDL_dynapi_overrides.h:279
sem
static SDL_sem * sem
Definition: testsem.c:23
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:36
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:268

int SDL_SemTryWait | ( | SDL_sem * | sem | ) |

Non-blocking variant of SDL_SemWait().

Returns
  0 if the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait would block, and -1 on error.

Definition at line 130 of file SDL_syssem.c.

References retval, SDL_LockMutex, SDL_MUTEX_TIMEDOUT, SDL_SetError, and SDL_UnlockMutex.

Referenced by SDL_SemWaitTimeout().

{
int retval;
if (!sem) {
return SDL_SetError("Passed a NULL semaphore");
}
retval = SDL_MUTEX_TIMEDOUT;
SDL_LockMutex(sem->count_lock);
if (sem->count > 0) {
--sem->count;
retval = 0;
}
SDL_UnlockMutex(sem->count_lock);
return retval;
}
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:266
sem
static SDL_sem * sem
Definition: testsem.c:23
SDL_MUTEX_TIMEDOUT
#define SDL_MUTEX_TIMEDOUT
Definition: SDL_mutex.h:44
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:36
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:268

Uint32 SDL_SemValue | ( | SDL_sem * | sem | ) |

Returns the current count of the semaphore.

Definition at line 186 of file SDL_syssem.c.

References SDL_LockMutex, and SDL_UnlockMutex.

{
Uint32 value;
value = 0;
if (sem) {
SDL_LockMutex(sem->count_lock);
value = sem->count;
SDL_UnlockMutex(sem->count_lock);
}
return value;
}
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:266
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:157
sem
static SDL_sem * sem
Definition: testsem.c:23
value
GLsizei const GLfloat * value
Definition: SDL_opengl_glext.h:698
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:268

int SDL_SemWait | ( | SDL_sem * | sem | ) |

This function suspends the calling thread until the semaphore pointed to by `sem` has a positive count. It then atomically decreases the semaphore count.

Definition at line 180 of file SDL_syssem.c.

References SDL_MUTEX_MAXWAIT, and SDL_SemWaitTimeout().

Referenced by SDL_SemWaitTimeout().

{
return SDL_SemWaitTimeout(sem, SDL_MUTEX_MAXWAIT);
}
sem
static SDL_sem * sem
Definition: testsem.c:23
SDL_MUTEX_MAXWAIT
#define SDL_MUTEX_MAXWAIT
Definition: SDL_mutex.h:49
SDL_SemWaitTimeout
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
Definition: SDL_syssem.c:150

int SDL_SemWaitTimeout | ( | SDL_sem * | sem,
|  | Uint32 | ms
| ) |  |

Variant of SDL_SemWait() with a timeout in milliseconds.

Returns
  0 if the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait does not succeed in the allotted time, and -1 on error.

Warning
  On some platforms this function is implemented by looping with a delay of 1 ms, and so should be avoided if possible.

Definition at line 150 of file SDL_syssem.c.

References retval, SDL_CondWaitTimeout, SDL_LockMutex, SDL_MUTEX_TIMEDOUT, SDL_SemTryWait(), SDL_SetError, and SDL_UnlockMutex.

Referenced by SDL_SemWait().

{
int retval;
if (!sem) {
return SDL_SetError("Passed a NULL semaphore");
}
/* A timeout of 0 is an easy case */
if (timeout == 0) {
return SDL_SemTryWait(sem);
}
SDL_LockMutex(sem->count_lock);
++sem->waiters_count;
retval = 0;
while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT)) {
retval = SDL_CondWaitTimeout(sem->count_nonzero,
sem->count_lock, timeout);
}
--sem->waiters_count;
if (retval == 0) {
--sem->count;
}
SDL_UnlockMutex(sem->count_lock);
return retval;
}
SDL_LockMutex
#define SDL_LockMutex
Definition: SDL_dynapi_overrides.h:266
sem
static SDL_sem * sem
Definition: testsem.c:23
SDL_MUTEX_TIMEDOUT
#define SDL_MUTEX_TIMEDOUT
Definition: SDL_mutex.h:44
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_CondWaitTimeout
#define SDL_CondWaitTimeout
Definition: SDL_dynapi_overrides.h:282
SDL_SemTryWait
int SDL_SemTryWait(SDL_sem *sem)
Definition: SDL_syssem.c:130
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:36
timeout
GLbitfield GLuint64 timeout
Definition: SDL_opengl_glext.h:1483
SDL_UnlockMutex
#define SDL_UnlockMutex
Definition: SDL_dynapi_overrides.h:268