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