SDL_mutex.h 16 KB

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