SDL  2.0
SDL_mutex.h
Go to the documentation of this file.
1 /*
2  Simple DirectMedia Layer
3  Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #ifndef SDL_mutex_h_
23 #define SDL_mutex_h_
24 
25 /**
26  * \file SDL_mutex.h
27  *
28  * Functions to provide thread synchronization primitives.
29  */
30 
31 #include "SDL_stdinc.h"
32 #include "SDL_error.h"
33 
34 #include "begin_code.h"
35 /* Set up for C function definitions, even when using C++ */
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39 
40 /**
41  * Synchronization functions which can time out return this value
42  * if they time out.
43  */
44 #define SDL_MUTEX_TIMEDOUT 1
45 
46 /**
47  * This is the timeout value which corresponds to never time out.
48  */
49 #define SDL_MUTEX_MAXWAIT (~(Uint32)0)
50 
51 
52 /**
53  * \name Mutex functions
54  */
55 /* @{ */
56 
57 /* The SDL mutex structure, defined in SDL_sysmutex.c */
58 struct SDL_mutex;
59 typedef struct SDL_mutex SDL_mutex;
60 
61 /**
62  * Create a new mutex.
63  *
64  * All newly-created mutexes begin in the _unlocked_ state.
65  *
66  * Calls to SDL_LockMutex() will not return while the mutex is locked by
67  * another thread. See SDL_TryLockMutex() to attempt to lock without blocking.
68  *
69  * SDL mutexes are reentrant.
70  *
71  * \returns the initialized and unlocked mutex or NULL on failure; call
72  * SDL_GetError() for more information.
73  *
74  * \sa SDL_DestroyMutex
75  * \sa SDL_LockMutex
76  * \sa SDL_TryLockMutex
77  * \sa SDL_UnlockMutex
78  */
79 extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
80 
81 /**
82  * Lock the mutex.
83  *
84  * This will block until the mutex is available, which is to say it is in the
85  * unlocked state and the OS has chosen the caller as the next thread to lock
86  * it. Of all threads waiting to lock the mutex, only one may do so at a time.
87  *
88  * It is legal for the owning thread to lock an already-locked mutex. It must
89  * unlock it the same number of times before it is actually made available for
90  * other threads in the system (this is known as a "recursive mutex").
91  *
92  * \param mutex the mutex to lock
93  * \return 0, or -1 on error.
94  */
95 extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex);
96 #define SDL_mutexP(m) SDL_LockMutex(m)
97 
98 /**
99  * Try to lock a mutex without blocking.
100  *
101  * This works just like SDL_LockMutex(), but if the mutex is not available,
102  * this function returns `SDL_MUTEX_TIMEOUT` immediately.
103  *
104  * This technique is useful if you need exclusive access to a resource but
105  * don't want to wait for it, and will return to it to try again later.
106  *
107  * \param mutex the mutex to try to lock
108  * \returns 0, `SDL_MUTEX_TIMEDOUT`, or -1 on error; call SDL_GetError() for
109  * more information.
110  *
111  * \sa SDL_CreateMutex
112  * \sa SDL_DestroyMutex
113  * \sa SDL_LockMutex
114  * \sa SDL_UnlockMutex
115  */
116 extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
117 
118 /**
119  * Unlock the mutex.
120  *
121  * It is legal for the owning thread to lock an already-locked mutex. It must
122  * unlock it the same number of times before it is actually made available for
123  * other threads in the system (this is known as a "recursive mutex").
124  *
125  * It is an error to unlock a mutex that has not been locked by the current
126  * thread, and doing so results in undefined behavior.
127  *
128  * It is also an error to unlock a mutex that isn't locked at all.
129  *
130  * \param mutex the mutex to unlock.
131  * \returns 0, or -1 on error.
132  */
133 extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex);
134 #define SDL_mutexV(m) SDL_UnlockMutex(m)
135 
136 /**
137  * Destroy a mutex created with SDL_CreateMutex().
138  *
139  * This function must be called on any mutex that is no longer needed. Failure
140  * to destroy a mutex will result in a system memory or resource leak. While
141  * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
142  * to destroy a locked mutex, and may result in undefined behavior depending
143  * on the platform.
144  *
145  * \param mutex the mutex to destroy
146  *
147  * \sa SDL_CreateMutex
148  * \sa SDL_LockMutex
149  * \sa SDL_TryLockMutex
150  * \sa SDL_UnlockMutex
151  */
152 extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
153 
154 /* @} *//* Mutex functions */
155 
156 
157 /**
158  * \name Semaphore functions
159  */
160 /* @{ */
161 
162 /* The SDL semaphore structure, defined in SDL_syssem.c */
163 struct SDL_semaphore;
164 typedef struct SDL_semaphore SDL_sem;
165 
166 /**
167  * Create a semaphore.
168  *
169  * This function creates a new semaphore and initializes it with the value
170  * `initial_value`. Each wait operation on the semaphore will atomically
171  * decrement the semaphore value and potentially block if the semaphore value
172  * is 0. Each post operation will atomically increment the semaphore value and
173  * wake waiting threads and allow them to retry the wait operation.
174  *
175  * \param initial_value the starting value of the semaphore
176  * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
177  * information.
178  *
179  * \sa SDL_DestroySemaphore
180  * \sa SDL_SemPost
181  * \sa SDL_SemTryWait
182  * \sa SDL_SemValue
183  * \sa SDL_SemWait
184  * \sa SDL_SemWaitTimeout
185  */
186 extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
187 
188 /**
189  * Destroy a semaphore.
190  *
191  * It is not safe to destroy a semaphore if there are threads currently
192  * waiting on it.
193  *
194  * \param sem the semaphore to destroy
195  *
196  * \sa SDL_CreateSemaphore
197  * \sa SDL_SemPost
198  * \sa SDL_SemTryWait
199  * \sa SDL_SemValue
200  * \sa SDL_SemWait
201  * \sa SDL_SemWaitTimeout
202  */
203 extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
204 
205 /**
206  * Wait until a semaphore has a positive value and then decrements it.
207  *
208  * This function suspends the calling thread until either the semaphore
209  * pointed to by `sem` has a positive value or the call is interrupted by a
210  * signal or error. If the call is successful it will atomically decrement the
211  * semaphore value.
212  *
213  * This function is the equivalent of calling SDL_SemWaitTimeout() with a time
214  * length of `SDL_MUTEX_MAXWAIT`.
215  *
216  * \param sem the semaphore wait on
217  * \returns 0 on success or a negative error code on failure; call
218  * SDL_GetError() for more information.
219  *
220  * \sa SDL_CreateSemaphore
221  * \sa SDL_DestroySemaphore
222  * \sa SDL_SemPost
223  * \sa SDL_SemTryWait
224  * \sa SDL_SemValue
225  * \sa SDL_SemWait
226  * \sa SDL_SemWaitTimeout
227  */
228 extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
229 
230 /**
231  * See if a semaphore has a positive value and decrement it if it does.
232  *
233  * This function checks to see if the semaphore pointed to by `sem` has a
234  * positive value and atomically decrements the semaphore value if it does. If
235  * the semaphore doesn't have a positive value, the function immediately
236  * returns SDL_MUTEX_TIMEDOUT.
237  *
238  * \param sem the semaphore to wait on
239  * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
240  * block, or a negative error code on failure; call SDL_GetError()
241  * for more information.
242  *
243  * \sa SDL_CreateSemaphore
244  * \sa SDL_DestroySemaphore
245  * \sa SDL_SemPost
246  * \sa SDL_SemValue
247  * \sa SDL_SemWait
248  * \sa SDL_SemWaitTimeout
249  */
250 extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
251 
252 /**
253  * Wait until a semaphore has a positive value and then decrements it.
254  *
255  * This function suspends the calling thread until either the semaphore
256  * pointed to by `sem` has a positive value, the call is interrupted by a
257  * signal or error, or the specified time has elapsed. If the call is
258  * successful it will atomically decrement the semaphore value.
259  *
260  * \param sem the semaphore to wait on
261  * \param ms the length of the timeout, in milliseconds
262  * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
263  * succeed in the allotted time, or a negative error code on failure;
264  * call SDL_GetError() for more information.
265  *
266  * \sa SDL_CreateSemaphore
267  * \sa SDL_DestroySemaphore
268  * \sa SDL_SemPost
269  * \sa SDL_SemTryWait
270  * \sa SDL_SemValue
271  * \sa SDL_SemWait
272  */
273 extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
274 
275 /**
276  * Atomically increment a semaphore's value and wake waiting threads.
277  *
278  * \param sem the semaphore to increment
279  * \returns 0 on success or a negative error code on failure; call
280  * SDL_GetError() for more information.
281  *
282  * \sa SDL_CreateSemaphore
283  * \sa SDL_DestroySemaphore
284  * \sa SDL_SemTryWait
285  * \sa SDL_SemValue
286  * \sa SDL_SemWait
287  * \sa SDL_SemWaitTimeout
288  */
289 extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
290 
291 /**
292  * Get the current value of a semaphore.
293  *
294  * \param sem the semaphore to query
295  * \returns the current value of the semaphore.
296  *
297  * \sa SDL_CreateSemaphore
298  */
299 extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
300 
301 /* @} *//* Semaphore functions */
302 
303 
304 /**
305  * \name Condition variable functions
306  */
307 /* @{ */
308 
309 /* The SDL condition variable structure, defined in SDL_syscond.c */
310 struct SDL_cond;
311 typedef struct SDL_cond SDL_cond;
312 
313 /**
314  * Create a condition variable.
315  *
316  * \returns a new condition variable or NULL on failure; call SDL_GetError()
317  * for more information.
318  *
319  * \sa SDL_CondBroadcast
320  * \sa SDL_CondSignal
321  * \sa SDL_CondWait
322  * \sa SDL_CondWaitTimeout
323  * \sa SDL_DestroyCond
324  */
325 extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
326 
327 /**
328  * Destroy a condition variable.
329  *
330  * \param cond the condition variable to destroy
331  *
332  * \sa SDL_CondBroadcast
333  * \sa SDL_CondSignal
334  * \sa SDL_CondWait
335  * \sa SDL_CondWaitTimeout
336  * \sa SDL_CreateCond
337  */
338 extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
339 
340 /**
341  * Restart one of the threads that are waiting on the condition variable.
342  *
343  * \param cond the condition variable to signal
344  * \returns 0 on success or a negative error code on failure; call
345  * SDL_GetError() for more information.
346  *
347  * \sa SDL_CondBroadcast
348  * \sa SDL_CondWait
349  * \sa SDL_CondWaitTimeout
350  * \sa SDL_CreateCond
351  * \sa SDL_DestroyCond
352  */
353 extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
354 
355 /**
356  * Restart all threads that are waiting on the condition variable.
357  *
358  * \param cond the condition variable to signal
359  * \returns 0 on success or a negative error code on failure; call
360  * SDL_GetError() for more information.
361  *
362  * \sa SDL_CondSignal
363  * \sa SDL_CondWait
364  * \sa SDL_CondWaitTimeout
365  * \sa SDL_CreateCond
366  * \sa SDL_DestroyCond
367  */
368 extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
369 
370 /**
371  * Wait until a condition variable is signaled.
372  *
373  * This function unlocks the specified `mutex` and waits for another thread to
374  * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
375  * `cond`. Once the condition variable is signaled, the mutex is re-locked and
376  * the function returns.
377  *
378  * The mutex must be locked before calling this function.
379  *
380  * This function is the equivalent of calling SDL_CondWaitTimeout() with a
381  * time length of `SDL_MUTEX_MAXWAIT`.
382  *
383  * \param cond the condition variable to wait on
384  * \param mutex the mutex used to coordinate thread access
385  * \returns 0 when it is signaled or a negative error code on failure; call
386  * SDL_GetError() for more information.
387  *
388  * \sa SDL_CondBroadcast
389  * \sa SDL_CondSignal
390  * \sa SDL_CondWaitTimeout
391  * \sa SDL_CreateCond
392  * \sa SDL_DestroyCond
393  */
394 extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
395 
396 /**
397  * Wait until a condition variable is signaled or a certain time has passed.
398  *
399  * This function unlocks the specified `mutex` and waits for another thread to
400  * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
401  * `cond`, or for the specified time to elapse. Once the condition variable is
402  * signaled or the time elapsed, the mutex is re-locked and the function
403  * returns.
404  *
405  * The mutex must be locked before calling this function.
406  *
407  * \param cond the condition variable to wait on
408  * \param mutex the mutex used to coordinate thread access
409  * \param ms the maximum time to wait, in milliseconds, or `SDL_MUTEX_MAXWAIT`
410  * to wait indefinitely
411  * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
412  * the condition is not signaled in the allotted time, or a negative
413  * error code on failure; call SDL_GetError() for more information.
414  *
415  * \sa SDL_CondBroadcast
416  * \sa SDL_CondSignal
417  * \sa SDL_CondWait
418  * \sa SDL_CreateCond
419  * \sa SDL_DestroyCond
420  */
421 extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
422  SDL_mutex * mutex, Uint32 ms);
423 
424 /* @} *//* Condition variable functions */
425 
426 
427 /* Ends C function definitions when using C++ */
428 #ifdef __cplusplus
429 }
430 #endif
431 #include "close_code.h"
432 
433 #endif /* SDL_mutex_h_ */
434 
435 /* vi: set ts=4 sw=4 expandtab: */
int SDL_SemWait(SDL_sem *sem)
SDL_cond * SDL_CreateCond(void)
void SDL_DestroyMutex(SDL_mutex *mutex)
void SDL_DestroySemaphore(SDL_sem *sem)
int SDL_CondBroadcast(SDL_cond *cond)
int SDL_SemPost(SDL_sem *sem)
int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms)
int SDL_LockMutex(SDL_mutex *mutex)
int SDL_SemWaitTimeout(SDL_sem *sem, Uint32 ms)
int SDL_CondSignal(SDL_cond *cond)
void SDL_DestroyCond(SDL_cond *cond)
int SDL_TryLockMutex(SDL_mutex *mutex)
SDL_mutex * SDL_CreateMutex(void)
struct SDL_semaphore SDL_sem
Definition: SDL_mutex.h:164
struct SDL_mutex SDL_mutex
Definition: SDL_mutex.h:59
struct SDL_cond SDL_cond
Definition: SDL_mutex.h:311
int SDL_UnlockMutex(SDL_mutex *mutex)
Uint32 SDL_SemValue(SDL_sem *sem)
int SDL_SemTryWait(SDL_sem *sem)
SDL_sem * SDL_CreateSemaphore(Uint32 initial_value)
int SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex)
uint32_t Uint32
Definition: SDL_stdinc.h:209