🛠️🐜 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.

545 lines
16 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. /******************************************************************************/
  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(x) \
  46. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_before(x))
  47. #define SDL_ACQUIRED_AFTER(x) \
  48. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquired_after(x))
  49. #define SDL_REQUIRES(x) \
  50. SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_capability(x))
  51. #define SDL_REQUIRES_SHARED(x) \
  52. SDL_THREAD_ANNOTATION_ATTRIBUTE__(requires_shared_capability(x))
  53. #define SDL_ACQUIRE(x) \
  54. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_capability(x))
  55. #define SDL_ACQUIRE_SHARED(x) \
  56. SDL_THREAD_ANNOTATION_ATTRIBUTE__(acquire_shared_capability(x))
  57. #define SDL_RELEASE(x) \
  58. SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_capability(x))
  59. #define SDL_RELEASE_SHARED(x) \
  60. SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_shared_capability(x))
  61. #define SDL_RELEASE_GENERIC(x) \
  62. SDL_THREAD_ANNOTATION_ATTRIBUTE__(release_generic_capability(x))
  63. #define SDL_TRY_ACQUIRE(x, y) \
  64. SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_capability(x, y))
  65. #define SDL_TRY_ACQUIRE_SHARED(x, y) \
  66. SDL_THREAD_ANNOTATION_ATTRIBUTE__(try_acquire_shared_capability(x, y))
  67. #define SDL_EXCLUDES(x) \
  68. SDL_THREAD_ANNOTATION_ATTRIBUTE__(locks_excluded(x))
  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 "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 (~(Uint32)0)
  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 2.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. * \return 0, or -1 on error.
  133. *
  134. * \since This function is available since SDL 2.0.0.
  135. */
  136. extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex) SDL_ACQUIRE(mutex);
  137. #define SDL_mutexP(m) SDL_LockMutex(m)
  138. /**
  139. * Try to lock a mutex without blocking.
  140. *
  141. * This works just like SDL_LockMutex(), but if the mutex is not available,
  142. * this function returns `SDL_MUTEX_TIMEOUT` immediately.
  143. *
  144. * This technique is useful if you need exclusive access to a resource but
  145. * don't want to wait for it, and will return to it to try again later.
  146. *
  147. * \param mutex the mutex to try to lock
  148. * \returns 0, `SDL_MUTEX_TIMEDOUT`, or -1 on error; call SDL_GetError() for
  149. * more information.
  150. *
  151. * \since This function is available since SDL 2.0.0.
  152. *
  153. * \sa SDL_CreateMutex
  154. * \sa SDL_DestroyMutex
  155. * \sa SDL_LockMutex
  156. * \sa SDL_UnlockMutex
  157. */
  158. extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex) SDL_TRY_ACQUIRE(0, mutex);
  159. /**
  160. * Unlock the mutex.
  161. *
  162. * It is legal for the owning thread to lock an already-locked mutex. It must
  163. * unlock it the same number of times before it is actually made available for
  164. * other threads in the system (this is known as a "recursive mutex").
  165. *
  166. * It is an error to unlock a mutex that has not been locked by the current
  167. * thread, and doing so results in undefined behavior.
  168. *
  169. * It is also an error to unlock a mutex that isn't locked at all.
  170. *
  171. * \param mutex the mutex to unlock.
  172. * \returns 0, or -1 on error.
  173. *
  174. * \since This function is available since SDL 2.0.0.
  175. */
  176. extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex) SDL_RELEASE(mutex);
  177. #define SDL_mutexV(m) SDL_UnlockMutex(m)
  178. /**
  179. * Destroy a mutex created with SDL_CreateMutex().
  180. *
  181. * This function must be called on any mutex that is no longer needed. Failure
  182. * to destroy a mutex will result in a system memory or resource leak. While
  183. * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
  184. * to destroy a locked mutex, and may result in undefined behavior depending
  185. * on the platform.
  186. *
  187. * \param mutex the mutex to destroy
  188. *
  189. * \since This function is available since SDL 2.0.0.
  190. *
  191. * \sa SDL_CreateMutex
  192. * \sa SDL_LockMutex
  193. * \sa SDL_TryLockMutex
  194. * \sa SDL_UnlockMutex
  195. */
  196. extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
  197. /* @} *//* Mutex functions */
  198. /**
  199. * \name Semaphore functions
  200. */
  201. /* @{ */
  202. /* The SDL semaphore structure, defined in SDL_syssem.c */
  203. struct SDL_semaphore;
  204. typedef struct SDL_semaphore SDL_sem;
  205. /**
  206. * Create a semaphore.
  207. *
  208. * This function creates a new semaphore and initializes it with the value
  209. * `initial_value`. Each wait operation on the semaphore will atomically
  210. * decrement the semaphore value and potentially block if the semaphore value
  211. * is 0. Each post operation will atomically increment the semaphore value and
  212. * wake waiting threads and allow them to retry the wait operation.
  213. *
  214. * \param initial_value the starting value of the semaphore
  215. * \returns a new semaphore or NULL on failure; call SDL_GetError() for more
  216. * information.
  217. *
  218. * \since This function is available since SDL 2.0.0.
  219. *
  220. * \sa SDL_DestroySemaphore
  221. * \sa SDL_SemPost
  222. * \sa SDL_SemTryWait
  223. * \sa SDL_SemValue
  224. * \sa SDL_SemWait
  225. * \sa SDL_SemWaitTimeout
  226. */
  227. extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
  228. /**
  229. * Destroy a semaphore.
  230. *
  231. * It is not safe to destroy a semaphore if there are threads currently
  232. * waiting on it.
  233. *
  234. * \param sem the semaphore to destroy
  235. *
  236. * \since This function is available since SDL 2.0.0.
  237. *
  238. * \sa SDL_CreateSemaphore
  239. * \sa SDL_SemPost
  240. * \sa SDL_SemTryWait
  241. * \sa SDL_SemValue
  242. * \sa SDL_SemWait
  243. * \sa SDL_SemWaitTimeout
  244. */
  245. extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
  246. /**
  247. * Wait until a semaphore has a positive value and then decrements it.
  248. *
  249. * This function suspends the calling thread until either the semaphore
  250. * pointed to by `sem` has a positive value or the call is interrupted by a
  251. * signal or error. If the call is successful it will atomically decrement the
  252. * semaphore value.
  253. *
  254. * This function is the equivalent of calling SDL_SemWaitTimeout() with a time
  255. * length of `SDL_MUTEX_MAXWAIT`.
  256. *
  257. * \param sem the semaphore wait on
  258. * \returns 0 on success or a negative error code on failure; call
  259. * SDL_GetError() for more information.
  260. *
  261. * \since This function is available since SDL 2.0.0.
  262. *
  263. * \sa SDL_CreateSemaphore
  264. * \sa SDL_DestroySemaphore
  265. * \sa SDL_SemPost
  266. * \sa SDL_SemTryWait
  267. * \sa SDL_SemValue
  268. * \sa SDL_SemWait
  269. * \sa SDL_SemWaitTimeout
  270. */
  271. extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
  272. /**
  273. * See if a semaphore has a positive value and decrement it if it does.
  274. *
  275. * This function checks to see if the semaphore pointed to by `sem` has a
  276. * positive value and atomically decrements the semaphore value if it does. If
  277. * the semaphore doesn't have a positive value, the function immediately
  278. * returns SDL_MUTEX_TIMEDOUT.
  279. *
  280. * \param sem the semaphore to wait on
  281. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait would
  282. * block, or a negative error code on failure; call SDL_GetError()
  283. * for more information.
  284. *
  285. * \since This function is available since SDL 2.0.0.
  286. *
  287. * \sa SDL_CreateSemaphore
  288. * \sa SDL_DestroySemaphore
  289. * \sa SDL_SemPost
  290. * \sa SDL_SemValue
  291. * \sa SDL_SemWait
  292. * \sa SDL_SemWaitTimeout
  293. */
  294. extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
  295. /**
  296. * Wait until a semaphore has a positive value and then decrements it.
  297. *
  298. * This function suspends the calling thread until either the semaphore
  299. * pointed to by `sem` has a positive value, the call is interrupted by a
  300. * signal or error, or the specified time has elapsed. If the call is
  301. * successful it will atomically decrement the semaphore value.
  302. *
  303. * \param sem the semaphore to wait on
  304. * \param timeout the length of the timeout, in milliseconds
  305. * \returns 0 if the wait succeeds, `SDL_MUTEX_TIMEDOUT` if the wait does not
  306. * succeed in the allotted time, or a negative error code on failure;
  307. * call SDL_GetError() for more information.
  308. *
  309. * \since This function is available since SDL 2.0.0.
  310. *
  311. * \sa SDL_CreateSemaphore
  312. * \sa SDL_DestroySemaphore
  313. * \sa SDL_SemPost
  314. * \sa SDL_SemTryWait
  315. * \sa SDL_SemValue
  316. * \sa SDL_SemWait
  317. */
  318. extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem *sem, Uint32 timeout);
  319. /**
  320. * Atomically increment a semaphore's value and wake waiting threads.
  321. *
  322. * \param sem the semaphore to increment
  323. * \returns 0 on success or a negative error code on failure; call
  324. * SDL_GetError() for more information.
  325. *
  326. * \since This function is available since SDL 2.0.0.
  327. *
  328. * \sa SDL_CreateSemaphore
  329. * \sa SDL_DestroySemaphore
  330. * \sa SDL_SemTryWait
  331. * \sa SDL_SemValue
  332. * \sa SDL_SemWait
  333. * \sa SDL_SemWaitTimeout
  334. */
  335. extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
  336. /**
  337. * Get the current value of a semaphore.
  338. *
  339. * \param sem the semaphore to query
  340. * \returns the current value of the semaphore.
  341. *
  342. * \since This function is available since SDL 2.0.0.
  343. *
  344. * \sa SDL_CreateSemaphore
  345. */
  346. extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
  347. /* @} *//* Semaphore functions */
  348. /**
  349. * \name Condition variable functions
  350. */
  351. /* @{ */
  352. /* The SDL condition variable structure, defined in SDL_syscond.c */
  353. struct SDL_cond;
  354. typedef struct SDL_cond SDL_cond;
  355. /**
  356. * Create a condition variable.
  357. *
  358. * \returns a new condition variable or NULL on failure; call SDL_GetError()
  359. * for more information.
  360. *
  361. * \since This function is available since SDL 2.0.0.
  362. *
  363. * \sa SDL_CondBroadcast
  364. * \sa SDL_CondSignal
  365. * \sa SDL_CondWait
  366. * \sa SDL_CondWaitTimeout
  367. * \sa SDL_DestroyCond
  368. */
  369. extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
  370. /**
  371. * Destroy a condition variable.
  372. *
  373. * \param cond the condition variable to destroy
  374. *
  375. * \since This function is available since SDL 2.0.0.
  376. *
  377. * \sa SDL_CondBroadcast
  378. * \sa SDL_CondSignal
  379. * \sa SDL_CondWait
  380. * \sa SDL_CondWaitTimeout
  381. * \sa SDL_CreateCond
  382. */
  383. extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
  384. /**
  385. * Restart one of the threads that are waiting on the condition variable.
  386. *
  387. * \param cond the condition variable to signal
  388. * \returns 0 on success or a negative error code on failure; call
  389. * SDL_GetError() for more information.
  390. *
  391. * \since This function is available since SDL 2.0.0.
  392. *
  393. * \sa SDL_CondBroadcast
  394. * \sa SDL_CondWait
  395. * \sa SDL_CondWaitTimeout
  396. * \sa SDL_CreateCond
  397. * \sa SDL_DestroyCond
  398. */
  399. extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
  400. /**
  401. * Restart all threads that are waiting on the condition variable.
  402. *
  403. * \param cond the condition variable to signal
  404. * \returns 0 on success or a negative error code on failure; call
  405. * SDL_GetError() for more information.
  406. *
  407. * \since This function is available since SDL 2.0.0.
  408. *
  409. * \sa SDL_CondSignal
  410. * \sa SDL_CondWait
  411. * \sa SDL_CondWaitTimeout
  412. * \sa SDL_CreateCond
  413. * \sa SDL_DestroyCond
  414. */
  415. extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
  416. /**
  417. * Wait until a condition variable is signaled.
  418. *
  419. * This function unlocks the specified `mutex` and waits for another thread to
  420. * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  421. * `cond`. Once the condition variable is signaled, the mutex is re-locked and
  422. * the function returns.
  423. *
  424. * The mutex must be locked before calling this function.
  425. *
  426. * This function is the equivalent of calling SDL_CondWaitTimeout() with a
  427. * time length of `SDL_MUTEX_MAXWAIT`.
  428. *
  429. * \param cond the condition variable to wait on
  430. * \param mutex the mutex used to coordinate thread access
  431. * \returns 0 when it is signaled or a negative error code on failure; call
  432. * SDL_GetError() for more information.
  433. *
  434. * \since This function is available since SDL 2.0.0.
  435. *
  436. * \sa SDL_CondBroadcast
  437. * \sa SDL_CondSignal
  438. * \sa SDL_CondWaitTimeout
  439. * \sa SDL_CreateCond
  440. * \sa SDL_DestroyCond
  441. */
  442. extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
  443. /**
  444. * Wait until a condition variable is signaled or a certain time has passed.
  445. *
  446. * This function unlocks the specified `mutex` and waits for another thread to
  447. * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  448. * `cond`, or for the specified time to elapse. Once the condition variable is
  449. * signaled or the time elapsed, the mutex is re-locked and the function
  450. * returns.
  451. *
  452. * The mutex must be locked before calling this function.
  453. *
  454. * \param cond the condition variable to wait on
  455. * \param mutex the mutex used to coordinate thread access
  456. * \param ms the maximum time to wait, in milliseconds, or `SDL_MUTEX_MAXWAIT`
  457. * to wait indefinitely
  458. * \returns 0 if the condition variable is signaled, `SDL_MUTEX_TIMEDOUT` if
  459. * the condition is not signaled in the allotted time, or a negative
  460. * error code on failure; call SDL_GetError() for more information.
  461. *
  462. * \since This function is available since SDL 2.0.0.
  463. *
  464. * \sa SDL_CondBroadcast
  465. * \sa SDL_CondSignal
  466. * \sa SDL_CondWait
  467. * \sa SDL_CreateCond
  468. * \sa SDL_DestroyCond
  469. */
  470. extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
  471. SDL_mutex * mutex, Uint32 ms);
  472. /* @} *//* Condition variable functions */
  473. /* Ends C function definitions when using C++ */
  474. #ifdef __cplusplus
  475. }
  476. #endif
  477. #include "close_code.h"
  478. #endif /* SDL_mutex_h_ */
  479. /* vi: set ts=4 sw=4 expandtab: */