SDL_mutex.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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 for
  76. * 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 0, `SDL_MUTEX_TIMEDOUT`, or -1 on error; call SDL_GetError() for
  94. * 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 for
  107. * 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 decrements it.
  228. *
  229. * This function suspends the calling thread until either the semaphore
  230. * pointed to by `sem` has a positive value, the call is interrupted by a
  231. * signal or error, or the specified time has elapsed. If the call is
  232. * successful it will atomically decrement the semaphore value.
  233. *
  234. * \param sem the semaphore to wait on
  235. * \param ms the length of the timeout, in milliseconds
  236. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
  237. * succeed in the allotted time, or a negative error code on failure;
  238. * call SDL_GetError() for more information.
  239. *
  240. * \sa SDL_CreateSemaphore
  241. * \sa SDL_DestroySemaphore
  242. * \sa SDL_SemPost
  243. * \sa SDL_SemTryWait
  244. * \sa SDL_SemValue
  245. * \sa SDL_SemWait
  246. */
  247. extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
  248. /**
  249. * Atomically increment a semaphore's value and wake waiting threads.
  250. *
  251. * \param sem the semaphore to increment
  252. * \returns 0 on success or a negative error code on failure; call
  253. * SDL_GetError() for more information.
  254. *
  255. * \sa SDL_CreateSemaphore
  256. * \sa SDL_DestroySemaphore
  257. * \sa SDL_SemTryWait
  258. * \sa SDL_SemValue
  259. * \sa SDL_SemWait
  260. * \sa SDL_SemWaitTimeout
  261. */
  262. extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
  263. /**
  264. * Get the current value of a semaphore.
  265. *
  266. * \param sem the semaphore to query
  267. * \returns the current value of the semaphore.
  268. *
  269. * \sa SDL_CreateSemaphore
  270. */
  271. extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
  272. /* @} *//* Semaphore functions */
  273. /**
  274. * \name Condition variable functions
  275. */
  276. /* @{ */
  277. /* The SDL condition variable structure, defined in SDL_syscond.c */
  278. struct SDL_cond;
  279. typedef struct SDL_cond SDL_cond;
  280. /**
  281. * Create a condition variable.
  282. *
  283. * \returns a new condition variable or NULL on failure; call SDL_GetError()
  284. * for more information.
  285. *
  286. * \sa SDL_CondBroadcast
  287. * \sa SDL_CondSignal
  288. * \sa SDL_CondWait
  289. * \sa SDL_CondWaitTimeout
  290. * \sa SDL_DestroyCond
  291. */
  292. extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
  293. /**
  294. * Destroy a condition variable.
  295. *
  296. * \param cond the condition variable to destroy
  297. *
  298. * \sa SDL_CondBroadcast
  299. * \sa SDL_CondSignal
  300. * \sa SDL_CondWait
  301. * \sa SDL_CondWaitTimeout
  302. * \sa SDL_CreateCond
  303. */
  304. extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
  305. /**
  306. * Restart one of the threads that are waiting on the condition variable.
  307. *
  308. * \param cond the condition variable to signal
  309. * \returns 0 on success or a negative error code on failure; call
  310. * SDL_GetError() for more information.
  311. *
  312. * \sa SDL_CondBroadcast
  313. * \sa SDL_CondWait
  314. * \sa SDL_CondWaitTimeout
  315. * \sa SDL_CreateCond
  316. * \sa SDL_DestroyCond
  317. */
  318. extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
  319. /**
  320. * Restart all threads that are waiting on the condition variable.
  321. *
  322. * \param cond the condition variable to signal
  323. * \returns 0 on success or a negative error code on failure; call
  324. * SDL_GetError() for more information.
  325. *
  326. * \sa SDL_CondSignal
  327. * \sa SDL_CondWait
  328. * \sa SDL_CondWaitTimeout
  329. * \sa SDL_CreateCond
  330. * \sa SDL_DestroyCond
  331. */
  332. extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
  333. /**
  334. * Wait until a condition variable is signaled.
  335. *
  336. * This function unlocks the specified `mutex` and waits for another thread to
  337. * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  338. * `cond`. Once the condition variable is signaled, the mutex is re-locked and
  339. * the function returns.
  340. *
  341. * The mutex must be locked before calling this function.
  342. *
  343. * This function is the equivalent of calling SDL_CondWaitTimeout() with a
  344. * time length of `SDL_MUTEX_MAXWAIT`.
  345. *
  346. * \param cond the condition variable to wait on
  347. * \param mutex the mutex used to coordinate thread access
  348. * \returns 0 when it is signaled or a negative error code on failure; call
  349. * SDL_GetError() for more information.
  350. *
  351. * \sa SDL_CondBroadcast
  352. * \sa SDL_CondSignal
  353. * \sa SDL_CondWaitTimeout
  354. * \sa SDL_CreateCond
  355. * \sa SDL_DestroyCond
  356. */
  357. extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
  358. /**
  359. * Wait until a condition variable is signaled or a certain time has passed.
  360. *
  361. * This function unlocks the specified `mutex` and waits for another thread to
  362. * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  363. * `cond`, or for the specified time to elapse. Once the condition variable is
  364. * signaled or the time elapsed, the mutex is re-locked and the function
  365. * returns.
  366. *
  367. * The mutex must be locked before calling this function.
  368. *
  369. * \param cond the condition variable to wait on
  370. * \param mutex the mutex used to coordinate thread access
  371. * \param ms the maximum time to wait, in milliseconds, or `SDL_MUTEX_MAXWAIT`
  372. * to wait indefinitely
  373. * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
  374. * the condition is not signaled in the allotted time, or a negative
  375. * error code on failure; call SDL_GetError() for more information.
  376. *
  377. * \sa SDL_CondBroadcast
  378. * \sa SDL_CondSignal
  379. * \sa SDL_CondWait
  380. * \sa SDL_CreateCond
  381. * \sa SDL_DestroyCond
  382. */
  383. extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
  384. SDL_mutex * mutex, Uint32 ms);
  385. /* @} *//* Condition variable functions */
  386. /* Ends C function definitions when using C++ */
  387. #ifdef __cplusplus
  388. }
  389. #endif
  390. #include "close_code.h"
  391. #endif /* SDL_mutex_h_ */
  392. /* vi: set ts=4 sw=4 expandtab: */