SDL_mutex.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. SDL - Simple DirectMedia Layer
  3. Copyright (C) 1997-2010 Sam Lantinga
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. Sam Lantinga
  16. [email protected]
  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. /* *INDENT-OFF* */
  31. extern "C" {
  32. /* *INDENT-ON* */
  33. #endif
  34. /**
  35. * Synchronization functions which can time out return this value
  36. * if they time out.
  37. */
  38. #define SDL_MUTEX_TIMEDOUT 1
  39. /**
  40. * This is the timeout value which corresponds to never time out.
  41. */
  42. #define SDL_MUTEX_MAXWAIT (~(Uint32)0)
  43. /**
  44. * \name Mutex functions
  45. */
  46. /*@{*/
  47. /* The SDL mutex structure, defined in SDL_mutex.c */
  48. struct SDL_mutex;
  49. typedef struct SDL_mutex SDL_mutex;
  50. /**
  51. * Create a mutex, initialized unlocked.
  52. */
  53. extern DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void);
  54. /**
  55. * Lock the mutex.
  56. *
  57. * \return 0, or -1 on error.
  58. */
  59. #define SDL_LockMutex(m) SDL_mutexP(m)
  60. extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex * mutex);
  61. /**
  62. * Unlock the mutex.
  63. *
  64. * \return 0, or -1 on error.
  65. *
  66. * \warning It is an error to unlock a mutex that has not been locked by
  67. * the current thread, and doing so results in undefined behavior.
  68. */
  69. #define SDL_UnlockMutex(m) SDL_mutexV(m)
  70. extern DECLSPEC int SDLCALL SDL_mutexV(SDL_mutex * mutex);
  71. /**
  72. * Destroy a mutex.
  73. */
  74. extern DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex * mutex);
  75. /*@}*//*Mutex functions*/
  76. /**
  77. * \name Semaphore functions
  78. */
  79. /*@{*/
  80. /* The SDL semaphore structure, defined in SDL_sem.c */
  81. struct SDL_semaphore;
  82. typedef struct SDL_semaphore SDL_sem;
  83. /**
  84. * Create a semaphore, initialized with value, returns NULL on failure.
  85. */
  86. extern DECLSPEC SDL_sem *SDLCALL SDL_CreateSemaphore(Uint32 initial_value);
  87. /**
  88. * Destroy a semaphore.
  89. */
  90. extern DECLSPEC void SDLCALL SDL_DestroySemaphore(SDL_sem * sem);
  91. /**
  92. * This function suspends the calling thread until the semaphore pointed
  93. * to by \c sem has a positive count. It then atomically decreases the
  94. * semaphore count.
  95. */
  96. extern DECLSPEC int SDLCALL SDL_SemWait(SDL_sem * sem);
  97. /**
  98. * Non-blocking variant of SDL_SemWait().
  99. *
  100. * \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait would
  101. * block, and -1 on error.
  102. */
  103. extern DECLSPEC int SDLCALL SDL_SemTryWait(SDL_sem * sem);
  104. /**
  105. * Variant of SDL_SemWait() with a timeout in milliseconds.
  106. *
  107. * \return 0 if the wait succeeds, ::SDL_MUTEX_TIMEDOUT if the wait does not
  108. * succeed in the allotted time, and -1 on error.
  109. *
  110. * \warning On some platforms this function is implemented by looping with a
  111. * delay of 1 ms, and so should be avoided if possible.
  112. */
  113. extern DECLSPEC int SDLCALL SDL_SemWaitTimeout(SDL_sem * sem, Uint32 ms);
  114. /**
  115. * Atomically increases the semaphore's count (not blocking).
  116. *
  117. * \return 0, or -1 on error.
  118. */
  119. extern DECLSPEC int SDLCALL SDL_SemPost(SDL_sem * sem);
  120. /**
  121. * Returns the current count of the semaphore.
  122. */
  123. extern DECLSPEC Uint32 SDLCALL SDL_SemValue(SDL_sem * sem);
  124. /*@}*//*Semaphore functions*/
  125. /**
  126. * \name Condition variable functions
  127. */
  128. /*@{*/
  129. /* The SDL condition variable structure, defined in SDL_cond.c */
  130. struct SDL_cond;
  131. typedef struct SDL_cond SDL_cond;
  132. /**
  133. * Create a condition variable.
  134. */
  135. extern DECLSPEC SDL_cond *SDLCALL SDL_CreateCond(void);
  136. /**
  137. * Destroy a condition variable.
  138. */
  139. extern DECLSPEC void SDLCALL SDL_DestroyCond(SDL_cond * cond);
  140. /**
  141. * Restart one of the threads that are waiting on the condition variable.
  142. *
  143. * \return 0 or -1 on error.
  144. */
  145. extern DECLSPEC int SDLCALL SDL_CondSignal(SDL_cond * cond);
  146. /**
  147. * Restart all threads that are waiting on the condition variable.
  148. * \return 0 or -1 on error.
  149. */
  150. extern DECLSPEC int SDLCALL SDL_CondBroadcast(SDL_cond * cond);
  151. /**
  152. * Wait on the condition variable, unlocking the provided mutex.
  153. *
  154. * \warning The mutex must be locked before entering this function!
  155. *
  156. * The mutex is re-locked once the condition variable is signaled.
  157. *
  158. * \return 0 when it is signaled, or -1 on error.
  159. */
  160. extern DECLSPEC int SDLCALL SDL_CondWait(SDL_cond * cond, SDL_mutex * mutex);
  161. /**
  162. * Waits for at most \c ms milliseconds, and returns 0 if the condition
  163. * variable is signaled, ::SDL_MUTEX_TIMEDOUT if the condition is not
  164. * signaled in the allotted time, and -1 on error.
  165. *
  166. * \warning On some platforms this function is implemented by looping with a
  167. * delay of 1 ms, and so should be avoided if possible.
  168. */
  169. extern DECLSPEC int SDLCALL SDL_CondWaitTimeout(SDL_cond * cond,
  170. SDL_mutex * mutex, Uint32 ms);
  171. /*@}*//*Condition variable functions*/
  172. /* Ends C function definitions when using C++ */
  173. #ifdef __cplusplus
  174. /* *INDENT-OFF* */
  175. }
  176. /* *INDENT-ON* */
  177. #endif
  178. #include "close_code.h"
  179. #endif /* _SDL_mutex_h */
  180. /* vi: set ts=4 sw=4 expandtab: */