SDL_mutex.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2021 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 "SDL_stdinc.h"
  26. #include "SDL_error.h"
  27. #include "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 (~(Uint32)0)
  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. * \sa SDL_DestroyMutex
  62. * \sa SDL_LockMutex
  63. * \sa SDL_TryLockMutex
  64. * \sa SDL_UnlockMutex
  65. */
  66. extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
  67. /**
  68. * Lock the mutex.
  69. *
  70. * This will block until the mutex is available, which is to say it is in the
  71. * unlocked state and the OS has chosen the caller as the next thread to lock
  72. * it. Of all threads waiting to lock the mutex, only one may do so at a time.
  73. *
  74. * It is legal for the owning thread to lock an already-locked mutex. It must
  75. * unlock it the same number of times before it is actually made available
  76. * for other threads in the system (this is known as a "recursive mutex").
  77. *
  78. * \param mutex the mutex to lock
  79. * \return 0, or -1 on error.
  80. */
  81. extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex);
  82. #define SDL_mutexP(m) SDL_LockMutex(m)
  83. /**
  84. * Try to lock a mutex without blocking.
  85. *
  86. * This works just like SDL_LockMutex(), but if the mutex is not available,
  87. * this function returns `SDL_MUTEX_TIMEOUT` immediately.
  88. *
  89. * This technique is useful if you need exclusive access to a resource but
  90. * don't want to wait for it, and will return to it to try again later.
  91. *
  92. * \param mutex the mutex to try to lock
  93. * \returns return 0, `SDL_MUTEX_TIMEDOUT`, or -1 on error; call
  94. * SDL_GetError() for more information.
  95. *
  96. * \sa SDL_CreateMutex
  97. * \sa SDL_DestroyMutex
  98. * \sa SDL_LockMutex
  99. * \sa SDL_UnlockMutex
  100. */
  101. extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
  102. /**
  103. * Unlock the mutex.
  104. *
  105. * It is legal for the owning thread to lock an already-locked mutex. It must
  106. * unlock it the same number of times before it is actually made available
  107. * for other threads in the system (this is known as a "recursive mutex").
  108. *
  109. * It is an error to unlock a mutex that has not been locked by the current
  110. * thread, and doing so results in undefined behavior.
  111. *
  112. * It is also an error to unlock a mutex that isn't locked at all.
  113. *
  114. * \param mutex the mutex to unlock.
  115. * \returns 0, or -1 on error.
  116. */
  117. extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex);
  118. #define SDL_mutexV(m) SDL_UnlockMutex(m)
  119. /**
  120. * Destroy a mutex created with SDL_CreateMutex().
  121. *
  122. * This function must be called on any mutex that is no longer needed. Failure
  123. * to destroy a mutex will result in a system memory or resource leak. While
  124. * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
  125. * to destroy a locked mutex, and may result in undefined behavior depending
  126. * on the platform.
  127. *
  128. * \param mutex the mutex to destroy
  129. *
  130. * \sa SDL_CreateMutex
  131. * \sa SDL_LockMutex
  132. * \sa SDL_TryLockMutex
  133. * \sa SDL_UnlockMutex
  134. */
  135. extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
  136. /* @} *//* Mutex functions */
  137. /**
  138. * \name Semaphore functions
  139. */
  140. /* @{ */
  141. /* The SDL semaphore structure, defined in SDL_syssem.c */
  142. struct SDL_semaphore;
  143. typedef struct SDL_semaphore SDL_sem;
  144. /**
  145. * Create a semaphore.
  146. *
  147. * This function creates a new semaphore and initializes it with the value
  148. * `initial_value`. Each wait operation on the semaphore will atomically
  149. * decrement the semaphore value and potentially block if the semaphore value
  150. * is 0. Each post operation will atomically increment the semaphore value and
  151. * wake waiting threads and allow them to retry the wait operation.
  152. *
  153. * \param initial_value the starting value of the semaphore
  154. * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
  155. * information.
  156. *
  157. * \sa SDL_DestroySemaphore
  158. * \sa SDL_SemPost
  159. * \sa SDL_SemTryWait
  160. * \sa SDL_SemValue
  161. * \sa SDL_SemWait
  162. * \sa SDL_SemWaitTimeout
  163. */
  164. extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
  165. /**
  166. * Destroy a semaphore.
  167. *
  168. * It is not safe to destroy a semaphore if there are threads currently
  169. * waiting on it.
  170. *
  171. * \param sem the semaphore to destroy
  172. *
  173. * \sa SDL_CreateSemaphore
  174. * \sa SDL_SemPost
  175. * \sa SDL_SemTryWait
  176. * \sa SDL_SemValue
  177. * \sa SDL_SemWait
  178. * \sa SDL_SemWaitTimeout
  179. */
  180. extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
  181. /**
  182. * Wait until a semaphore has a positive value and then decrements it.
  183. *
  184. * This function suspends the calling thread until either the semaphore
  185. * pointed to by `sem` has a positive value or the call is interrupted by a
  186. * signal or error. If the call is successful it will atomically decrement the
  187. * semaphore value.
  188. *
  189. * This function is the equivalent of calling SDL_SemWaitTimeout() with a time
  190. * length of `SDL_MUTEX_MAXWAIT`.
  191. *
  192. * \param sem the semaphore wait on
  193. * \returns 0 on success or a negative error code on failure; call
  194. * SDL_GetError() for more information.
  195. *
  196. * \sa SDL_CreateSemaphore
  197. * \sa SDL_DestroySemaphore
  198. * \sa SDL_SemPost
  199. * \sa SDL_SemTryWait
  200. * \sa SDL_SemValue
  201. * \sa SDL_SemWait
  202. * \sa SDL_SemWaitTimeout
  203. */
  204. extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
  205. /**
  206. * See if a semaphore has a positive value and decrement it if it does.
  207. *
  208. * This function checks to see if the semaphore pointed to by `sem` has a
  209. * positive value and atomically decrements the semaphore value if it does. If
  210. * the semaphore doesn't have a positive value, the function immediately
  211. * returns SDL_MUTEX_TIMEDOUT.
  212. *
  213. * \param sem the semaphore to wait on
  214. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
  215. * block, or a negative error code on failure; call SDL_GetError()
  216. * for more information.
  217. *
  218. * \sa SDL_CreateSemaphore
  219. * \sa SDL_DestroySemaphore
  220. * \sa SDL_SemPost
  221. * \sa SDL_SemValue
  222. * \sa SDL_SemWait
  223. * \sa SDL_SemWaitTimeout
  224. */
  225. extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
  226. /**
  227. * Wait until a semaphore has a positive value and then
  228. * decrements it.
  229. *
  230. * This function suspends the calling thread until either the semaphore
  231. * pointed to by `sem` has a positive value, the call is interrupted by a
  232. * signal or error, or the specified time has elapsed. If the call is
  233. * successful it will atomically decrement the semaphore value.
  234. *
  235. * \param sem the semaphore to wait on
  236. * \param ms the length of the timeout, in milliseconds
  237. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
  238. * succeed in the allotted time, or a negative error code on failure;
  239. * call SDL_GetError() for more information.
  240. *
  241. * \sa SDL_CreateSemaphore
  242. * \sa SDL_DestroySemaphore
  243. * \sa SDL_SemPost
  244. * \sa SDL_SemTryWait
  245. * \sa SDL_SemValue
  246. * \sa SDL_SemWait
  247. */
  248. extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
  249. /**
  250. * Atomically increment a semaphore's value and wake waiting threads.
  251. *
  252. * \param sem the semaphore to increment
  253. * \returns 0 on success or a negative error code on failure; call
  254. * SDL_GetError() for more information.
  255. *
  256. * \sa SDL_CreateSemaphore
  257. * \sa SDL_DestroySemaphore
  258. * \sa SDL_SemTryWait
  259. * \sa SDL_SemValue
  260. * \sa SDL_SemWait
  261. * \sa SDL_SemWaitTimeout
  262. */
  263. extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
  264. /**
  265. * Get the current value of a semaphore.
  266. *
  267. * \param sem the semaphore to query
  268. * \returns the current value of the semaphore.
  269. *
  270. * \sa SDL_CreateSemaphore
  271. */
  272. extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
  273. /* @} *//* Semaphore functions */
  274. /**
  275. * \name Condition variable functions
  276. */
  277. /* @{ */
  278. /* The SDL condition variable structure, defined in SDL_syscond.c */
  279. struct SDL_cond;
  280. typedef struct SDL_cond SDL_cond;
  281. /**
  282. * Create a condition variable.
  283. *
  284. * \returns a new condition variable or NULL on failure; call SDL_GetError()
  285. * for more information.
  286. *
  287. * \sa SDL_CondBroadcast
  288. * \sa SDL_CondSignal
  289. * \sa SDL_CondWait
  290. * \sa SDL_CondWaitTimeout
  291. * \sa SDL_DestroyCond
  292. */
  293. extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
  294. /**
  295. * Destroy a condition variable.
  296. *
  297. * \param cond the condition variable to destroy
  298. *
  299. * \sa SDL_CondBroadcast
  300. * \sa SDL_CondSignal
  301. * \sa SDL_CondWait
  302. * \sa SDL_CondWaitTimeout
  303. * \sa SDL_CreateCond
  304. */
  305. extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
  306. /**
  307. * Restart one of the threads that are waiting on the condition variable.
  308. *
  309. * \param cond the condition variable to signal
  310. * \returns 0 on success or a negative error code on failure; call
  311. * SDL_GetError() for more information.
  312. *
  313. * \sa SDL_CondBroadcast
  314. * \sa SDL_CondWait
  315. * \sa SDL_CondWaitTimeout
  316. * \sa SDL_CreateCond
  317. * \sa SDL_DestroyCond
  318. */
  319. extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
  320. /**
  321. * Restart all threads that are waiting on the condition variable.
  322. *
  323. * \param cond the condition variable to signal
  324. * \returns 0 on success or a negative error code on failure; call
  325. * SDL_GetError() for more information.
  326. *
  327. * \sa SDL_CondSignal
  328. * \sa SDL_CondWait
  329. * \sa SDL_CondWaitTimeout
  330. * \sa SDL_CreateCond
  331. * \sa SDL_DestroyCond
  332. */
  333. extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
  334. /**
  335. * Wait until a condition variable is signaled.
  336. *
  337. * This function unlocks the specified `mutex` and waits for another thread
  338. * to call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  339. * `cond`. Once the condition variable is signaled, the mutex is re-locked
  340. * and the function returns.
  341. *
  342. * The mutex must be locked before calling this function.
  343. *
  344. * This function is the equivalent of calling SDL_CondWaitTimeout() with a
  345. * time length of `SDL_MUTEX_MAXWAIT`.
  346. *
  347. * \param cond the condition variable to wait on
  348. * \param mutex the mutex used to coordinate thread access
  349. * \returns 0 when it is signaled or a negative error code on failure; call
  350. * SDL_GetError() for more information.
  351. *
  352. * \sa SDL_CondBroadcast
  353. * \sa SDL_CondSignal
  354. * \sa SDL_CondWaitTimeout
  355. * \sa SDL_CreateCond
  356. * \sa SDL_DestroyCond
  357. */
  358. extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
  359. /**
  360. * Wait until a condition variable is signaled or a certain time has passed.
  361. *
  362. * This function unlocks the specified `mutex` and waits for another thread
  363. * to call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  364. * `cond`, or for the specified time to elapse. Once the condition variable
  365. * is signaled or the time elapsed, the mutex is re-locked and the function
  366. * returns.
  367. *
  368. * The mutex must be locked before calling this function.
  369. *
  370. * \param cond the condition variable to wait on
  371. * \param mutex the mutex used to coordinate thread access
  372. * \param ms the maximum time to wait, in milliseconds, or `SDL_MUTEX_MAXWAIT`
  373. * to wait indefinitely
  374. * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
  375. * the condition is not signaled in the allotted time, or a negative
  376. * error code on failure; call SDL_GetError() for more information.
  377. *
  378. * \sa SDL_CondBroadcast
  379. * \sa SDL_CondSignal
  380. * \sa SDL_CondWait
  381. * \sa SDL_CreateCond
  382. * \sa SDL_DestroyCond
  383. */
  384. extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
  385. SDL_mutex * mutex, Uint32 ms);
  386. /* @} *//* Condition variable functions */
  387. /* Ends C function definitions when using C++ */
  388. #ifdef __cplusplus
  389. }
  390. #endif
  391. #include "close_code.h"
  392. #endif /* SDL_mutex_h_ */
  393. /* vi: set ts=4 sw=4 expandtab: */