SDL_mutex.h 34 KB

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