SDL_mutex.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 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. * 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. * \since This function is available since SDL 3.2.0.
  277. *
  278. * \sa SDL_DestroyMutex
  279. * \sa SDL_LockMutex
  280. * \sa SDL_TryLockMutex
  281. * \sa SDL_UnlockMutex
  282. */
  283. extern SDL_DECLSPEC SDL_Mutex * SDLCALL SDL_CreateMutex(void);
  284. /**
  285. * Lock the mutex.
  286. *
  287. * This will block until the mutex is available, which is to say it is in the
  288. * unlocked state and the OS has chosen the caller as the next thread to lock
  289. * it. Of all threads waiting to lock the mutex, only one may do so at a time.
  290. *
  291. * It is legal for the owning thread to lock an already-locked mutex. It must
  292. * unlock it the same number of times before it is actually made available for
  293. * other threads in the system (this is known as a "recursive mutex").
  294. *
  295. * This function does not fail; if mutex is NULL, it will return immediately
  296. * having locked nothing. If the mutex is valid, this function will always
  297. * block until it can lock the mutex, and return with it locked.
  298. *
  299. * \param mutex the mutex to lock.
  300. *
  301. * \since This function is available since SDL 3.2.0.
  302. *
  303. * \sa SDL_TryLockMutex
  304. * \sa SDL_UnlockMutex
  305. */
  306. extern SDL_DECLSPEC void SDLCALL SDL_LockMutex(SDL_Mutex *mutex) SDL_ACQUIRE(mutex);
  307. /**
  308. * Try to lock a mutex without blocking.
  309. *
  310. * This works just like SDL_LockMutex(), but if the mutex is not available,
  311. * this function returns false immediately.
  312. *
  313. * This technique is useful if you need exclusive access to a resource but
  314. * don't want to wait for it, and will return to it to try again later.
  315. *
  316. * This function returns true if passed a NULL mutex.
  317. *
  318. * \param mutex the mutex to try to lock.
  319. * \returns true on success, false if the mutex would block.
  320. *
  321. * \since This function is available since SDL 3.2.0.
  322. *
  323. * \sa SDL_LockMutex
  324. * \sa SDL_UnlockMutex
  325. */
  326. extern SDL_DECLSPEC bool SDLCALL SDL_TryLockMutex(SDL_Mutex *mutex) SDL_TRY_ACQUIRE(0, mutex);
  327. /**
  328. * Unlock the mutex.
  329. *
  330. * It is legal for the owning thread to lock an already-locked mutex. It must
  331. * unlock it the same number of times before it is actually made available for
  332. * other threads in the system (this is known as a "recursive mutex").
  333. *
  334. * It is illegal to unlock a mutex that has not been locked by the current
  335. * thread, and doing so results in undefined behavior.
  336. *
  337. * \param mutex the mutex to unlock.
  338. *
  339. * \since This function is available since SDL 3.2.0.
  340. *
  341. * \sa SDL_LockMutex
  342. * \sa SDL_TryLockMutex
  343. */
  344. extern SDL_DECLSPEC void SDLCALL SDL_UnlockMutex(SDL_Mutex *mutex) SDL_RELEASE(mutex);
  345. /**
  346. * Destroy a mutex created with SDL_CreateMutex().
  347. *
  348. * This function must be called on any mutex that is no longer needed. Failure
  349. * to destroy a mutex will result in a system memory or resource leak. While
  350. * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
  351. * to destroy a locked mutex, and may result in undefined behavior depending
  352. * on the platform.
  353. *
  354. * \param mutex the mutex to destroy.
  355. *
  356. * \since This function is available since SDL 3.2.0.
  357. *
  358. * \sa SDL_CreateMutex
  359. */
  360. extern SDL_DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_Mutex *mutex);
  361. /* @} *//* Mutex functions */
  362. /**
  363. * \name Read/write lock functions
  364. */
  365. /* @{ */
  366. /**
  367. * A mutex that allows read-only threads to run in parallel.
  368. *
  369. * A rwlock is roughly the same concept as SDL_Mutex, but allows threads that
  370. * request read-only access to all hold the lock at the same time. If a thread
  371. * requests write access, it will block until all read-only threads have
  372. * released the lock, and no one else can hold the thread (for reading or
  373. * writing) at the same time as the writing thread.
  374. *
  375. * This can be more efficient in cases where several threads need to access
  376. * data frequently, but changes to that data are rare.
  377. *
  378. * There are other rules that apply to rwlocks that don't apply to mutexes,
  379. * about how threads are scheduled and when they can be recursively locked.
  380. * These are documented in the other rwlock functions.
  381. *
  382. * \since This struct is available since SDL 3.2.0.
  383. */
  384. typedef struct SDL_RWLock SDL_RWLock;
  385. /**
  386. * Create a new read/write lock.
  387. *
  388. * A read/write lock is useful for situations where you have multiple threads
  389. * trying to access a resource that is rarely updated. All threads requesting
  390. * a read-only lock will be allowed to run in parallel; if a thread requests a
  391. * write lock, it will be provided exclusive access. This makes it safe for
  392. * multiple threads to use a resource at the same time if they promise not to
  393. * change it, and when it has to be changed, the rwlock will serve as a
  394. * gateway to make sure those changes can be made safely.
  395. *
  396. * In the right situation, a rwlock can be more efficient than a mutex, which
  397. * only lets a single thread proceed at a time, even if it won't be modifying
  398. * the data.
  399. *
  400. * All newly-created read/write locks begin in the _unlocked_ state.
  401. *
  402. * Calls to SDL_LockRWLockForReading() and SDL_LockRWLockForWriting will not
  403. * return while the rwlock is locked _for writing_ by another thread. See
  404. * SDL_TryLockRWLockForReading() and SDL_TryLockRWLockForWriting() to attempt
  405. * to lock without blocking.
  406. *
  407. * SDL read/write locks are only recursive for read-only locks! They are not
  408. * guaranteed to be fair, or provide access in a FIFO manner! They are not
  409. * guaranteed to favor writers. You may not lock a rwlock for both read-only
  410. * and write access at the same time from the same thread (so you can't
  411. * promote your read-only lock to a write lock without unlocking first).
  412. *
  413. * \returns the initialized and unlocked read/write lock or NULL on failure;
  414. * call SDL_GetError() for more information.
  415. *
  416. * \since This function is available since SDL 3.2.0.
  417. *
  418. * \sa SDL_DestroyRWLock
  419. * \sa SDL_LockRWLockForReading
  420. * \sa SDL_LockRWLockForWriting
  421. * \sa SDL_TryLockRWLockForReading
  422. * \sa SDL_TryLockRWLockForWriting
  423. * \sa SDL_UnlockRWLock
  424. */
  425. extern SDL_DECLSPEC SDL_RWLock * SDLCALL SDL_CreateRWLock(void);
  426. /**
  427. * Lock the read/write lock for _read only_ operations.
  428. *
  429. * This will block until the rwlock is available, which is to say it is not
  430. * locked for writing by any other thread. Of all threads waiting to lock the
  431. * rwlock, all may do so at the same time as long as they are requesting
  432. * read-only access; if a thread wants to lock for writing, only one may do so
  433. * at a time, and no other threads, read-only or not, may hold the lock at the
  434. * same time.
  435. *
  436. * It is legal for the owning thread to lock an already-locked rwlock for
  437. * reading. It must unlock it the same number of times before it is actually
  438. * made available for other threads in the system (this is known as a
  439. * "recursive rwlock").
  440. *
  441. * Note that locking for writing is not recursive (this is only available to
  442. * read-only locks).
  443. *
  444. * It is illegal to request a read-only lock from a thread that already holds
  445. * the write lock. Doing so results in undefined behavior. Unlock the write
  446. * lock before requesting a read-only lock. (But, of course, if you have the
  447. * write lock, you don't need further locks to read in any case.)
  448. *
  449. * This function does not fail; if rwlock is NULL, it will return immediately
  450. * having locked nothing. If the rwlock is valid, this function will always
  451. * block until it can lock the mutex, and return with it locked.
  452. *
  453. * \param rwlock the read/write lock to lock.
  454. *
  455. * \since This function is available since SDL 3.2.0.
  456. *
  457. * \sa SDL_LockRWLockForWriting
  458. * \sa SDL_TryLockRWLockForReading
  459. * \sa SDL_UnlockRWLock
  460. */
  461. extern SDL_DECLSPEC void SDLCALL SDL_LockRWLockForReading(SDL_RWLock *rwlock) SDL_ACQUIRE_SHARED(rwlock);
  462. /**
  463. * Lock the read/write lock for _write_ operations.
  464. *
  465. * This will block until the rwlock is available, which is to say it is not
  466. * locked for reading or writing by any other thread. Only one thread may hold
  467. * the lock when it requests write access; all other threads, whether they
  468. * also want to write or only want read-only access, must wait until the
  469. * writer thread has released the lock.
  470. *
  471. * It is illegal for the owning thread to lock an already-locked rwlock for
  472. * writing (read-only may be locked recursively, writing can not). Doing so
  473. * results in undefined behavior.
  474. *
  475. * It is illegal to request a write lock from a thread that already holds a
  476. * read-only lock. Doing so results in undefined behavior. Unlock the
  477. * read-only lock before requesting a write lock.
  478. *
  479. * This function does not fail; if rwlock is NULL, it will return immediately
  480. * having locked nothing. If the rwlock is valid, this function will always
  481. * block until it can lock the mutex, and return with it locked.
  482. *
  483. * \param rwlock the read/write lock to lock.
  484. *
  485. * \since This function is available since SDL 3.2.0.
  486. *
  487. * \sa SDL_LockRWLockForReading
  488. * \sa SDL_TryLockRWLockForWriting
  489. * \sa SDL_UnlockRWLock
  490. */
  491. extern SDL_DECLSPEC void SDLCALL SDL_LockRWLockForWriting(SDL_RWLock *rwlock) SDL_ACQUIRE(rwlock);
  492. /**
  493. * Try to lock a read/write lock _for reading_ without blocking.
  494. *
  495. * This works just like SDL_LockRWLockForReading(), but if the rwlock is not
  496. * available, then this function returns false immediately.
  497. *
  498. * This technique is useful if you need access to a resource but don't want to
  499. * wait for it, and will return to it to try again later.
  500. *
  501. * Trying to lock for read-only access can succeed if other threads are
  502. * holding read-only locks, as this won't prevent access.
  503. *
  504. * This function returns true if passed a NULL rwlock.
  505. *
  506. * \param rwlock the rwlock to try to lock.
  507. * \returns true on success, false if the lock would block.
  508. *
  509. * \since This function is available since SDL 3.2.0.
  510. *
  511. * \sa SDL_LockRWLockForReading
  512. * \sa SDL_TryLockRWLockForWriting
  513. * \sa SDL_UnlockRWLock
  514. */
  515. extern SDL_DECLSPEC bool SDLCALL SDL_TryLockRWLockForReading(SDL_RWLock *rwlock) SDL_TRY_ACQUIRE_SHARED(0, rwlock);
  516. /**
  517. * Try to lock a read/write lock _for writing_ without blocking.
  518. *
  519. * This works just like SDL_LockRWLockForWriting(), but if the rwlock is not
  520. * available, then this function returns false immediately.
  521. *
  522. * This technique is useful if you need exclusive access to a resource but
  523. * don't want to wait for it, and will return to it to try again later.
  524. *
  525. * It is illegal for the owning thread to lock an already-locked rwlock for
  526. * writing (read-only may be locked recursively, writing can not). Doing so
  527. * results in undefined behavior.
  528. *
  529. * It is illegal to request a write lock from a thread that already holds a
  530. * read-only lock. Doing so results in undefined behavior. Unlock the
  531. * read-only lock before requesting a write lock.
  532. *
  533. * This function returns true if passed a NULL rwlock.
  534. *
  535. * \param rwlock the rwlock to try to lock.
  536. * \returns true on success, false if the lock would block.
  537. *
  538. * \since This function is available since SDL 3.2.0.
  539. *
  540. * \sa SDL_LockRWLockForWriting
  541. * \sa SDL_TryLockRWLockForReading
  542. * \sa SDL_UnlockRWLock
  543. */
  544. extern SDL_DECLSPEC bool SDLCALL SDL_TryLockRWLockForWriting(SDL_RWLock *rwlock) SDL_TRY_ACQUIRE(0, rwlock);
  545. /**
  546. * Unlock the read/write lock.
  547. *
  548. * Use this function to unlock the rwlock, whether it was locked for read-only
  549. * or write operations.
  550. *
  551. * It is legal for the owning thread to lock an already-locked read-only lock.
  552. * It must unlock it the same number of times before it is actually made
  553. * available for other threads in the system (this is known as a "recursive
  554. * rwlock").
  555. *
  556. * It is illegal to unlock a rwlock that has not been locked by the current
  557. * thread, and doing so results in undefined behavior.
  558. *
  559. * \param rwlock the rwlock to unlock.
  560. *
  561. * \since This function is available since SDL 3.2.0.
  562. *
  563. * \sa SDL_LockRWLockForReading
  564. * \sa SDL_LockRWLockForWriting
  565. * \sa SDL_TryLockRWLockForReading
  566. * \sa SDL_TryLockRWLockForWriting
  567. */
  568. extern SDL_DECLSPEC void SDLCALL SDL_UnlockRWLock(SDL_RWLock *rwlock) SDL_RELEASE_GENERIC(rwlock);
  569. /**
  570. * Destroy a read/write lock created with SDL_CreateRWLock().
  571. *
  572. * This function must be called on any read/write lock that is no longer
  573. * needed. Failure to destroy a rwlock will result in a system memory or
  574. * resource leak. While it is safe to destroy a rwlock that is _unlocked_, it
  575. * is not safe to attempt to destroy a locked rwlock, and may result in
  576. * undefined behavior depending on the platform.
  577. *
  578. * \param rwlock the rwlock to destroy.
  579. *
  580. * \since This function is available since SDL 3.2.0.
  581. *
  582. * \sa SDL_CreateRWLock
  583. */
  584. extern SDL_DECLSPEC void SDLCALL SDL_DestroyRWLock(SDL_RWLock *rwlock);
  585. /* @} *//* Read/write lock functions */
  586. /**
  587. * \name Semaphore functions
  588. */
  589. /* @{ */
  590. /**
  591. * A means to manage access to a resource, by count, between threads.
  592. *
  593. * Semaphores (specifically, "counting semaphores"), let X number of threads
  594. * request access at the same time, each thread granted access decrementing a
  595. * counter. When the counter reaches zero, future requests block until a prior
  596. * thread releases their request, incrementing the counter again.
  597. *
  598. * Wikipedia has a thorough explanation of the concept:
  599. *
  600. * https://en.wikipedia.org/wiki/Semaphore_(programming)
  601. *
  602. * \since This struct is available since SDL 3.2.0.
  603. */
  604. typedef struct SDL_Semaphore SDL_Semaphore;
  605. /**
  606. * Create a semaphore.
  607. *
  608. * This function creates a new semaphore and initializes it with the value
  609. * `initial_value`. Each wait operation on the semaphore will atomically
  610. * decrement the semaphore value and potentially block if the semaphore value
  611. * is 0. Each post operation will atomically increment the semaphore value and
  612. * wake waiting threads and allow them to retry the wait operation.
  613. *
  614. * \param initial_value the starting value of the semaphore.
  615. * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
  616. * information.
  617. *
  618. * \since This function is available since SDL 3.2.0.
  619. *
  620. * \sa SDL_DestroySemaphore
  621. * \sa SDL_SignalSemaphore
  622. * \sa SDL_TryWaitSemaphore
  623. * \sa SDL_GetSemaphoreValue
  624. * \sa SDL_WaitSemaphore
  625. * \sa SDL_WaitSemaphoreTimeout
  626. */
  627. extern SDL_DECLSPEC SDL_Semaphore * SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
  628. /**
  629. * Destroy a semaphore.
  630. *
  631. * It is not safe to destroy a semaphore if there are threads currently
  632. * waiting on it.
  633. *
  634. * \param sem the semaphore to destroy.
  635. *
  636. * \since This function is available since SDL 3.2.0.
  637. *
  638. * \sa SDL_CreateSemaphore
  639. */
  640. extern SDL_DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_Semaphore *sem);
  641. /**
  642. * Wait until a semaphore has a positive value and then decrements it.
  643. *
  644. * This function suspends the calling thread until the semaphore pointed to by
  645. * `sem` has a positive value, and then atomically decrement the semaphore
  646. * value.
  647. *
  648. * This function is the equivalent of calling SDL_WaitSemaphoreTimeout() with
  649. * a time length of -1.
  650. *
  651. * \param sem the semaphore wait on.
  652. *
  653. * \since This function is available since SDL 3.2.0.
  654. *
  655. * \sa SDL_SignalSemaphore
  656. * \sa SDL_TryWaitSemaphore
  657. * \sa SDL_WaitSemaphoreTimeout
  658. */
  659. extern SDL_DECLSPEC void SDLCALL SDL_WaitSemaphore(SDL_Semaphore *sem);
  660. /**
  661. * See if a semaphore has a positive value and decrement it if it does.
  662. *
  663. * This function checks to see if the semaphore pointed to by `sem` has a
  664. * positive value and atomically decrements the semaphore value if it does. If
  665. * the semaphore doesn't have a positive value, the function immediately
  666. * returns false.
  667. *
  668. * \param sem the semaphore to wait on.
  669. * \returns true if the wait succeeds, false if the wait would block.
  670. *
  671. * \since This function is available since SDL 3.2.0.
  672. *
  673. * \sa SDL_SignalSemaphore
  674. * \sa SDL_WaitSemaphore
  675. * \sa SDL_WaitSemaphoreTimeout
  676. */
  677. extern SDL_DECLSPEC bool SDLCALL SDL_TryWaitSemaphore(SDL_Semaphore *sem);
  678. /**
  679. * Wait until a semaphore has a positive value and then decrements it.
  680. *
  681. * This function suspends the calling thread until either the semaphore
  682. * pointed to by `sem` has a positive value or the specified time has elapsed.
  683. * If the call is successful it will atomically decrement the semaphore value.
  684. *
  685. * \param sem the semaphore to wait on.
  686. * \param timeoutMS the length of the timeout, in milliseconds, or -1 to wait
  687. * indefinitely.
  688. * \returns true if the wait succeeds or false if the wait times out.
  689. *
  690. * \since This function is available since SDL 3.2.0.
  691. *
  692. * \sa SDL_SignalSemaphore
  693. * \sa SDL_TryWaitSemaphore
  694. * \sa SDL_WaitSemaphore
  695. */
  696. extern SDL_DECLSPEC bool SDLCALL SDL_WaitSemaphoreTimeout(SDL_Semaphore *sem, Sint32 timeoutMS);
  697. /**
  698. * Atomically increment a semaphore's value and wake waiting threads.
  699. *
  700. * \param sem the semaphore to increment.
  701. *
  702. * \since This function is available since SDL 3.2.0.
  703. *
  704. * \sa SDL_TryWaitSemaphore
  705. * \sa SDL_WaitSemaphore
  706. * \sa SDL_WaitSemaphoreTimeout
  707. */
  708. extern SDL_DECLSPEC void SDLCALL SDL_SignalSemaphore(SDL_Semaphore *sem);
  709. /**
  710. * Get the current value of a semaphore.
  711. *
  712. * \param sem the semaphore to query.
  713. * \returns the current value of the semaphore.
  714. *
  715. * \since This function is available since SDL 3.2.0.
  716. */
  717. extern SDL_DECLSPEC Uint32 SDLCALL SDL_GetSemaphoreValue(SDL_Semaphore *sem);
  718. /* @} *//* Semaphore functions */
  719. /**
  720. * \name Condition variable functions
  721. */
  722. /* @{ */
  723. /**
  724. * A means to block multiple threads until a condition is satisfied.
  725. *
  726. * Condition variables, paired with an SDL_Mutex, let an app halt multiple
  727. * threads until a condition has occurred, at which time the app can release
  728. * one or all waiting threads.
  729. *
  730. * Wikipedia has a thorough explanation of the concept:
  731. *
  732. * https://en.wikipedia.org/wiki/Condition_variable
  733. *
  734. * \since This struct is available since SDL 3.2.0.
  735. */
  736. typedef struct SDL_Condition SDL_Condition;
  737. /**
  738. * Create a condition variable.
  739. *
  740. * \returns a new condition variable or NULL on failure; call SDL_GetError()
  741. * for more information.
  742. *
  743. * \since This function is available since SDL 3.2.0.
  744. *
  745. * \sa SDL_BroadcastCondition
  746. * \sa SDL_SignalCondition
  747. * \sa SDL_WaitCondition
  748. * \sa SDL_WaitConditionTimeout
  749. * \sa SDL_DestroyCondition
  750. */
  751. extern SDL_DECLSPEC SDL_Condition * SDLCALL SDL_CreateCondition(void);
  752. /**
  753. * Destroy a condition variable.
  754. *
  755. * \param cond the condition variable to destroy.
  756. *
  757. * \since This function is available since SDL 3.2.0.
  758. *
  759. * \sa SDL_CreateCondition
  760. */
  761. extern SDL_DECLSPEC void SDLCALL SDL_DestroyCondition(SDL_Condition *cond);
  762. /**
  763. * Restart one of the threads that are waiting on the condition variable.
  764. *
  765. * \param cond the condition variable to signal.
  766. *
  767. * \threadsafety It is safe to call this function from any thread.
  768. *
  769. * \since This function is available since SDL 3.2.0.
  770. *
  771. * \sa SDL_BroadcastCondition
  772. * \sa SDL_WaitCondition
  773. * \sa SDL_WaitConditionTimeout
  774. */
  775. extern SDL_DECLSPEC void SDLCALL SDL_SignalCondition(SDL_Condition *cond);
  776. /**
  777. * Restart all threads that are waiting on the condition variable.
  778. *
  779. * \param cond the condition variable to signal.
  780. *
  781. * \threadsafety It is safe to call this function from any thread.
  782. *
  783. * \since This function is available since SDL 3.2.0.
  784. *
  785. * \sa SDL_SignalCondition
  786. * \sa SDL_WaitCondition
  787. * \sa SDL_WaitConditionTimeout
  788. */
  789. extern SDL_DECLSPEC void SDLCALL SDL_BroadcastCondition(SDL_Condition *cond);
  790. /**
  791. * Wait until a condition variable is signaled.
  792. *
  793. * This function unlocks the specified `mutex` and waits for another thread to
  794. * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
  795. * variable `cond`. Once the condition variable is signaled, the mutex is
  796. * re-locked and the function returns.
  797. *
  798. * The mutex must be locked before calling this function. Locking the mutex
  799. * recursively (more than once) is not supported and leads to undefined
  800. * behavior.
  801. *
  802. * This function is the equivalent of calling SDL_WaitConditionTimeout() with
  803. * a time length of -1.
  804. *
  805. * \param cond the condition variable to wait on.
  806. * \param mutex the mutex used to coordinate thread access.
  807. *
  808. * \threadsafety It is safe to call this function from any thread.
  809. *
  810. * \since This function is available since SDL 3.2.0.
  811. *
  812. * \sa SDL_BroadcastCondition
  813. * \sa SDL_SignalCondition
  814. * \sa SDL_WaitConditionTimeout
  815. */
  816. extern SDL_DECLSPEC void SDLCALL SDL_WaitCondition(SDL_Condition *cond, SDL_Mutex *mutex);
  817. /**
  818. * Wait until a condition variable is signaled or a certain time has passed.
  819. *
  820. * This function unlocks the specified `mutex` and waits for another thread to
  821. * call SDL_SignalCondition() or SDL_BroadcastCondition() on the condition
  822. * variable `cond`, or for the specified time to elapse. Once the condition
  823. * variable is signaled or the time elapsed, the mutex is re-locked and the
  824. * function returns.
  825. *
  826. * The mutex must be locked before calling this function. Locking the mutex
  827. * recursively (more than once) is not supported and leads to undefined
  828. * behavior.
  829. *
  830. * \param cond the condition variable to wait on.
  831. * \param mutex the mutex used to coordinate thread access.
  832. * \param timeoutMS the maximum time to wait, in milliseconds, or -1 to wait
  833. * indefinitely.
  834. * \returns true if the condition variable is signaled, false if the condition
  835. * is not signaled in the allotted time.
  836. *
  837. * \threadsafety It is safe to call this function from any thread.
  838. *
  839. * \since This function is available since SDL 3.2.0.
  840. *
  841. * \sa SDL_BroadcastCondition
  842. * \sa SDL_SignalCondition
  843. * \sa SDL_WaitCondition
  844. */
  845. extern SDL_DECLSPEC bool SDLCALL SDL_WaitConditionTimeout(SDL_Condition *cond,
  846. SDL_Mutex *mutex, Sint32 timeoutMS);
  847. /* @} *//* Condition variable functions */
  848. /**
  849. * \name Thread-safe initialization state functions
  850. */
  851. /* @{ */
  852. /**
  853. * The current status of an SDL_InitState structure.
  854. *
  855. * \since This enum is available since SDL 3.2.0.
  856. */
  857. typedef enum SDL_InitStatus
  858. {
  859. SDL_INIT_STATUS_UNINITIALIZED,
  860. SDL_INIT_STATUS_INITIALIZING,
  861. SDL_INIT_STATUS_INITIALIZED,
  862. SDL_INIT_STATUS_UNINITIALIZING
  863. } SDL_InitStatus;
  864. /**
  865. * A structure used for thread-safe initialization and shutdown.
  866. *
  867. * Here is an example of using this:
  868. *
  869. * ```c
  870. * static SDL_InitState init;
  871. *
  872. * bool InitSystem(void)
  873. * {
  874. * if (!SDL_ShouldInit(&init)) {
  875. * // The system is initialized
  876. * return true;
  877. * }
  878. *
  879. * // At this point, you should not leave this function without calling SDL_SetInitialized()
  880. *
  881. * bool initialized = DoInitTasks();
  882. * SDL_SetInitialized(&init, initialized);
  883. * return initialized;
  884. * }
  885. *
  886. * bool UseSubsystem(void)
  887. * {
  888. * if (SDL_ShouldInit(&init)) {
  889. * // Error, the subsystem isn't initialized
  890. * SDL_SetInitialized(&init, false);
  891. * return false;
  892. * }
  893. *
  894. * // Do work using the initialized subsystem
  895. *
  896. * return true;
  897. * }
  898. *
  899. * void QuitSystem(void)
  900. * {
  901. * if (!SDL_ShouldQuit(&init)) {
  902. * // The system is not initialized
  903. * return;
  904. * }
  905. *
  906. * // At this point, you should not leave this function without calling SDL_SetInitialized()
  907. *
  908. * DoQuitTasks();
  909. * SDL_SetInitialized(&init, false);
  910. * }
  911. * ```
  912. *
  913. * Note that this doesn't protect any resources created during initialization,
  914. * or guarantee that nobody is using those resources during cleanup. You
  915. * should use other mechanisms to protect those, if that's a concern for your
  916. * code.
  917. *
  918. * \since This struct is available since SDL 3.2.0.
  919. */
  920. typedef struct SDL_InitState
  921. {
  922. SDL_AtomicInt status;
  923. SDL_ThreadID thread;
  924. void *reserved;
  925. } SDL_InitState;
  926. /**
  927. * Return whether initialization should be done.
  928. *
  929. * This function checks the passed in state and if initialization should be
  930. * done, sets the status to `SDL_INIT_STATUS_INITIALIZING` and returns true.
  931. * If another thread is already modifying this state, it will wait until
  932. * that's done before returning.
  933. *
  934. * If this function returns true, the calling code must call
  935. * SDL_SetInitialized() to complete the initialization.
  936. *
  937. * \param state the initialization state to check.
  938. * \returns true if initialization needs to be done, false otherwise.
  939. *
  940. * \threadsafety It is safe to call this function from any thread.
  941. *
  942. * \since This function is available since SDL 3.2.0.
  943. *
  944. * \sa SDL_SetInitialized
  945. * \sa SDL_ShouldQuit
  946. */
  947. extern SDL_DECLSPEC bool SDLCALL SDL_ShouldInit(SDL_InitState *state);
  948. /**
  949. * Return whether cleanup should be done.
  950. *
  951. * This function checks the passed in state and if cleanup should be done,
  952. * sets the status to `SDL_INIT_STATUS_UNINITIALIZING` and returns true.
  953. *
  954. * If this function returns true, the calling code must call
  955. * SDL_SetInitialized() to complete the cleanup.
  956. *
  957. * \param state the initialization state to check.
  958. * \returns true if cleanup needs to be done, false otherwise.
  959. *
  960. * \threadsafety It is safe to call this function from any thread.
  961. *
  962. * \since This function is available since SDL 3.2.0.
  963. *
  964. * \sa SDL_SetInitialized
  965. * \sa SDL_ShouldInit
  966. */
  967. extern SDL_DECLSPEC bool SDLCALL SDL_ShouldQuit(SDL_InitState *state);
  968. /**
  969. * Finish an initialization state transition.
  970. *
  971. * This function sets the status of the passed in state to
  972. * `SDL_INIT_STATUS_INITIALIZED` or `SDL_INIT_STATUS_UNINITIALIZED` and allows
  973. * any threads waiting for the status to proceed.
  974. *
  975. * \param state the initialization state to check.
  976. * \param initialized the new initialization state.
  977. *
  978. * \threadsafety It is safe to call this function from any thread.
  979. *
  980. * \since This function is available since SDL 3.2.0.
  981. *
  982. * \sa SDL_ShouldInit
  983. * \sa SDL_ShouldQuit
  984. */
  985. extern SDL_DECLSPEC void SDLCALL SDL_SetInitialized(SDL_InitState *state, bool initialized);
  986. /* @} *//* Thread-safe initialization state functions */
  987. /* Ends C function definitions when using C++ */
  988. #ifdef __cplusplus
  989. }
  990. #endif
  991. #include <SDL3/SDL_close_code.h>
  992. #endif /* SDL_mutex_h_ */