SDL_mutex.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #ifndef SDL_mutex_h_
  19. #define SDL_mutex_h_
  20. /**
  21. * # CategoryMutex
  22. *
  23. * Functions to provide thread synchronization primitives.
  24. */
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. /******************************************************************************/
  28. /* Enable thread safety attributes only with clang.
  29. * The attributes can be safely erased when compiling with other compilers.
  30. *
  31. * To enable analysis, set these environment variables before running cmake:
  32. * export CC=clang
  33. * export CFLAGS="-DSDL_THREAD_SAFETY_ANALYSIS -Wthread-safety"
  34. */
  35. #if defined(SDL_THREAD_SAFETY_ANALYSIS) && \
  36. defined(__clang__) && (!defined(SWIG))
  37. #define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
  38. #else
  39. #define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x) /* no-op */
  40. #endif
  41. #define SDL_CAPABILITY(x) \
  42. SDL_THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
  43. #define SDL_SCOPED_CAPABILITY \
  44. SDL_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
  45. #define SDL_GUARDED_BY(x) \
  46. SDL_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
  47. #define SDL_PT_GUARDED_BY(x) \
  48. SDL_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
  49. #define SDL_ACQUIRED_BEFORE(x) \
  50. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))
  51. #define SDL_ACQUIRED_AFTER(x) \
  52. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))
  53. #define SDL_REQUIRES(x) \
  54. SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(x))
  55. #define SDL_REQUIRES_SHARED(x) \
  56. SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(x))
  57. #define SDL_ACQUIRE(x) \
  58. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(x))
  59. #define SDL_ACQUIRE_SHARED(x) \
  60. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(x))
  61. #define SDL_RELEASE(x) \
  62. SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(x))
  63. #define SDL_RELEASE_SHARED(x) \
  64. SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(x))
  65. #define SDL_RELEASE_GENERIC(x) \
  66. SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(x))
  67. #define SDL_TRY_ACQUIRE(x, y) \
  68. SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(x, y))
  69. #define SDL_TRY_ACQUIRE_SHARED(x, y) \
  70. SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(x, y))
  71. #define SDL_EXCLUDES(x) \
  72. SDL_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(x))
  73. #define SDL_ASSERT_CAPABILITY(x) \
  74. SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
  75. #define SDL_ASSERT_SHARED_CAPABILITY(x) \
  76. SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
  77. #define SDL_RETURN_CAPABILITY(x) \
  78. SDL_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
  79. #define SDL_NO_THREAD_SAFETY_ANALYSIS \
  80. SDL_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
  81. /******************************************************************************/
  82. #include <SDL3/SDL_begin_code.h>
  83. /* Set up for C function definitions, even when using C++ */
  84. #ifdef __cplusplus
  85. extern "C" {
  86. #endif
  87. /**
  88. * Synchronization functions return this value if they time out.
  89. *
  90. * Not all functions _can_ time out; some will block indefinitely.
  91. *
  92. * \since This macro is available since SDL 3.0.0.
  93. */
  94. #define SDL_MUTEX_TIMEDOUT 1
  95. /**
  96. * \name Mutex functions
  97. */
  98. /* @{ */
  99. /**
  100. * A means to serialize access to a resource between threads.
  101. *
  102. * Mutexes (short for "mutual exclusion") are a synchronization primitive that
  103. * allows exactly one thread to proceed at a time.
  104. *
  105. * Wikipedia has a thorough explanation of the concept:
  106. *
  107. * https://en.wikipedia.org/wiki/Mutex
  108. *
  109. * \since This struct is available since SDL 3.0.0.
  110. */
  111. typedef struct SDL_Mutex SDL_Mutex;
  112. /**
  113. * Create a new mutex.
  114. *
  115. * All newly-created mutexes begin in the _unlocked_ state.
  116. *
  117. * Calls to SDL_LockMutex() will not return while the mutex is locked by
  118. * another thread. See SDL_TryLockMutex() to attempt to lock without blocking.
  119. *
  120. * SDL mutexes are reentrant.
  121. *
  122. * \returns the initialized and unlocked mutex or NULL on failure; call
  123. * SDL_GetError() for more information.
  124. *
  125. * \since This function is available since SDL 3.0.0.
  126. *
  127. * \sa SDL_DestroyMutex
  128. * \sa SDL_LockMutex
  129. * \sa SDL_TryLockMutex
  130. * \sa SDL_UnlockMutex
  131. */
  132. extern SDL_DECLSPEC SDL_Mutex * SDLCALL SDL_CreateMutex(void);
  133. /**
  134. * Lock the mutex.
  135. *
  136. * This will block until the mutex is available, which is to say it is in the
  137. * unlocked state and the OS has chosen the caller as the next thread to lock
  138. * it. Of all threads waiting to lock the mutex, only one may do so at a time.
  139. *
  140. * It is legal for the owning thread to lock an already-locked mutex. It must
  141. * unlock it the same number of times before it is actually made available for
  142. * other threads in the system (this is known as a "recursive mutex").
  143. *
  144. * This function does not fail; if mutex is NULL, it will return immediately
  145. * having locked nothing. If the mutex is valid, this function will always
  146. * block until it can lock the mutex, and return with it locked.
  147. *
  148. * \param mutex the mutex to lock.
  149. *
  150. * \since This function is available since SDL 3.0.0.
  151. *
  152. * \sa SDL_TryLockMutex
  153. * \sa SDL_UnlockMutex
  154. */
  155. extern SDL_DECLSPEC void SDLCALL SDL_LockMutex(SDL_Mutex *mutex) SDL_ACQUIRE(mutex);
  156. /**
  157. * Try to lock a mutex without blocking.
  158. *
  159. * This works just like SDL_LockMutex(), but if the mutex is not available,
  160. * this function returns `SDL_MUTEX_TIMEDOUT` immediately.
  161. *
  162. * This technique is useful if you need exclusive access to a resource but
  163. * don't want to wait for it, and will return to it to try again later.
  164. *
  165. * This function does not fail; if mutex is NULL, it will return 0 immediately
  166. * having locked nothing. If the mutex is valid, this function will always
  167. * either lock the mutex and return 0, or return SDL_MUTEX_TIMEOUT and lock
  168. * nothing.
  169. *
  170. * \param mutex the mutex to try to lock.
  171. * \returns 0 or `SDL_MUTEX_TIMEDOUT`.
  172. *
  173. * \since This function is available since SDL 3.0.0.
  174. *
  175. * \sa SDL_LockMutex
  176. * \sa SDL_UnlockMutex
  177. */
  178. extern SDL_DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_Mutex *mutex) SDL_TRY_ACQUIRE(0, mutex);
  179. /**
  180. * Unlock the mutex.
  181. *
  182. * It is legal for the owning thread to lock an already-locked mutex. It must
  183. * unlock it the same number of times before it is actually made available for
  184. * other threads in the system (this is known as a "recursive mutex").
  185. *
  186. * It is illegal to unlock a mutex that has not been locked by the current
  187. * thread, and doing so results in undefined behavior.
  188. *
  189. * \param mutex the mutex to unlock.
  190. *
  191. * \since This function is available since SDL 3.0.0.
  192. *
  193. * \sa SDL_LockMutex
  194. * \sa SDL_TryLockMutex
  195. */
  196. extern SDL_DECLSPEC void SDLCALL SDL_UnlockMutex(SDL_Mutex *mutex) SDL_RELEASE(mutex);
  197. /**
  198. * Destroy a mutex created with SDL_CreateMutex().
  199. *
  200. * This function must be called on any mutex that is no longer needed. Failure
  201. * to destroy a mutex will result in a system memory or resource leak. While
  202. * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
  203. * to destroy a locked mutex, and may result in undefined behavior depending
  204. * on the platform.
  205. *
  206. * \param mutex the mutex to destroy.
  207. *
  208. * \since This function is available since SDL 3.0.0.
  209. *
  210. * \sa SDL_CreateMutex
  211. */
  212. extern SDL_DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_Mutex *mutex);
  213. /* @} *//* Mutex functions */
  214. /**
  215. * \name Read/write lock functions
  216. */
  217. /* @{ */
  218. /**
  219. * A mutex that allows read-only threads to run in parallel.
  220. *
  221. * A rwlock is roughly the same concept as SDL_Mutex, but allows threads that
  222. * request read-only access to all hold the lock at the same time. If a thread
  223. * requests write access, it will block until all read-only threads have
  224. * released the lock, and no one else can hold the thread (for reading or
  225. * writing) at the same time as the writing thread.
  226. *
  227. * This can be more efficient in cases where several threads need to access
  228. * data frequently, but changes to that data are rare.
  229. *
  230. * There are other rules that apply to rwlocks that don't apply to mutexes,
  231. * about how threads are scheduled and when they can be recursively locked.
  232. * These are documented in the other rwlock functions.
  233. *
  234. * \since This struct is available since SDL 3.0.0.
  235. */
  236. typedef struct SDL_RWLock SDL_RWLock;
  237. /*
  238. * Synchronization functions return this value if they time out.
  239. *
  240. * Not all functions _can_ time out; some will block indefinitely.
  241. *
  242. * This symbol is just for clarity when dealing with SDL_RWLock
  243. * functions; its value is equivalent to SDL_MUTEX_TIMEOUT.
  244. *
  245. * \since This macro is available since SDL 3.0.0.
  246. */
  247. #define SDL_RWLOCK_TIMEDOUT SDL_MUTEX_TIMEDOUT
  248. /**
  249. * Create a new read/write lock.
  250. *
  251. * A read/write lock is useful for situations where you have multiple threads
  252. * trying to access a resource that is rarely updated. All threads requesting
  253. * a read-only lock will be allowed to run in parallel; if a thread requests a
  254. * write lock, it will be provided exclusive access. This makes it safe for
  255. * multiple threads to use a resource at the same time if they promise not to
  256. * change it, and when it has to be changed, the rwlock will serve as a
  257. * gateway to make sure those changes can be made safely.
  258. *
  259. * In the right situation, a rwlock can be more efficient than a mutex, which
  260. * only lets a single thread proceed at a time, even if it won't be modifying
  261. * the data.
  262. *
  263. * All newly-created read/write locks begin in the _unlocked_ state.
  264. *
  265. * Calls to SDL_LockRWLockForReading() and SDL_LockRWLockForWriting will not
  266. * return while the rwlock is locked _for writing_ by another thread. See
  267. * SDL_TryLockRWLockForReading() and SDL_TryLockRWLockForWriting() to attempt
  268. * to lock without blocking.
  269. *
  270. * SDL read/write locks are only recursive for read-only locks! They are not
  271. * guaranteed to be fair, or provide access in a FIFO manner! They are not
  272. * guaranteed to favor writers. You may not lock a rwlock for both read-only
  273. * and write access at the same time from the same thread (so you can't
  274. * promote your read-only lock to a write lock without unlocking first).
  275. *
  276. * \returns the initialized and unlocked read/write lock or NULL on failure;
  277. * call SDL_GetError() for more information.
  278. *
  279. * \since This function is available since SDL 3.0.0.
  280. *
  281. * \sa SDL_DestroyRWLock
  282. * \sa SDL_LockRWLockForReading
  283. * \sa SDL_LockRWLockForWriting
  284. * \sa SDL_TryLockRWLockForReading
  285. * \sa SDL_TryLockRWLockForWriting
  286. * \sa SDL_UnlockRWLock
  287. */
  288. extern SDL_DECLSPEC SDL_RWLock * SDLCALL SDL_CreateRWLock(void);
  289. /**
  290. * Lock the read/write lock for _read only_ operations.
  291. *
  292. * This will block until the rwlock is available, which is to say it is not
  293. * locked for writing by any other thread. Of all threads waiting to lock the
  294. * rwlock, all may do so at the same time as long as they are requesting
  295. * read-only access; if a thread wants to lock for writing, only one may do so
  296. * at a time, and no other threads, read-only or not, may hold the lock at the
  297. * same time.
  298. *
  299. * It is legal for the owning thread to lock an already-locked rwlock for
  300. * reading. It must unlock it the same number of times before it is actually
  301. * made available for other threads in the system (this is known as a
  302. * "recursive rwlock").
  303. *
  304. * Note that locking for writing is not recursive (this is only available to
  305. * read-only locks).
  306. *
  307. * It is illegal to request a read-only lock from a thread that already holds
  308. * the write lock. Doing so results in undefined behavior. Unlock the write
  309. * lock before requesting a read-only lock. (But, of course, if you have the
  310. * write lock, you don't need further locks to read in any case.)
  311. *
  312. * This function does not fail; if rwlock is NULL, it will return immediately
  313. * having locked nothing. If the rwlock is valid, this function will always
  314. * block until it can lock the mutex, and return with it locked.
  315. *
  316. * \param rwlock the read/write lock to lock.
  317. *
  318. * \since This function is available since SDL 3.0.0.
  319. *
  320. * \sa SDL_LockRWLockForWriting
  321. * \sa SDL_TryLockRWLockForReading
  322. * \sa SDL_UnlockRWLock
  323. */
  324. extern SDL_DECLSPEC void SDLCALL SDL_LockRWLockForReading(SDL_RWLock *rwlock) SDL_ACQUIRE_SHARED(rwlock);
  325. /**
  326. * Lock the read/write lock for _write_ operations.
  327. *
  328. * This will block until the rwlock is available, which is to say it is not
  329. * locked for reading or writing by any other thread. Only one thread may hold
  330. * the lock when it requests write access; all other threads, whether they
  331. * also want to write or only want read-only access, must wait until the
  332. * writer thread has released the lock.
  333. *
  334. * It is illegal for the owning thread to lock an already-locked rwlock for
  335. * writing (read-only may be locked recursively, writing can not). Doing so
  336. * results in undefined behavior.
  337. *
  338. * It is illegal to request a write lock from a thread that already holds a
  339. * read-only lock. Doing so results in undefined behavior. Unlock the
  340. * read-only lock before requesting a write lock.
  341. *
  342. * This function does not fail; if rwlock is NULL, it will return immediately
  343. * having locked nothing. If the rwlock is valid, this function will always
  344. * block until it can lock the mutex, and return with it locked.
  345. *
  346. * \param rwlock the read/write lock to lock.
  347. *
  348. * \since This function is available since SDL 3.0.0.
  349. *
  350. * \sa SDL_LockRWLockForReading
  351. * \sa SDL_TryLockRWLockForWriting
  352. * \sa SDL_UnlockRWLock
  353. */
  354. extern SDL_DECLSPEC void SDLCALL SDL_LockRWLockForWriting(SDL_RWLock *rwlock) SDL_ACQUIRE(rwlock);
  355. /**
  356. * Try to lock a read/write lock _for reading_ without blocking.
  357. *
  358. * This works just like SDL_LockRWLockForReading(), but if the rwlock is not
  359. * available, then this function returns `SDL_RWLOCK_TIMEDOUT` immediately.
  360. *
  361. * This technique is useful if you need access to a resource but don't want to
  362. * wait for it, and will return to it to try again later.
  363. *
  364. * Trying to lock for read-only access can succeed if other threads are
  365. * holding read-only locks, as this won't prevent access.
  366. *
  367. * This function does not fail; if rwlock is NULL, it will return 0
  368. * immediately having locked nothing. If rwlock is valid, this function will
  369. * always either lock the rwlock and return 0, or return SDL_RWLOCK_TIMEOUT
  370. * and lock nothing.
  371. *
  372. * \param rwlock the rwlock to try to lock.
  373. * \returns 0 or `SDL_RWLOCK_TIMEDOUT`.
  374. *
  375. * \since This function is available since SDL 3.0.0.
  376. *
  377. * \sa SDL_LockRWLockForReading
  378. * \sa SDL_TryLockRWLockForWriting
  379. * \sa SDL_UnlockRWLock
  380. */
  381. extern SDL_DECLSPEC int SDLCALL SDL_TryLockRWLockForReading(SDL_RWLock *rwlock) SDL_TRY_ACQUIRE_SHARED(0, rwlock);
  382. /**
  383. * Try to lock a read/write lock _for writing_ without blocking.
  384. *
  385. * This works just like SDL_LockRWLockForWriting(), but if the rwlock is not
  386. * available, this function returns `SDL_RWLOCK_TIMEDOUT` immediately.
  387. *
  388. * This technique is useful if you need exclusive access to a resource but
  389. * don't want to wait for it, and will return to it to try again later.
  390. *
  391. * It is illegal for the owning thread to lock an already-locked rwlock for
  392. * writing (read-only may be locked recursively, writing can not). Doing so
  393. * results in undefined behavior.
  394. *
  395. * It is illegal to request a write lock from a thread that already holds a
  396. * read-only lock. Doing so results in undefined behavior. Unlock the
  397. * read-only lock before requesting a write lock.
  398. *
  399. * This function does not fail; if rwlock is NULL, it will return 0
  400. * immediately having locked nothing. If rwlock is valid, this function will
  401. * always either lock the rwlock and return 0, or return SDL_RWLOCK_TIMEOUT
  402. * and lock nothing.
  403. *
  404. * \param rwlock the rwlock to try to lock.
  405. * \returns 0 or `SDL_RWLOCK_TIMEDOUT`.
  406. *
  407. * \since This function is available since SDL 3.0.0.
  408. *
  409. * \sa SDL_LockRWLockForWriting
  410. * \sa SDL_TryLockRWLockForReading
  411. * \sa SDL_UnlockRWLock
  412. */
  413. extern SDL_DECLSPEC int SDLCALL SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock) SDL_TRY_ACQUIRE(0, rwlock);
  414. /**
  415. * Unlock the read/write lock.
  416. *
  417. * Use this function to unlock the rwlock, whether it was locked for read-only
  418. * or write operations.
  419. *
  420. * It is legal for the owning thread to lock an already-locked read-only lock.
  421. * It must unlock it the same number of times before it is actually made
  422. * available for other threads in the system (this is known as a "recursive
  423. * rwlock").
  424. *
  425. * It is illegal to unlock a rwlock that has not been locked by the current
  426. * thread, and doing so results in undefined behavior.
  427. *
  428. * \param rwlock the rwlock to unlock.
  429. *
  430. * \since This function is available since SDL 3.0.0.
  431. *
  432. * \sa SDL_LockRWLockForReading
  433. * \sa SDL_LockRWLockForWriting
  434. * \sa SDL_TryLockRWLockForReading
  435. * \sa SDL_TryLockRWLockForWriting
  436. */
  437. extern SDL_DECLSPEC void SDLCALL SDL_UnlockRWLock(SDL_RWLock *rwlock) SDL_RELEASE_GENERIC(rwlock);
  438. /**
  439. * Destroy a read/write lock created with SDL_CreateRWLock().
  440. *
  441. * This function must be called on any read/write lock that is no longer
  442. * needed. Failure to destroy a rwlock will result in a system memory or
  443. * resource leak. While it is safe to destroy a rwlock that is _unlocked_, it
  444. * is not safe to attempt to destroy a locked rwlock, and may result in
  445. * undefined behavior depending on the platform.
  446. *
  447. * \param rwlock the rwlock to destroy.
  448. *
  449. * \since This function is available since SDL 3.0.0.
  450. *
  451. * \sa SDL_CreateRWLock
  452. */
  453. extern SDL_DECLSPEC void SDLCALL SDL_DestroyRWLock(SDL_RWLock *rwlock);
  454. /* @} *//* Read/write lock functions */
  455. /**
  456. * \name Semaphore functions
  457. */
  458. /* @{ */
  459. /**
  460. * A means to manage access to a resource, by count, between threads.
  461. *
  462. * Semaphores (specifically, "counting semaphores"), let X number of threads
  463. * request access at the same time, each thread granted access decrementing a
  464. * counter. When the counter reaches zero, future requests block until a prior
  465. * thread releases their request, incrementing the counter again.
  466. *
  467. * Wikipedia has a thorough explanation of the concept:
  468. *
  469. * https://en.wikipedia.org/wiki/Semaphore_(programming)
  470. *
  471. * \since This struct is available since SDL 3.0.0.
  472. */
  473. typedef struct SDL_Semaphore SDL_Semaphore;
  474. /**
  475. * Create a semaphore.
  476. *
  477. * This function creates a new semaphore and initializes it with the value
  478. * `initial_value`. Each wait operation on the semaphore will atomically
  479. * decrement the semaphore value and potentially block if the semaphore value
  480. * is 0. Each post operation will atomically increment the semaphore value and
  481. * wake waiting threads and allow them to retry the wait operation.
  482. *
  483. * \param initial_value the starting value of the semaphore.
  484. * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
  485. * information.
  486. *
  487. * \since This function is available since SDL 3.0.0.
  488. *
  489. * \sa SDL_DestroySemaphore
  490. * \sa SDL_SignalSemaphore
  491. * \sa SDL_TryWaitSemaphore
  492. * \sa SDL_GetSemaphoreValue
  493. * \sa SDL_WaitSemaphore
  494. * \sa SDL_WaitSemaphoreTimeout
  495. */
  496. extern SDL_DECLSPEC SDL_Semaphore * SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
  497. /**
  498. * Destroy a semaphore.
  499. *
  500. * It is not safe to destroy a semaphore if there are threads currently
  501. * waiting on it.
  502. *
  503. * \param sem the semaphore to destroy.
  504. *
  505. * \since This function is available since SDL 3.0.0.
  506. *
  507. * \sa SDL_CreateSemaphore
  508. */
  509. extern SDL_DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_Semaphore *sem);
  510. /**
  511. * Wait until a semaphore has a positive value and then decrements it.
  512. *
  513. * This function suspends the calling thread until either the semaphore
  514. * pointed to by `sem` has a positive value or the call is interrupted by a
  515. * signal or error. If the call is successful it will atomically decrement the
  516. * semaphore value.
  517. *
  518. * This function is the equivalent of calling SDL_WaitSemaphoreTimeout() with
  519. * a time length of -1.
  520. *
  521. * \param sem the semaphore wait on.
  522. * \returns 0 on success or a negative error code on failure; call
  523. * SDL_GetError() for more information.
  524. *
  525. * \since This function is available since SDL 3.0.0.
  526. *
  527. * \sa SDL_SignalSemaphore
  528. * \sa SDL_TryWaitSemaphore
  529. * \sa SDL_WaitSemaphoreTimeout
  530. */
  531. extern SDL_DECLSPEC int SDLCALL SDL_WaitSemaphore(SDL_Semaphore *sem);
  532. /**
  533. * See if a semaphore has a positive value and decrement it if it does.
  534. *
  535. * This function checks to see if the semaphore pointed to by `sem` has a
  536. * positive value and atomically decrements the semaphore value if it does. If
  537. * the semaphore doesn't have a positive value, the function immediately
  538. * returns SDL_MUTEX_TIMEDOUT.
  539. *
  540. * \param sem the semaphore to wait on.
  541. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
  542. * block, or a negative error code on failure; call SDL_GetError()
  543. * for more information.
  544. *
  545. * \since This function is available since SDL 3.0.0.
  546. *
  547. * \sa SDL_SignalSemaphore
  548. * \sa SDL_WaitSemaphore
  549. * \sa SDL_WaitSemaphoreTimeout
  550. */
  551. extern SDL_DECLSPEC int SDLCALL SDL_TryWaitSemaphore(SDL_Semaphore *sem);
  552. /**
  553. * Wait until a semaphore has a positive value and then decrements it.
  554. *
  555. * This function suspends the calling thread until either the semaphore
  556. * pointed to by `sem` has a positive value, the call is interrupted by a
  557. * signal or error, or the specified time has elapsed. If the call is
  558. * successful it will atomically decrement the semaphore value.
  559. *
  560. * \param sem the semaphore to wait on.
  561. * \param timeoutMS the length of the timeout, in milliseconds.
  562. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
  563. * succeed in the allotted time, or a negative error code on failure;
  564. * call SDL_GetError() for more information.
  565. *
  566. * \since This function is available since SDL 3.0.0.
  567. *
  568. * \sa SDL_SignalSemaphore
  569. * \sa SDL_TryWaitSemaphore
  570. * \sa SDL_WaitSemaphore
  571. */
  572. extern SDL_DECLSPEC int SDLCALL SDL_WaitSemaphoreTimeout(SDL_Semaphore *sem, Sint32 timeoutMS);
  573. /**
  574. * Atomically increment a semaphore's value and wake waiting threads.
  575. *
  576. * \param sem the semaphore to increment.
  577. * \returns 0 on success or a negative error code on failure; call
  578. * SDL_GetError() for more information.
  579. *
  580. * \since This function is available since SDL 3.0.0.
  581. *
  582. * \sa SDL_TryWaitSemaphore
  583. * \sa SDL_WaitSemaphore
  584. * \sa SDL_WaitSemaphoreTimeout
  585. */
  586. extern SDL_DECLSPEC int SDLCALL SDL_SignalSemaphore(SDL_Semaphore *sem);
  587. /**
  588. * Get the current value of a semaphore.
  589. *
  590. * \param sem the semaphore to query.
  591. * \returns the current value of the semaphore.
  592. *
  593. * \since This function is available since SDL 3.0.0.
  594. */
  595. extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetSemaphoreValue(SDL_Semaphore *sem);
  596. /* @} *//* Semaphore functions */
  597. /**
  598. * \name Condition variable functions
  599. */
  600. /* @{ */
  601. /**
  602. * A means to block multiple threads until a condition is satisfied.
  603. *
  604. * Condition variables, paired with an SDL_Mutex, let an app halt multiple
  605. * threads until a condition has occurred, at which time the app can release
  606. * one or all waiting threads.
  607. *
  608. * Wikipedia has a thorough explanation of the concept:
  609. *
  610. * https://en.wikipedia.org/wiki/Condition_variable
  611. *
  612. * \since This struct is available since SDL 3.0.0.
  613. */
  614. typedef struct SDL_Condition SDL_Condition;
  615. /**
  616. * Create a condition variable.
  617. *
  618. * \returns a new condition variable or NULL on failure; call SDL_GetError()
  619. * for more information.
  620. *
  621. * \since This function is available since SDL 3.0.0.
  622. *
  623. * \sa SDL_BroadcastCondition
  624. * \sa SDL_SignalCondition
  625. * \sa SDL_WaitCondition
  626. * \sa SDL_WaitConditionTimeout
  627. * \sa SDL_DestroyCondition
  628. */
  629. extern SDL_DECLSPEC SDL_Condition * SDLCALL SDL_CreateCondition(void);
  630. /**
  631. * Destroy a condition variable.
  632. *
  633. * \param cond the condition variable to destroy.
  634. *
  635. * \since This function is available since SDL 3.0.0.
  636. *
  637. * \sa SDL_CreateCondition
  638. */
  639. extern SDL_DECLSPEC void SDLCALL SDL_DestroyCondition(SDL_Condition *cond);
  640. /**
  641. * Restart one of the threads that are waiting on the condition variable.
  642. *
  643. * \param cond the condition variable to signal.
  644. * \returns 0 on success or a negative error code on failure; call
  645. * SDL_GetError() for more information.
  646. *
  647. * \threadsafety It is safe to call this function from any thread.
  648. *
  649. * \since This function is available since SDL 3.0.0.
  650. *
  651. * \sa SDL_BroadcastCondition
  652. * \sa SDL_WaitCondition
  653. * \sa SDL_WaitConditionTimeout
  654. */
  655. extern SDL_DECLSPEC int SDLCALL SDL_SignalCondition(SDL_Condition *cond);
  656. /**
  657. * Restart all threads that are waiting on the condition variable.
  658. *
  659. * \param cond the condition variable to signal.
  660. * \returns 0 on success or a negative error code on failure; call
  661. * SDL_GetError() for more information.
  662. *
  663. * \threadsafety It is safe to call this function from any thread.
  664. *
  665. * \since This function is available since SDL 3.0.0.
  666. *
  667. * \sa SDL_SignalCondition
  668. * \sa SDL_WaitCondition
  669. * \sa SDL_WaitConditionTimeout
  670. */
  671. extern SDL_DECLSPEC int SDLCALL SDL_BroadcastCondition(SDL_Condition *cond);
  672. /**
  673. * Wait until a condition variable is signaled.
  674. *
  675. * This function unlocks the specified `mutex` and waits for another thread to
  676. * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
  677. * variable `cond`. Once the condition variable is signaled, the mutex is
  678. * re-locked and the function returns.
  679. *
  680. * The mutex must be locked before calling this function. Locking the mutex
  681. * recursively (more than once) is not supported and leads to undefined
  682. * behavior.
  683. *
  684. * This function is the equivalent of calling SDL_WaitConditionTimeout() with
  685. * a time length of -1.
  686. *
  687. * \param cond the condition variable to wait on.
  688. * \param mutex the mutex used to coordinate thread access.
  689. * \returns 0 when it is signaled or a negative error code on failure; call
  690. * SDL_GetError() for more information.
  691. *
  692. * \threadsafety It is safe to call this function from any thread.
  693. *
  694. * \since This function is available since SDL 3.0.0.
  695. *
  696. * \sa SDL_BroadcastCondition
  697. * \sa SDL_SignalCondition
  698. * \sa SDL_WaitConditionTimeout
  699. */
  700. extern SDL_DECLSPEC int SDLCALL SDL_WaitCondition(SDL_Condition *cond, SDL_Mutex *mutex);
  701. /**
  702. * Wait until a condition variable is signaled or a certain time has passed.
  703. *
  704. * This function unlocks the specified `mutex` and waits for another thread to
  705. * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
  706. * variable `cond`, or for the specified time to elapse. Once the condition
  707. * variable is signaled or the time elapsed, the mutex is re-locked and the
  708. * function returns.
  709. *
  710. * The mutex must be locked before calling this function. Locking the mutex
  711. * recursively (more than once) is not supported and leads to undefined
  712. * behavior.
  713. *
  714. * \param cond the condition variable to wait on.
  715. * \param mutex the mutex used to coordinate thread access.
  716. * \param timeoutMS the maximum time to wait, in milliseconds, or -1 to wait
  717. * indefinitely.
  718. * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
  719. * the condition is not signaled in the allotted time, or a negative
  720. * error code on failure; call SDL_GetError() for more information.
  721. *
  722. * \threadsafety It is safe to call this function from any thread.
  723. *
  724. * \since This function is available since SDL 3.0.0.
  725. *
  726. * \sa SDL_BroadcastCondition
  727. * \sa SDL_SignalCondition
  728. * \sa SDL_WaitCondition
  729. */
  730. extern SDL_DECLSPEC int SDLCALL SDL_WaitConditionTimeout(SDL_Condition *cond,
  731. SDL_Mutex *mutex, Sint32 timeoutMS);
  732. /* @} *//* Condition variable functions */
  733. /* Ends C function definitions when using C++ */
  734. #ifdef __cplusplus
  735. }
  736. #endif
  737. #include <SDL3/SDL_close_code.h>
  738. #endif /* SDL_mutex_h_ */