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.

SDL_mutex.h 6.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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 mutex, initialized unlocked.
  50. */
  51. extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
  52. /**
  53. * Lock the mutex.
  54. *
  55. * \return 0, or -1 on error.
  56. */
  57. #define SDL_mutexP(m) SDL_LockMutex(m)
  58. extern DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex * mutex);
  59. /**
  60. * Try to lock the mutex
  61. *
  62. * \return 0, SDL_MUTEX_TIMEDOUT, or -1 on error
  63. */
  64. extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
  65. /**
  66. * Unlock the mutex.
  67. *
  68. * \return 0, or -1 on error.
  69. *
  70. * \warning It is an error to unlock a mutex that has not been locked by
  71. * the current thread, and doing so results in undefined behavior.
  72. */
  73. #define SDL_mutexV(m) SDL_UnlockMutex(m)
  74. extern DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex * mutex);
  75. /**
  76. * Destroy a mutex.
  77. */
  78. extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
  79. /* @} *//* Mutex functions */
  80. /**
  81. * \name Semaphore functions
  82. */
  83. /* @{ */
  84. /* The SDL semaphore structure, defined in SDL_syssem.c */
  85. struct SDL_semaphore;
  86. typedef struct SDL_semaphore SDL_sem;
  87. /**
  88. * Create a semaphore, initialized with value, returns NULL on failure.
  89. */
  90. extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
  91. /**
  92. * Destroy a semaphore.
  93. */
  94. extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
  95. /**
  96. * This function suspends the calling thread until the semaphore pointed
  97. * to by \c sem has a positive count. It then atomically decreases the
  98. * semaphore count.
  99. */
  100. extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
  101. /**
  102. * Non-blocking variant of SDL_SemWait().
  103. *
  104. * \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait would
  105. * block, and -1 on error.
  106. */
  107. extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
  108. /**
  109. * Variant of SDL_SemWait() with a timeout in milliseconds.
  110. *
  111. * \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait does not
  112. * succeed in the allotted time, and -1 on error.
  113. *
  114. * \warning On some platforms this function is implemented by looping with a
  115. * delay of 1 ms, and so should be avoided if possible.
  116. */
  117. extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
  118. /**
  119. * Atomically increases the semaphore's count (not blocking).
  120. *
  121. * \return 0, or -1 on error.
  122. */
  123. extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
  124. /**
  125. * Returns the current count of the semaphore.
  126. */
  127. extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
  128. /* @} *//* Semaphore functions */
  129. /**
  130. * \name Condition variable functions
  131. */
  132. /* @{ */
  133. /* The SDL condition variable structure, defined in SDL_syscond.c */
  134. struct SDL_cond;
  135. typedef struct SDL_cond SDL_cond;
  136. /**
  137. * Create a condition variable.
  138. *
  139. * Typical use of condition variables:
  140. *
  141. * Thread A:
  142. * SDL_LockMutex(lock);
  143. * while ( ! condition ) {
  144. * SDL_CondWait(cond, lock);
  145. * }
  146. * SDL_UnlockMutex(lock);
  147. *
  148. * Thread B:
  149. * SDL_LockMutex(lock);
  150. * ...
  151. * condition = true;
  152. * ...
  153. * SDL_CondSignal(cond);
  154. * SDL_UnlockMutex(lock);
  155. *
  156. * There is some discussion whether to signal the condition variable
  157. * with the mutex locked or not. There is some potential performance
  158. * benefit to unlocking first on some platforms, but there are some
  159. * potential race conditions depending on how your code is structured.
  160. *
  161. * In general it's safer to signal the condition variable while the
  162. * mutex is locked.
  163. */
  164. extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
  165. /**
  166. * Destroy a condition variable.
  167. */
  168. extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
  169. /**
  170. * Restart one of the threads that are waiting on the condition variable.
  171. *
  172. * \return 0 or -1 on error.
  173. */
  174. extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
  175. /**
  176. * Restart all threads that are waiting on the condition variable.
  177. *
  178. * \return 0 or -1 on error.
  179. */
  180. extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
  181. /**
  182. * Wait on the condition variable, unlocking the provided mutex.
  183. *
  184. * \warning The mutex must be locked before entering this function!
  185. *
  186. * The mutex is re-locked once the condition variable is signaled.
  187. *
  188. * \return 0 when it is signaled, or -1 on error.
  189. */
  190. extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
  191. /**
  192. * Waits for at most \c ms milliseconds, and returns 0 if the condition
  193. * variable is signaled, ::SDL_MUTEX_TIMEDOUT if the condition is not
  194. * signaled in the allotted time, and -1 on error.
  195. *
  196. * \warning On some platforms this function is implemented by looping with a
  197. * delay of 1 ms, and so should be avoided if possible.
  198. */
  199. extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
  200. SDL_mutex * mutex, Uint32 ms);
  201. /* @} *//* Condition variable functions */
  202. /* Ends C function definitions when using C++ */
  203. #ifdef __cplusplus
  204. }
  205. #endif
  206. #include "close_code.h"
  207. #endif /* SDL_mutex_h_ */
  208. /* vi: set ts=4 sw=4 expandtab: */