SDL_mutex.h 25 KB

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