sdlmutex.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // based on "sdl_mutex.h"
  2. const
  3. {**
  4. * Synchronization functions which can time out return this value
  5. * if they time out.
  6. *}
  7. SDL_MUTEX_TIMEDOUT = 1;
  8. {**
  9. * This is the timeout value which corresponds to never time out.
  10. *}
  11. SDL_MUTEX_MAXWAIT = Not cuint32(0);
  12. { -- Mutex functions -- }
  13. type
  14. { The SDL mutex structure, defined in SDL_sysmutex.c }
  15. PPSDL_Mutex = ^PSDL_Mutex;
  16. PSDL_Mutex = Type Pointer;
  17. {**
  18. * Create a new mutex.
  19. *
  20. * All newly-created mutexes begin in the _unlocked_ state.
  21. *
  22. * Calls to SDL_LockMutex() will not return while the mutex is locked by
  23. * another thread. See SDL_TryLockMutex() to attempt to lock without blocking.
  24. *
  25. * SDL mutexes are reentrant.
  26. *
  27. * \returns the initialized and unlocked mutex or NIL on failure;
  28. * call SDL_GetError() for more information.
  29. *
  30. * \since This function is available since SDL 2.0.0.
  31. *}
  32. function SDL_CreateMutex: PSDL_Mutex; cdecl;
  33. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CreateMutex' {$ENDIF} {$ENDIF};
  34. {**
  35. * Lock the mutex.
  36. *
  37. * This will block until the mutex is available, which is to say it is in the
  38. * unlocked state and the OS has chosen the caller as the next thread to lock
  39. * it. Of all threads waiting to lock the mutex, only one may do so at a time.
  40. *
  41. * It is legal for the owning thread to lock an already-locked mutex. It must
  42. * unlock it the same number of times before it is actually made available for
  43. * other threads in the system (this is known as a "recursive mutex").
  44. *
  45. * \param mutex the mutex to lock
  46. * \return 0, or -1 on error.
  47. *
  48. * \since This function is available since SDL 2.0.0.
  49. *}
  50. function SDL_LockMutex(mutex: PSDL_Mutex): cint; cdecl;
  51. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_LockMutex' {$ENDIF} {$ENDIF};
  52. { SDL2-for-Pascal: SDL_mutexP macro ignored; no benefit for the Pascal units }
  53. //C: #define SDL_mutexP(m) SDL_UnlockMutex(m)
  54. {**
  55. * Try to lock a mutex without blocking.
  56. *
  57. * This works just like SDL_LockMutex(), but if the mutex is not available,
  58. * this function returns SDL_MUTEX_TIMEDOUT immediately.
  59. *
  60. * This technique is useful if you need exclusive access to a resource but
  61. * don't want to wait for it, and will return to it to try again later.
  62. *
  63. * \param mutex the mutex to try to lock
  64. * \returns 0, SDL_MUTEX_TIMEDOUT, or -1 on error; call SDL_GetError() for
  65. * more information.
  66. *
  67. * \since This function is available since SDL 2.0.0.
  68. *}
  69. function SDL_TryLockMutex(mutex: PSDL_Mutex): cint; cdecl;
  70. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_TryLockMutex' {$ENDIF} {$ENDIF};
  71. {**
  72. * Unlock the mutex.
  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. * It is an error to unlock a mutex that has not been locked by the current
  79. * thread, and doing so results in undefined behavior.
  80. *
  81. * It is also an error to unlock a mutex that isn't locked at all.
  82. *
  83. * \param mutex the mutex to unlock.
  84. * \returns 0, or -1 on error.
  85. *
  86. * \since This function is available since SDL 2.0.0.
  87. *}
  88. function SDL_UnlockMutex(mutex: PSDL_Mutex): cint; cdecl;
  89. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_UnlockMutex' {$ENDIF} {$ENDIF};
  90. { SDL2-for-Pascal: SDL_mutexV macro ignored; no benefit for the Pascal units }
  91. //C: #define SDL_mutexV(m) SDL_UnlockMutex(m)
  92. {**
  93. * Destroy a mutex created with SDL_CreateMutex().
  94. *
  95. * This function must be called on any mutex that is no longer needed. Failure
  96. * to destroy a mutex will result in a system memory or resource leak. While
  97. * it is safe to destroy a mutex that is _unlocked_, it is not safe to attempt
  98. * to destroy a locked mutex, and may result in undefined behavior depending
  99. * on the platform.
  100. *
  101. * \param mutex the mutex to destroy
  102. *
  103. * \since This function is available since SDL 2.0.0.
  104. *}
  105. procedure SDL_DestroyMutex(mutex: PSDL_Mutex); cdecl;
  106. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_DestroyMutex' {$ENDIF} {$ENDIF};
  107. { -- Semaphore functions -- }
  108. type
  109. { The SDL semaphore structure, defined in SDL_sem.c }
  110. PPSDL_Sem = ^PSDL_Sem;
  111. PSDL_Sem = Type Pointer;
  112. {**
  113. * Create a semaphore.
  114. *
  115. * This function creates a new semaphore and initializes it with the provided
  116. * initial value. Each wait operation on the semaphore will atomically
  117. * decrement the semaphore value and potentially block if the semaphore value
  118. * is 0. Each post operation will atomically increment the semaphore value and
  119. * wake waiting threads and allow them to retry the wait operation.
  120. *
  121. * \param initial_value the starting value of the semaphore
  122. * \returns a new semaphore or NIL on failure; call SDL_GetError() for more
  123. * information.
  124. *
  125. * \since This function is available since SDL 2.0.0.
  126. *}
  127. function SDL_CreateSemaphore(initial_value: cuint32): PSDL_sem; cdecl;
  128. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CreateSemaphore' {$ENDIF} {$ENDIF};
  129. {**
  130. * Destroy a semaphore.
  131. *
  132. * It is not safe to destroy a semaphore if there are threads currently
  133. * waiting on it.
  134. *
  135. * \param sem the semaphore to destroy
  136. *
  137. * \since This function is available since SDL 2.0.0.
  138. *}
  139. procedure SDL_DestroySemaphore(sem: PSDL_Sem); cdecl;
  140. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_DestroySemaphore' {$ENDIF} {$ENDIF};
  141. {**
  142. * Wait until a semaphore has a positive value and then decrements it.
  143. *
  144. * This function suspends the calling thread until either the semaphore
  145. * pointed to by `sem` has a positive value or the call is interrupted by a
  146. * signal or error. If the call is successful it will atomically decrement the
  147. * semaphore value.
  148. *
  149. * This function is the equivalent of calling SDL_SemWaitTimeout() with a time
  150. * length of SDL_MUTEX_MAXWAIT.
  151. *
  152. * \param sem the semaphore wait on
  153. * \returns 0 on success or a negative error code on failure; call
  154. * SDL_GetError() for more information.
  155. *
  156. * \since This function is available since SDL 2.0.0.
  157. *}
  158. function SDL_SemWait(sem: PSDL_Sem): cint; cdecl;
  159. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SemWait' {$ENDIF} {$ENDIF};
  160. {**
  161. * See if a semaphore has a positive value and decrement it if it does.
  162. *
  163. * This function checks to see if the semaphore pointed to by `sem` has a
  164. * positive value and atomically decrements the semaphore value if it does. If
  165. * the semaphore doesn't have a positive value, the function immediately
  166. * returns SDL_MUTEX_TIMEDOUT.
  167. *
  168. * \param sem the semaphore to wait on
  169. * \returns 0 if the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait would
  170. * block, or a negative error code on failure; call SDL_GetError()
  171. * for more information.
  172. *
  173. * \since This function is available since SDL 2.0.0.
  174. *}
  175. function SDL_SemTryWait(sem: PSDL_Sem): cint; cdecl;
  176. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SemTryWait' {$ENDIF} {$ENDIF};
  177. {**
  178. * Wait until a semaphore has a positive value and then decrements it.
  179. *
  180. * This function suspends the calling thread until either the semaphore
  181. * pointed to by `sem` has a positive value, the call is interrupted by a
  182. * signal or error, or the specified time has elapsed. If the call is
  183. * successful it will atomically decrement the semaphore value.
  184. *
  185. * \param sem the semaphore to wait on
  186. * \param ms the length of the timeout, in milliseconds
  187. * \returns 0 if the wait succeeds, SDL_MUTEX_TIMEDOUT if the wait does not
  188. * succeed in the allotted time, or a negative error code on failure;
  189. * call SDL_GetError() for more information.
  190. *
  191. * \since This function is available since SDL 2.0.0.
  192. *}
  193. function SDL_SemWaitTimeout(sem: PSDL_Sem; ms: cuint32): cint; cdecl;
  194. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SemWaitTimeout' {$ENDIF} {$ENDIF};
  195. {**
  196. * Atomically increment a semaphore's value and wake waiting threads.
  197. *
  198. * \param sem the semaphore to increment
  199. * \returns 0 on success or a negative error code on failure;
  200. * call SDL_GetError() for more information.
  201. *
  202. * \since This function is available since SDL 2.0.0.
  203. *}
  204. function SDL_SemPost(sem: PSDL_Sem): cint; cdecl;
  205. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SemPost' {$ENDIF} {$ENDIF};
  206. {**
  207. * Get the current value of a semaphore.
  208. *
  209. * \param sem the semaphore to query
  210. * \returns the current value of the semaphore.
  211. *
  212. * \since This function is available since SDL 2.0.0.
  213. *}
  214. function SDL_SemValue(sem: PSDL_Sem): cuint32; cdecl;
  215. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_SemValue' {$ENDIF} {$ENDIF};
  216. { -- Condition variable functions -- }
  217. type
  218. { The SDL condition variable structure, defined in SDL_cond.c }
  219. PPSDL_Cond = ^PSDL_Cond;
  220. {**
  221. * The condition variable type.
  222. *
  223. * Typical use of condition variables:
  224. *
  225. * Thread A:
  226. * SDL_LockMutex(lock);
  227. * while ( not condition )
  228. * begin
  229. * SDL_CondWait(cond, lock);
  230. * end;
  231. * SDL_UnlockMutex(lock);
  232. *
  233. * Thread B:
  234. * SDL_LockMutex(lock);
  235. * ...
  236. * condition := true;
  237. * ...
  238. * SDL_CondSignal(cond);
  239. * SDL_UnlockMutex(lock);
  240. *
  241. * There is some discussion whether to signal the condition variable
  242. * with the mutex locked or not. There is some potential performance
  243. * benefit to unlocking first on some platforms, but there are some
  244. * potential race conditions depending on how your code is structured.
  245. *
  246. * In general it's safer to signal the condition variable while the
  247. * mutex is locked.
  248. *}
  249. PSDL_Cond = Type Pointer;
  250. {**
  251. * Create a condition variable.
  252. *
  253. * \returns a new condition variable or NIL on failure; call SDL_GetError()
  254. * for more information.
  255. *
  256. * \since This function is available since SDL 2.0.0.
  257. *}
  258. function SDL_CreateCond: PSDL_Cond; cdecl;
  259. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CreateCond' {$ENDIF} {$ENDIF};
  260. {**
  261. * Destroy a condition variable.
  262. *
  263. * \param cond the condition variable to destroy
  264. *
  265. * \since This function is available since SDL 2.0.0.
  266. *}
  267. procedure SDL_DestroyCond(cond: PSDL_Cond); cdecl;
  268. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_DestroyCond' {$ENDIF} {$ENDIF};
  269. {**
  270. * Restart one of the threads that are waiting on the condition variable.
  271. *
  272. * \param cond the condition variable to signal
  273. * \returns 0 on success or a negative error code on failure;
  274. * call SDL_GetError() for more information.
  275. *
  276. * \since This function is available since SDL 2.0.0.
  277. *}
  278. function SDL_CondSignal(cond: PSDL_Cond): cint; cdecl;
  279. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CondSignal' {$ENDIF} {$ENDIF};
  280. {**
  281. * Restart all threads that are waiting on the condition variable.
  282. *
  283. * \param cond the condition variable to signal
  284. * \returns 0 on success or a negative error code on failure;
  285. * call SDL_GetError() for more information.
  286. *
  287. * \since This function is available since SDL 2.0.0.
  288. *}
  289. function SDL_CondBroadcast(cond: PSDL_Cond): cint; cdecl;
  290. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CondBroadcast' {$ENDIF} {$ENDIF};
  291. {**
  292. * Wait until a condition variable is signaled.
  293. *
  294. * This function unlocks the specified `mutex` and waits for another thread to
  295. * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  296. * `cond`. Once the condition variable is signaled, the mutex is re-locked and
  297. * the function returns.
  298. *
  299. * The mutex must be locked before calling this function.
  300. *
  301. * This function is the equivalent of calling SDL_CondWaitTimeout() with a
  302. * time length of SDL_MUTEX_MAXWAIT.
  303. *
  304. * \param cond the condition variable to wait on
  305. * \param mutex the mutex used to coordinate thread access
  306. * \returns 0 when it is signaled or a negative error code on failure; call
  307. * SDL_GetError() for more information.
  308. *
  309. * \since This function is available since SDL 2.0.0.
  310. *}
  311. function SDL_CondWait(cond: PSDL_Cond; mutex: PSDL_Mutex): cint; cdecl;
  312. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CondWait' {$ENDIF} {$ENDIF};
  313. {**
  314. * Wait until a condition variable is signaled or a certain time has passed.
  315. *
  316. * This function unlocks the specified `mutex` and waits for another thread to
  317. * call SDL_CondSignal() or SDL_CondBroadcast() on the condition variable
  318. * `cond`, or for the specified time to elapse. Once the condition variable is
  319. * signaled or the time elapsed, the mutex is re-locked and the function
  320. * returns.
  321. *
  322. * The mutex must be locked before calling this function.
  323. *
  324. * \param cond the condition variable to wait on
  325. * \param mutex the mutex used to coordinate thread access
  326. * \param ms the maximum time to wait, in milliseconds, or SDL_MUTEX_MAXWAIT
  327. * to wait indefinitely
  328. * \returns 0 if the condition variable is signaled, SDL_MUTEX_TIMEDOUT if
  329. * the condition is not signaled in the allotted time, or a negative
  330. * error code on failure; call SDL_GetError() for more information.
  331. *
  332. * \since This function is available since SDL 2.0.0.
  333. *}
  334. function SDL_CondWaitTimeout(cond: PSDL_Cond; mutex: PSDL_Mutex; ms: cuint32): cint; cdecl;
  335. external {$IFDEF DYNAMIC_LINK}SDL_LibName{$ENDIF} {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_CondWaitTimeout' {$ENDIF} {$ENDIF};