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