🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

471 lines
14 KiB

  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. * \since This function is available since SDL 2.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 2.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 2.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 2.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 2.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 2.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. */
  176. extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
  177. /**
  178. * Destroy a semaphore.
  179. *
  180. * It is not safe to destroy a semaphore if there are threads currently
  181. * waiting on it.
  182. *
  183. * \param sem the semaphore to destroy
  184. *
  185. * \since This function is available since SDL 2.0.0.
  186. *
  187. * \sa SDL_CreateSemaphore
  188. * \sa SDL_SemPost
  189. * \sa SDL_SemTryWait
  190. * \sa SDL_SemValue
  191. * \sa SDL_SemWait
  192. * \sa SDL_SemWaitTimeout
  193. */
  194. extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
  195. /**
  196. * Wait until a semaphore has a positive value and then decrements it.
  197. *
  198. * This function suspends the calling thread until either the semaphore
  199. * pointed to by `sem` has a positive value or the call is interrupted by a
  200. * signal or error. If the call is successful it will atomically decrement the
  201. * semaphore value.
  202. *
  203. * This function is the equivalent of calling SDL_SemWaitTimeout() with a time
  204. * length of `SDL_MUTEX_MAXWAIT`.
  205. *
  206. * \param sem the semaphore wait on
  207. * \returns 0 on success or a negative error code on failure; call
  208. * SDL_GetError() for more information.
  209. *
  210. * \since This function is available since SDL 2.0.0.
  211. *
  212. * \sa SDL_CreateSemaphore
  213. * \sa SDL_DestroySemaphore
  214. * \sa SDL_SemPost
  215. * \sa SDL_SemTryWait
  216. * \sa SDL_SemValue
  217. * \sa SDL_SemWait
  218. * \sa SDL_SemWaitTimeout
  219. */
  220. extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
  221. /**
  222. * See if a semaphore has a positive value and decrement it if it does.
  223. *
  224. * This function checks to see if the semaphore pointed to by `sem` has a
  225. * positive value and atomically decrements the semaphore value if it does. If
  226. * the semaphore doesn't have a positive value, the function immediately
  227. * returns SDL_MUTEX_TIMEDOUT.
  228. *
  229. * \param sem the semaphore to wait on
  230. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
  231. * block, or a negative error code on failure; call SDL_GetError()
  232. * for more information.
  233. *
  234. * \since This function is available since SDL 2.0.0.
  235. *
  236. * \sa SDL_CreateSemaphore
  237. * \sa SDL_DestroySemaphore
  238. * \sa SDL_SemPost
  239. * \sa SDL_SemValue
  240. * \sa SDL_SemWait
  241. * \sa SDL_SemWaitTimeout
  242. */
  243. extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
  244. /**
  245. * Wait until a semaphore has a positive value and then decrements it.
  246. *
  247. * This function suspends the calling thread until either the semaphore
  248. * pointed to by `sem` has a positive value, the call is interrupted by a
  249. * signal or error, or the specified time has elapsed. If the call is
  250. * successful it will atomically decrement the semaphore value.
  251. *
  252. * \param sem the semaphore to wait on
  253. * \param ms the length of the timeout, in milliseconds
  254. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
  255. * succeed in the allotted time, or a negative error code on failure;
  256. * call SDL_GetError() for more information.
  257. *
  258. * \since This function is available since SDL 2.0.0.
  259. *
  260. * \sa SDL_CreateSemaphore
  261. * \sa SDL_DestroySemaphore
  262. * \sa SDL_SemPost
  263. * \sa SDL_SemTryWait
  264. * \sa SDL_SemValue
  265. * \sa SDL_SemWait
  266. */
  267. extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
  268. /**
  269. * Atomically increment a semaphore's value and wake waiting threads.
  270. *
  271. * \param sem the semaphore to increment
  272. * \returns 0 on success or a negative error code on failure; call
  273. * SDL_GetError() for more information.
  274. *
  275. * \since This function is available since SDL 2.0.0.
  276. *
  277. * \sa SDL_CreateSemaphore
  278. * \sa SDL_DestroySemaphore
  279. * \sa SDL_SemTryWait
  280. * \sa SDL_SemValue
  281. * \sa SDL_SemWait
  282. * \sa SDL_SemWaitTimeout
  283. */
  284. extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
  285. /**
  286. * Get the current value of a semaphore.
  287. *
  288. * \param sem the semaphore to query
  289. * \returns the current value of the semaphore.
  290. *
  291. * \since This function is available since SDL 2.0.0.
  292. *
  293. * \sa SDL_CreateSemaphore
  294. */
  295. extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
  296. /* @} *//* Semaphore functions */
  297. /**
  298. * \name Condition variable functions
  299. */
  300. /* @{ */
  301. /* The SDL condition variable structure, defined in SDL_syscond.c */
  302. struct SDL_cond;
  303. typedef struct SDL_cond SDL_cond;
  304. /**
  305. * Create a condition variable.
  306. *
  307. * \returns a new condition variable or NULL on failure; call SDL_GetError()
  308. * for more information.
  309. *
  310. * \since This function is available since SDL 2.0.0.
  311. *
  312. * \sa SDL_CondBroadcast
  313. * \sa SDL_CondSignal
  314. * \sa SDL_CondWait
  315. * \sa SDL_CondWaitTimeout
  316. * \sa SDL_DestroyCond
  317. */
  318. extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
  319. /**
  320. * Destroy a condition variable.
  321. *
  322. * \param cond the condition variable to destroy
  323. *
  324. * \since This function is available since SDL 2.0.0.
  325. *
  326. * \sa SDL_CondBroadcast
  327. * \sa SDL_CondSignal
  328. * \sa SDL_CondWait
  329. * \sa SDL_CondWaitTimeout
  330. * \sa SDL_CreateCond
  331. */
  332. extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
  333. /**
  334. * Restart one of the threads that are waiting on the condition variable.
  335. *
  336. * \param cond the condition variable to signal
  337. * \returns 0 on success or a negative error code on failure; call
  338. * SDL_GetError() for more information.
  339. *
  340. * \since This function is available since SDL 2.0.0.
  341. *
  342. * \sa SDL_CondBroadcast
  343. * \sa SDL_CondWait
  344. * \sa SDL_CondWaitTimeout
  345. * \sa SDL_CreateCond
  346. * \sa SDL_DestroyCond
  347. */
  348. extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
  349. /**
  350. * Restart all threads that are waiting on the condition variable.
  351. *
  352. * \param cond the condition variable to signal
  353. * \returns 0 on success or a negative error code on failure; call
  354. * SDL_GetError() for more information.
  355. *
  356. * \since This function is available since SDL 2.0.0.
  357. *
  358. * \sa SDL_CondSignal
  359. * \sa SDL_CondWait
  360. * \sa SDL_CondWaitTimeout
  361. * \sa SDL_CreateCond
  362. * \sa SDL_DestroyCond
  363. */
  364. extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
  365. /**
  366. * Wait until a condition variable is signaled.
  367. *
  368. * This function unlocks the specified `mutex` and waits for another thread to
  369. * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  370. * `cond`. Once the condition variable is signaled, the mutex is re-locked and
  371. * the function returns.
  372. *
  373. * The mutex must be locked before calling this function.
  374. *
  375. * This function is the equivalent of calling SDL_CondWaitTimeout() with a
  376. * time length of `SDL_MUTEX_MAXWAIT`.
  377. *
  378. * \param cond the condition variable to wait on
  379. * \param mutex the mutex used to coordinate thread access
  380. * \returns 0 when it is signaled or a negative error code on failure; call
  381. * SDL_GetError() for more information.
  382. *
  383. * \since This function is available since SDL 2.0.0.
  384. *
  385. * \sa SDL_CondBroadcast
  386. * \sa SDL_CondSignal
  387. * \sa SDL_CondWaitTimeout
  388. * \sa SDL_CreateCond
  389. * \sa SDL_DestroyCond
  390. */
  391. extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
  392. /**
  393. * Wait until a condition variable is signaled or a certain time has passed.
  394. *
  395. * This function unlocks the specified `mutex` and waits for another thread to
  396. * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  397. * `cond`, or for the specified time to elapse. Once the condition variable is
  398. * signaled or the time elapsed, the mutex is re-locked and the function
  399. * returns.
  400. *
  401. * The mutex must be locked before calling this function.
  402. *
  403. * \param cond the condition variable to wait on
  404. * \param mutex the mutex used to coordinate thread access
  405. * \param ms the maximum time to wait, in milliseconds, or `SDL_MUTEX_MAXWAIT`
  406. * to wait indefinitely
  407. * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
  408. * the condition is not signaled in the allotted time, or a negative
  409. * error code on failure; call SDL_GetError() for more information.
  410. *
  411. * \since This function is available since SDL 2.0.0.
  412. *
  413. * \sa SDL_CondBroadcast
  414. * \sa SDL_CondSignal
  415. * \sa SDL_CondWait
  416. * \sa SDL_CreateCond
  417. * \sa SDL_DestroyCond
  418. */
  419. extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
  420. SDL_mutex * mutex, Uint32 ms);
  421. /* @} *//* Condition variable functions */
  422. /* Ends C function definitions when using C++ */
  423. #ifdef __cplusplus
  424. }
  425. #endif
  426. #include "close_code.h"
  427. #endif /* SDL_mutex_h_ */
  428. /* vi: set ts=4 sw=4 expandtab: */