SDL_mutex.h 36 KB

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