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.

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)
 
#define SDL_MUTEX_TIMEDOUT   1
 

Read/write lock functions

#define SDL_RWLOCK_TIMEDOUT   SDL_MUTEX_TIMEDOUT
 
typedef struct SDL_RWLock SDL_RWLock
 
int rwlock
 
SDL_RWLockSDL_CreateRWLock (void)
 
void SDL_LockRWLockForReading (SDL_RWLock *rwlock) SDL_ACQUIRE_SHARED(rwlock)
 
void SDL_LockRWLockForWriting (SDL_RWLock *rwlock) SDL_ACQUIRE(rwlock)
 
int SDL_TryLockRWLockForReading (SDL_RWLock *rwlock) SDL_TRY_ACQUIRE_SHARED(0
 
int 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)
 

Mutex functions

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

Semaphore functions

typedef struct SDL_Semaphore SDL_Semaphore
 
SDL_SemaphoreSDL_CreateSemaphore (Uint32 initial_value)
 
void SDL_DestroySemaphore (SDL_Semaphore *sem)
 
int SDL_WaitSemaphore (SDL_Semaphore *sem)
 
int SDL_TryWaitSemaphore (SDL_Semaphore *sem)
 
int SDL_WaitSemaphoreTimeout (SDL_Semaphore *sem, Sint32 timeoutMS)
 
int 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)
 
int SDL_SignalCondition (SDL_Condition *cond)
 
int SDL_BroadcastCondition (SDL_Condition *cond)
 
int SDL_WaitCondition (SDL_Condition *cond, SDL_Mutex *mutex)
 
int 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 73 of file SDL_mutex.h.

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

◆ SDL_ACQUIRE_SHARED

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

Definition at line 76 of file SDL_mutex.h.

◆ SDL_ACQUIRED_AFTER

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

Definition at line 64 of file SDL_mutex.h.

◆ SDL_ACQUIRED_BEFORE

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

Definition at line 61 of file SDL_mutex.h.

◆ SDL_ASSERT_CAPABILITY

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

Definition at line 97 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 100 of file SDL_mutex.h.

◆ SDL_CAPABILITY

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

Definition at line 49 of file SDL_mutex.h.

◆ SDL_EXCLUDES

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

Definition at line 94 of file SDL_mutex.h.

◆ SDL_GUARDED_BY

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

Definition at line 55 of file SDL_mutex.h.

◆ SDL_MUTEX_TIMEDOUT

#define SDL_MUTEX_TIMEDOUT   1

Synchronization functions return this value if they time out.

Not all functions can time out; some will block indefinitely.

Since
This macro is available since SDL 3.0.0.

Definition at line 125 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 106 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 58 of file SDL_mutex.h.

◆ SDL_RELEASE

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

Definition at line 79 of file SDL_mutex.h.

◆ SDL_RELEASE_GENERIC

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

Definition at line 85 of file SDL_mutex.h.

◆ SDL_RELEASE_SHARED

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

Definition at line 82 of file SDL_mutex.h.

◆ SDL_REQUIRES

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

Definition at line 67 of file SDL_mutex.h.

◆ SDL_REQUIRES_SHARED

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

Definition at line 70 of file SDL_mutex.h.

◆ SDL_RETURN_CAPABILITY

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

Definition at line 103 of file SDL_mutex.h.

◆ SDL_RWLOCK_TIMEDOUT

#define SDL_RWLOCK_TIMEDOUT   SDL_MUTEX_TIMEDOUT

Definition at line 291 of file SDL_mutex.h.

◆ SDL_SCOPED_CAPABILITY

#define SDL_SCOPED_CAPABILITY    SDL_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)

Definition at line 52 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 46 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 88 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 91 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.0.0.

Definition at line 681 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.0.0.

Definition at line 145 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.0.0.

Definition at line 279 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.0.0.

Definition at line 529 of file SDL_mutex.h.

Function Documentation

◆ SDL_BroadcastCondition()

int SDL_BroadcastCondition ( SDL_Condition cond)
extern

Restart all threads that are waiting on the condition variable.

Parameters
condthe condition variable to signal.
Returns
0 on success or a negative error code on failure; call SDL_GetError() for more information.

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

Since
This function is available since SDL 3.0.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.0.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.0.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.0.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.0.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.0.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.0.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.0.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.0.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.0.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.0.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.0.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.0.0.
See also
SDL_LockRWLockForReading
SDL_TryLockRWLockForWriting
SDL_UnlockRWLock

◆ SDL_SignalCondition()

int SDL_SignalCondition ( SDL_Condition cond)
extern

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

Parameters
condthe condition variable to signal.
Returns
0 on success or a negative error code on failure; call SDL_GetError() for more information.

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

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

◆ SDL_SignalSemaphore()

int SDL_SignalSemaphore ( SDL_Semaphore sem)
extern

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

Parameters
semthe semaphore to increment.
Returns
0 on success or a negative error code on failure; call SDL_GetError() for more information.
Since
This function is available since SDL 3.0.0.
See also
SDL_TryWaitSemaphore
SDL_WaitSemaphore
SDL_WaitSemaphoreTimeout

◆ SDL_TryLockMutex()

int 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 SDL_MUTEX_TIMEDOUT 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 does not fail; if mutex is NULL, it will return 0 immediately having locked nothing. If the mutex is valid, this function will always either lock the mutex and return 0, or return SDL_MUTEX_TIMEOUT and lock nothing.

Parameters
mutexthe mutex to try to lock.
Returns
0 or SDL_MUTEX_TIMEDOUT.
Since
This function is available since SDL 3.0.0.
See also
SDL_LockMutex
SDL_UnlockMutex

◆ SDL_TryLockRWLockForReading()

int 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 SDL_RWLOCK_TIMEDOUT 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 does not fail; if rwlock is NULL, it will return 0 immediately having locked nothing. If rwlock is valid, this function will always either lock the rwlock and return 0, or return SDL_RWLOCK_TIMEOUT and lock nothing.

Parameters
rwlockthe rwlock to try to lock.
Returns
0 or SDL_RWLOCK_TIMEDOUT.
Since
This function is available since SDL 3.0.0.
See also
SDL_LockRWLockForReading
SDL_TryLockRWLockForWriting
SDL_UnlockRWLock

◆ SDL_TryLockRWLockForWriting()

int 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, this function returns SDL_RWLOCK_TIMEDOUT 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 does not fail; if rwlock is NULL, it will return 0 immediately having locked nothing. If rwlock is valid, this function will always either lock the rwlock and return 0, or return SDL_RWLOCK_TIMEOUT and lock nothing.

Parameters
rwlockthe rwlock to try to lock.
Returns
0 or SDL_RWLOCK_TIMEDOUT.
Since
This function is available since SDL 3.0.0.
See also
SDL_LockRWLockForWriting
SDL_TryLockRWLockForReading
SDL_UnlockRWLock

◆ SDL_TryWaitSemaphore()

int 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 SDL_MUTEX_TIMEDOUT.

Parameters
semthe semaphore to wait on.
Returns
0 if the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait would block, or a negative error code on failure; call SDL_GetError() for more information.
Since
This function is available since SDL 3.0.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.0.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.0.0.
See also
SDL_LockRWLockForReading
SDL_LockRWLockForWriting
SDL_TryLockRWLockForReading
SDL_TryLockRWLockForWriting

◆ SDL_WaitCondition()

int 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.
Returns
0 when it is signaled or a negative error code on failure; call SDL_GetError() for more information.

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

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

◆ SDL_WaitConditionTimeout()

int 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
0 if the condition variable is signaled, SDL_MUTEX_TIMEDOUT if the condition is not signaled in the allotted time, or a negative error code on failure; call SDL_GetError() for more information.

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

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

◆ SDL_WaitSemaphore()

int 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 either the semaphore pointed to by sem has a positive value or the call is interrupted by a signal or error. If the call is successful it will 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.
Returns
0 on success or a negative error code on failure; call SDL_GetError() for more information.
Since
This function is available since SDL 3.0.0.
See also
SDL_SignalSemaphore
SDL_TryWaitSemaphore
SDL_WaitSemaphoreTimeout

◆ SDL_WaitSemaphoreTimeout()

int 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, the call is interrupted by a signal or error, 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.
Returns
0 if the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait does not succeed in the allotted time, or a negative error code on failure; call SDL_GetError() for more information.
Since
This function is available since SDL 3.0.0.
See also
SDL_SignalSemaphore
SDL_TryWaitSemaphore
SDL_WaitSemaphore

Variable Documentation

◆ mutex

int mutex

Definition at line 215 of file SDL_mutex.h.

◆ rwlock

int rwlock

Definition at line 430 of file SDL_mutex.h.