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