1
0

SDL_mutex.h 30 KB

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