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

◆ 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 which can time out return this value if they time out.

Since
This macro is available since SDL 3.0.0.

Definition at line 124 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 258 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 625 of file SDL_mutex.h.

◆ SDL_Mutex

typedef struct SDL_Mutex SDL_Mutex

Definition at line 134 of file SDL_mutex.h.

◆ SDL_RWLock

typedef struct SDL_RWLock SDL_RWLock

Definition at line 252 of file SDL_mutex.h.

◆ SDL_Semaphore

typedef struct SDL_Semaphore SDL_Semaphore

Definition at line 484 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_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_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_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_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_TryWaitSemaphore
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_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_PostSemaphore
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.
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.
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_PostSemaphore
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_PostSemaphore
SDL_TryWaitSemaphore
SDL_WaitSemaphore

Variable Documentation

◆ mutex

int mutex

Definition at line 204 of file SDL_mutex.h.

◆ rwlock

int rwlock

Definition at line 397 of file SDL_mutex.h.