SDL 2.0
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_CreateSemaphore()

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.

86{
87 SDL_sem *sem;
88
89 sem = (SDL_sem *) SDL_malloc(sizeof(*sem));
90 if (!sem) {
92 return NULL;
93 }
94 sem->count = initial_value;
95 sem->waiters_count = 0;
96
97 sem->count_lock = SDL_CreateMutex();
98 sem->count_nonzero = SDL_CreateCond();
99 if (!sem->count_lock || !sem->count_nonzero) {
101 return NULL;
102 }
103
104 return sem;
105}
#define SDL_CreateCond
#define SDL_malloc
#define SDL_CreateMutex
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
#define NULL
Definition: begin_code.h:167
void SDL_DestroySemaphore(SDL_sem *sem)
Definition: SDL_syssem.c:111
static SDL_sem * sem
Definition: testsem.c:23

◆ SDL_DestroySemaphore()

void SDL_DestroySemaphore ( SDL_sem *  sem)

Destroy a semaphore.

Definition at line 111 of file SDL_syssem.c.

112{
113 if (sem) {
114 sem->count = 0xFFFFFFFF;
115 while (sem->waiters_count > 0) {
116 SDL_CondSignal(sem->count_nonzero);
117 SDL_Delay(10);
118 }
119 SDL_DestroyCond(sem->count_nonzero);
120 if (sem->count_lock) {
121 SDL_LockMutex(sem->count_lock);
122 SDL_UnlockMutex(sem->count_lock);
123 SDL_DestroyMutex(sem->count_lock);
124 }
125 SDL_free(sem);
126 }
127}
#define SDL_LockMutex
#define SDL_CondSignal
#define SDL_free
#define SDL_Delay
#define SDL_DestroyCond
#define SDL_DestroyMutex
#define SDL_UnlockMutex

Referenced by SDL_CreateSemaphore().

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

201{
202 if (!sem) {
203 return SDL_SetError("Passed a NULL semaphore");
204 }
205
206 SDL_LockMutex(sem->count_lock);
207 if (sem->waiters_count > 0) {
208 SDL_CondSignal(sem->count_nonzero);
209 }
210 ++sem->count;
211 SDL_UnlockMutex(sem->count_lock);
212
213 return 0;
214}
#define SDL_SetError

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

131{
132 int retval;
133
134 if (!sem) {
135 return SDL_SetError("Passed a NULL semaphore");
136 }
137
139 SDL_LockMutex(sem->count_lock);
140 if (sem->count > 0) {
141 --sem->count;
142 retval = 0;
143 }
144 SDL_UnlockMutex(sem->count_lock);
145
146 return retval;
147}
#define SDL_MUTEX_TIMEDOUT
Definition: SDL_mutex.h:44
SDL_bool retval

Referenced by SDL_SemWaitTimeout().

◆ SDL_SemValue()

Uint32 SDL_SemValue ( SDL_sem *  sem)

Returns the current count of the semaphore.

Definition at line 186 of file SDL_syssem.c.

187{
189
190 value = 0;
191 if (sem) {
192 SDL_LockMutex(sem->count_lock);
193 value = sem->count;
194 SDL_UnlockMutex(sem->count_lock);
195 }
196 return value;
197}
GLsizei const GLfloat * value
uint32_t Uint32
Definition: SDL_stdinc.h:203

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

181{
183}
#define SDL_MUTEX_MAXWAIT
Definition: SDL_mutex.h:49
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout)
Definition: SDL_syssem.c:150

Referenced by SDL_SemWaitTimeout().

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

151{
152 int retval;
153
154 if (!sem) {
155 return SDL_SetError("Passed a NULL semaphore");
156 }
157
158 /* A timeout of 0 is an easy case */
159 if (timeout == 0) {
160 return SDL_SemTryWait(sem);
161 }
162
163 SDL_LockMutex(sem->count_lock);
164 ++sem->waiters_count;
165 retval = 0;
166 while ((sem->count == 0) && (retval != SDL_MUTEX_TIMEDOUT)) {
167 retval = SDL_CondWaitTimeout(sem->count_nonzero,
168 sem->count_lock, timeout);
169 }
170 --sem->waiters_count;
171 if (retval == 0) {
172 --sem->count;
173 }
174 SDL_UnlockMutex(sem->count_lock);
175
176 return retval;
177}
#define SDL_CondWaitTimeout
GLbitfield GLuint64 timeout
int SDL_SemTryWait(SDL_sem *sem)
Definition: SDL_syssem.c:130

Referenced by SDL_SemWait().