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