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