2 Simple DirectMedia Layer
3 Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
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.
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:
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.
28 * Functions to provide thread synchronization primitives.
31 #include "SDL_stdinc.h"
32 #include "SDL_error.h"
34 #include "begin_code.h"
35 /* Set up for C function definitions, even when using C++ */
41 * Synchronization functions which can time out return this value
44 #define SDL_MUTEX_TIMEDOUT 1
47 * This is the timeout value which corresponds to never time out.
49 #define SDL_MUTEX_MAXWAIT (~(Uint32)0)
53 * \name Mutex functions
57 /* The SDL mutex structure, defined in SDL_sysmutex.c */
59 typedef struct SDL_mutex SDL_mutex;
64 * All newly-created mutexes begin in the _unlocked_ state.
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.
69 * SDL mutexes are reentrant.
71 * \returns the initialized and unlocked mutex or NULL on failure; call
72 * SDL_GetError() for more information.
74 * \since This function is available since SDL 2.0.0.
76 * \sa SDL_DestroyMutex
78 * \sa SDL_TryLockMutex
81 extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
86 * This will block until the mutex is available, which is to say it is in the
87 * unlocked state and the OS has chosen the caller as the next thread to lock
88 * it. Of all threads waiting to lock the mutex, only one may do so at a time.
90 * It is legal for the owning thread to lock an already-locked mutex. It must
91 * unlock it the same number of times before it is actually made available for
92 * other threads in the system (this is known as a "recursive mutex").
94 * \param mutex the mutex to lock
95 * \return 0, or -1 on error.
97 * \since This function is available since SDL 2.0.0.
99 extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex);
100 #define SDL_mutexP(m) SDL_LockMutex(m)
103 * Try to lock a mutex without blocking.
105 * This works just like SDL_LockMutex(), but if the mutex is not available,
106 * this function returns `SDL_MUTEX_TIMEOUT` immediately.
108 * This technique is useful if you need exclusive access to a resource but
109 * don't want to wait for it, and will return to it to try again later.
111 * \param mutex the mutex to try to lock
112 * \returns 0, `SDL_MUTEX_TIMEDOUT`, or -1 on error; call SDL_GetError() for
115 * \since This function is available since SDL 2.0.0.
117 * \sa SDL_CreateMutex
118 * \sa SDL_DestroyMutex
120 * \sa SDL_UnlockMutex
122 extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
127 * It is legal for the owning thread to lock an already-locked mutex. It must
128 * unlock it the same number of times before it is actually made available for
129 * other threads in the system (this is known as a "recursive mutex").
131 * It is an error to unlock a mutex that has not been locked by the current
132 * thread, and doing so results in undefined behavior.
134 * It is also an error to unlock a mutex that isn't locked at all.
136 * \param mutex the mutex to unlock.
137 * \returns 0, or -1 on error.
139 * \since This function is available since SDL 2.0.0.
141 extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex);
142 #define SDL_mutexV(m) SDL_UnlockMutex(m)
145 * Destroy a mutex created with SDL_CreateMutex().
147 * This function must be called on any mutex that is no longer needed. Failure
148 * to destroy a mutex will result in a system memory or resource leak. While
149 * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
150 * to destroy a locked mutex, and may result in undefined behavior depending
153 * \param mutex the mutex to destroy
155 * \since This function is available since SDL 2.0.0.
157 * \sa SDL_CreateMutex
159 * \sa SDL_TryLockMutex
160 * \sa SDL_UnlockMutex
162 extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
164 /* @} *//* Mutex functions */
168 * \name Semaphore functions
172 /* The SDL semaphore structure, defined in SDL_syssem.c */
173 struct SDL_semaphore;
174 typedef struct SDL_semaphore SDL_sem;
177 * Create a semaphore.
179 * This function creates a new semaphore and initializes it with the value
180 * `initial_value`. Each wait operation on the semaphore will atomically
181 * decrement the semaphore value and potentially block if the semaphore value
182 * is 0. Each post operation will atomically increment the semaphore value and
183 * wake waiting threads and allow them to retry the wait operation.
185 * \param initial_value the starting value of the semaphore
186 * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
189 * \since This function is available since SDL 2.0.0.
191 * \sa SDL_DestroySemaphore
196 * \sa SDL_SemWaitTimeout
198 extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
201 * Destroy a semaphore.
203 * It is not safe to destroy a semaphore if there are threads currently
206 * \param sem the semaphore to destroy
208 * \since This function is available since SDL 2.0.0.
210 * \sa SDL_CreateSemaphore
215 * \sa SDL_SemWaitTimeout
217 extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
220 * Wait until a semaphore has a positive value and then decrements it.
222 * This function suspends the calling thread until either the semaphore
223 * pointed to by `sem` has a positive value or the call is interrupted by a
224 * signal or error. If the call is successful it will atomically decrement the
227 * This function is the equivalent of calling SDL_SemWaitTimeout() with a time
228 * length of `SDL_MUTEX_MAXWAIT`.
230 * \param sem the semaphore wait on
231 * \returns 0 on success or a negative error code on failure; call
232 * SDL_GetError() for more information.
234 * \since This function is available since SDL 2.0.0.
236 * \sa SDL_CreateSemaphore
237 * \sa SDL_DestroySemaphore
242 * \sa SDL_SemWaitTimeout
244 extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
247 * See if a semaphore has a positive value and decrement it if it does.
249 * This function checks to see if the semaphore pointed to by `sem` has a
250 * positive value and atomically decrements the semaphore value if it does. If
251 * the semaphore doesn't have a positive value, the function immediately
252 * returns SDL_MUTEX_TIMEDOUT.
254 * \param sem the semaphore to wait on
255 * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
256 * block, or a negative error code on failure; call SDL_GetError()
257 * for more information.
259 * \since This function is available since SDL 2.0.0.
261 * \sa SDL_CreateSemaphore
262 * \sa SDL_DestroySemaphore
266 * \sa SDL_SemWaitTimeout
268 extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
271 * Wait until a semaphore has a positive value and then decrements it.
273 * This function suspends the calling thread until either the semaphore
274 * pointed to by `sem` has a positive value, the call is interrupted by a
275 * signal or error, or the specified time has elapsed. If the call is
276 * successful it will atomically decrement the semaphore value.
278 * \param sem the semaphore to wait on
279 * \param ms the length of the timeout, in milliseconds
280 * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
281 * succeed in the allotted time, or a negative error code on failure;
282 * call SDL_GetError() for more information.
284 * \since This function is available since SDL 2.0.0.
286 * \sa SDL_CreateSemaphore
287 * \sa SDL_DestroySemaphore
293 extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
296 * Atomically increment a semaphore's value and wake waiting threads.
298 * \param sem the semaphore to increment
299 * \returns 0 on success or a negative error code on failure; call
300 * SDL_GetError() for more information.
302 * \since This function is available since SDL 2.0.0.
304 * \sa SDL_CreateSemaphore
305 * \sa SDL_DestroySemaphore
309 * \sa SDL_SemWaitTimeout
311 extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
314 * Get the current value of a semaphore.
316 * \param sem the semaphore to query
317 * \returns the current value of the semaphore.
319 * \since This function is available since SDL 2.0.0.
321 * \sa SDL_CreateSemaphore
323 extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
325 /* @} *//* Semaphore functions */
329 * \name Condition variable functions
333 /* The SDL condition variable structure, defined in SDL_syscond.c */
335 typedef struct SDL_cond SDL_cond;
338 * Create a condition variable.
340 * \returns a new condition variable or NULL on failure; call SDL_GetError()
341 * for more information.
343 * \since This function is available since SDL 2.0.0.
345 * \sa SDL_CondBroadcast
348 * \sa SDL_CondWaitTimeout
349 * \sa SDL_DestroyCond
351 extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
354 * Destroy a condition variable.
356 * \param cond the condition variable to destroy
358 * \since This function is available since SDL 2.0.0.
360 * \sa SDL_CondBroadcast
363 * \sa SDL_CondWaitTimeout
366 extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
369 * Restart one of the threads that are waiting on the condition variable.
371 * \param cond the condition variable to signal
372 * \returns 0 on success or a negative error code on failure; call
373 * SDL_GetError() for more information.
375 * \since This function is available since SDL 2.0.0.
377 * \sa SDL_CondBroadcast
379 * \sa SDL_CondWaitTimeout
381 * \sa SDL_DestroyCond
383 extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
386 * Restart all threads that are waiting on the condition variable.
388 * \param cond the condition variable to signal
389 * \returns 0 on success or a negative error code on failure; call
390 * SDL_GetError() for more information.
392 * \since This function is available since SDL 2.0.0.
396 * \sa SDL_CondWaitTimeout
398 * \sa SDL_DestroyCond
400 extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
403 * Wait until a condition variable is signaled.
405 * This function unlocks the specified `mutex` and waits for another thread to
406 * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
407 * `cond`. Once the condition variable is signaled, the mutex is re-locked and
408 * the function returns.
410 * The mutex must be locked before calling this function.
412 * This function is the equivalent of calling SDL_CondWaitTimeout() with a
413 * time length of `SDL_MUTEX_MAXWAIT`.
415 * \param cond the condition variable to wait on
416 * \param mutex the mutex used to coordinate thread access
417 * \returns 0 when it is signaled or a negative error code on failure; call
418 * SDL_GetError() for more information.
420 * \since This function is available since SDL 2.0.0.
422 * \sa SDL_CondBroadcast
424 * \sa SDL_CondWaitTimeout
426 * \sa SDL_DestroyCond
428 extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
431 * Wait until a condition variable is signaled or a certain time has passed.
433 * This function unlocks the specified `mutex` and waits for another thread to
434 * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
435 * `cond`, or for the specified time to elapse. Once the condition variable is
436 * signaled or the time elapsed, the mutex is re-locked and the function
439 * The mutex must be locked before calling this function.
441 * \param cond the condition variable to wait on
442 * \param mutex the mutex used to coordinate thread access
443 * \param ms the maximum time to wait, in milliseconds, or `SDL_MUTEX_MAXWAIT`
444 * to wait indefinitely
445 * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
446 * the condition is not signaled in the allotted time, or a negative
447 * error code on failure; call SDL_GetError() for more information.
449 * \since This function is available since SDL 2.0.0.
451 * \sa SDL_CondBroadcast
455 * \sa SDL_DestroyCond
457 extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
458 SDL_mutex * mutex, Uint32 ms);
460 /* @} *//* Condition variable functions */
463 /* Ends C function definitions when using C++ */
467 #include "close_code.h"
469 #endif /* SDL_mutex_h_ */
471 /* vi: set ts=4 sw=4 expandtab: */