SDL_syssem.c File Reference
`#include "../../SDL_internal.h"`
`#include <errno.h>`
`#include <pthread.h>`
`#include <semaphore.h>`
`#include <sys/time.h>`
`#include <time.h>`
`#include "SDL_thread.h"`
`#include "SDL_timer.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_SemWait (SDL_sem *sem)
int | SDL_SemWaitTimeout (SDL_sem *sem, Uint32 timeout)
Uint32 | SDL_SemValue (SDL_sem *sem)
int | SDL_SemPost (SDL_sem *sem)

## Function Documentation

## SDL_CreateSemaphore()

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

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

Definition at line 46 of file SDL_syssem.c.

References NULL, SDL_free, SDL_malloc, SDL_OutOfMemory, SDL_SetError, and SDL_sem::sem.

{
SDL_sem *sem = (SDL_sem *) SDL_malloc(sizeof(SDL_sem));
if (sem) {
if (sem_init(&sem->sem, 0, initial_value) < 0) {
SDL_SetError("sem_init() failed");
SDL_free(sem);
sem = NULL;
}
} else {
SDL_OutOfMemory();
}
return sem;
}
sem
static SDL_sem * sem
Definition: testsem.c:23
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
NULL
#define NULL
Definition: begin_code.h:164
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374

## SDL_DestroySemaphore()

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

Destroy a semaphore.

Definition at line 62 of file SDL_syssem.c.

References SDL_free.

{
if (sem) {
sem_destroy(&sem->sem);
SDL_free(sem);
}
}
sem
static SDL_sem * sem
Definition: testsem.c:23
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377

## SDL_SemPost()

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

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

Returns
  0, or -1 on error.

Definition at line 193 of file SDL_syssem.c.

References retval, and SDL_SetError.

{
int retval;
if (!sem) {
return SDL_SetError("Passed a NULL semaphore");
}
retval = sem_post(&sem->sem);
if (retval < 0) {
SDL_SetError("sem_post() failed");
}
return retval;
}
sem
static SDL_sem * sem
Definition: testsem.c:23
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30

## SDL_SemTryWait()

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 71 of file SDL_syssem.c.

References retval, SDL_MUTEX_TIMEDOUT, and SDL_SetError.

{
int retval;
if (!sem) {
return SDL_SetError("Passed a NULL semaphore");
}
retval = SDL_MUTEX_TIMEDOUT;
if (sem_trywait(&sem->sem) == 0) {
retval = 0;
}
return retval;
}
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:30

## SDL_SemValue()

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

Returns the current count of the semaphore.

Definition at line 180 of file SDL_syssem.c.

{
int ret = 0;
if (sem) {
sem_getvalue(&sem->sem, &ret);
if (ret < 0) {
ret = 0;
}
}
return (Uint32) ret;
}
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
sem
static SDL_sem * sem
Definition: testsem.c:23

## SDL_SemWait()

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 86 of file SDL_syssem.c.

References retval, and SDL_SetError.

{
int retval;
if (!sem) {
return SDL_SetError("Passed a NULL semaphore");
}
do {
retval = sem_wait(&sem->sem);
} while (retval < 0 && errno == EINTR);
if (retval < 0) {
retval = SDL_SetError("sem_wait() failed");
}
return retval;
}
sem
static SDL_sem * sem
Definition: testsem.c:23
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30

## SDL_SemWaitTimeout()

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 105 of file SDL_syssem.c.

References NULL, retval, SDL_Delay, SDL_GetTicks(), SDL_MUTEX_MAXWAIT, SDL_MUTEX_TIMEDOUT, SDL_SemTryWait(), SDL_SemWait(), SDL_SetError, and SDL_TICKS_PASSED.

{
int retval;
#ifdef HAVE_SEM_TIMEDWAIT
#ifndef HAVE_CLOCK_GETTIME
struct timeval now;
#endif
struct timespec ts_timeout;
#else
Uint32 end;
#endif
if (!sem) {
return SDL_SetError("Passed a NULL semaphore");
}
/* Try the easy cases first */
if (timeout == 0) {
return SDL_SemTryWait(sem);
}
if (timeout == SDL_MUTEX_MAXWAIT) {
return SDL_SemWait(sem);
}
#ifdef HAVE_SEM_TIMEDWAIT
/* Setup the timeout. sem_timedwait doesn't wait for
* a lapse of time, but until we reach a certain time.
* This time is now plus the timeout.
*/
#ifdef HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &ts_timeout);
/* Add our timeout to current time */
ts_timeout.tv_nsec += (timeout % 1000) * 1000000;
ts_timeout.tv_sec += timeout / 1000;
#else
gettimeofday(&now, NULL);
/* Add our timeout to current time */
ts_timeout.tv_sec = now.tv_sec + (timeout / 1000);
ts_timeout.tv_nsec = (now.tv_usec + (timeout % 1000) * 1000) * 1000;
#endif
/* Wrap the second if needed */
if (ts_timeout.tv_nsec > 1000000000) {
ts_timeout.tv_sec += 1;
ts_timeout.tv_nsec -= 1000000000;
}
/* Wait. */
do {
retval = sem_timedwait(&sem->sem, &ts_timeout);
} while (retval < 0 && errno == EINTR);
if (retval < 0) {
if (errno == ETIMEDOUT) {
retval = SDL_MUTEX_TIMEDOUT;
} else {
SDL_SetError("sem_timedwait returned an error: %s", strerror(errno));
}
}
#else
end = SDL_GetTicks() + timeout;
while ((retval = SDL_SemTryWait(sem)) == SDL_MUTEX_TIMEDOUT) {
if (SDL_TICKS_PASSED(SDL_GetTicks(), end)) {
break;
}
SDL_Delay(1);
}
#endif /* HAVE_SEM_TIMEDWAIT */
return retval;
}
end
GLuint GLuint end
Definition: SDL_opengl.h:1571
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:181
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_MUTEX_MAXWAIT
#define SDL_MUTEX_MAXWAIT
Definition: SDL_mutex.h:49
SDL_GetTicks
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
SDL_SemTryWait
int SDL_SemTryWait(SDL_sem *sem)
Definition: SDL_syssem.c:130
SDL_SemWait
int SDL_SemWait(SDL_sem *sem)
Definition: SDL_syssem.c:180
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
NULL
#define NULL
Definition: begin_code.h:164
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
timeout
GLbitfield GLuint64 timeout
Definition: SDL_opengl_glext.h:1483
SDL_TICKS_PASSED
#define SDL_TICKS_PASSED(A, B)
Compare SDL ticks values, and return true if A has passed B.
Definition: SDL_timer.h:56