SDL 3.0
SDL_mutex.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22#ifndef SDL_mutex_h_
23#define SDL_mutex_h_
24
25/**
26 * \file SDL_mutex.h
27 *
28 * Functions to provide thread synchronization primitives.
29 */
30
31#include <SDL3/SDL_stdinc.h>
32#include <SDL3/SDL_error.h>
33
34/******************************************************************************/
35/* Enable thread safety attributes only with clang.
36 * The attributes can be safely erased when compiling with other compilers.
37 *
38 * To enable analysis, set these environment variables before running cmake:
39 * export CC=clang
40 * export CFLAGS="-DSDL_THREAD_SAFETY_ANALYSIS -Wthread-safety"
41 */
42#if defined(SDL_THREAD_SAFETY_ANALYSIS) && \
43 defined(__clang__) && (!defined(SWIG))
44#define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
45#else
46#define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x) /* no-op */
47#endif
48
49#define SDL_CAPABILITY(x) \
50 SDL_THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
51
52#define SDL_SCOPED_CAPABILITY \
53 SDL_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
54
55#define SDL_GUARDED_BY(x) \
56 SDL_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
57
58#define SDL_PT_GUARDED_BY(x) \
59 SDL_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
60
61#define SDL_ACQUIRED_BEFORE(x) \
62 SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))
63
64#define SDL_ACQUIRED_AFTER(x) \
65 SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))
66
67#define SDL_REQUIRES(x) \
68 SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(x))
69
70#define SDL_REQUIRES_SHARED(x) \
71 SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(x))
72
73#define SDL_ACQUIRE(x) \
74 SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(x))
75
76#define SDL_ACQUIRE_SHARED(x) \
77 SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(x))
78
79#define SDL_RELEASE(x) \
80 SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(x))
81
82#define SDL_RELEASE_SHARED(x) \
83 SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(x))
84
85#define SDL_RELEASE_GENERIC(x) \
86 SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(x))
87
88#define SDL_TRY_ACQUIRE(x, y) \
89 SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(x, y))
90
91#define SDL_TRY_ACQUIRE_SHARED(x, y) \
92 SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(x, y))
93
94#define SDL_EXCLUDES(x) \
95 SDL_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(x))
96
97#define SDL_ASSERT_CAPABILITY(x) \
98 SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
99
100#define SDL_ASSERT_SHARED_CAPABILITY(x) \
101 SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
102
103#define SDL_RETURN_CAPABILITY(x) \
104 SDL_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
105
106#define SDL_NO_THREAD_SAFETY_ANALYSIS \
107 SDL_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
108
109/******************************************************************************/
110
111
112#include <SDL3/SDL_begin_code.h>
113/* Set up for C function definitions, even when using C++ */
114#ifdef __cplusplus
115extern "C" {
116#endif
117
118/**
119 * Synchronization functions which can time out return this value
120 * if they time out.
121 */
122#define SDL_MUTEX_TIMEDOUT 1
123
124
125/**
126 * \name Mutex functions
127 */
128/* @{ */
129
130/* The SDL mutex structure, defined in SDL_sysmutex.c */
131struct SDL_Mutex;
132typedef struct SDL_Mutex SDL_Mutex;
133
134/**
135 * Create a new mutex.
136 *
137 * All newly-created mutexes begin in the _unlocked_ state.
138 *
139 * Calls to SDL_LockMutex() will not return while the mutex is locked by
140 * another thread. See SDL_TryLockMutex() to attempt to lock without blocking.
141 *
142 * SDL mutexes are reentrant.
143 *
144 * \returns the initialized and unlocked mutex or NULL on failure; call
145 * SDL_GetError() for more information.
146 *
147 * \since This function is available since SDL 3.0.0.
148 *
149 * \sa SDL_DestroyMutex
150 * \sa SDL_LockMutex
151 * \sa SDL_TryLockMutex
152 * \sa SDL_UnlockMutex
153 */
154extern DECLSPEC SDL_Mutex *SDLCALL SDL_CreateMutex(void);
155
156/**
157 * Lock the mutex.
158 *
159 * This will block until the mutex is available, which is to say it is in the
160 * unlocked state and the OS has chosen the caller as the next thread to lock
161 * it. Of all threads waiting to lock the mutex, only one may do so at a time.
162 *
163 * It is legal for the owning thread to lock an already-locked mutex. It must
164 * unlock it the same number of times before it is actually made available for
165 * other threads in the system (this is known as a "recursive mutex").
166 *
167 * This function does not fail; if mutex is NULL, it will return immediately
168 * having locked nothing. If the mutex is valid, this function will always
169 * block until it can lock the mutex, and return with it locked.
170 *
171 * \param mutex the mutex to lock
172 *
173 * \since This function is available since SDL 3.0.0.
174 */
175extern DECLSPEC void SDLCALL SDL_LockMutex(SDL_Mutex *mutex) SDL_ACQUIRE(mutex);
176
177/**
178 * Try to lock a mutex without blocking.
179 *
180 * This works just like SDL_LockMutex(), but if the mutex is not available,
181 * this function returns `SDL_MUTEX_TIMEDOUT` immediately.
182 *
183 * This technique is useful if you need exclusive access to a resource but
184 * don't want to wait for it, and will return to it to try again later.
185 *
186 * This function does not fail; if mutex is NULL, it will return 0 immediately
187 * having locked nothing. If the mutex is valid, this function will always
188 * either lock the mutex and return 0, or return SDL_MUTEX_TIMEOUT and lock
189 * nothing.
190 *
191 * \param mutex the mutex to try to lock
192 * \returns 0 or `SDL_MUTEX_TIMEDOUT`
193 *
194 * \since This function is available since SDL 3.0.0.
195 *
196 * \sa SDL_CreateMutex
197 * \sa SDL_DestroyMutex
198 * \sa SDL_LockMutex
199 * \sa SDL_UnlockMutex
200 */
201extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_Mutex *mutex) SDL_TRY_ACQUIRE(0, mutex);
202
203/**
204 * Unlock the mutex.
205 *
206 * It is legal for the owning thread to lock an already-locked mutex. It must
207 * unlock it the same number of times before it is actually made available for
208 * other threads in the system (this is known as a "recursive mutex").
209 *
210 * It is illegal to unlock a mutex that has not been locked by the current
211 * thread, and doing so results in undefined behavior.
212 *
213 * \param mutex the mutex to unlock.
214 *
215 * \since This function is available since SDL 3.0.0.
216 */
217extern DECLSPEC void 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 * This function does not fail; if rwlock is NULL, it will return immediately
324 * having locked nothing. If the rwlock is valid, this function will always
325 * block until it can lock the mutex, and return with it locked.
326 *
327 * \param rwlock the read/write lock to lock
328 *
329 * \since This function is available since SDL 3.0.0.
330 *
331 * \sa SDL_UnlockRWLock
332 */
334
335/**
336 * Lock the read/write lock for _write_ operations.
337 *
338 * This will block until the rwlock is available, which is to say it is not
339 * locked for reading or writing by any other thread. Only one thread may hold
340 * the lock when it requests write access; all other threads, whether they
341 * also want to write or only want read-only access, must wait until the
342 * writer thread has released the lock.
343 *
344 * It is illegal for the owning thread to lock an already-locked rwlock for
345 * writing (read-only may be locked recursively, writing can not). Doing so
346 * results in undefined behavior.
347 *
348 * It is illegal to request a write lock from a thread that already holds a
349 * read-only lock. Doing so results in undefined behavior. Unlock the
350 * read-only lock before requesting a write lock.
351 *
352 * This function does not fail; if rwlock is NULL, it will return immediately
353 * having locked nothing. If the rwlock is valid, this function will always
354 * block until it can lock the mutex, and return with it locked.
355 *
356 * \param rwlock the read/write lock to lock
357 *
358 * \since This function is available since SDL 3.0.0.
359 *
360 * \sa SDL_UnlockRWLock
361 */
363
364/**
365 * Try to lock a read/write lock _for reading_ without blocking.
366 *
367 * This works just like SDL_LockRWLockForReading(), but if the rwlock is not
368 * available, then this function returns `SDL_RWLOCK_TIMEDOUT` immediately.
369 *
370 * This technique is useful if you need access to a resource but don't want to
371 * wait for it, and will return to it to try again later.
372 *
373 * Trying to lock for read-only access can succeed if other threads are
374 * holding read-only locks, as this won't prevent access.
375 *
376 * This function does not fail; if rwlock is NULL, it will return 0
377 * immediately having locked nothing. If rwlock is valid, this function will
378 * always either lock the rwlock and return 0, or return SDL_RWLOCK_TIMEOUT
379 * and lock nothing.
380 *
381 * \param rwlock the rwlock to try to lock
382 * \returns 0 or `SDL_RWLOCK_TIMEDOUT`
383 *
384 * \since This function is available since SDL 3.0.0.
385 *
386 * \sa SDL_CreateRWLock
387 * \sa SDL_DestroyRWLock
388 * \sa SDL_TryLockRWLockForReading
389 * \sa SDL_UnlockRWLock
390 */
392
393/**
394 * Try to lock a read/write lock _for writing_ without blocking.
395 *
396 * This works just like SDL_LockRWLockForWriting(), but if the rwlock is not
397 * available, this function returns `SDL_RWLOCK_TIMEDOUT` immediately.
398 *
399 * This technique is useful if you need exclusive access to a resource but
400 * don't want to wait for it, and will return to it to try again later.
401 *
402 * It is illegal for the owning thread to lock an already-locked rwlock for
403 * writing (read-only may be locked recursively, writing can not). Doing so
404 * results in undefined behavior.
405 *
406 * It is illegal to request a write lock from a thread that already holds a
407 * read-only lock. Doing so results in undefined behavior. Unlock the
408 * read-only lock before requesting a write lock.
409 *
410 * This function does not fail; if rwlock is NULL, it will return 0
411 * immediately having locked nothing. If rwlock is valid, this function will
412 * always either lock the rwlock and return 0, or return SDL_RWLOCK_TIMEOUT
413 * and lock nothing.
414 *
415 * \param rwlock the rwlock to try to lock
416 * \returns 0 or `SDL_RWLOCK_TIMEDOUT`
417 *
418 * \since This function is available since SDL 3.0.0.
419 *
420 * \sa SDL_CreateRWLock
421 * \sa SDL_DestroyRWLock
422 * \sa SDL_TryLockRWLockForWriting
423 * \sa SDL_UnlockRWLock
424 */
426
427/**
428 * Unlock the read/write lock.
429 *
430 * Use this function to unlock the rwlock, whether it was locked for read-only
431 * or write operations.
432 *
433 * It is legal for the owning thread to lock an already-locked read-only lock.
434 * It must unlock it the same number of times before it is actually made
435 * available for other threads in the system (this is known as a "recursive
436 * rwlock").
437 *
438 * It is illegal to unlock a rwlock that has not been locked by the current
439 * thread, and doing so results in undefined behavior.
440 *
441 * \param rwlock the rwlock to unlock.
442 *
443 * \since This function is available since SDL 3.0.0.
444 */
446
447/**
448 * Destroy a read/write lock created with SDL_CreateRWLock().
449 *
450 * This function must be called on any read/write lock that is no longer
451 * needed. Failure to destroy a rwlock will result in a system memory or
452 * resource leak. While it is safe to destroy a rwlock that is _unlocked_, it
453 * is not safe to attempt to destroy a locked rwlock, and may result in
454 * undefined behavior depending on the platform.
455 *
456 * \param rwlock the rwlock to destroy
457 *
458 * \since This function is available since SDL 3.0.0.
459 *
460 * \sa SDL_CreateRWLock
461 * \sa SDL_LockRWLockForReading
462 * \sa SDL_LockRWLockForWriting
463 * \sa SDL_TryLockRWLockForReading
464 * \sa SDL_TryLockRWLockForWriting
465 * \sa SDL_UnlockRWLock
466 */
467extern DECLSPEC void SDLCALL SDL_DestroyRWLock(SDL_RWLock *rwlock);
468
469/* @} *//* Read/write lock functions */
470
471
472/**
473 * \name Semaphore functions
474 */
475/* @{ */
476
477/* The SDL semaphore structure, defined in SDL_syssem.c */
478struct SDL_Semaphore;
480
481/**
482 * Create a semaphore.
483 *
484 * This function creates a new semaphore and initializes it with the value
485 * `initial_value`. Each wait operation on the semaphore will atomically
486 * decrement the semaphore value and potentially block if the semaphore value
487 * is 0. Each post operation will atomically increment the semaphore value and
488 * wake waiting threads and allow them to retry the wait operation.
489 *
490 * \param initial_value the starting value of the semaphore
491 * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
492 * information.
493 *
494 * \since This function is available since SDL 3.0.0.
495 *
496 * \sa SDL_DestroySemaphore
497 * \sa SDL_PostSemaphore
498 * \sa SDL_TryWaitSemaphore
499 * \sa SDL_GetSemaphoreValue
500 * \sa SDL_WaitSemaphore
501 * \sa SDL_WaitSemaphoreTimeout
502 */
503extern DECLSPEC SDL_Semaphore *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
504
505/**
506 * Destroy a semaphore.
507 *
508 * It is not safe to destroy a semaphore if there are threads currently
509 * waiting on it.
510 *
511 * \param sem the semaphore to destroy
512 *
513 * \since This function is available since SDL 3.0.0.
514 *
515 * \sa SDL_CreateSemaphore
516 * \sa SDL_PostSemaphore
517 * \sa SDL_TryWaitSemaphore
518 * \sa SDL_GetSemaphoreValue
519 * \sa SDL_WaitSemaphore
520 * \sa SDL_WaitSemaphoreTimeout
521 */
522extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_Semaphore *sem);
523
524/**
525 * Wait until a semaphore has a positive value and then decrements it.
526 *
527 * This function suspends the calling thread until either the semaphore
528 * pointed to by `sem` has a positive value or the call is interrupted by a
529 * signal or error. If the call is successful it will atomically decrement the
530 * semaphore value.
531 *
532 * This function is the equivalent of calling SDL_WaitSemaphoreTimeout() with
533 * a time length of -1.
534 *
535 * \param sem the semaphore wait on
536 * \returns 0 on success or a negative error code on failure; call
537 * SDL_GetError() for more information.
538 *
539 * \since This function is available since SDL 3.0.0.
540 *
541 * \sa SDL_CreateSemaphore
542 * \sa SDL_DestroySemaphore
543 * \sa SDL_PostSemaphore
544 * \sa SDL_TryWaitSemaphore
545 * \sa SDL_GetSemaphoreValue
546 * \sa SDL_WaitSemaphore
547 * \sa SDL_WaitSemaphoreTimeout
548 */
549extern DECLSPEC int SDLCALL SDL_WaitSemaphore(SDL_Semaphore *sem);
550
551/**
552 * See if a semaphore has a positive value and decrement it if it does.
553 *
554 * This function checks to see if the semaphore pointed to by `sem` has a
555 * positive value and atomically decrements the semaphore value if it does. If
556 * the semaphore doesn't have a positive value, the function immediately
557 * returns SDL_MUTEX_TIMEDOUT.
558 *
559 * \param sem the semaphore to wait on
560 * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
561 * block, or a negative error code on failure; call SDL_GetError()
562 * for more information.
563 *
564 * \since This function is available since SDL 3.0.0.
565 *
566 * \sa SDL_CreateSemaphore
567 * \sa SDL_DestroySemaphore
568 * \sa SDL_PostSemaphore
569 * \sa SDL_GetSemaphoreValue
570 * \sa SDL_WaitSemaphore
571 * \sa SDL_WaitSemaphoreTimeout
572 */
573extern DECLSPEC int SDLCALL SDL_TryWaitSemaphore(SDL_Semaphore *sem);
574
575/**
576 * Wait until a semaphore has a positive value and then decrements it.
577 *
578 * This function suspends the calling thread until either the semaphore
579 * pointed to by `sem` has a positive value, the call is interrupted by a
580 * signal or error, or the specified time has elapsed. If the call is
581 * successful it will atomically decrement the semaphore value.
582 *
583 * \param sem the semaphore to wait on
584 * \param timeoutMS the length of the timeout, in milliseconds
585 * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
586 * succeed in the allotted time, or a negative error code on failure;
587 * call SDL_GetError() for more information.
588 *
589 * \since This function is available since SDL 3.0.0.
590 *
591 * \sa SDL_CreateSemaphore
592 * \sa SDL_DestroySemaphore
593 * \sa SDL_PostSemaphore
594 * \sa SDL_TryWaitSemaphore
595 * \sa SDL_GetSemaphoreValue
596 * \sa SDL_WaitSemaphore
597 */
598extern DECLSPEC int SDLCALL SDL_WaitSemaphoreTimeout(SDL_Semaphore *sem, Sint32 timeoutMS);
599
600/**
601 * Atomically increment a semaphore's value and wake waiting threads.
602 *
603 * \param sem the semaphore to increment
604 * \returns 0 on success or a negative error code on failure; call
605 * SDL_GetError() for more information.
606 *
607 * \since This function is available since SDL 3.0.0.
608 *
609 * \sa SDL_CreateSemaphore
610 * \sa SDL_DestroySemaphore
611 * \sa SDL_TryWaitSemaphore
612 * \sa SDL_GetSemaphoreValue
613 * \sa SDL_WaitSemaphore
614 * \sa SDL_WaitSemaphoreTimeout
615 */
616extern DECLSPEC int SDLCALL SDL_PostSemaphore(SDL_Semaphore *sem);
617
618/**
619 * Get the current value of a semaphore.
620 *
621 * \param sem the semaphore to query
622 * \returns the current value of the semaphore.
623 *
624 * \since This function is available since SDL 3.0.0.
625 *
626 * \sa SDL_CreateSemaphore
627 */
628extern DECLSPEC Uint32 SDLCALL SDL_GetSemaphoreValue(SDL_Semaphore *sem);
629
630/* @} *//* Semaphore functions */
631
632
633/**
634 * \name Condition variable functions
635 */
636/* @{ */
637
638/* The SDL condition variable structure, defined in SDL_syscond.c */
639struct SDL_Condition;
641
642/**
643 * Create a condition variable.
644 *
645 * \returns a new condition variable or NULL on failure; call SDL_GetError()
646 * for more information.
647 *
648 * \since This function is available since SDL 3.0.0.
649 *
650 * \sa SDL_BroadcastCondition
651 * \sa SDL_SignalCondition
652 * \sa SDL_WaitCondition
653 * \sa SDL_WaitConditionTimeout
654 * \sa SDL_DestroyCondition
655 */
656extern DECLSPEC SDL_Condition *SDLCALL SDL_CreateCondition(void);
657
658/**
659 * Destroy a condition variable.
660 *
661 * \param cond the condition variable to destroy
662 *
663 * \since This function is available since SDL 3.0.0.
664 *
665 * \sa SDL_BroadcastCondition
666 * \sa SDL_SignalCondition
667 * \sa SDL_WaitCondition
668 * \sa SDL_WaitConditionTimeout
669 * \sa SDL_CreateCondition
670 */
671extern DECLSPEC void SDLCALL SDL_DestroyCondition(SDL_Condition *cond);
672
673/**
674 * Restart one of the threads that are waiting on the condition variable.
675 *
676 * \param cond the condition variable to signal
677 * \returns 0 on success or a negative error code on failure; call
678 * SDL_GetError() for more information.
679 *
680 * \since This function is available since SDL 3.0.0.
681 *
682 * \sa SDL_BroadcastCondition
683 * \sa SDL_WaitCondition
684 * \sa SDL_WaitConditionTimeout
685 * \sa SDL_CreateCondition
686 * \sa SDL_DestroyCondition
687 */
688extern DECLSPEC int SDLCALL SDL_SignalCondition(SDL_Condition *cond);
689
690/**
691 * Restart all threads that are waiting on the condition variable.
692 *
693 * \param cond the condition variable to signal
694 * \returns 0 on success or a negative error code on failure; call
695 * SDL_GetError() for more information.
696 *
697 * \since This function is available since SDL 3.0.0.
698 *
699 * \sa SDL_SignalCondition
700 * \sa SDL_WaitCondition
701 * \sa SDL_WaitConditionTimeout
702 * \sa SDL_CreateCondition
703 * \sa SDL_DestroyCondition
704 */
705extern DECLSPEC int SDLCALL SDL_BroadcastCondition(SDL_Condition *cond);
706
707/**
708 * Wait until a condition variable is signaled.
709 *
710 * This function unlocks the specified `mutex` and waits for another thread to
711 * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
712 * variable `cond`. Once the condition variable is signaled, the mutex is
713 * re-locked and the function returns.
714 *
715 * The mutex must be locked before calling this function. Locking the mutex
716 * recursively (more than once) is not supported and leads to undefined
717 * behavior.
718 *
719 * This function is the equivalent of calling SDL_WaitConditionTimeout() with
720 * a time length of -1.
721 *
722 * \param cond the condition variable to wait on
723 * \param mutex the mutex used to coordinate thread access
724 * \returns 0 when it is signaled or a negative error code on failure; call
725 * SDL_GetError() for more information.
726 *
727 * \since This function is available since SDL 3.0.0.
728 *
729 * \sa SDL_BroadcastCondition
730 * \sa SDL_SignalCondition
731 * \sa SDL_WaitConditionTimeout
732 * \sa SDL_CreateCondition
733 * \sa SDL_DestroyCondition
734 */
735extern DECLSPEC int SDLCALL SDL_WaitCondition(SDL_Condition *cond, SDL_Mutex *mutex);
736
737/**
738 * Wait until a condition variable is signaled or a certain time has passed.
739 *
740 * This function unlocks the specified `mutex` and waits for another thread to
741 * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
742 * variable `cond`, or for the specified time to elapse. Once the condition
743 * variable is signaled or the time elapsed, the mutex is re-locked and the
744 * function returns.
745 *
746 * The mutex must be locked before calling this function. Locking the mutex
747 * recursively (more than once) is not supported and leads to undefined
748 * behavior.
749 *
750 * \param cond the condition variable to wait on
751 * \param mutex the mutex used to coordinate thread access
752 * \param timeoutMS the maximum time to wait, in milliseconds, or -1 to wait
753 * indefinitely
754 * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
755 * the condition is not signaled in the allotted time, or a negative
756 * error code on failure; call SDL_GetError() for more information.
757 *
758 * \since This function is available since SDL 3.0.0.
759 *
760 * \sa SDL_BroadcastCondition
761 * \sa SDL_SignalCondition
762 * \sa SDL_WaitCondition
763 * \sa SDL_CreateCondition
764 * \sa SDL_DestroyCondition
765 */
766extern DECLSPEC int SDLCALL SDL_WaitConditionTimeout(SDL_Condition *cond,
767 SDL_Mutex *mutex, Sint32 timeoutMS);
768
769/* @} *//* Condition variable functions */
770
771
772/* Ends C function definitions when using C++ */
773#ifdef __cplusplus
774}
775#endif
776#include <SDL3/SDL_close_code.h>
777
778#endif /* SDL_mutex_h_ */
void SDL_DestroyRWLock(SDL_RWLock *rwlock)
int rwlock
Definition SDL_mutex.h:391
#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:132
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:479
int mutex
Definition SDL_mutex.h:201
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:249
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:640
int32_t Sint32
Definition SDL_stdinc.h:167
uint32_t Uint32
Definition SDL_stdinc.h:173