SDL_mutex.h 30 KB

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