SDL_mutex.h 26 KB

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