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

◆ 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 801 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 427 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 658 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 931 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 363 of file SDL_mutex.h.

◆ rwlock

bool rwlock

Definition at line 562 of file SDL_mutex.h.