SDL 3.0
SDL_mutex.h File Reference
+ Include dependency graph for SDL_mutex.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDL_InitState
 

Macros

#define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x)   /* no-op */
 
#define SDL_CAPABILITY(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
 
#define SDL_SCOPED_CAPABILITY    SDL_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
 
#define SDL_GUARDED_BY(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
 
#define SDL_PT_GUARDED_BY(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
 
#define SDL_ACQUIRED_BEFORE(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))
 
#define SDL_ACQUIRED_AFTER(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))
 
#define SDL_REQUIRES(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(x))
 
#define SDL_REQUIRES_SHARED(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(x))
 
#define SDL_ACQUIRE(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(x))
 
#define SDL_ACQUIRE_SHARED(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(x))
 
#define SDL_RELEASE(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(x))
 
#define SDL_RELEASE_SHARED(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(x))
 
#define SDL_RELEASE_GENERIC(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(x))
 
#define SDL_TRY_ACQUIRE(x, y)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(x, y))
 
#define SDL_TRY_ACQUIRE_SHARED(x, y)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(x, y))
 
#define SDL_EXCLUDES(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(x))
 
#define SDL_ASSERT_CAPABILITY(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
 
#define SDL_ASSERT_SHARED_CAPABILITY(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
 
#define SDL_RETURN_CAPABILITY(x)    SDL_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
 
#define SDL_NO_THREAD_SAFETY_ANALYSIS    SDL_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
 

Thread-safe initialization state functions

enum  SDL_InitStatus {
  SDL_INIT_STATUS_UNINITIALIZED ,
  SDL_INIT_STATUS_INITIALIZING ,
  SDL_INIT_STATUS_INITIALIZED ,
  SDL_INIT_STATUS_UNINITIALIZING
}
 
bool SDL_ShouldInit (SDL_InitState *state)
 
bool SDL_ShouldQuit (SDL_InitState *state)
 
void SDL_SetInitialized (SDL_InitState *state, bool initialized)
 

Mutex functions

typedef struct SDL_Mutex SDL_Mutex
 
bool mutex
 
SDL_MutexSDL_CreateMutex (void)
 
void SDL_LockMutex (SDL_Mutex *mutex) SDL_ACQUIRE(mutex)
 
bool SDL_TryLockMutex (SDL_Mutex *mutex) SDL_TRY_ACQUIRE(true
 
void SDL_UnlockMutex (SDL_Mutex *mutex) SDL_RELEASE(mutex)
 
void SDL_DestroyMutex (SDL_Mutex *mutex)
 

Read/write lock functions

typedef struct SDL_RWLock SDL_RWLock
 
bool rwlock
 
SDL_RWLockSDL_CreateRWLock (void)
 
void SDL_LockRWLockForReading (SDL_RWLock *rwlock) SDL_ACQUIRE_SHARED(rwlock)
 
void SDL_LockRWLockForWriting (SDL_RWLock *rwlock) SDL_ACQUIRE(rwlock)
 
bool SDL_TryLockRWLockForReading (SDL_RWLock *rwlock) SDL_TRY_ACQUIRE_SHARED(true
 
bool SDL_TryLockRWLockForWriting (SDL_RWLock *rwlock) SDL_TRY_ACQUIRE(true
 
void SDL_UnlockRWLock (SDL_RWLock *rwlock) SDL_RELEASE_GENERIC(rwlock)
 
void SDL_DestroyRWLock (SDL_RWLock *rwlock)
 

Semaphore functions

typedef struct SDL_Semaphore SDL_Semaphore
 
SDL_SemaphoreSDL_CreateSemaphore (Uint32 initial_value)
 
void SDL_DestroySemaphore (SDL_Semaphore *sem)
 
void SDL_WaitSemaphore (SDL_Semaphore *sem)
 
bool SDL_TryWaitSemaphore (SDL_Semaphore *sem)
 
bool SDL_WaitSemaphoreTimeout (SDL_Semaphore *sem, Sint32 timeoutMS)
 
void SDL_SignalSemaphore (SDL_Semaphore *sem)
 
Uint32 SDL_GetSemaphoreValue (SDL_Semaphore *sem)
 

Condition variable functions

typedef struct SDL_Condition SDL_Condition
 
SDL_ConditionSDL_CreateCondition (void)
 
void SDL_DestroyCondition (SDL_Condition *cond)
 
void SDL_SignalCondition (SDL_Condition *cond)
 
void SDL_BroadcastCondition (SDL_Condition *cond)
 
void SDL_WaitCondition (SDL_Condition *cond, SDL_Mutex *mutex)
 
bool SDL_WaitConditionTimeout (SDL_Condition *cond, SDL_Mutex *mutex, Sint32 timeoutMS)
 

Macro Definition Documentation

◆ SDL_ACQUIRE

#define SDL_ACQUIRE (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 157 of file SDL_mutex.h.

275 {
276#endif
277
278/**
279 * \name Mutex functions
280 */
281/* @{ */
282
283/**
284 * A means to serialize access to a resource between threads.
285 *
286 * Mutexes (short for "mutual exclusion") are a synchronization primitive that
287 * allows exactly one thread to proceed at a time.
288 *
289 * Wikipedia has a thorough explanation of the concept:
290 *
291 * https://en.wikipedia.org/wiki/Mutex
292 *
293 * \since This struct is available since SDL 3.2.0.
294 */
295typedef struct SDL_Mutex SDL_Mutex;
296
297/**
298 * Create a new mutex.
299 *
300 * All newly-created mutexes begin in the _unlocked_ state.
301 *
302 * Calls to SDL_LockMutex() will not return while the mutex is locked by
303 * another thread. See SDL_TryLockMutex() to attempt to lock without blocking.
304 *
305 * SDL mutexes are reentrant.
306 *
307 * \returns the initialized and unlocked mutex or NULL on failure; call
308 * SDL_GetError() for more information.
309 *
310 * \threadsafety It is safe to call this function from any thread.
311 *
312 * \since This function is available since SDL 3.2.0.
313 *
314 * \sa SDL_DestroyMutex
315 * \sa SDL_LockMutex
316 * \sa SDL_TryLockMutex
317 * \sa SDL_UnlockMutex
318 */
319extern SDL_DECLSPEC SDL_Mutex * SDLCALL SDL_CreateMutex(void);
320
321/**
322 * Lock the mutex.
323 *
324 * This will block until the mutex is available, which is to say it is in the
325 * unlocked state and the OS has chosen the caller as the next thread to lock
326 * it. Of all threads waiting to lock the mutex, only one may do so at a time.
327 *
328 * It is legal for the owning thread to lock an already-locked mutex. It must
329 * unlock it the same number of times before it is actually made available for
330 * other threads in the system (this is known as a "recursive mutex").
331 *
332 * This function does not fail; if mutex is NULL, it will return immediately
333 * having locked nothing. If the mutex is valid, this function will always
334 * block until it can lock the mutex, and return with it locked.
335 *
336 * \param mutex the mutex to lock.
337 *
338 * \threadsafety It is safe to call this function from any thread.
339 *
340 * \since This function is available since SDL 3.2.0.
341 *
342 * \sa SDL_TryLockMutex
343 * \sa SDL_UnlockMutex
344 */
345extern SDL_DECLSPEC void SDLCALL SDL_LockMutex(SDL_Mutex *mutex) SDL_ACQUIRE(mutex);
346
347/**
348 * Try to lock a mutex without blocking.
349 *
350 * This works just like SDL_LockMutex(), but if the mutex is not available,
351 * this function returns false immediately.
352 *
353 * This technique is useful if you need exclusive access to a resource but
354 * don't want to wait for it, and will return to it to try again later.
355 *
356 * This function returns true if passed a NULL mutex.
357 *
358 * \param mutex the mutex to try to lock.
359 * \returns true on success, false if the mutex would block.
360 *
361 * \threadsafety It is safe to call this function from any thread.
362 *
363 * \since This function is available since SDL 3.2.0.
364 *
365 * \sa SDL_LockMutex
366 * \sa SDL_UnlockMutex
367 */
368extern SDL_DECLSPEC bool SDLCALL SDL_TryLockMutex(SDL_Mutex *mutex) SDL_TRY_ACQUIRE(true, mutex);
369
370/**
371 * Unlock the mutex.
372 *
373 * It is legal for the owning thread to lock an already-locked mutex. It must
374 * unlock it the same number of times before it is actually made available for
375 * other threads in the system (this is known as a "recursive mutex").
376 *
377 * It is illegal to unlock a mutex that has not been locked by the current
378 * thread, and doing so results in undefined behavior.
379 *
380 * \param mutex the mutex to unlock.
381 *
382 * \threadsafety This call must be paired with a previous locking call on the
383 * 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 */
520extern SDL_DECLSPEC void SDLCALL SDL_LockRWLockForReading(SDL_RWLock *rwlock) SDL_ACQUIRE_SHARED(rwlock);
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 */
580extern SDL_DECLSPEC bool SDLCALL SDL_TryLockRWLockForReading(SDL_RWLock *rwlock) SDL_TRY_ACQUIRE_SHARED(true, rwlock);
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
631 * same thread.
632 *
633 * \since This function is available since SDL 3.2.0.
634 *
635 * \sa SDL_LockRWLockForReading
636 * \sa SDL_LockRWLockForWriting
637 * \sa SDL_TryLockRWLockForReading
638 * \sa SDL_TryLockRWLockForWriting
639 */
640extern SDL_DECLSPEC void SDLCALL SDL_UnlockRWLock(SDL_RWLock *rwlock) SDL_RELEASE_GENERIC(rwlock);
641
642/**
643 * Destroy a read/write lock created with SDL_CreateRWLock().
644 *
645 * This function must be called on any read/write lock that is no longer
646 * needed. Failure to destroy a rwlock will result in a system memory or
647 * resource leak. While it is safe to destroy a rwlock that is _unlocked_, it
648 * is not safe to attempt to destroy a locked rwlock, and may result in
649 * undefined behavior depending on the platform.
650 *
651 * \param rwlock the rwlock to destroy.
652 *
653 * \threadsafety It is safe to call this function from any thread.
654 *
655 * \since This function is available since SDL 3.2.0.
656 *
657 * \sa SDL_CreateRWLock
658 */
659extern SDL_DECLSPEC void SDLCALL SDL_DestroyRWLock(SDL_RWLock *rwlock);
660
661/* @} *//* Read/write lock functions */
662
663
664/**
665 * \name Semaphore functions
666 */
667/* @{ */
668
669/**
670 * A means to manage access to a resource, by count, between threads.
671 *
672 * Semaphores (specifically, "counting semaphores"), let X number of threads
673 * request access at the same time, each thread granted access decrementing a
674 * counter. When the counter reaches zero, future requests block until a prior
675 * thread releases their request, incrementing the counter again.
676 *
677 * Wikipedia has a thorough explanation of the concept:
678 *
679 * https://en.wikipedia.org/wiki/Semaphore_(programming)
680 *
681 * \since This struct is available since SDL 3.2.0.
682 */
683typedef struct SDL_Semaphore SDL_Semaphore;
684
685/**
686 * Create a semaphore.
687 *
688 * This function creates a new semaphore and initializes it with the value
689 * `initial_value`. Each wait operation on the semaphore will atomically
690 * decrement the semaphore value and potentially block if the semaphore value
691 * is 0. Each post operation will atomically increment the semaphore value and
692 * wake waiting threads and allow them to retry the wait operation.
693 *
694 * \param initial_value the starting value of the semaphore.
695 * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
696 * information.
697 *
698 * \threadsafety It is safe to call this function from any thread.
699 *
700 * \since This function is available since SDL 3.2.0.
701 *
702 * \sa SDL_DestroySemaphore
703 * \sa SDL_SignalSemaphore
704 * \sa SDL_TryWaitSemaphore
705 * \sa SDL_GetSemaphoreValue
706 * \sa SDL_WaitSemaphore
707 * \sa SDL_WaitSemaphoreTimeout
708 */
709extern SDL_DECLSPEC SDL_Semaphore * SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
710
711/**
712 * Destroy a semaphore.
713 *
714 * It is not safe to destroy a semaphore if there are threads currently
715 * waiting on it.
716 *
717 * \param sem the semaphore to destroy.
718 *
719 * \threadsafety It is safe to call this function from any thread.
720 *
721 * \since This function is available since SDL 3.2.0.
722 *
723 * \sa SDL_CreateSemaphore
724 */
725extern SDL_DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_Semaphore *sem);
726
727/**
728 * Wait until a semaphore has a positive value and then decrements it.
729 *
730 * This function suspends the calling thread until the semaphore pointed to by
731 * `sem` has a positive value, and then atomically decrement the semaphore
732 * value.
733 *
734 * This function is the equivalent of calling SDL_WaitSemaphoreTimeout() with
735 * a time length of -1.
736 *
737 * \param sem the semaphore wait on.
738 *
739 * \threadsafety It is safe to call this function from any thread.
740 *
741 * \since This function is available since SDL 3.2.0.
742 *
743 * \sa SDL_SignalSemaphore
744 * \sa SDL_TryWaitSemaphore
745 * \sa SDL_WaitSemaphoreTimeout
746 */
747extern SDL_DECLSPEC void SDLCALL SDL_WaitSemaphore(SDL_Semaphore *sem);
748
749/**
750 * See if a semaphore has a positive value and decrement it if it does.
751 *
752 * This function checks to see if the semaphore pointed to by `sem` has a
753 * positive value and atomically decrements the semaphore value if it does. If
754 * the semaphore doesn't have a positive value, the function immediately
755 * returns false.
756 *
757 * \param sem the semaphore to wait on.
758 * \returns true if the wait succeeds, false if the wait would block.
759 *
760 * \threadsafety It is safe to call this function from any thread.
761 *
762 * \since This function is available since SDL 3.2.0.
763 *
764 * \sa SDL_SignalSemaphore
765 * \sa SDL_WaitSemaphore
766 * \sa SDL_WaitSemaphoreTimeout
767 */
768extern SDL_DECLSPEC bool SDLCALL SDL_TryWaitSemaphore(SDL_Semaphore *sem);
769
770/**
771 * Wait until a semaphore has a positive value and then decrements it.
772 *
773 * This function suspends the calling thread until either the semaphore
774 * pointed to by `sem` has a positive value or the specified time has elapsed.
775 * If the call is successful it will atomically decrement the semaphore value.
776 *
777 * \param sem the semaphore to wait on.
778 * \param timeoutMS the length of the timeout, in milliseconds, or -1 to wait
779 * indefinitely.
780 * \returns true if the wait succeeds or false if the wait times out.
781 *
782 * \threadsafety It is safe to call this function from any thread.
783 *
784 * \since This function is available since SDL 3.2.0.
785 *
786 * \sa SDL_SignalSemaphore
787 * \sa SDL_TryWaitSemaphore
788 * \sa SDL_WaitSemaphore
789 */
790extern SDL_DECLSPEC bool SDLCALL SDL_WaitSemaphoreTimeout(SDL_Semaphore *sem, Sint32 timeoutMS);
791
792/**
793 * Atomically increment a semaphore's value and wake waiting threads.
794 *
795 * \param sem the semaphore to increment.
796 *
797 * \threadsafety It is safe to call this function from any thread.
798 *
799 * \since This function is available since SDL 3.2.0.
800 *
801 * \sa SDL_TryWaitSemaphore
802 * \sa SDL_WaitSemaphore
803 * \sa SDL_WaitSemaphoreTimeout
804 */
805extern SDL_DECLSPEC void SDLCALL SDL_SignalSemaphore(SDL_Semaphore *sem);
806
807/**
808 * Get the current value of a semaphore.
809 *
810 * \param sem the semaphore to query.
811 * \returns the current value of the semaphore.
812 *
813 * \threadsafety It is safe to call this function from any thread.
814 *
815 * \since This function is available since SDL 3.2.0.
816 */
817extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetSemaphoreValue(SDL_Semaphore *sem);
818
819/* @} *//* Semaphore functions */
820
821
822/**
823 * \name Condition variable functions
824 */
825/* @{ */
826
827/**
828 * A means to block multiple threads until a condition is satisfied.
829 *
830 * Condition variables, paired with an SDL_Mutex, let an app halt multiple
831 * threads until a condition has occurred, at which time the app can release
832 * one or all waiting threads.
833 *
834 * Wikipedia has a thorough explanation of the concept:
835 *
836 * https://en.wikipedia.org/wiki/Condition_variable
837 *
838 * \since This struct is available since SDL 3.2.0.
839 */
840typedef struct SDL_Condition SDL_Condition;
841
842/**
843 * Create a condition variable.
844 *
845 * \returns a new condition variable or NULL on failure; call SDL_GetError()
846 * for more information.
847 *
848 * \threadsafety It is safe to call this function from any thread.
849 *
850 * \since This function is available since SDL 3.2.0.
851 *
852 * \sa SDL_BroadcastCondition
853 * \sa SDL_SignalCondition
854 * \sa SDL_WaitCondition
855 * \sa SDL_WaitConditionTimeout
856 * \sa SDL_DestroyCondition
857 */
858extern SDL_DECLSPEC SDL_Condition * SDLCALL SDL_CreateCondition(void);
859
860/**
861 * Destroy a condition variable.
862 *
863 * \param cond the condition variable to destroy.
864 *
865 * \threadsafety It is safe to call this function from any thread.
866 *
867 * \since This function is available since SDL 3.2.0.
868 *
869 * \sa SDL_CreateCondition
870 */
871extern SDL_DECLSPEC void SDLCALL SDL_DestroyCondition(SDL_Condition *cond);
872
873/**
874 * Restart one of the threads that are waiting on the condition variable.
875 *
876 * \param cond the condition variable to signal.
877 *
878 * \threadsafety It is safe to call this function from any thread.
879 *
880 * \since This function is available since SDL 3.2.0.
881 *
882 * \sa SDL_BroadcastCondition
883 * \sa SDL_WaitCondition
884 * \sa SDL_WaitConditionTimeout
885 */
886extern SDL_DECLSPEC void SDLCALL SDL_SignalCondition(SDL_Condition *cond);
887
888/**
889 * Restart all threads that are waiting on the condition variable.
890 *
891 * \param cond the condition variable to signal.
892 *
893 * \threadsafety It is safe to call this function from any thread.
894 *
895 * \since This function is available since SDL 3.2.0.
896 *
897 * \sa SDL_SignalCondition
898 * \sa SDL_WaitCondition
899 * \sa SDL_WaitConditionTimeout
900 */
901extern SDL_DECLSPEC void SDLCALL SDL_BroadcastCondition(SDL_Condition *cond);
902
903/**
904 * Wait until a condition variable is signaled.
905 *
906 * This function unlocks the specified `mutex` and waits for another thread to
907 * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
908 * variable `cond`. Once the condition variable is signaled, the mutex is
909 * re-locked and the function returns.
910 *
911 * The mutex must be locked before calling this function. Locking the mutex
912 * recursively (more than once) is not supported and leads to undefined
913 * behavior.
914 *
915 * This function is the equivalent of calling SDL_WaitConditionTimeout() with
916 * a time length of -1.
917 *
918 * \param cond the condition variable to wait on.
919 * \param mutex the mutex used to coordinate thread access.
920 *
921 * \threadsafety It is safe to call this function from any thread.
922 *
923 * \since This function is available since SDL 3.2.0.
924 *
925 * \sa SDL_BroadcastCondition
926 * \sa SDL_SignalCondition
927 * \sa SDL_WaitConditionTimeout
928 */
929extern SDL_DECLSPEC void SDLCALL SDL_WaitCondition(SDL_Condition *cond, SDL_Mutex *mutex);
930
931/**
932 * Wait until a condition variable is signaled or a certain time has passed.
933 *
934 * This function unlocks the specified `mutex` and waits for another thread to
935 * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
936 * variable `cond`, or for the specified time to elapse. Once the condition
937 * variable is signaled or the time elapsed, the mutex is re-locked and the
938 * function returns.
939 *
940 * The mutex must be locked before calling this function. Locking the mutex
941 * recursively (more than once) is not supported and leads to undefined
942 * behavior.
943 *
944 * \param cond the condition variable to wait on.
945 * \param mutex the mutex used to coordinate thread access.
946 * \param timeoutMS the maximum time to wait, in milliseconds, or -1 to wait
947 * indefinitely.
948 * \returns true if the condition variable is signaled, false if the condition
949 * is not signaled in the allotted time.
950 *
951 * \threadsafety It is safe to call this function from any thread.
952 *
953 * \since This function is available since SDL 3.2.0.
954 *
955 * \sa SDL_BroadcastCondition
956 * \sa SDL_SignalCondition
957 * \sa SDL_WaitCondition
958 */
959extern SDL_DECLSPEC bool SDLCALL SDL_WaitConditionTimeout(SDL_Condition *cond,
960 SDL_Mutex *mutex, Sint32 timeoutMS);
961
962/* @} *//* Condition variable functions */
963
964/**
965 * \name Thread-safe initialization state functions
966 */
967/* @{ */
968
969/**
970 * The current status of an SDL_InitState structure.
971 *
972 * \since This enum is available since SDL 3.2.0.
973 */
974typedef enum SDL_InitStatus
975{
981
982/**
983 * A structure used for thread-safe initialization and shutdown.
984 *
985 * Here is an example of using this:
986 *
987 * ```c
988 * static SDL_InitState init;
989 *
990 * bool InitSystem(void)
991 * {
992 * if (!SDL_ShouldInit(&init)) {
993 * // The system is initialized
994 * return true;
995 * }
996 *
997 * // At this point, you should not leave this function without calling SDL_SetInitialized()
998 *
999 * bool initialized = DoInitTasks();
1000 * SDL_SetInitialized(&init, initialized);
1001 * return initialized;
1002 * }
1003 *
1004 * bool UseSubsystem(void)
1005 * {
1006 * if (SDL_ShouldInit(&init)) {
1007 * // Error, the subsystem isn't initialized
1008 * SDL_SetInitialized(&init, false);
1009 * return false;
1010 * }
1011 *
1012 * // Do work using the initialized subsystem
1013 *
1014 * return true;
1015 * }
1016 *
1017 * void QuitSystem(void)
1018 * {
1019 * if (!SDL_ShouldQuit(&init)) {
1020 * // The system is not initialized
1021 * return;
1022 * }
1023 *
1024 * // At this point, you should not leave this function without calling SDL_SetInitialized()
1025 *
1026 * DoQuitTasks();
1027 * SDL_SetInitialized(&init, false);
1028 * }
1029 * ```
1030 *
1031 * Note that this doesn't protect any resources created during initialization,
1032 * or guarantee that nobody is using those resources during cleanup. You
1033 * should use other mechanisms to protect those, if that's a concern for your
1034 * code.
1035 *
1036 * \since This struct is available since SDL 3.2.0.
1037 */
1038typedef struct SDL_InitState
1039{
1042 void *reserved;
1044
1045/**
1046 * Return whether initialization should be done.
1047 *
1048 * This function checks the passed in state and if initialization should be
1049 * done, sets the status to `SDL_INIT_STATUS_INITIALIZING` and returns true.
1050 * If another thread is already modifying this state, it will wait until
1051 * that's done before returning.
1052 *
1053 * If this function returns true, the calling code must call
1054 * SDL_SetInitialized() to complete the initialization.
1055 *
1056 * \param state the initialization state to check.
1057 * \returns true if initialization needs to be done, false otherwise.
1058 *
1059 * \threadsafety It is safe to call this function from any thread.
1060 *
1061 * \since This function is available since SDL 3.2.0.
1062 *
1063 * \sa SDL_SetInitialized
1064 * \sa SDL_ShouldQuit
1065 */
1066extern SDL_DECLSPEC bool SDLCALL SDL_ShouldInit(SDL_InitState *state);
1067
1068/**
1069 * Return whether cleanup should be done.
1070 *
1071 * This function checks the passed in state and if cleanup should be done,
1072 * sets the status to `SDL_INIT_STATUS_UNINITIALIZING` and returns true.
1073 *
1074 * If this function returns true, the calling code must call
1075 * SDL_SetInitialized() to complete the cleanup.
1076 *
1077 * \param state the initialization state to check.
1078 * \returns true if cleanup needs to be done, false otherwise.
1079 *
1080 * \threadsafety It is safe to call this function from any thread.
1081 *
1082 * \since This function is available since SDL 3.2.0.
1083 *
1084 * \sa SDL_SetInitialized
1085 * \sa SDL_ShouldInit
1086 */
1087extern SDL_DECLSPEC bool SDLCALL SDL_ShouldQuit(SDL_InitState *state);
1088
1089/**
1090 * Finish an initialization state transition.
1091 *
1092 * This function sets the status of the passed in state to
1093 * `SDL_INIT_STATUS_INITIALIZED` or `SDL_INIT_STATUS_UNINITIALIZED` and allows
1094 * any threads waiting for the status to proceed.
1095 *
1096 * \param state the initialization state to check.
1097 * \param initialized the new initialization state.
1098 *
1099 * \threadsafety It is safe to call this function from any thread.
1100 *
1101 * \since This function is available since SDL 3.2.0.
1102 *
1103 * \sa SDL_ShouldInit
1104 * \sa SDL_ShouldQuit
1105 */
1106extern SDL_DECLSPEC void SDLCALL SDL_SetInitialized(SDL_InitState *state, bool initialized);
1107
1108/* @} *//* Thread-safe initialization state functions */
1109
1110/* Ends C function definitions when using C++ */
1111#ifdef __cplusplus
1112}
1113#endif
1114#include <SDL3/SDL_close_code.h>
1115
1116#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

◆ SDL_ACQUIRE_SHARED

#define SDL_ACQUIRE_SHARED (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 167 of file SDL_mutex.h.

◆ SDL_ACQUIRED_AFTER

#define SDL_ACQUIRED_AFTER (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 127 of file SDL_mutex.h.

◆ SDL_ACQUIRED_BEFORE

#define SDL_ACQUIRED_BEFORE (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 117 of file SDL_mutex.h.

◆ SDL_ASSERT_CAPABILITY

#define SDL_ASSERT_CAPABILITY (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 237 of file SDL_mutex.h.

◆ SDL_ASSERT_SHARED_CAPABILITY

#define SDL_ASSERT_SHARED_CAPABILITY (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 247 of file SDL_mutex.h.

◆ SDL_CAPABILITY

#define SDL_CAPABILITY (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(capability(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 77 of file SDL_mutex.h.

◆ SDL_EXCLUDES

#define SDL_EXCLUDES (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 227 of file SDL_mutex.h.

◆ SDL_GUARDED_BY

#define SDL_GUARDED_BY (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 97 of file SDL_mutex.h.

◆ SDL_NO_THREAD_SAFETY_ANALYSIS

#define SDL_NO_THREAD_SAFETY_ANALYSIS    SDL_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 267 of file SDL_mutex.h.

◆ SDL_PT_GUARDED_BY

#define SDL_PT_GUARDED_BY (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 107 of file SDL_mutex.h.

◆ SDL_RELEASE

#define SDL_RELEASE (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 177 of file SDL_mutex.h.

◆ SDL_RELEASE_GENERIC

#define SDL_RELEASE_GENERIC (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 197 of file SDL_mutex.h.

◆ SDL_RELEASE_SHARED

#define SDL_RELEASE_SHARED (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 187 of file SDL_mutex.h.

◆ SDL_REQUIRES

#define SDL_REQUIRES (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 137 of file SDL_mutex.h.

◆ SDL_REQUIRES_SHARED

#define SDL_REQUIRES_SHARED (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 147 of file SDL_mutex.h.

◆ SDL_RETURN_CAPABILITY

#define SDL_RETURN_CAPABILITY (   x)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 257 of file SDL_mutex.h.

◆ SDL_SCOPED_CAPABILITY

#define SDL_SCOPED_CAPABILITY    SDL_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 87 of file SDL_mutex.h.

◆ SDL_THREAD_ANNOTATION_ATTRIBUTE__

#define SDL_THREAD_ANNOTATION_ATTRIBUTE__ (   x)    /* no-op */

CategoryMutex

SDL offers several thread synchronization primitives. This document can't cover the complicated topic of thread safety, but reading up on what each of these primitives are, why they are useful, and how to correctly use them is vital to writing correct and safe multithreaded programs.

SDL also offers a datatype, SDL_InitState, which can be used to make sure only one thread initializes/deinitializes some resource that several threads might try to use for the first time simultaneously.

Definition at line 67 of file SDL_mutex.h.

◆ SDL_TRY_ACQUIRE

#define SDL_TRY_ACQUIRE (   x,
 
)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(x, y))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 207 of file SDL_mutex.h.

◆ SDL_TRY_ACQUIRE_SHARED

#define SDL_TRY_ACQUIRE_SHARED (   x,
 
)     SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(x, y))

Wrapper around Clang thread safety analysis annotations.

Please see https://clang.llvm.org/docs/ThreadSafetyAnalysis.html#mutex-h

Since
This macro is available since SDL 3.2.0.

Definition at line 217 of file SDL_mutex.h.

Typedef Documentation

◆ SDL_Condition

typedef struct SDL_Condition SDL_Condition

A means to block multiple threads until a condition is satisfied.

Condition variables, paired with an SDL_Mutex, let an app halt multiple threads until a condition has occurred, at which time the app can release one or all waiting threads.

Wikipedia has a thorough explanation of the concept:

https://en.wikipedia.org/wiki/Condition_variable

Since
This struct is available since SDL 3.2.0.

Definition at line 841 of file SDL_mutex.h.

◆ SDL_Mutex

typedef struct SDL_Mutex SDL_Mutex

A means to serialize access to a resource between threads.

Mutexes (short for "mutual exclusion") are a synchronization primitive that allows exactly one thread to proceed at a time.

Wikipedia has a thorough explanation of the concept:

https://en.wikipedia.org/wiki/Mutex

Since
This struct is available since SDL 3.2.0.

Definition at line 296 of file SDL_mutex.h.

◆ SDL_RWLock

typedef struct SDL_RWLock SDL_RWLock

A mutex that allows read-only threads to run in parallel.

A rwlock is roughly the same concept as SDL_Mutex, but allows threads that request read-only access to all hold the lock at the same time. If a thread requests write access, it will block until all read-only threads have released the lock, and no one else can hold the thread (for reading or writing) at the same time as the writing thread.

This can be more efficient in cases where several threads need to access data frequently, but changes to that data are rare.

There are other rules that apply to rwlocks that don't apply to mutexes, about how threads are scheduled and when they can be recursively locked. These are documented in the other rwlock functions.

Since
This struct is available since SDL 3.2.0.

Definition at line 438 of file SDL_mutex.h.

◆ SDL_Semaphore

typedef struct SDL_Semaphore SDL_Semaphore

A means to manage access to a resource, by count, between threads.

Semaphores (specifically, "counting semaphores"), let X number of threads request access at the same time, each thread granted access decrementing a counter. When the counter reaches zero, future requests block until a prior thread releases their request, incrementing the counter again.

Wikipedia has a thorough explanation of the concept:

https://en.wikipedia.org/wiki/Semaphore_(programming)

Since
This struct is available since SDL 3.2.0.

Definition at line 684 of file SDL_mutex.h.

Enumeration Type Documentation

◆ SDL_InitStatus

The current status of an SDL_InitState structure.

Since
This enum is available since SDL 3.2.0.
Enumerator
SDL_INIT_STATUS_UNINITIALIZED 
SDL_INIT_STATUS_INITIALIZING 
SDL_INIT_STATUS_INITIALIZED 
SDL_INIT_STATUS_UNINITIALIZING 

Definition at line 975 of file SDL_mutex.h.

Function Documentation

◆ SDL_BroadcastCondition()

void SDL_BroadcastCondition ( SDL_Condition cond)
extern

Restart all threads that are waiting on the condition variable.

Parameters
condthe condition variable to signal.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_SignalCondition
SDL_WaitCondition
SDL_WaitConditionTimeout

◆ SDL_CreateCondition()

SDL_Condition * SDL_CreateCondition ( void  )
extern

Create a condition variable.

Returns
a new condition variable or NULL on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_BroadcastCondition
SDL_SignalCondition
SDL_WaitCondition
SDL_WaitConditionTimeout
SDL_DestroyCondition

◆ SDL_CreateMutex()

SDL_Mutex * SDL_CreateMutex ( void  )
extern

Create a new mutex.

All newly-created mutexes begin in the unlocked state.

Calls to SDL_LockMutex() will not return while the mutex is locked by another thread. See SDL_TryLockMutex() to attempt to lock without blocking.

SDL mutexes are reentrant.

Returns
the initialized and unlocked mutex or NULL on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_DestroyMutex
SDL_LockMutex
SDL_TryLockMutex
SDL_UnlockMutex

◆ SDL_CreateRWLock()

SDL_RWLock * SDL_CreateRWLock ( void  )
extern

Create a new read/write lock.

A read/write lock is useful for situations where you have multiple threads trying to access a resource that is rarely updated. All threads requesting a read-only lock will be allowed to run in parallel; if a thread requests a write lock, it will be provided exclusive access. This makes it safe for multiple threads to use a resource at the same time if they promise not to change it, and when it has to be changed, the rwlock will serve as a gateway to make sure those changes can be made safely.

In the right situation, a rwlock can be more efficient than a mutex, which only lets a single thread proceed at a time, even if it won't be modifying the data.

All newly-created read/write locks begin in the unlocked state.

Calls to SDL_LockRWLockForReading() and SDL_LockRWLockForWriting will not return while the rwlock is locked for writing by another thread. See SDL_TryLockRWLockForReading() and SDL_TryLockRWLockForWriting() to attempt to lock without blocking.

SDL read/write locks are only recursive for read-only locks! They are not guaranteed to be fair, or provide access in a FIFO manner! They are not guaranteed to favor writers. You may not lock a rwlock for both read-only and write access at the same time from the same thread (so you can't promote your read-only lock to a write lock without unlocking first).

Returns
the initialized and unlocked read/write lock or NULL on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_DestroyRWLock
SDL_LockRWLockForReading
SDL_LockRWLockForWriting
SDL_TryLockRWLockForReading
SDL_TryLockRWLockForWriting
SDL_UnlockRWLock

◆ SDL_CreateSemaphore()

SDL_Semaphore * SDL_CreateSemaphore ( Uint32  initial_value)
extern

Create a semaphore.

This function creates a new semaphore and initializes it with the value initial_value. Each wait operation on the semaphore will atomically decrement the semaphore value and potentially block if the semaphore value is 0. Each post operation will atomically increment the semaphore value and wake waiting threads and allow them to retry the wait operation.

Parameters
initial_valuethe starting value of the semaphore.
Returns
a new semaphore or NULL on failure; call SDL_GetError() for more information.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_DestroySemaphore
SDL_SignalSemaphore
SDL_TryWaitSemaphore
SDL_GetSemaphoreValue
SDL_WaitSemaphore
SDL_WaitSemaphoreTimeout

◆ SDL_DestroyCondition()

void SDL_DestroyCondition ( SDL_Condition cond)
extern

Destroy a condition variable.

Parameters
condthe condition variable to destroy.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_CreateCondition

◆ SDL_DestroyMutex()

void SDL_DestroyMutex ( SDL_Mutex mutex)
extern

Destroy a mutex created with SDL_CreateMutex().

This function must be called on any mutex that is no longer needed. Failure to destroy a mutex will result in a system memory or resource leak. While it is safe to destroy a mutex that is unlocked, it is not safe to attempt to destroy a locked mutex, and may result in undefined behavior depending on the platform.

Parameters
mutexthe mutex to destroy.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_CreateMutex

◆ SDL_DestroyRWLock()

void SDL_DestroyRWLock ( SDL_RWLock rwlock)
extern

Destroy a read/write lock created with SDL_CreateRWLock().

This function must be called on any read/write lock that is no longer needed. Failure to destroy a rwlock will result in a system memory or resource leak. While it is safe to destroy a rwlock that is unlocked, it is not safe to attempt to destroy a locked rwlock, and may result in undefined behavior depending on the platform.

Parameters
rwlockthe rwlock to destroy.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_CreateRWLock

◆ SDL_DestroySemaphore()

void SDL_DestroySemaphore ( SDL_Semaphore sem)
extern

Destroy a semaphore.

It is not safe to destroy a semaphore if there are threads currently waiting on it.

Parameters
semthe semaphore to destroy.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_CreateSemaphore

◆ SDL_GetSemaphoreValue()

Uint32 SDL_GetSemaphoreValue ( SDL_Semaphore sem)
extern

Get the current value of a semaphore.

Parameters
semthe semaphore to query.
Returns
the current value of the semaphore.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.

◆ SDL_LockMutex()

void SDL_LockMutex ( SDL_Mutex mutex)
extern

Lock the mutex.

This will block until the mutex is available, which is to say it is in the unlocked state and the OS has chosen the caller as the next thread to lock it. Of all threads waiting to lock the mutex, only one may do so at a time.

It is legal for the owning thread to lock an already-locked mutex. It must unlock it the same number of times before it is actually made available for other threads in the system (this is known as a "recursive mutex").

This function does not fail; if mutex is NULL, it will return immediately having locked nothing. If the mutex is valid, this function will always block until it can lock the mutex, and return with it locked.

Parameters
mutexthe mutex to lock.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_TryLockMutex
SDL_UnlockMutex

◆ SDL_LockRWLockForReading()

void SDL_LockRWLockForReading ( SDL_RWLock rwlock)
extern

Lock the read/write lock for read only operations.

This will block until the rwlock is available, which is to say it is not locked for writing by any other thread. Of all threads waiting to lock the rwlock, all may do so at the same time as long as they are requesting read-only access; if a thread wants to lock for writing, only one may do so at a time, and no other threads, read-only or not, may hold the lock at the same time.

It is legal for the owning thread to lock an already-locked rwlock for reading. It must unlock it the same number of times before it is actually made available for other threads in the system (this is known as a "recursive rwlock").

Note that locking for writing is not recursive (this is only available to read-only locks).

It is illegal to request a read-only lock from a thread that already holds the write lock. Doing so results in undefined behavior. Unlock the write lock before requesting a read-only lock. (But, of course, if you have the write lock, you don't need further locks to read in any case.)

This function does not fail; if rwlock is NULL, it will return immediately having locked nothing. If the rwlock is valid, this function will always block until it can lock the mutex, and return with it locked.

Parameters
rwlockthe read/write lock to lock.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_LockRWLockForWriting
SDL_TryLockRWLockForReading
SDL_UnlockRWLock

◆ SDL_LockRWLockForWriting()

void SDL_LockRWLockForWriting ( SDL_RWLock rwlock)
extern

Lock the read/write lock for write operations.

This will block until the rwlock is available, which is to say it is not locked for reading or writing by any other thread. Only one thread may hold the lock when it requests write access; all other threads, whether they also want to write or only want read-only access, must wait until the writer thread has released the lock.

It is illegal for the owning thread to lock an already-locked rwlock for writing (read-only may be locked recursively, writing can not). Doing so results in undefined behavior.

It is illegal to request a write lock from a thread that already holds a read-only lock. Doing so results in undefined behavior. Unlock the read-only lock before requesting a write lock.

This function does not fail; if rwlock is NULL, it will return immediately having locked nothing. If the rwlock is valid, this function will always block until it can lock the mutex, and return with it locked.

Parameters
rwlockthe read/write lock to lock.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_LockRWLockForReading
SDL_TryLockRWLockForWriting
SDL_UnlockRWLock

◆ SDL_SetInitialized()

void SDL_SetInitialized ( SDL_InitState state,
bool  initialized 
)
extern

Finish an initialization state transition.

This function sets the status of the passed in state to SDL_INIT_STATUS_INITIALIZED or SDL_INIT_STATUS_UNINITIALIZED and allows any threads waiting for the status to proceed.

Parameters
statethe initialization state to check.
initializedthe new initialization state.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_ShouldInit
SDL_ShouldQuit

◆ SDL_ShouldInit()

bool SDL_ShouldInit ( SDL_InitState state)
extern

Return whether initialization should be done.

This function checks the passed in state and if initialization should be done, sets the status to SDL_INIT_STATUS_INITIALIZING and returns true. If another thread is already modifying this state, it will wait until that's done before returning.

If this function returns true, the calling code must call SDL_SetInitialized() to complete the initialization.

Parameters
statethe initialization state to check.
Returns
true if initialization needs to be done, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetInitialized
SDL_ShouldQuit

◆ SDL_ShouldQuit()

bool SDL_ShouldQuit ( SDL_InitState state)
extern

Return whether cleanup should be done.

This function checks the passed in state and if cleanup should be done, sets the status to SDL_INIT_STATUS_UNINITIALIZING and returns true.

If this function returns true, the calling code must call SDL_SetInitialized() to complete the cleanup.

Parameters
statethe initialization state to check.
Returns
true if cleanup needs to be done, false otherwise.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_SetInitialized
SDL_ShouldInit

◆ SDL_SignalCondition()

void SDL_SignalCondition ( SDL_Condition cond)
extern

Restart one of the threads that are waiting on the condition variable.

Parameters
condthe condition variable to signal.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_BroadcastCondition
SDL_WaitCondition
SDL_WaitConditionTimeout

◆ SDL_SignalSemaphore()

void SDL_SignalSemaphore ( SDL_Semaphore sem)
extern

Atomically increment a semaphore's value and wake waiting threads.

Parameters
semthe semaphore to increment.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_TryWaitSemaphore
SDL_WaitSemaphore
SDL_WaitSemaphoreTimeout

◆ SDL_TryLockMutex()

bool SDL_TryLockMutex ( SDL_Mutex mutex)
extern

Try to lock a mutex without blocking.

This works just like SDL_LockMutex(), but if the mutex is not available, this function returns false immediately.

This technique is useful if you need exclusive access to a resource but don't want to wait for it, and will return to it to try again later.

This function returns true if passed a NULL mutex.

Parameters
mutexthe mutex to try to lock.
Returns
true on success, false if the mutex would block.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_LockMutex
SDL_UnlockMutex

◆ SDL_TryLockRWLockForReading()

bool SDL_TryLockRWLockForReading ( SDL_RWLock rwlock)
extern

Try to lock a read/write lock for reading without blocking.

This works just like SDL_LockRWLockForReading(), but if the rwlock is not available, then this function returns false immediately.

This technique is useful if you need access to a resource but don't want to wait for it, and will return to it to try again later.

Trying to lock for read-only access can succeed if other threads are holding read-only locks, as this won't prevent access.

This function returns true if passed a NULL rwlock.

Parameters
rwlockthe rwlock to try to lock.
Returns
true on success, false if the lock would block.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_LockRWLockForReading
SDL_TryLockRWLockForWriting
SDL_UnlockRWLock

◆ SDL_TryLockRWLockForWriting()

bool SDL_TryLockRWLockForWriting ( SDL_RWLock rwlock)
extern

Try to lock a read/write lock for writing without blocking.

This works just like SDL_LockRWLockForWriting(), but if the rwlock is not available, then this function returns false immediately.

This technique is useful if you need exclusive access to a resource but don't want to wait for it, and will return to it to try again later.

It is illegal for the owning thread to lock an already-locked rwlock for writing (read-only may be locked recursively, writing can not). Doing so results in undefined behavior.

It is illegal to request a write lock from a thread that already holds a read-only lock. Doing so results in undefined behavior. Unlock the read-only lock before requesting a write lock.

This function returns true if passed a NULL rwlock.

Parameters
rwlockthe rwlock to try to lock.
Returns
true on success, false if the lock would block.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_LockRWLockForWriting
SDL_TryLockRWLockForReading
SDL_UnlockRWLock

◆ SDL_TryWaitSemaphore()

bool SDL_TryWaitSemaphore ( SDL_Semaphore sem)
extern

See if a semaphore has a positive value and decrement it if it does.

This function checks to see if the semaphore pointed to by sem has a positive value and atomically decrements the semaphore value if it does. If the semaphore doesn't have a positive value, the function immediately returns false.

Parameters
semthe semaphore to wait on.
Returns
true if the wait succeeds, false if the wait would block.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_SignalSemaphore
SDL_WaitSemaphore
SDL_WaitSemaphoreTimeout

◆ SDL_UnlockMutex()

void SDL_UnlockMutex ( SDL_Mutex mutex)
extern

Unlock the mutex.

It is legal for the owning thread to lock an already-locked mutex. It must unlock it the same number of times before it is actually made available for other threads in the system (this is known as a "recursive mutex").

It is illegal to unlock a mutex that has not been locked by the current thread, and doing so results in undefined behavior.

Parameters
mutexthe mutex to unlock.

\threadsafety This call must be paired with a previous locking call on the same thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_LockMutex
SDL_TryLockMutex

◆ SDL_UnlockRWLock()

void SDL_UnlockRWLock ( SDL_RWLock rwlock)
extern

Unlock the read/write lock.

Use this function to unlock the rwlock, whether it was locked for read-only or write operations.

It is legal for the owning thread to lock an already-locked read-only lock. It must unlock it the same number of times before it is actually made available for other threads in the system (this is known as a "recursive rwlock").

It is illegal to unlock a rwlock that has not been locked by the current thread, and doing so results in undefined behavior.

Parameters
rwlockthe rwlock to unlock.

\threadsafety This call must be paired with a previous locking call on the same thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_LockRWLockForReading
SDL_LockRWLockForWriting
SDL_TryLockRWLockForReading
SDL_TryLockRWLockForWriting

◆ SDL_WaitCondition()

void SDL_WaitCondition ( SDL_Condition cond,
SDL_Mutex mutex 
)
extern

Wait until a condition variable is signaled.

This function unlocks the specified mutex and waits for another thread to call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition variable cond. Once the condition variable is signaled, the mutex is re-locked and the function returns.

The mutex must be locked before calling this function. Locking the mutex recursively (more than once) is not supported and leads to undefined behavior.

This function is the equivalent of calling SDL_WaitConditionTimeout() with a time length of -1.

Parameters
condthe condition variable to wait on.
mutexthe mutex used to coordinate thread access.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_BroadcastCondition
SDL_SignalCondition
SDL_WaitConditionTimeout

◆ SDL_WaitConditionTimeout()

bool SDL_WaitConditionTimeout ( SDL_Condition cond,
SDL_Mutex mutex,
Sint32  timeoutMS 
)
extern

Wait until a condition variable is signaled or a certain time has passed.

This function unlocks the specified mutex and waits for another thread to call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition variable cond, or for the specified time to elapse. Once the condition variable is signaled or the time elapsed, the mutex is re-locked and the function returns.

The mutex must be locked before calling this function. Locking the mutex recursively (more than once) is not supported and leads to undefined behavior.

Parameters
condthe condition variable to wait on.
mutexthe mutex used to coordinate thread access.
timeoutMSthe maximum time to wait, in milliseconds, or -1 to wait indefinitely.
Returns
true if the condition variable is signaled, false if the condition is not signaled in the allotted time.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_BroadcastCondition
SDL_SignalCondition
SDL_WaitCondition

◆ SDL_WaitSemaphore()

void SDL_WaitSemaphore ( SDL_Semaphore sem)
extern

Wait until a semaphore has a positive value and then decrements it.

This function suspends the calling thread until the semaphore pointed to by sem has a positive value, and then atomically decrement the semaphore value.

This function is the equivalent of calling SDL_WaitSemaphoreTimeout() with a time length of -1.

Parameters
semthe semaphore wait on.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_SignalSemaphore
SDL_TryWaitSemaphore
SDL_WaitSemaphoreTimeout

◆ SDL_WaitSemaphoreTimeout()

bool SDL_WaitSemaphoreTimeout ( SDL_Semaphore sem,
Sint32  timeoutMS 
)
extern

Wait until a semaphore has a positive value and then decrements it.

This function suspends the calling thread until either the semaphore pointed to by sem has a positive value or the specified time has elapsed. If the call is successful it will atomically decrement the semaphore value.

Parameters
semthe semaphore to wait on.
timeoutMSthe length of the timeout, in milliseconds, or -1 to wait indefinitely.
Returns
true if the wait succeeds or false if the wait times out.

\threadsafety It is safe to call this function from any thread.

Since
This function is available since SDL 3.2.0.
See also
SDL_SignalSemaphore
SDL_TryWaitSemaphore
SDL_WaitSemaphore

Variable Documentation

◆ mutex

bool mutex

Definition at line 369 of file SDL_mutex.h.

◆ rwlock

bool rwlock

Definition at line 581 of file SDL_mutex.h.