SDL_mutex.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. * \file SDL_mutex.h
  22. *
  23. * \brief Functions to provide thread synchronization primitives.
  24. */
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_error.h>
  27. /******************************************************************************/
  28. /* Enable thread safety attributes only with clang.
  29. * The attributes can be safely erased when compiling with other compilers.
  30. */
  31. #if defined(SDL_THREAD_SAFETY_ANALYSIS) && \
  32. defined(__clang__) && (!defined(SWIG))
  33. #define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x) __attribute__((x))
  34. #else
  35. #define SDL_THREAD_ANNOTATION_ATTRIBUTE__(x) /* no-op */
  36. #endif
  37. #define SDL_CAPABILITY(x) \
  38. SDL_THREAD_ANNOTATION_ATTRIBUTE__(capability(x))
  39. #define SDL_SCOPED_CAPABILITY \
  40. SDL_THREAD_ANNOTATION_ATTRIBUTE__(scoped_lockable)
  41. #define SDL_GUARDED_BY(x) \
  42. SDL_THREAD_ANNOTATION_ATTRIBUTE__(guarded_by(x))
  43. #define SDL_PT_GUARDED_BY(x) \
  44. SDL_THREAD_ANNOTATION_ATTRIBUTE__(pt_guarded_by(x))
  45. #define SDL_ACQUIRED_BEFORE(...) \
  46. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(__VA_ARGS__))
  47. #define SDL_ACQUIRED_AFTER(...) \
  48. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(__VA_ARGS__))
  49. #define SDL_REQUIRES(...) \
  50. SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(__VA_ARGS__))
  51. #define SDL_REQUIRES_SHARED(...) \
  52. SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(__VA_ARGS__))
  53. #define SDL_ACQUIRE(...) \
  54. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(__VA_ARGS__))
  55. #define SDL_ACQUIRE_SHARED(...) \
  56. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(__VA_ARGS__))
  57. #define SDL_RELEASE(...) \
  58. SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(__VA_ARGS__))
  59. #define SDL_RELEASE_SHARED(...) \
  60. SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(__VA_ARGS__))
  61. #define SDL_RELEASE_GENERIC(...) \
  62. SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(__VA_ARGS__))
  63. #define SDL_TRY_ACQUIRE(...) \
  64. SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(__VA_ARGS__))
  65. #define SDL_TRY_ACQUIRE_SHARED(...) \
  66. SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(__VA_ARGS__))
  67. #define SDL_EXCLUDES(...) \
  68. SDL_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(__VA_ARGS__))
  69. #define SDL_ASSERT_CAPABILITY(x) \
  70. SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_capability(x))
  71. #define SDL_ASSERT_SHARED_CAPABILITY(x) \
  72. SDL_THREAD_ANNOTATION_ATTRIBUTE__(assert_shared_capability(x))
  73. #define SDL_RETURN_CAPABILITY(x) \
  74. SDL_THREAD_ANNOTATION_ATTRIBUTE__(lock_returned(x))
  75. #define SDL_NO_THREAD_SAFETY_ANALYSIS \
  76. SDL_THREAD_ANNOTATION_ATTRIBUTE__(no_thread_safety_analysis)
  77. /******************************************************************************/
  78. #include <SDL3/SDL_begin_code.h>
  79. /* Set up for C function definitions, even when using C++ */
  80. #ifdef __cplusplus
  81. extern "C" {
  82. #endif
  83. /**
  84. * Synchronization functions which can time out return this value
  85. * if they time out.
  86. */
  87. #define SDL_MUTEX_TIMEDOUT 1
  88. /**
  89. * This is the timeout value which corresponds to never time out.
  90. */
  91. #define SDL_MUTEX_MAXWAIT -1
  92. /**
  93. * \name Mutex functions
  94. */
  95. /* @{ */
  96. /* The SDL mutex structure, defined in SDL_sysmutex.c */
  97. struct SDL_mutex;
  98. typedef struct SDL_mutex SDL_mutex;
  99. /**
  100. * Create a new mutex.
  101. *
  102. * All newly-created mutexes begin in the _unlocked_ state.
  103. *
  104. * Calls to SDL_LockMutex() will not return while the mutex is locked by
  105. * another thread. See SDL_TryLockMutex() to attempt to lock without blocking.
  106. *
  107. * SDL mutexes are reentrant.
  108. *
  109. * \returns the initialized and unlocked mutex or NULL on failure; call
  110. * SDL_GetError() for more information.
  111. *
  112. * \since This function is available since SDL 3.0.0.
  113. *
  114. * \sa SDL_DestroyMutex
  115. * \sa SDL_LockMutex
  116. * \sa SDL_TryLockMutex
  117. * \sa SDL_UnlockMutex
  118. */
  119. extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
  120. /**
  121. * Lock the mutex.
  122. *
  123. * This will block until the mutex is available, which is to say it is in the
  124. * unlocked state and the OS has chosen the caller as the next thread to lock
  125. * it. Of all threads waiting to lock the mutex, only one may do so at a time.
  126. *
  127. * It is legal for the owning thread to lock an already-locked mutex. It must
  128. * unlock it the same number of times before it is actually made available for
  129. * other threads in the system (this is known as a "recursive mutex").
  130. *
  131. * \param mutex the mutex to lock
  132. * \returns 0 on success or a negative error code on failure; call
  133. * SDL_GetError() for more information.
  134. *
  135. * \since This function is available since SDL 3.0.0.
  136. */
  137. extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex) SDL_ACQUIRE(mutex);
  138. #define SDL_mutexP(m) SDL_LockMutex(m)
  139. /**
  140. * Try to lock a mutex without blocking.
  141. *
  142. * This works just like SDL_LockMutex(), but if the mutex is not available,
  143. * this function returns `SDL_MUTEX_TIMEOUT` immediately.
  144. *
  145. * This technique is useful if you need exclusive access to a resource but
  146. * don't want to wait for it, and will return to it to try again later.
  147. *
  148. * \param mutex the mutex to try to lock
  149. * \returns 0, `SDL_MUTEX_TIMEDOUT`, or -1 on error; call SDL_GetError() for
  150. * more information.
  151. *
  152. * \since This function is available since SDL 3.0.0.
  153. *
  154. * \sa SDL_CreateMutex
  155. * \sa SDL_DestroyMutex
  156. * \sa SDL_LockMutex
  157. * \sa SDL_UnlockMutex
  158. */
  159. extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex) SDL_TRY_ACQUIRE(0, mutex);
  160. /**
  161. * Unlock the mutex.
  162. *
  163. * It is legal for the owning thread to lock an already-locked mutex. It must
  164. * unlock it the same number of times before it is actually made available for
  165. * other threads in the system (this is known as a "recursive mutex").
  166. *
  167. * It is an error to unlock a mutex that has not been locked by the current
  168. * thread, and doing so results in undefined behavior.
  169. *
  170. * It is also an error to unlock a mutex that isn't locked at all.
  171. *
  172. * \param mutex the mutex to unlock.
  173. * \returns 0 on success or a negative error code on failure; call
  174. * SDL_GetError() for more information.
  175. *
  176. * \since This function is available since SDL 3.0.0.
  177. */
  178. extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex) SDL_RELEASE(mutex);
  179. #define SDL_mutexV(m) SDL_UnlockMutex(m)
  180. /**
  181. * Destroy a mutex created with SDL_CreateMutex().
  182. *
  183. * This function must be called on any mutex that is no longer needed. Failure
  184. * to destroy a mutex will result in a system memory or resource leak. While
  185. * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
  186. * to destroy a locked mutex, and may result in undefined behavior depending
  187. * on the platform.
  188. *
  189. * \param mutex the mutex to destroy
  190. *
  191. * \since This function is available since SDL 3.0.0.
  192. *
  193. * \sa SDL_CreateMutex
  194. * \sa SDL_LockMutex
  195. * \sa SDL_TryLockMutex
  196. * \sa SDL_UnlockMutex
  197. */
  198. extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
  199. /* @} *//* Mutex functions */
  200. /**
  201. * \name Semaphore functions
  202. */
  203. /* @{ */
  204. /* The SDL semaphore structure, defined in SDL_syssem.c */
  205. struct SDL_semaphore;
  206. typedef struct SDL_semaphore SDL_sem;
  207. /**
  208. * Create a semaphore.
  209. *
  210. * This function creates a new semaphore and initializes it with the value
  211. * `initial_value`. Each wait operation on the semaphore will atomically
  212. * decrement the semaphore value and potentially block if the semaphore value
  213. * is 0. Each post operation will atomically increment the semaphore value and
  214. * wake waiting threads and allow them to retry the wait operation.
  215. *
  216. * \param initial_value the starting value of the semaphore
  217. * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
  218. * information.
  219. *
  220. * \since This function is available since SDL 3.0.0.
  221. *
  222. * \sa SDL_DestroySemaphore
  223. * \sa SDL_SemPost
  224. * \sa SDL_SemTryWait
  225. * \sa SDL_SemValue
  226. * \sa SDL_SemWait
  227. * \sa SDL_SemWaitTimeout
  228. */
  229. extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
  230. /**
  231. * Destroy a semaphore.
  232. *
  233. * It is not safe to destroy a semaphore if there are threads currently
  234. * waiting on it.
  235. *
  236. * \param sem the semaphore to destroy
  237. *
  238. * \since This function is available since SDL 3.0.0.
  239. *
  240. * \sa SDL_CreateSemaphore
  241. * \sa SDL_SemPost
  242. * \sa SDL_SemTryWait
  243. * \sa SDL_SemValue
  244. * \sa SDL_SemWait
  245. * \sa SDL_SemWaitTimeout
  246. */
  247. extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem *sem);
  248. /**
  249. * Wait until a semaphore has a positive value and then decrements it.
  250. *
  251. * This function suspends the calling thread until either the semaphore
  252. * pointed to by `sem` has a positive value or the call is interrupted by a
  253. * signal or error. If the call is successful it will atomically decrement the
  254. * semaphore value.
  255. *
  256. * This function is the equivalent of calling SDL_SemWaitTimeout() with a time
  257. * length of `SDL_MUTEX_MAXWAIT`.
  258. *
  259. * \param sem the semaphore wait on
  260. * \returns 0 on success or a negative error code on failure; call
  261. * SDL_GetError() for more information.
  262. *
  263. * \since This function is available since SDL 3.0.0.
  264. *
  265. * \sa SDL_CreateSemaphore
  266. * \sa SDL_DestroySemaphore
  267. * \sa SDL_SemPost
  268. * \sa SDL_SemTryWait
  269. * \sa SDL_SemValue
  270. * \sa SDL_SemWait
  271. * \sa SDL_SemWaitTimeout
  272. */
  273. extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem *sem);
  274. /**
  275. * See if a semaphore has a positive value and decrement it if it does.
  276. *
  277. * This function checks to see if the semaphore pointed to by `sem` has a
  278. * positive value and atomically decrements the semaphore value if it does. If
  279. * the semaphore doesn't have a positive value, the function immediately
  280. * returns SDL_MUTEX_TIMEDOUT.
  281. *
  282. * \param sem the semaphore to wait on
  283. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
  284. * block, or a negative error code on failure; call SDL_GetError()
  285. * for more information.
  286. *
  287. * \since This function is available since SDL 3.0.0.
  288. *
  289. * \sa SDL_CreateSemaphore
  290. * \sa SDL_DestroySemaphore
  291. * \sa SDL_SemPost
  292. * \sa SDL_SemValue
  293. * \sa SDL_SemWait
  294. * \sa SDL_SemWaitTimeout
  295. */
  296. extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem *sem);
  297. /**
  298. * Wait until a semaphore has a positive value and then decrements it.
  299. *
  300. * This function suspends the calling thread until either the semaphore
  301. * pointed to by `sem` has a positive value, the call is interrupted by a
  302. * signal or error, or the specified time has elapsed. If the call is
  303. * successful it will atomically decrement the semaphore value.
  304. *
  305. * \param sem the semaphore to wait on
  306. * \param timeoutMS the length of the timeout, in milliseconds
  307. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
  308. * succeed in the allotted time, or a negative error code on failure;
  309. * call SDL_GetError() for more information.
  310. *
  311. * \since This function is available since SDL 3.0.0.
  312. *
  313. * \sa SDL_CreateSemaphore
  314. * \sa SDL_DestroySemaphore
  315. * \sa SDL_SemPost
  316. * \sa SDL_SemTryWait
  317. * \sa SDL_SemValue
  318. * \sa SDL_SemWait
  319. */
  320. extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Sint32 timeoutMS);
  321. /**
  322. * Atomically increment a semaphore's value and wake waiting threads.
  323. *
  324. * \param sem the semaphore to increment
  325. * \returns 0 on success or a negative error code on failure; call
  326. * SDL_GetError() for more information.
  327. *
  328. * \since This function is available since SDL 3.0.0.
  329. *
  330. * \sa SDL_CreateSemaphore
  331. * \sa SDL_DestroySemaphore
  332. * \sa SDL_SemTryWait
  333. * \sa SDL_SemValue
  334. * \sa SDL_SemWait
  335. * \sa SDL_SemWaitTimeout
  336. */
  337. extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem *sem);
  338. /**
  339. * Get the current value of a semaphore.
  340. *
  341. * \param sem the semaphore to query
  342. * \returns the current value of the semaphore.
  343. *
  344. * \since This function is available since SDL 3.0.0.
  345. *
  346. * \sa SDL_CreateSemaphore
  347. */
  348. extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem *sem);
  349. /* @} *//* Semaphore functions */
  350. /**
  351. * \name Condition variable functions
  352. */
  353. /* @{ */
  354. /* The SDL condition variable structure, defined in SDL_syscond.c */
  355. struct SDL_cond;
  356. typedef struct SDL_cond SDL_cond;
  357. /**
  358. * Create a condition variable.
  359. *
  360. * \returns a new condition variable or NULL on failure; call SDL_GetError()
  361. * for more information.
  362. *
  363. * \since This function is available since SDL 3.0.0.
  364. *
  365. * \sa SDL_CondBroadcast
  366. * \sa SDL_CondSignal
  367. * \sa SDL_CondWait
  368. * \sa SDL_CondWaitTimeout
  369. * \sa SDL_DestroyCond
  370. */
  371. extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
  372. /**
  373. * Destroy a condition variable.
  374. *
  375. * \param cond the condition variable to destroy
  376. *
  377. * \since This function is available since SDL 3.0.0.
  378. *
  379. * \sa SDL_CondBroadcast
  380. * \sa SDL_CondSignal
  381. * \sa SDL_CondWait
  382. * \sa SDL_CondWaitTimeout
  383. * \sa SDL_CreateCond
  384. */
  385. extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond *cond);
  386. /**
  387. * Restart one of the threads that are waiting on the condition variable.
  388. *
  389. * \param cond the condition variable to signal
  390. * \returns 0 on success or a negative error code on failure; call
  391. * SDL_GetError() for more information.
  392. *
  393. * \since This function is available since SDL 3.0.0.
  394. *
  395. * \sa SDL_CondBroadcast
  396. * \sa SDL_CondWait
  397. * \sa SDL_CondWaitTimeout
  398. * \sa SDL_CreateCond
  399. * \sa SDL_DestroyCond
  400. */
  401. extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond *cond);
  402. /**
  403. * Restart all threads that are waiting on the condition variable.
  404. *
  405. * \param cond the condition variable to signal
  406. * \returns 0 on success or a negative error code on failure; call
  407. * SDL_GetError() for more information.
  408. *
  409. * \since This function is available since SDL 3.0.0.
  410. *
  411. * \sa SDL_CondSignal
  412. * \sa SDL_CondWait
  413. * \sa SDL_CondWaitTimeout
  414. * \sa SDL_CreateCond
  415. * \sa SDL_DestroyCond
  416. */
  417. extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond *cond);
  418. /**
  419. * Wait until a condition variable is signaled.
  420. *
  421. * This function unlocks the specified `mutex` and waits for another thread to
  422. * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  423. * `cond`. Once the condition variable is signaled, the mutex is re-locked and
  424. * the function returns.
  425. *
  426. * The mutex must be locked before calling this function. Locking the mutex
  427. * recursively (more than once) is not supported and leads to undefined
  428. * behavior.
  429. *
  430. * This function is the equivalent of calling SDL_CondWaitTimeout() with a
  431. * time length of `SDL_MUTEX_MAXWAIT`.
  432. *
  433. * \param cond the condition variable to wait on
  434. * \param mutex the mutex used to coordinate thread access
  435. * \returns 0 when it is signaled or a negative error code on failure; call
  436. * SDL_GetError() for more information.
  437. *
  438. * \since This function is available since SDL 3.0.0.
  439. *
  440. * \sa SDL_CondBroadcast
  441. * \sa SDL_CondSignal
  442. * \sa SDL_CondWaitTimeout
  443. * \sa SDL_CreateCond
  444. * \sa SDL_DestroyCond
  445. */
  446. extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond *cond, SDL_mutex *mutex);
  447. /**
  448. * Wait until a condition variable is signaled or a certain time has passed.
  449. *
  450. * This function unlocks the specified `mutex` and waits for another thread to
  451. * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  452. * `cond`, or for the specified time to elapse. Once the condition variable is
  453. * signaled or the time elapsed, the mutex is re-locked and the function
  454. * returns.
  455. *
  456. * The mutex must be locked before calling this function. Locking the mutex
  457. * recursively (more than once) is not supported and leads to undefined
  458. * behavior.
  459. *
  460. * \param cond the condition variable to wait on
  461. * \param mutex the mutex used to coordinate thread access
  462. * \param timeoutMS the maximum time to wait, in milliseconds, or
  463. * `SDL_MUTEX_MAXWAIT` to wait indefinitely
  464. * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
  465. * the condition is not signaled in the allotted time, or a negative
  466. * error code on failure; call SDL_GetError() for more information.
  467. *
  468. * \since This function is available since SDL 3.0.0.
  469. *
  470. * \sa SDL_CondBroadcast
  471. * \sa SDL_CondSignal
  472. * \sa SDL_CondWait
  473. * \sa SDL_CreateCond
  474. * \sa SDL_DestroyCond
  475. */
  476. extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond *cond,
  477. SDL_mutex *mutex, Sint32 timeoutMS);
  478. /* @} *//* Condition variable functions */
  479. /* Ends C function definitions when using C++ */
  480. #ifdef __cplusplus
  481. }
  482. #endif
  483. #include <SDL3/SDL_close_code.h>
  484. #endif /* SDL_mutex_h_ */