SDL_mutex.h 25 KB

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