SDL 3.0
SDL_mutex.h File Reference

Functions to provide thread synchronization primitives. More...

+ 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
 
#define SDL_MUTEX_MAXWAIT   -1
 

Read/write lock functions

#define SDL_RWLOCK_TIMEDOUT   SDL_MUTEX_TIMEDOUT
 
typedef struct SDL_RWLock SDL_RWLock
 
int rwlock
 
SDL_RWLockSDL_CreateRWLock (void)
 
int SDL_LockRWLockForReading (SDL_RWLock *rwlock) SDL_ACQUIRE_SHARED(rwlock)
 
int 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
 
int SDL_UnlockRWLock (SDL_RWLock *rwlock) SDL_RELEASE_SHARED(rwlock)
 
void SDL_DestroyRWLock (SDL_RWLock *rwlock)
 

Mutex functions

typedef struct SDL_Mutex SDL_Mutex
 
int mutex
 
SDL_MutexSDL_CreateMutex (void)
 
int SDL_LockMutex (SDL_Mutex *mutex) SDL_ACQUIRE(mutex)
 
int SDL_TryLockMutex (SDL_Mutex *mutex) SDL_TRY_ACQUIRE(0
 
int 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_PostSemaphore (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)
 

Detailed Description

Functions to provide thread synchronization primitives.

Definition in file SDL_mutex.h.

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

◆ 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_MAXWAIT

#define SDL_MUTEX_MAXWAIT   -1

This is the timeout value which corresponds to never time out.

Definition at line 127 of file SDL_mutex.h.

◆ SDL_MUTEX_TIMEDOUT

#define SDL_MUTEX_TIMEDOUT   1

Synchronization functions which can time out return this value if they time out.

Definition at line 122 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 256 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 */

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

Definition at line 631 of file SDL_mutex.h.

◆ SDL_Mutex

typedef struct SDL_Mutex SDL_Mutex

Definition at line 137 of file SDL_mutex.h.

◆ SDL_RWLock

typedef struct SDL_RWLock SDL_RWLock

Definition at line 250 of file SDL_mutex.h.

◆ SDL_Semaphore

typedef struct SDL_Semaphore SDL_Semaphore

Definition at line 470 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.
Since
This function is available since SDL 3.0.0.
See also
SDL_SignalCondition
SDL_WaitCondition
SDL_WaitConditionTimeout
SDL_CreateCondition
SDL_DestroyCondition

◆ 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_TryLockRWLockForReading
SDL_LockRWLockForWriting
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_PostSemaphore
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_BroadcastCondition
SDL_SignalCondition
SDL_WaitCondition
SDL_WaitConditionTimeout
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_LockMutex
SDL_TryLockMutex
SDL_UnlockMutex

◆ 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_LockRWLockForReading
SDL_LockRWLockForWriting
SDL_TryLockRWLockForReading
SDL_TryLockRWLockForWriting
SDL_UnlockRWLock

◆ 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_PostSemaphore
SDL_TryWaitSemaphore
SDL_GetSemaphoreValue
SDL_WaitSemaphore
SDL_WaitSemaphoreTimeout

◆ 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.
See also
SDL_CreateSemaphore

◆ SDL_LockMutex()

int 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").

Parameters
mutexthe mutex to lock
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.

◆ SDL_LockRWLockForReading()

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

Parameters
rwlockthe read/write lock to lock
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_UnlockRWLock

◆ SDL_LockRWLockForWriting()

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

Parameters
rwlockthe read/write lock to lock
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_UnlockRWLock

◆ SDL_PostSemaphore()

int SDL_PostSemaphore ( 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_CreateSemaphore
SDL_DestroySemaphore
SDL_TryWaitSemaphore
SDL_GetSemaphoreValue
SDL_WaitSemaphore
SDL_WaitSemaphoreTimeout

◆ 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.
Since
This function is available since SDL 3.0.0.
See also
SDL_BroadcastCondition
SDL_WaitCondition
SDL_WaitConditionTimeout
SDL_CreateCondition
SDL_DestroyCondition

◆ 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.

Parameters
mutexthe mutex to try to lock
Returns
0, SDL_MUTEX_TIMEDOUT, or -1 on error; call SDL_GetError() for more information.
Since
This function is available since SDL 3.0.0.
See also
SDL_CreateMutex
SDL_DestroyMutex
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.

Parameters
rwlockthe rwlock to try to lock
Returns
0, SDL_RWLOCK_TIMEDOUT, or -1 on error; call SDL_GetError() for more information.
Since
This function is available since SDL 3.0.0.
See also
SDL_CreateRWLock
SDL_DestroyRWLock
SDL_TryLockRWLockForReading
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.

Parameters
rwlockthe rwlock to try to lock
Returns
0, SDL_RWLOCK_TIMEDOUT, or -1 on error; call SDL_GetError() for more information.
Since
This function is available since SDL 3.0.0.
See also
SDL_CreateRWLock
SDL_DestroyRWLock
SDL_TryLockRWLockForWriting
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_CreateSemaphore
SDL_DestroySemaphore
SDL_PostSemaphore
SDL_GetSemaphoreValue
SDL_WaitSemaphore
SDL_WaitSemaphoreTimeout

◆ SDL_UnlockMutex()

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

◆ SDL_UnlockRWLock()

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

◆ 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 SDL_MUTEX_MAXWAIT.

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.
Since
This function is available since SDL 3.0.0.
See also
SDL_BroadcastCondition
SDL_SignalCondition
SDL_WaitConditionTimeout
SDL_CreateCondition
SDL_DestroyCondition

◆ 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 SDL_MUTEX_MAXWAIT 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.
Since
This function is available since SDL 3.0.0.
See also
SDL_BroadcastCondition
SDL_SignalCondition
SDL_WaitCondition
SDL_CreateCondition
SDL_DestroyCondition

◆ 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 SDL_MUTEX_MAXWAIT.

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_CreateSemaphore
SDL_DestroySemaphore
SDL_PostSemaphore
SDL_TryWaitSemaphore
SDL_GetSemaphoreValue
SDL_WaitSemaphore
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_CreateSemaphore
SDL_DestroySemaphore
SDL_PostSemaphore
SDL_TryWaitSemaphore
SDL_GetSemaphoreValue
SDL_WaitSemaphore

Variable Documentation

◆ mutex

int mutex

Definition at line 200 of file SDL_mutex.h.

◆ rwlock

int rwlock

Definition at line 384 of file SDL_mutex.h.