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))

Definition at line 75 of file SDL_mutex.h.

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

◆ SDL_ACQUIRE_SHARED

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

Definition at line 78 of file SDL_mutex.h.

◆ SDL_ACQUIRED_AFTER

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

Definition at line 66 of file SDL_mutex.h.

◆ SDL_ACQUIRED_BEFORE

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

Definition at line 63 of file SDL_mutex.h.

◆ SDL_ASSERT_CAPABILITY

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

Definition at line 99 of file SDL_mutex.h.

◆ SDL_ASSERT_SHARED_CAPABILITY

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

Definition at line 102 of file SDL_mutex.h.

◆ SDL_CAPABILITY

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

Definition at line 51 of file SDL_mutex.h.

◆ SDL_EXCLUDES

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

Definition at line 96 of file SDL_mutex.h.

◆ SDL_GUARDED_BY

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

Definition at line 57 of file SDL_mutex.h.

◆ SDL_NO_THREAD_SAFETY_ANALYSIS

#define SDL_NO_THREAD_SAFETY_ANALYSIS    SDL_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)

Definition at line 108 of file SDL_mutex.h.

◆ SDL_PT_GUARDED_BY

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

Definition at line 60 of file SDL_mutex.h.

◆ SDL_RELEASE

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

Definition at line 81 of file SDL_mutex.h.

◆ SDL_RELEASE_GENERIC

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

Definition at line 87 of file SDL_mutex.h.

◆ SDL_RELEASE_SHARED

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

Definition at line 84 of file SDL_mutex.h.

◆ SDL_REQUIRES

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

Definition at line 69 of file SDL_mutex.h.

◆ SDL_REQUIRES_SHARED

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

Definition at line 72 of file SDL_mutex.h.

◆ SDL_RETURN_CAPABILITY

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

Definition at line 105 of file SDL_mutex.h.

◆ SDL_SCOPED_CAPABILITY

#define SDL_SCOPED_CAPABILITY    SDL_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)

Definition at line 54 of file SDL_mutex.h.

◆ SDL_THREAD_ANNOTATION_ATTRIBUTE__

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

CategoryMutex

Functions to provide thread synchronization primitives.

Definition at line 48 of file SDL_mutex.h.

◆ SDL_TRY_ACQUIRE

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

Definition at line 90 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))

Definition at line 93 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.1.3.

Definition at line 642 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.1.3.

Definition at line 137 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.1.3.

Definition at line 268 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.1.3.

Definition at line 499 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.1.3.
Enumerator
SDL_INIT_STATUS_UNINITIALIZED 
SDL_INIT_STATUS_INITIALIZING 
SDL_INIT_STATUS_INITIALIZED 
SDL_INIT_STATUS_UNINITIALIZING 

Definition at line 772 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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.

◆ 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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
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.1.3.
See also
SDL_SignalSemaphore
SDL_TryWaitSemaphore
SDL_WaitSemaphore

Variable Documentation

◆ mutex

bool mutex

Definition at line 204 of file SDL_mutex.h.

◆ rwlock

bool rwlock

Definition at line 403 of file SDL_mutex.h.