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
384 * same thread.
385 *
386 * \since This function is available since SDL 3.2.0.
387 *
388 * \sa SDL_LockMutex
389 * \sa SDL_TryLockMutex
390 */
391extern SDL_DECLSPEC void SDLCALL SDL_UnlockMutex(SDL_Mutex *mutex) SDL_RELEASE(mutex);
392
393/**
394 * Destroy a mutex created with SDL_CreateMutex().
395 *
396 * This function must be called on any mutex that is no longer needed. Failure
397 * to destroy a mutex will result in a system memory or resource leak. While
398 * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
399 * to destroy a locked mutex, and may result in undefined behavior depending
400 * on the platform.
401 *
402 * \param mutex the mutex to destroy.
403 *
404 * \threadsafety It is safe to call this function from any thread.
405 *
406 * \since This function is available since SDL 3.2.0.
407 *
408 * \sa SDL_CreateMutex
409 */
410extern SDL_DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_Mutex *mutex);
411
412/* @} *//* Mutex functions */
413
414
415/**
416 * \name Read/write lock functions
417 */
418/* @{ */
419
420/**
421 * A mutex that allows read-only threads to run in parallel.
422 *
423 * A rwlock is roughly the same concept as SDL_Mutex, but allows threads that
424 * request read-only access to all hold the lock at the same time. If a thread
425 * requests write access, it will block until all read-only threads have
426 * released the lock, and no one else can hold the thread (for reading or
427 * writing) at the same time as the writing thread.
428 *
429 * This can be more efficient in cases where several threads need to access
430 * data frequently, but changes to that data are rare.
431 *
432 * There are other rules that apply to rwlocks that don't apply to mutexes,
433 * about how threads are scheduled and when they can be recursively locked.
434 * These are documented in the other rwlock functions.
435 *
436 * \since This struct is available since SDL 3.2.0.
437 */
438typedef struct SDL_RWLock SDL_RWLock;
439
440/**
441 * Create a new read/write lock.
442 *
443 * A read/write lock is useful for situations where you have multiple threads
444 * trying to access a resource that is rarely updated. All threads requesting
445 * a read-only lock will be allowed to run in parallel; if a thread requests a
446 * write lock, it will be provided exclusive access. This makes it safe for
447 * multiple threads to use a resource at the same time if they promise not to
448 * change it, and when it has to be changed, the rwlock will serve as a
449 * gateway to make sure those changes can be made safely.
450 *
451 * In the right situation, a rwlock can be more efficient than a mutex, which
452 * only lets a single thread proceed at a time, even if it won't be modifying
453 * the data.
454 *
455 * All newly-created read/write locks begin in the _unlocked_ state.
456 *
457 * Calls to SDL_LockRWLockForReading() and SDL_LockRWLockForWriting will not
458 * return while the rwlock is locked _for writing_ by another thread. See
459 * SDL_TryLockRWLockForReading() and SDL_TryLockRWLockForWriting() to attempt
460 * to lock without blocking.
461 *
462 * SDL read/write locks are only recursive for read-only locks! They are not
463 * guaranteed to be fair, or provide access in a FIFO manner! They are not
464 * guaranteed to favor writers. You may not lock a rwlock for both read-only
465 * and write access at the same time from the same thread (so you can't
466 * promote your read-only lock to a write lock without unlocking first).
467 *
468 * \returns the initialized and unlocked read/write lock or NULL on failure;
469 * call SDL_GetError() for more information.
470 *
471 * \threadsafety It is safe to call this function from any thread.
472 *
473 * \since This function is available since SDL 3.2.0.
474 *
475 * \sa SDL_DestroyRWLock
476 * \sa SDL_LockRWLockForReading
477 * \sa SDL_LockRWLockForWriting
478 * \sa SDL_TryLockRWLockForReading
479 * \sa SDL_TryLockRWLockForWriting
480 * \sa SDL_UnlockRWLock
481 */
482extern SDL_DECLSPEC SDL_RWLock * SDLCALL SDL_CreateRWLock(void);
483
484/**
485 * Lock the read/write lock for _read only_ operations.
486 *
487 * This will block until the rwlock is available, which is to say it is not
488 * locked for writing by any other thread. Of all threads waiting to lock the
489 * rwlock, all may do so at the same time as long as they are requesting
490 * read-only access; if a thread wants to lock for writing, only one may do so
491 * at a time, and no other threads, read-only or not, may hold the lock at the
492 * same time.
493 *
494 * It is legal for the owning thread to lock an already-locked rwlock for
495 * reading. It must unlock it the same number of times before it is actually
496 * made available for other threads in the system (this is known as a
497 * "recursive rwlock").
498 *
499 * Note that locking for writing is not recursive (this is only available to
500 * read-only locks).
501 *
502 * It is illegal to request a read-only lock from a thread that already holds
503 * the write lock. Doing so results in undefined behavior. Unlock the write
504 * lock before requesting a read-only lock. (But, of course, if you have the
505 * write lock, you don't need further locks to read in any case.)
506 *
507 * This function does not fail; if rwlock is NULL, it will return immediately
508 * having locked nothing. If the rwlock is valid, this function will always
509 * block until it can lock the mutex, and return with it locked.
510 *
511 * \param rwlock the read/write lock to lock.
512 *
513 * \threadsafety It is safe to call this function from any thread.
514 *
515 * \since This function is available since SDL 3.2.0.
516 *
517 * \sa SDL_LockRWLockForWriting
518 * \sa SDL_TryLockRWLockForReading
519 * \sa SDL_UnlockRWLock
520 */
522
523/**
524 * Lock the read/write lock for _write_ operations.
525 *
526 * This will block until the rwlock is available, which is to say it is not
527 * locked for reading or writing by any other thread. Only one thread may hold
528 * the lock when it requests write access; all other threads, whether they
529 * also want to write or only want read-only access, must wait until the
530 * writer thread has released the lock.
531 *
532 * It is illegal for the owning thread to lock an already-locked rwlock for
533 * writing (read-only may be locked recursively, writing can not). Doing so
534 * results in undefined behavior.
535 *
536 * It is illegal to request a write lock from a thread that already holds a
537 * read-only lock. Doing so results in undefined behavior. Unlock the
538 * read-only lock before requesting a write lock.
539 *
540 * This function does not fail; if rwlock is NULL, it will return immediately
541 * having locked nothing. If the rwlock is valid, this function will always
542 * block until it can lock the mutex, and return with it locked.
543 *
544 * \param rwlock the read/write lock to lock.
545 *
546 * \threadsafety It is safe to call this function from any thread.
547 *
548 * \since This function is available since SDL 3.2.0.
549 *
550 * \sa SDL_LockRWLockForReading
551 * \sa SDL_TryLockRWLockForWriting
552 * \sa SDL_UnlockRWLock
553 */
554extern SDL_DECLSPEC void SDLCALL SDL_LockRWLockForWriting(SDL_RWLock *rwlock) SDL_ACQUIRE(rwlock);
555
556/**
557 * Try to lock a read/write lock _for reading_ without blocking.
558 *
559 * This works just like SDL_LockRWLockForReading(), but if the rwlock is not
560 * available, then this function returns false immediately.
561 *
562 * This technique is useful if you need access to a resource but don't want to
563 * wait for it, and will return to it to try again later.
564 *
565 * Trying to lock for read-only access can succeed if other threads are
566 * holding read-only locks, as this won't prevent access.
567 *
568 * This function returns true if passed a NULL rwlock.
569 *
570 * \param rwlock the rwlock to try to lock.
571 * \returns true on success, false if the lock would block.
572 *
573 * \threadsafety It is safe to call this function from any thread.
574 *
575 * \since This function is available since SDL 3.2.0.
576 *
577 * \sa SDL_LockRWLockForReading
578 * \sa SDL_TryLockRWLockForWriting
579 * \sa SDL_UnlockRWLock
580 */
582
583/**
584 * Try to lock a read/write lock _for writing_ without blocking.
585 *
586 * This works just like SDL_LockRWLockForWriting(), but if the rwlock is not
587 * available, then this function returns false immediately.
588 *
589 * This technique is useful if you need exclusive access to a resource but
590 * don't want to wait for it, and will return to it to try again later.
591 *
592 * It is illegal for the owning thread to lock an already-locked rwlock for
593 * writing (read-only may be locked recursively, writing can not). Doing so
594 * results in undefined behavior.
595 *
596 * It is illegal to request a write lock from a thread that already holds a
597 * read-only lock. Doing so results in undefined behavior. Unlock the
598 * read-only lock before requesting a write lock.
599 *
600 * This function returns true if passed a NULL rwlock.
601 *
602 * \param rwlock the rwlock to try to lock.
603 * \returns true on success, false if the lock would block.
604 *
605 * \threadsafety It is safe to call this function from any thread.
606 *
607 * \since This function is available since SDL 3.2.0.
608 *
609 * \sa SDL_LockRWLockForWriting
610 * \sa SDL_TryLockRWLockForReading
611 * \sa SDL_UnlockRWLock
612 */
613extern SDL_DECLSPEC bool SDLCALL SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock) SDL_TRY_ACQUIRE(true, rwlock);
614
615/**
616 * Unlock the read/write lock.
617 *
618 * Use this function to unlock the rwlock, whether it was locked for read-only
619 * or write operations.
620 *
621 * It is legal for the owning thread to lock an already-locked read-only lock.
622 * It must unlock it the same number of times before it is actually made
623 * available for other threads in the system (this is known as a "recursive
624 * rwlock").
625 *
626 * It is illegal to unlock a rwlock that has not been locked by the current
627 * thread, and doing so results in undefined behavior.
628 *
629 * \param rwlock the rwlock to unlock.
630 *
631 * \threadsafety This call must be paired with a previous locking call on the
632 * same thread.
633 *
634 * \since This function is available since SDL 3.2.0.
635 *
636 * \sa SDL_LockRWLockForReading
637 * \sa SDL_LockRWLockForWriting
638 * \sa SDL_TryLockRWLockForReading
639 * \sa SDL_TryLockRWLockForWriting
640 */
641extern SDL_DECLSPEC void SDLCALL SDL_UnlockRWLock(SDL_RWLock *rwlock) SDL_RELEASE_GENERIC(rwlock);
642
643/**
644 * Destroy a read/write lock created with SDL_CreateRWLock().
645 *
646 * This function must be called on any read/write lock that is no longer
647 * needed. Failure to destroy a rwlock will result in a system memory or
648 * resource leak. While it is safe to destroy a rwlock that is _unlocked_, it
649 * is not safe to attempt to destroy a locked rwlock, and may result in
650 * undefined behavior depending on the platform.
651 *
652 * \param rwlock the rwlock to destroy.
653 *
654 * \threadsafety It is safe to call this function from any thread.
655 *
656 * \since This function is available since SDL 3.2.0.
657 *
658 * \sa SDL_CreateRWLock
659 */
660extern SDL_DECLSPEC void SDLCALL SDL_DestroyRWLock(SDL_RWLock *rwlock);
661
662/* @} *//* Read/write lock functions */
663
664
665/**
666 * \name Semaphore functions
667 */
668/* @{ */
669
670/**
671 * A means to manage access to a resource, by count, between threads.
672 *
673 * Semaphores (specifically, "counting semaphores"), let X number of threads
674 * request access at the same time, each thread granted access decrementing a
675 * counter. When the counter reaches zero, future requests block until a prior
676 * thread releases their request, incrementing the counter again.
677 *
678 * Wikipedia has a thorough explanation of the concept:
679 *
680 * https://en.wikipedia.org/wiki/Semaphore_(programming)
681 *
682 * \since This struct is available since SDL 3.2.0.
683 */
685
686/**
687 * Create a semaphore.
688 *
689 * This function creates a new semaphore and initializes it with the value
690 * `initial_value`. Each wait operation on the semaphore will atomically
691 * decrement the semaphore value and potentially block if the semaphore value
692 * is 0. Each post operation will atomically increment the semaphore value and
693 * wake waiting threads and allow them to retry the wait operation.
694 *
695 * \param initial_value the starting value of the semaphore.
696 * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
697 * information.
698 *
699 * \threadsafety It is safe to call this function from any thread.
700 *
701 * \since This function is available since SDL 3.2.0.
702 *
703 * \sa SDL_DestroySemaphore
704 * \sa SDL_SignalSemaphore
705 * \sa SDL_TryWaitSemaphore
706 * \sa SDL_GetSemaphoreValue
707 * \sa SDL_WaitSemaphore
708 * \sa SDL_WaitSemaphoreTimeout
709 */
710extern SDL_DECLSPEC SDL_Semaphore * SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
711
712/**
713 * Destroy a semaphore.
714 *
715 * It is not safe to destroy a semaphore if there are threads currently
716 * waiting on it.
717 *
718 * \param sem the semaphore to destroy.
719 *
720 * \threadsafety It is safe to call this function from any thread.
721 *
722 * \since This function is available since SDL 3.2.0.
723 *
724 * \sa SDL_CreateSemaphore
725 */
726extern SDL_DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_Semaphore *sem);
727
728/**
729 * Wait until a semaphore has a positive value and then decrements it.
730 *
731 * This function suspends the calling thread until the semaphore pointed to by
732 * `sem` has a positive value, and then atomically decrement the semaphore
733 * value.
734 *
735 * This function is the equivalent of calling SDL_WaitSemaphoreTimeout() with
736 * a time length of -1.
737 *
738 * \param sem the semaphore wait on.
739 *
740 * \threadsafety It is safe to call this function from any thread.
741 *
742 * \since This function is available since SDL 3.2.0.
743 *
744 * \sa SDL_SignalSemaphore
745 * \sa SDL_TryWaitSemaphore
746 * \sa SDL_WaitSemaphoreTimeout
747 */
748extern SDL_DECLSPEC void SDLCALL SDL_WaitSemaphore(SDL_Semaphore *sem);
749
750/**
751 * See if a semaphore has a positive value and decrement it if it does.
752 *
753 * This function checks to see if the semaphore pointed to by `sem` has a
754 * positive value and atomically decrements the semaphore value if it does. If
755 * the semaphore doesn't have a positive value, the function immediately
756 * returns false.
757 *
758 * \param sem the semaphore to wait on.
759 * \returns true if the wait succeeds, false if the wait would block.
760 *
761 * \threadsafety It is safe to call this function from any thread.
762 *
763 * \since This function is available since SDL 3.2.0.
764 *
765 * \sa SDL_SignalSemaphore
766 * \sa SDL_WaitSemaphore
767 * \sa SDL_WaitSemaphoreTimeout
768 */
769extern SDL_DECLSPEC bool SDLCALL SDL_TryWaitSemaphore(SDL_Semaphore *sem);
770
771/**
772 * Wait until a semaphore has a positive value and then decrements it.
773 *
774 * This function suspends the calling thread until either the semaphore
775 * pointed to by `sem` has a positive value or the specified time has elapsed.
776 * If the call is successful it will atomically decrement the semaphore value.
777 *
778 * \param sem the semaphore to wait on.
779 * \param timeoutMS the length of the timeout, in milliseconds, or -1 to wait
780 * indefinitely.
781 * \returns true if the wait succeeds or false if the wait times out.
782 *
783 * \threadsafety It is safe to call this function from any thread.
784 *
785 * \since This function is available since SDL 3.2.0.
786 *
787 * \sa SDL_SignalSemaphore
788 * \sa SDL_TryWaitSemaphore
789 * \sa SDL_WaitSemaphore
790 */
791extern SDL_DECLSPEC bool SDLCALL SDL_WaitSemaphoreTimeout(SDL_Semaphore *sem, Sint32 timeoutMS);
792
793/**
794 * Atomically increment a semaphore's value and wake waiting threads.
795 *
796 * \param sem the semaphore to increment.
797 *
798 * \threadsafety It is safe to call this function from any thread.
799 *
800 * \since This function is available since SDL 3.2.0.
801 *
802 * \sa SDL_TryWaitSemaphore
803 * \sa SDL_WaitSemaphore
804 * \sa SDL_WaitSemaphoreTimeout
805 */
806extern SDL_DECLSPEC void SDLCALL SDL_SignalSemaphore(SDL_Semaphore *sem);
807
808/**
809 * Get the current value of a semaphore.
810 *
811 * \param sem the semaphore to query.
812 * \returns the current value of the semaphore.
813 *
814 * \threadsafety It is safe to call this function from any thread.
815 *
816 * \since This function is available since SDL 3.2.0.
817 */
818extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetSemaphoreValue(SDL_Semaphore *sem);
819
820/* @} *//* Semaphore functions */
821
822
823/**
824 * \name Condition variable functions
825 */
826/* @{ */
827
828/**
829 * A means to block multiple threads until a condition is satisfied.
830 *
831 * Condition variables, paired with an SDL_Mutex, let an app halt multiple
832 * threads until a condition has occurred, at which time the app can release
833 * one or all waiting threads.
834 *
835 * Wikipedia has a thorough explanation of the concept:
836 *
837 * https://en.wikipedia.org/wiki/Condition_variable
838 *
839 * \since This struct is available since SDL 3.2.0.
840 */
842
843/**
844 * Create a condition variable.
845 *
846 * \returns a new condition variable or NULL on failure; call SDL_GetError()
847 * for more information.
848 *
849 * \threadsafety It is safe to call this function from any thread.
850 *
851 * \since This function is available since SDL 3.2.0.
852 *
853 * \sa SDL_BroadcastCondition
854 * \sa SDL_SignalCondition
855 * \sa SDL_WaitCondition
856 * \sa SDL_WaitConditionTimeout
857 * \sa SDL_DestroyCondition
858 */
859extern SDL_DECLSPEC SDL_Condition * SDLCALL SDL_CreateCondition(void);
860
861/**
862 * Destroy a condition variable.
863 *
864 * \param cond the condition variable to destroy.
865 *
866 * \threadsafety It is safe to call this function from any thread.
867 *
868 * \since This function is available since SDL 3.2.0.
869 *
870 * \sa SDL_CreateCondition
871 */
872extern SDL_DECLSPEC void SDLCALL SDL_DestroyCondition(SDL_Condition *cond);
873
874/**
875 * Restart one of the threads that are waiting on the condition variable.
876 *
877 * \param cond the condition variable to signal.
878 *
879 * \threadsafety It is safe to call this function from any thread.
880 *
881 * \since This function is available since SDL 3.2.0.
882 *
883 * \sa SDL_BroadcastCondition
884 * \sa SDL_WaitCondition
885 * \sa SDL_WaitConditionTimeout
886 */
887extern SDL_DECLSPEC void SDLCALL SDL_SignalCondition(SDL_Condition *cond);
888
889/**
890 * Restart all threads that are waiting on the condition variable.
891 *
892 * \param cond the condition variable to signal.
893 *
894 * \threadsafety It is safe to call this function from any thread.
895 *
896 * \since This function is available since SDL 3.2.0.
897 *
898 * \sa SDL_SignalCondition
899 * \sa SDL_WaitCondition
900 * \sa SDL_WaitConditionTimeout
901 */
902extern SDL_DECLSPEC void SDLCALL SDL_BroadcastCondition(SDL_Condition *cond);
903
904/**
905 * Wait until a condition variable is signaled.
906 *
907 * This function unlocks the specified `mutex` and waits for another thread to
908 * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
909 * variable `cond`. Once the condition variable is signaled, the mutex is
910 * re-locked and the function returns.
911 *
912 * The mutex must be locked before calling this function. Locking the mutex
913 * recursively (more than once) is not supported and leads to undefined
914 * behavior.
915 *
916 * This function is the equivalent of calling SDL_WaitConditionTimeout() with
917 * a time length of -1.
918 *
919 * \param cond the condition variable to wait on.
920 * \param mutex the mutex used to coordinate thread access.
921 *
922 * \threadsafety It is safe to call this function from any thread.
923 *
924 * \since This function is available since SDL 3.2.0.
925 *
926 * \sa SDL_BroadcastCondition
927 * \sa SDL_SignalCondition
928 * \sa SDL_WaitConditionTimeout
929 */
930extern SDL_DECLSPEC void SDLCALL SDL_WaitCondition(SDL_Condition *cond, SDL_Mutex *mutex);
931
932/**
933 * Wait until a condition variable is signaled or a certain time has passed.
934 *
935 * This function unlocks the specified `mutex` and waits for another thread to
936 * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
937 * variable `cond`, or for the specified time to elapse. Once the condition
938 * variable is signaled or the time elapsed, the mutex is re-locked and the
939 * function returns.
940 *
941 * The mutex must be locked before calling this function. Locking the mutex
942 * recursively (more than once) is not supported and leads to undefined
943 * behavior.
944 *
945 * \param cond the condition variable to wait on.
946 * \param mutex the mutex used to coordinate thread access.
947 * \param timeoutMS the maximum time to wait, in milliseconds, or -1 to wait
948 * indefinitely.
949 * \returns true if the condition variable is signaled, false if the condition
950 * is not signaled in the allotted time.
951 *
952 * \threadsafety It is safe to call this function from any thread.
953 *
954 * \since This function is available since SDL 3.2.0.
955 *
956 * \sa SDL_BroadcastCondition
957 * \sa SDL_SignalCondition
958 * \sa SDL_WaitCondition
959 */
960extern SDL_DECLSPEC bool SDLCALL SDL_WaitConditionTimeout(SDL_Condition *cond,
961 SDL_Mutex *mutex, Sint32 timeoutMS);
962
963/* @} *//* Condition variable functions */
964
965/**
966 * \name Thread-safe initialization state functions
967 */
968/* @{ */
969
970/**
971 * The current status of an SDL_InitState structure.
972 *
973 * \since This enum is available since SDL 3.2.0.
974 */
982
983/**
984 * A structure used for thread-safe initialization and shutdown.
985 *
986 * Here is an example of using this:
987 *
988 * ```c
989 * static SDL_InitState init;
990 *
991 * bool InitSystem(void)
992 * {
993 * if (!SDL_ShouldInit(&init)) {
994 * // The system is initialized
995 * return true;
996 * }
997 *
998 * // At this point, you should not leave this function without calling SDL_SetInitialized()
999 *
1000 * bool initialized = DoInitTasks();
1001 * SDL_SetInitialized(&init, initialized);
1002 * return initialized;
1003 * }
1004 *
1005 * bool UseSubsystem(void)
1006 * {
1007 * if (SDL_ShouldInit(&init)) {
1008 * // Error, the subsystem isn't initialized
1009 * SDL_SetInitialized(&init, false);
1010 * return false;
1011 * }
1012 *
1013 * // Do work using the initialized subsystem
1014 *
1015 * return true;
1016 * }
1017 *
1018 * void QuitSystem(void)
1019 * {
1020 * if (!SDL_ShouldQuit(&init)) {
1021 * // The system is not initialized
1022 * return;
1023 * }
1024 *
1025 * // At this point, you should not leave this function without calling SDL_SetInitialized()
1026 *
1027 * DoQuitTasks();
1028 * SDL_SetInitialized(&init, false);
1029 * }
1030 * ```
1031 *
1032 * Note that this doesn't protect any resources created during initialization,
1033 * or guarantee that nobody is using those resources during cleanup. You
1034 * should use other mechanisms to protect those, if that's a concern for your
1035 * code.
1036 *
1037 * \since This struct is available since SDL 3.2.0.
1038 */
1045
1046/**
1047 * Return whether initialization should be done.
1048 *
1049 * This function checks the passed in state and if initialization should be
1050 * done, sets the status to `SDL_INIT_STATUS_INITIALIZING` and returns true.
1051 * If another thread is already modifying this state, it will wait until
1052 * that's done before returning.
1053 *
1054 * If this function returns true, the calling code must call
1055 * SDL_SetInitialized() to complete the initialization.
1056 *
1057 * \param state the initialization state to check.
1058 * \returns true if initialization needs to be done, false otherwise.
1059 *
1060 * \threadsafety It is safe to call this function from any thread.
1061 *
1062 * \since This function is available since SDL 3.2.0.
1063 *
1064 * \sa SDL_SetInitialized
1065 * \sa SDL_ShouldQuit
1066 */
1067extern SDL_DECLSPEC bool SDLCALL SDL_ShouldInit(SDL_InitState *state);
1068
1069/**
1070 * Return whether cleanup should be done.
1071 *
1072 * This function checks the passed in state and if cleanup should be done,
1073 * sets the status to `SDL_INIT_STATUS_UNINITIALIZING` and returns true.
1074 *
1075 * If this function returns true, the calling code must call
1076 * SDL_SetInitialized() to complete the cleanup.
1077 *
1078 * \param state the initialization state to check.
1079 * \returns true if cleanup needs to be done, false otherwise.
1080 *
1081 * \threadsafety It is safe to call this function from any thread.
1082 *
1083 * \since This function is available since SDL 3.2.0.
1084 *
1085 * \sa SDL_SetInitialized
1086 * \sa SDL_ShouldInit
1087 */
1088extern SDL_DECLSPEC bool SDLCALL SDL_ShouldQuit(SDL_InitState *state);
1089
1090/**
1091 * Finish an initialization state transition.
1092 *
1093 * This function sets the status of the passed in state to
1094 * `SDL_INIT_STATUS_INITIALIZED` or `SDL_INIT_STATUS_UNINITIALIZED` and allows
1095 * any threads waiting for the status to proceed.
1096 *
1097 * \param state the initialization state to check.
1098 * \param initialized the new initialization state.
1099 *
1100 * \threadsafety It is safe to call this function from any thread.
1101 *
1102 * \since This function is available since SDL 3.2.0.
1103 *
1104 * \sa SDL_ShouldInit
1105 * \sa SDL_ShouldQuit
1106 */
1107extern SDL_DECLSPEC void SDLCALL SDL_SetInitialized(SDL_InitState *state, bool initialized);
1108
1109/* @} *//* Thread-safe initialization state functions */
1110
1111/* Ends C function definitions when using C++ */
1112#ifdef __cplusplus
1113}
1114#endif
1115#include <SDL3/SDL_close_code.h>
1116
1117#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:581
#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:684
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:438
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:976
@ SDL_INIT_STATUS_INITIALIZED
Definition SDL_mutex.h:979
@ SDL_INIT_STATUS_UNINITIALIZED
Definition SDL_mutex.h:977
@ SDL_INIT_STATUS_UNINITIALIZING
Definition SDL_mutex.h:980
@ SDL_INIT_STATUS_INITIALIZING
Definition SDL_mutex.h:978
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:841
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:1043
SDL_AtomicInt status
Definition SDL_mutex.h:1041
SDL_ThreadID thread
Definition SDL_mutex.h:1042