SDL 3.0
SDL_mutex.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 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 * # CategoryMutex
27 *
28 * Functions to provide thread synchronization primitives.
29 */
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33
34/******************************************************************************/
35/* Enable thread safety attributes only with clang.
36 * The attributes can be safely erased when compiling with other compilers.
37 *
38 * To enable analysis, set these environment variables before running cmake:
39 * export CC=clang
40 * export CFLAGS="-DSDL_THREAD_SAFETY_ANALYSIS -Wthread-safety"
41 */
42#if defined(SDL_THREAD_SAFETY_ANALYSIS) && \
43 defined(__clang__) && (!defined(SWIG))
44#define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
45#else
46#define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x) /* no-op */
47#endif
48
49#define SDL_CAPABILITY(x) \
50 SDL_THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
51
52#define SDL_SCOPED_CAPABILITY \
53 SDL_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
54
55#define SDL_GUARDED_BY(x) \
56 SDL_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
57
58#define SDL_PT_GUARDED_BY(x) \
59 SDL_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
60
61#define SDL_ACQUIRED_BEFORE(x) \
62 SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))
63
64#define SDL_ACQUIRED_AFTER(x) \
65 SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))
66
67#define SDL_REQUIRES(x) \
68 SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(x))
69
70#define SDL_REQUIRES_SHARED(x) \
71 SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(x))
72
73#define SDL_ACQUIRE(x) \
74 SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(x))
75
76#define SDL_ACQUIRE_SHARED(x) \
77 SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(x))
78
79#define SDL_RELEASE(x) \
80 SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(x))
81
82#define SDL_RELEASE_SHARED(x) \
83 SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(x))
84
85#define SDL_RELEASE_GENERIC(x) \
86 SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(x))
87
88#define SDL_TRY_ACQUIRE(x, y) \
89 SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(x, y))
90
91#define SDL_TRY_ACQUIRE_SHARED(x, y) \
92 SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(x, y))
93
94#define SDL_EXCLUDES(x) \
95 SDL_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(x))
96
97#define SDL_ASSERT_CAPABILITY(x) \
98 SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
99
100#define SDL_ASSERT_SHARED_CAPABILITY(x) \
101 SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
102
103#define SDL_RETURN_CAPABILITY(x) \
104 SDL_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
105
106#define SDL_NO_THREAD_SAFETY_ANALYSIS \
107 SDL_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
108
109/******************************************************************************/
110
111
112#include <SDL3/SDL_begin_code.h>
113/* Set up for C function definitions, even when using C++ */
114#ifdef __cplusplus
115extern "C" {
116#endif
117
118/**
119 * Synchronization functions return this value if they time out.
120 *
121 * Not all functions _can_ time out; some will block indefinitely.
122 *
123 * \since This macro is available since SDL 3.0.0.
124 */
125#define SDL_MUTEX_TIMEDOUT 1
126
127
128/**
129 * \name Mutex functions
130 */
131/* @{ */
132
133/**
134 * A means to serialize access to a resource between threads.
135 *
136 * Mutexes (short for "mutual exclusion") are a synchronization primitive that
137 * allows exactly one thread to proceed at a time.
138 *
139 * Wikipedia has a thorough explanation of the concept:
140 *
141 * https://en.wikipedia.org/wiki/Mutex
142 *
143 * \since This struct is available since SDL 3.0.0.
144 */
145typedef struct SDL_Mutex SDL_Mutex;
146
147/**
148 * Create a new mutex.
149 *
150 * All newly-created mutexes begin in the _unlocked_ state.
151 *
152 * Calls to SDL_LockMutex() will not return while the mutex is locked by
153 * another thread. See SDL_TryLockMutex() to attempt to lock without blocking.
154 *
155 * SDL mutexes are reentrant.
156 *
157 * \returns the initialized and unlocked mutex or NULL on failure; call
158 * SDL_GetError() for more information.
159 *
160 * \since This function is available since SDL 3.0.0.
161 *
162 * \sa SDL_DestroyMutex
163 * \sa SDL_LockMutex
164 * \sa SDL_TryLockMutex
165 * \sa SDL_UnlockMutex
166 */
167extern SDL_DECLSPEC SDL_Mutex * SDLCALL SDL_CreateMutex(void);
168
169/**
170 * Lock the mutex.
171 *
172 * This will block until the mutex is available, which is to say it is in the
173 * unlocked state and the OS has chosen the caller as the next thread to lock
174 * it. Of all threads waiting to lock the mutex, only one may do so at a time.
175 *
176 * It is legal for the owning thread to lock an already-locked mutex. It must
177 * unlock it the same number of times before it is actually made available for
178 * other threads in the system (this is known as a "recursive mutex").
179 *
180 * This function does not fail; if mutex is NULL, it will return immediately
181 * having locked nothing. If the mutex is valid, this function will always
182 * block until it can lock the mutex, and return with it locked.
183 *
184 * \param mutex the mutex to lock.
185 *
186 * \since This function is available since SDL 3.0.0.
187 *
188 * \sa SDL_TryLockMutex
189 * \sa SDL_UnlockMutex
190 */
191extern SDL_DECLSPEC void SDLCALL SDL_LockMutex(SDL_Mutex *mutex) SDL_ACQUIRE(mutex);
192
193/**
194 * Try to lock a mutex without blocking.
195 *
196 * This works just like SDL_LockMutex(), but if the mutex is not available,
197 * this function returns `SDL_MUTEX_TIMEDOUT` immediately.
198 *
199 * This technique is useful if you need exclusive access to a resource but
200 * don't want to wait for it, and will return to it to try again later.
201 *
202 * This function does not fail; if mutex is NULL, it will return 0 immediately
203 * having locked nothing. If the mutex is valid, this function will always
204 * either lock the mutex and return 0, or return SDL_MUTEX_TIMEOUT and lock
205 * nothing.
206 *
207 * \param mutex the mutex to try to lock.
208 * \returns 0 or `SDL_MUTEX_TIMEDOUT`.
209 *
210 * \since This function is available since SDL 3.0.0.
211 *
212 * \sa SDL_LockMutex
213 * \sa SDL_UnlockMutex
214 */
215extern SDL_DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_Mutex *mutex) SDL_TRY_ACQUIRE(0, mutex);
216
217/**
218 * Unlock the mutex.
219 *
220 * It is legal for the owning thread to lock an already-locked mutex. It must
221 * unlock it the same number of times before it is actually made available for
222 * other threads in the system (this is known as a "recursive mutex").
223 *
224 * It is illegal to unlock a mutex that has not been locked by the current
225 * thread, and doing so results in undefined behavior.
226 *
227 * \param mutex the mutex to unlock.
228 *
229 * \since This function is available since SDL 3.0.0.
230 *
231 * \sa SDL_LockMutex
232 * \sa SDL_TryLockMutex
233 */
234extern SDL_DECLSPEC void SDLCALL SDL_UnlockMutex(SDL_Mutex *mutex) SDL_RELEASE(mutex);
235
236/**
237 * Destroy a mutex created with SDL_CreateMutex().
238 *
239 * This function must be called on any mutex that is no longer needed. Failure
240 * to destroy a mutex will result in a system memory or resource leak. While
241 * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
242 * to destroy a locked mutex, and may result in undefined behavior depending
243 * on the platform.
244 *
245 * \param mutex the mutex to destroy.
246 *
247 * \since This function is available since SDL 3.0.0.
248 *
249 * \sa SDL_CreateMutex
250 */
251extern SDL_DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_Mutex *mutex);
252
253/* @} *//* Mutex functions */
254
255
256/**
257 * \name Read/write lock functions
258 */
259/* @{ */
260
261/**
262 * A mutex that allows read-only threads to run in parallel.
263 *
264 * A rwlock is roughly the same concept as SDL_Mutex, but allows threads that
265 * request read-only access to all hold the lock at the same time. If a thread
266 * requests write access, it will block until all read-only threads have
267 * released the lock, and no one else can hold the thread (for reading or
268 * writing) at the same time as the writing thread.
269 *
270 * This can be more efficient in cases where several threads need to access
271 * data frequently, but changes to that data are rare.
272 *
273 * There are other rules that apply to rwlocks that don't apply to mutexes,
274 * about how threads are scheduled and when they can be recursively locked.
275 * These are documented in the other rwlock functions.
276 *
277 * \since This struct is available since SDL 3.0.0.
278 */
279typedef struct SDL_RWLock SDL_RWLock;
280
281/*
282 * Synchronization functions return this value if they time out.
283 *
284 * Not all functions _can_ time out; some will block indefinitely.
285 *
286 * This symbol is just for clarity when dealing with SDL_RWLock
287 * functions; its value is equivalent to SDL_MUTEX_TIMEOUT.
288 *
289 * \since This macro is available since SDL 3.0.0.
290 */
291#define SDL_RWLOCK_TIMEDOUT SDL_MUTEX_TIMEDOUT
292
293
294/**
295 * Create a new read/write lock.
296 *
297 * A read/write lock is useful for situations where you have multiple threads
298 * trying to access a resource that is rarely updated. All threads requesting
299 * a read-only lock will be allowed to run in parallel; if a thread requests a
300 * write lock, it will be provided exclusive access. This makes it safe for
301 * multiple threads to use a resource at the same time if they promise not to
302 * change it, and when it has to be changed, the rwlock will serve as a
303 * gateway to make sure those changes can be made safely.
304 *
305 * In the right situation, a rwlock can be more efficient than a mutex, which
306 * only lets a single thread proceed at a time, even if it won't be modifying
307 * the data.
308 *
309 * All newly-created read/write locks begin in the _unlocked_ state.
310 *
311 * Calls to SDL_LockRWLockForReading() and SDL_LockRWLockForWriting will not
312 * return while the rwlock is locked _for writing_ by another thread. See
313 * SDL_TryLockRWLockForReading() and SDL_TryLockRWLockForWriting() to attempt
314 * to lock without blocking.
315 *
316 * SDL read/write locks are only recursive for read-only locks! They are not
317 * guaranteed to be fair, or provide access in a FIFO manner! They are not
318 * guaranteed to favor writers. You may not lock a rwlock for both read-only
319 * and write access at the same time from the same thread (so you can't
320 * promote your read-only lock to a write lock without unlocking first).
321 *
322 * \returns the initialized and unlocked read/write lock or NULL on failure;
323 * call SDL_GetError() for more information.
324 *
325 * \since This function is available since SDL 3.0.0.
326 *
327 * \sa SDL_DestroyRWLock
328 * \sa SDL_LockRWLockForReading
329 * \sa SDL_LockRWLockForWriting
330 * \sa SDL_TryLockRWLockForReading
331 * \sa SDL_TryLockRWLockForWriting
332 * \sa SDL_UnlockRWLock
333 */
334extern SDL_DECLSPEC SDL_RWLock * SDLCALL SDL_CreateRWLock(void);
335
336/**
337 * Lock the read/write lock for _read only_ operations.
338 *
339 * This will block until the rwlock is available, which is to say it is not
340 * locked for writing by any other thread. Of all threads waiting to lock the
341 * rwlock, all may do so at the same time as long as they are requesting
342 * read-only access; if a thread wants to lock for writing, only one may do so
343 * at a time, and no other threads, read-only or not, may hold the lock at the
344 * same time.
345 *
346 * It is legal for the owning thread to lock an already-locked rwlock for
347 * reading. It must unlock it the same number of times before it is actually
348 * made available for other threads in the system (this is known as a
349 * "recursive rwlock").
350 *
351 * Note that locking for writing is not recursive (this is only available to
352 * read-only locks).
353 *
354 * It is illegal to request a read-only lock from a thread that already holds
355 * the write lock. Doing so results in undefined behavior. Unlock the write
356 * lock before requesting a read-only lock. (But, of course, if you have the
357 * write lock, you don't need further locks to read in any case.)
358 *
359 * This function does not fail; if rwlock is NULL, it will return immediately
360 * having locked nothing. If the rwlock is valid, this function will always
361 * block until it can lock the mutex, and return with it locked.
362 *
363 * \param rwlock the read/write lock to lock.
364 *
365 * \since This function is available since SDL 3.0.0.
366 *
367 * \sa SDL_LockRWLockForWriting
368 * \sa SDL_TryLockRWLockForReading
369 * \sa SDL_UnlockRWLock
370 */
372
373/**
374 * Lock the read/write lock for _write_ operations.
375 *
376 * This will block until the rwlock is available, which is to say it is not
377 * locked for reading or writing by any other thread. Only one thread may hold
378 * the lock when it requests write access; all other threads, whether they
379 * also want to write or only want read-only access, must wait until the
380 * writer thread has released the lock.
381 *
382 * It is illegal for the owning thread to lock an already-locked rwlock for
383 * writing (read-only may be locked recursively, writing can not). Doing so
384 * results in undefined behavior.
385 *
386 * It is illegal to request a write lock from a thread that already holds a
387 * read-only lock. Doing so results in undefined behavior. Unlock the
388 * read-only lock before requesting a write lock.
389 *
390 * This function does not fail; if rwlock is NULL, it will return immediately
391 * having locked nothing. If the rwlock is valid, this function will always
392 * block until it can lock the mutex, and return with it locked.
393 *
394 * \param rwlock the read/write lock to lock.
395 *
396 * \since This function is available since SDL 3.0.0.
397 *
398 * \sa SDL_LockRWLockForReading
399 * \sa SDL_TryLockRWLockForWriting
400 * \sa SDL_UnlockRWLock
401 */
402extern SDL_DECLSPEC void SDLCALL SDL_LockRWLockForWriting(SDL_RWLock *rwlock) SDL_ACQUIRE(rwlock);
403
404/**
405 * Try to lock a read/write lock _for reading_ without blocking.
406 *
407 * This works just like SDL_LockRWLockForReading(), but if the rwlock is not
408 * available, then this function returns `SDL_RWLOCK_TIMEDOUT` immediately.
409 *
410 * This technique is useful if you need access to a resource but don't want to
411 * wait for it, and will return to it to try again later.
412 *
413 * Trying to lock for read-only access can succeed if other threads are
414 * holding read-only locks, as this won't prevent access.
415 *
416 * This function does not fail; if rwlock is NULL, it will return 0
417 * immediately having locked nothing. If rwlock is valid, this function will
418 * always either lock the rwlock and return 0, or return SDL_RWLOCK_TIMEOUT
419 * and lock nothing.
420 *
421 * \param rwlock the rwlock to try to lock.
422 * \returns 0 or `SDL_RWLOCK_TIMEDOUT`.
423 *
424 * \since This function is available since SDL 3.0.0.
425 *
426 * \sa SDL_LockRWLockForReading
427 * \sa SDL_TryLockRWLockForWriting
428 * \sa SDL_UnlockRWLock
429 */
431
432/**
433 * Try to lock a read/write lock _for writing_ without blocking.
434 *
435 * This works just like SDL_LockRWLockForWriting(), but if the rwlock is not
436 * available, this function returns `SDL_RWLOCK_TIMEDOUT` immediately.
437 *
438 * This technique is useful if you need exclusive access to a resource but
439 * don't want to wait for it, and will return to it to try again later.
440 *
441 * It is illegal for the owning thread to lock an already-locked rwlock for
442 * writing (read-only may be locked recursively, writing can not). Doing so
443 * results in undefined behavior.
444 *
445 * It is illegal to request a write lock from a thread that already holds a
446 * read-only lock. Doing so results in undefined behavior. Unlock the
447 * read-only lock before requesting a write lock.
448 *
449 * This function does not fail; if rwlock is NULL, it will return 0
450 * immediately having locked nothing. If rwlock is valid, this function will
451 * always either lock the rwlock and return 0, or return SDL_RWLOCK_TIMEOUT
452 * and lock nothing.
453 *
454 * \param rwlock the rwlock to try to lock.
455 * \returns 0 or `SDL_RWLOCK_TIMEDOUT`.
456 *
457 * \since This function is available since SDL 3.0.0.
458 *
459 * \sa SDL_LockRWLockForWriting
460 * \sa SDL_TryLockRWLockForReading
461 * \sa SDL_UnlockRWLock
462 */
464
465/**
466 * Unlock the read/write lock.
467 *
468 * Use this function to unlock the rwlock, whether it was locked for read-only
469 * or write operations.
470 *
471 * It is legal for the owning thread to lock an already-locked read-only lock.
472 * It must unlock it the same number of times before it is actually made
473 * available for other threads in the system (this is known as a "recursive
474 * rwlock").
475 *
476 * It is illegal to unlock a rwlock that has not been locked by the current
477 * thread, and doing so results in undefined behavior.
478 *
479 * \param rwlock the rwlock to unlock.
480 *
481 * \since This function is available since SDL 3.0.0.
482 *
483 * \sa SDL_LockRWLockForReading
484 * \sa SDL_LockRWLockForWriting
485 * \sa SDL_TryLockRWLockForReading
486 * \sa SDL_TryLockRWLockForWriting
487 */
488extern SDL_DECLSPEC void SDLCALL SDL_UnlockRWLock(SDL_RWLock *rwlock) SDL_RELEASE_GENERIC(rwlock);
489
490/**
491 * Destroy a read/write lock created with SDL_CreateRWLock().
492 *
493 * This function must be called on any read/write lock that is no longer
494 * needed. Failure to destroy a rwlock will result in a system memory or
495 * resource leak. While it is safe to destroy a rwlock that is _unlocked_, it
496 * is not safe to attempt to destroy a locked rwlock, and may result in
497 * undefined behavior depending on the platform.
498 *
499 * \param rwlock the rwlock to destroy.
500 *
501 * \since This function is available since SDL 3.0.0.
502 *
503 * \sa SDL_CreateRWLock
504 */
505extern SDL_DECLSPEC void SDLCALL SDL_DestroyRWLock(SDL_RWLock *rwlock);
506
507/* @} *//* Read/write lock functions */
508
509
510/**
511 * \name Semaphore functions
512 */
513/* @{ */
514
515/**
516 * A means to manage access to a resource, by count, between threads.
517 *
518 * Semaphores (specifically, "counting semaphores"), let X number of threads
519 * request access at the same time, each thread granted access decrementing a
520 * counter. When the counter reaches zero, future requests block until a prior
521 * thread releases their request, incrementing the counter again.
522 *
523 * Wikipedia has a thorough explanation of the concept:
524 *
525 * https://en.wikipedia.org/wiki/Semaphore_(programming)
526 *
527 * \since This struct is available since SDL 3.0.0.
528 */
530
531/**
532 * Create a semaphore.
533 *
534 * This function creates a new semaphore and initializes it with the value
535 * `initial_value`. Each wait operation on the semaphore will atomically
536 * decrement the semaphore value and potentially block if the semaphore value
537 * is 0. Each post operation will atomically increment the semaphore value and
538 * wake waiting threads and allow them to retry the wait operation.
539 *
540 * \param initial_value the starting value of the semaphore.
541 * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
542 * information.
543 *
544 * \since This function is available since SDL 3.0.0.
545 *
546 * \sa SDL_DestroySemaphore
547 * \sa SDL_SignalSemaphore
548 * \sa SDL_TryWaitSemaphore
549 * \sa SDL_GetSemaphoreValue
550 * \sa SDL_WaitSemaphore
551 * \sa SDL_WaitSemaphoreTimeout
552 */
553extern SDL_DECLSPEC SDL_Semaphore * SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
554
555/**
556 * Destroy a semaphore.
557 *
558 * It is not safe to destroy a semaphore if there are threads currently
559 * waiting on it.
560 *
561 * \param sem the semaphore to destroy.
562 *
563 * \since This function is available since SDL 3.0.0.
564 *
565 * \sa SDL_CreateSemaphore
566 */
567extern SDL_DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_Semaphore *sem);
568
569/**
570 * Wait until a semaphore has a positive value and then decrements it.
571 *
572 * This function suspends the calling thread until either the semaphore
573 * pointed to by `sem` has a positive value or the call is interrupted by a
574 * signal or error. If the call is successful it will atomically decrement the
575 * semaphore value.
576 *
577 * This function is the equivalent of calling SDL_WaitSemaphoreTimeout() with
578 * a time length of -1.
579 *
580 * \param sem the semaphore wait on.
581 * \returns 0 on success or a negative error code on failure; call
582 * SDL_GetError() for more information.
583 *
584 * \since This function is available since SDL 3.0.0.
585 *
586 * \sa SDL_SignalSemaphore
587 * \sa SDL_TryWaitSemaphore
588 * \sa SDL_WaitSemaphoreTimeout
589 */
590extern SDL_DECLSPEC int SDLCALL SDL_WaitSemaphore(SDL_Semaphore *sem);
591
592/**
593 * See if a semaphore has a positive value and decrement it if it does.
594 *
595 * This function checks to see if the semaphore pointed to by `sem` has a
596 * positive value and atomically decrements the semaphore value if it does. If
597 * the semaphore doesn't have a positive value, the function immediately
598 * returns SDL_MUTEX_TIMEDOUT.
599 *
600 * \param sem the semaphore to wait on.
601 * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
602 * block, or a negative error code on failure; call SDL_GetError()
603 * for more information.
604 *
605 * \since This function is available since SDL 3.0.0.
606 *
607 * \sa SDL_SignalSemaphore
608 * \sa SDL_WaitSemaphore
609 * \sa SDL_WaitSemaphoreTimeout
610 */
611extern SDL_DECLSPEC int SDLCALL SDL_TryWaitSemaphore(SDL_Semaphore *sem);
612
613/**
614 * Wait until a semaphore has a positive value and then decrements it.
615 *
616 * This function suspends the calling thread until either the semaphore
617 * pointed to by `sem` has a positive value, the call is interrupted by a
618 * signal or error, or the specified time has elapsed. If the call is
619 * successful it will atomically decrement the semaphore value.
620 *
621 * \param sem the semaphore to wait on.
622 * \param timeoutMS the length of the timeout, in milliseconds.
623 * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
624 * succeed in the allotted time, or a negative error code on failure;
625 * call SDL_GetError() for more information.
626 *
627 * \since This function is available since SDL 3.0.0.
628 *
629 * \sa SDL_SignalSemaphore
630 * \sa SDL_TryWaitSemaphore
631 * \sa SDL_WaitSemaphore
632 */
633extern SDL_DECLSPEC int SDLCALL SDL_WaitSemaphoreTimeout(SDL_Semaphore *sem, Sint32 timeoutMS);
634
635/**
636 * Atomically increment a semaphore's value and wake waiting threads.
637 *
638 * \param sem the semaphore to increment.
639 * \returns 0 on success or a negative error code on failure; call
640 * SDL_GetError() for more information.
641 *
642 * \since This function is available since SDL 3.0.0.
643 *
644 * \sa SDL_TryWaitSemaphore
645 * \sa SDL_WaitSemaphore
646 * \sa SDL_WaitSemaphoreTimeout
647 */
648extern SDL_DECLSPEC int SDLCALL SDL_SignalSemaphore(SDL_Semaphore *sem);
649
650/**
651 * Get the current value of a semaphore.
652 *
653 * \param sem the semaphore to query.
654 * \returns the current value of the semaphore.
655 *
656 * \since This function is available since SDL 3.0.0.
657 */
658extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetSemaphoreValue(SDL_Semaphore *sem);
659
660/* @} *//* Semaphore functions */
661
662
663/**
664 * \name Condition variable functions
665 */
666/* @{ */
667
668/**
669 * A means to block multiple threads until a condition is satisfied.
670 *
671 * Condition variables, paired with an SDL_Mutex, let an app halt multiple
672 * threads until a condition has occurred, at which time the app can release
673 * one or all waiting threads.
674 *
675 * Wikipedia has a thorough explanation of the concept:
676 *
677 * https://en.wikipedia.org/wiki/Condition_variable
678 *
679 * \since This struct is available since SDL 3.0.0.
680 */
682
683/**
684 * Create a condition variable.
685 *
686 * \returns a new condition variable or NULL on failure; call SDL_GetError()
687 * for more information.
688 *
689 * \since This function is available since SDL 3.0.0.
690 *
691 * \sa SDL_BroadcastCondition
692 * \sa SDL_SignalCondition
693 * \sa SDL_WaitCondition
694 * \sa SDL_WaitConditionTimeout
695 * \sa SDL_DestroyCondition
696 */
697extern SDL_DECLSPEC SDL_Condition * SDLCALL SDL_CreateCondition(void);
698
699/**
700 * Destroy a condition variable.
701 *
702 * \param cond the condition variable to destroy.
703 *
704 * \since This function is available since SDL 3.0.0.
705 *
706 * \sa SDL_CreateCondition
707 */
708extern SDL_DECLSPEC void SDLCALL SDL_DestroyCondition(SDL_Condition *cond);
709
710/**
711 * Restart one of the threads that are waiting on the condition variable.
712 *
713 * \param cond the condition variable to signal.
714 * \returns 0 on success or a negative error code on failure; call
715 * SDL_GetError() for more information.
716 *
717 * \threadsafety It is safe to call this function from any thread.
718 *
719 * \since This function is available since SDL 3.0.0.
720 *
721 * \sa SDL_BroadcastCondition
722 * \sa SDL_WaitCondition
723 * \sa SDL_WaitConditionTimeout
724 */
725extern SDL_DECLSPEC int SDLCALL SDL_SignalCondition(SDL_Condition *cond);
726
727/**
728 * Restart all threads that are waiting on the condition variable.
729 *
730 * \param cond the condition variable to signal.
731 * \returns 0 on success or a negative error code on failure; call
732 * SDL_GetError() for more information.
733 *
734 * \threadsafety It is safe to call this function from any thread.
735 *
736 * \since This function is available since SDL 3.0.0.
737 *
738 * \sa SDL_SignalCondition
739 * \sa SDL_WaitCondition
740 * \sa SDL_WaitConditionTimeout
741 */
742extern SDL_DECLSPEC int SDLCALL SDL_BroadcastCondition(SDL_Condition *cond);
743
744/**
745 * Wait until a condition variable is signaled.
746 *
747 * This function unlocks the specified `mutex` and waits for another thread to
748 * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
749 * variable `cond`. Once the condition variable is signaled, the mutex is
750 * re-locked and the function returns.
751 *
752 * The mutex must be locked before calling this function. Locking the mutex
753 * recursively (more than once) is not supported and leads to undefined
754 * behavior.
755 *
756 * This function is the equivalent of calling SDL_WaitConditionTimeout() with
757 * a time length of -1.
758 *
759 * \param cond the condition variable to wait on.
760 * \param mutex the mutex used to coordinate thread access.
761 * \returns 0 when it is signaled or a negative error code on failure; call
762 * SDL_GetError() for more information.
763 *
764 * \threadsafety It is safe to call this function from any thread.
765 *
766 * \since This function is available since SDL 3.0.0.
767 *
768 * \sa SDL_BroadcastCondition
769 * \sa SDL_SignalCondition
770 * \sa SDL_WaitConditionTimeout
771 */
772extern SDL_DECLSPEC int SDLCALL SDL_WaitCondition(SDL_Condition *cond, SDL_Mutex *mutex);
773
774/**
775 * Wait until a condition variable is signaled or a certain time has passed.
776 *
777 * This function unlocks the specified `mutex` and waits for another thread to
778 * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
779 * variable `cond`, or for the specified time to elapse. Once the condition
780 * variable is signaled or the time elapsed, the mutex is re-locked and the
781 * function returns.
782 *
783 * The mutex must be locked before calling this function. Locking the mutex
784 * recursively (more than once) is not supported and leads to undefined
785 * behavior.
786 *
787 * \param cond the condition variable to wait on.
788 * \param mutex the mutex used to coordinate thread access.
789 * \param timeoutMS the maximum time to wait, in milliseconds, or -1 to wait
790 * indefinitely.
791 * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
792 * the condition is not signaled in the allotted time, or a negative
793 * error code on failure; call SDL_GetError() for more information.
794 *
795 * \threadsafety It is safe to call this function from any thread.
796 *
797 * \since This function is available since SDL 3.0.0.
798 *
799 * \sa SDL_BroadcastCondition
800 * \sa SDL_SignalCondition
801 * \sa SDL_WaitCondition
802 */
803extern SDL_DECLSPEC int SDLCALL SDL_WaitConditionTimeout(SDL_Condition *cond,
804 SDL_Mutex *mutex, Sint32 timeoutMS);
805
806/* @} *//* Condition variable functions */
807
808
809/* Ends C function definitions when using C++ */
810#ifdef __cplusplus
811}
812#endif
813#include <SDL3/SDL_close_code.h>
814
815#endif /* SDL_mutex_h_ */
void SDL_DestroyRWLock(SDL_RWLock *rwlock)
int rwlock
Definition SDL_mutex.h:430
#define SDL_ACQUIRE(x)
Definition SDL_mutex.h:73
int SDL_TryLockMutex(SDL_Mutex *mutex) SDL_TRY_ACQUIRE(0
#define SDL_TRY_ACQUIRE(x, y)
Definition SDL_mutex.h:88
SDL_RWLock * SDL_CreateRWLock(void)
void SDL_DestroySemaphore(SDL_Semaphore *sem)
#define SDL_TRY_ACQUIRE_SHARED(x, y)
Definition SDL_mutex.h:91
#define SDL_ACQUIRE_SHARED(x)
Definition SDL_mutex.h:76
void SDL_UnlockMutex(SDL_Mutex *mutex) SDL_RELEASE(mutex)
int SDL_WaitCondition(SDL_Condition *cond, SDL_Mutex *mutex)
void SDL_LockRWLockForWriting(SDL_RWLock *rwlock) SDL_ACQUIRE(rwlock)
int SDL_SignalCondition(SDL_Condition *cond)
struct SDL_Mutex SDL_Mutex
Definition SDL_mutex.h:145
int SDL_TryLockRWLockForReading(SDL_RWLock *rwlock) SDL_TRY_ACQUIRE_SHARED(0
#define SDL_RELEASE_GENERIC(x)
Definition SDL_mutex.h:85
SDL_Semaphore * SDL_CreateSemaphore(Uint32 initial_value)
void SDL_LockMutex(SDL_Mutex *mutex) SDL_ACQUIRE(mutex)
int SDL_WaitSemaphoreTimeout(SDL_Semaphore *sem, Sint32 timeoutMS)
int SDL_TryWaitSemaphore(SDL_Semaphore *sem)
Uint32 SDL_GetSemaphoreValue(SDL_Semaphore *sem)
struct SDL_Semaphore SDL_Semaphore
Definition SDL_mutex.h:529
int mutex
Definition SDL_mutex.h:215
int SDL_WaitConditionTimeout(SDL_Condition *cond, SDL_Mutex *mutex, Sint32 timeoutMS)
void SDL_UnlockRWLock(SDL_RWLock *rwlock) SDL_RELEASE_GENERIC(rwlock)
int SDL_BroadcastCondition(SDL_Condition *cond)
struct SDL_RWLock SDL_RWLock
Definition SDL_mutex.h:279
void SDL_DestroyCondition(SDL_Condition *cond)
void SDL_DestroyMutex(SDL_Mutex *mutex)
SDL_Condition * SDL_CreateCondition(void)
#define SDL_RELEASE(x)
Definition SDL_mutex.h:79
int SDL_SignalSemaphore(SDL_Semaphore *sem)
SDL_Mutex * SDL_CreateMutex(void)
int SDL_WaitSemaphore(SDL_Semaphore *sem)
void SDL_LockRWLockForReading(SDL_RWLock *rwlock) SDL_ACQUIRE_SHARED(rwlock)
int SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock) SDL_TRY_ACQUIRE(0
struct SDL_Condition SDL_Condition
Definition SDL_mutex.h:681
int32_t Sint32
Definition SDL_stdinc.h:256
uint32_t Uint32
Definition SDL_stdinc.h:265