SDL_syscond.c File Reference
`#include "../../SDL_internal.h"`
`#include <sys/time.h>`
`#include <time.h>`
`#include <unistd.h>`
`#include <errno.h>`
`#include <pthread.h>`
`#include "SDL_thread.h"`
`#include "SDL_sysmutex_c.h"`
Include dependency graph for SDL_syscond.c:

Go to the source code of this file.

## Data Structures

struct | SDL_cond

## Functions

SDL_cond * | SDL_CreateCond (void)
void | SDL_DestroyCond (SDL_cond *cond)
int | SDL_CondSignal (SDL_cond *cond)
int | SDL_CondBroadcast (SDL_cond *cond)
int | SDL_CondWaitTimeout (SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
int | SDL_CondWait (SDL_cond *cond, SDL_mutex *mutex)

## Function Documentation

## SDL_CondBroadcast()

int SDL_CondBroadcast | ( | SDL_cond * | cond | ) |

Restart all threads that are waiting on the condition variable.

Returns
  0 or -1 on error.

Definition at line 83 of file SDL_syscond.c.

References SDL_cond::cond, retval, and SDL_SetError.

{
int retval;
if (!cond) {
return SDL_SetError("Passed a NULL condition variable");
}
retval = 0;
if (pthread_cond_broadcast(&cond->cond) != 0) {
return SDL_SetError("pthread_cond_broadcast() failed");
}
return retval;
}
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_cond::cond
pthread_cond_t cond
Definition: SDL_syscond.c:34
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30

## SDL_CondSignal()

int SDL_CondSignal | ( | SDL_cond * | cond | ) |

Restart one of the threads that are waiting on the condition variable.

Returns
  0 or -1 on error.

Definition at line 66 of file SDL_syscond.c.

References SDL_cond::cond, retval, and SDL_SetError.

{
int retval;
if (!cond) {
return SDL_SetError("Passed a NULL condition variable");
}
retval = 0;
if (pthread_cond_signal(&cond->cond) != 0) {
return SDL_SetError("pthread_cond_signal() failed");
}
return retval;
}
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_cond::cond
pthread_cond_t cond
Definition: SDL_syscond.c:34
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30

## SDL_CondWait()

int SDL_CondWait | ( | SDL_cond * | cond,
|  | SDL_mutex * | mutex
| ) |  |

Wait on the condition variable, unlocking the provided mutex.

Warning
  The mutex must be locked before entering this function!

The mutex is re-locked once the condition variable is signaled.

Returns
  0 when it is signaled, or -1 on error.

Definition at line 148 of file SDL_syscond.c.

References SDL_cond::cond, SDL_mutex::id, and SDL_SetError.

{
if (!cond) {
return SDL_SetError("Passed a NULL condition variable");
} else if (pthread_cond_wait(&cond->cond, &mutex->id) != 0) {
return SDL_SetError("pthread_cond_wait() failed");
}
return 0;
}
SDL_cond::cond
pthread_cond_t cond
Definition: SDL_syscond.c:34
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_mutex::id
pthread_mutex_t id
Definition: SDL_sysmutex.c:35

## SDL_CondWaitTimeout()

int SDL_CondWaitTimeout | ( | SDL_cond * | cond,
|  | SDL_mutex * | mutex,
|  | Uint32 | ms
| ) |  |

Waits for at most `ms` milliseconds, and returns 0 if the condition variable is signaled, SDL_MUTEX_TIMEDOUT if the condition is not signaled 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 99 of file SDL_syscond.c.

References SDL_cond::cond, SDL_mutex::id, NULL, retval, SDL_MUTEX_TIMEDOUT, and SDL_SetError.

{
int retval;
#ifndef HAVE_CLOCK_GETTIME
struct timeval delta;
#endif
struct timespec abstime;
if (!cond) {
return SDL_SetError("Passed a NULL condition variable");
}
#ifdef HAVE_CLOCK_GETTIME
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_nsec += (ms % 1000) * 1000000;
abstime.tv_sec += ms / 1000;
#else
gettimeofday(&delta, NULL);
abstime.tv_sec = delta.tv_sec + (ms / 1000);
abstime.tv_nsec = (delta.tv_usec + (ms % 1000) * 1000) * 1000;
#endif
if (abstime.tv_nsec > 1000000000) {
abstime.tv_sec += 1;
abstime.tv_nsec -= 1000000000;
}
tryagain:
retval = pthread_cond_timedwait(&cond->cond, &mutex->id, &abstime);
switch (retval) {
case EINTR:
goto tryagain;
/* break; -Wunreachable-code-break */
case ETIMEDOUT:
retval = SDL_MUTEX_TIMEDOUT;
break;
case 0:
break;
default:
retval = SDL_SetError("pthread_cond_timedwait() failed");
}
return retval;
}
SDL_MUTEX_TIMEDOUT
#define SDL_MUTEX_TIMEDOUT
Definition: SDL_mutex.h:44
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_cond::cond
pthread_cond_t cond
Definition: SDL_syscond.c:34
NULL
#define NULL
Definition: begin_code.h:164
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_mutex::id
pthread_mutex_t id
Definition: SDL_sysmutex.c:35

## SDL_CreateCond()

SDL_cond* SDL_CreateCond | ( | void |  | ) |

Create a condition variable.

Typical use of condition variables:

Thread A: SDL_LockMutex(lock); while ( ! condition ) { SDL_CondWait(cond, lock); } SDL_UnlockMutex(lock);

Thread B: SDL_LockMutex(lock); ... condition = true; ... SDL_CondSignal(cond); SDL_UnlockMutex(lock);

There is some discussion whether to signal the condition variable with the mutex locked or not. There is some potential performance benefit to unlocking first on some platforms, but there are some potential race conditions depending on how your code is structured.

In general it's safer to signal the condition variable while the mutex is locked.

Definition at line 39 of file SDL_syscond.c.

References SDL_cond::cond, NULL, SDL_free, SDL_malloc, and SDL_SetError.

{
SDL_cond *cond;
cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond));
if (cond) {
if (pthread_cond_init(&cond->cond, NULL) != 0) {
SDL_SetError("pthread_cond_init() failed");
SDL_free(cond);
cond = NULL;
}
}
return (cond);
}
SDL_cond
Definition: SDL_syscond.c:31
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_cond::cond
pthread_cond_t cond
Definition: SDL_syscond.c:34
NULL
#define NULL
Definition: begin_code.h:164
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374

## SDL_DestroyCond()

void SDL_DestroyCond | ( | SDL_cond * | cond | ) |

Destroy a condition variable.

Definition at line 56 of file SDL_syscond.c.

References SDL_cond::cond, and SDL_free.

{
if (cond) {
pthread_cond_destroy(&cond->cond);
SDL_free(cond);
}
}
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377
SDL_cond::cond
pthread_cond_t cond
Definition: SDL_syscond.c:34