sem.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2010-2015 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #ifndef BX_SEM_H_HEADER_GUARD
  6. #define BX_SEM_H_HEADER_GUARD
  7. #include "bx.h"
  8. #include "mutex.h"
  9. #if BX_CONFIG_SUPPORTS_THREADING
  10. #if BX_PLATFORM_POSIX
  11. # include <errno.h>
  12. # include <semaphore.h>
  13. # include <time.h>
  14. # include <pthread.h>
  15. #elif BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_WINRT
  16. # include <windows.h>
  17. # include <limits.h>
  18. #endif // BX_PLATFORM_
  19. namespace bx
  20. {
  21. #if BX_PLATFORM_POSIX
  22. # if BX_CONFIG_SEMAPHORE_PTHREAD
  23. class Semaphore
  24. {
  25. BX_CLASS(Semaphore
  26. , NO_COPY
  27. , NO_ASSIGNMENT
  28. );
  29. public:
  30. Semaphore()
  31. : m_count(0)
  32. {
  33. int result;
  34. result = pthread_mutex_init(&m_mutex, NULL);
  35. BX_CHECK(0 == result, "pthread_mutex_init %d", result);
  36. result = pthread_cond_init(&m_cond, NULL);
  37. BX_CHECK(0 == result, "pthread_cond_init %d", result);
  38. BX_UNUSED(result);
  39. }
  40. ~Semaphore()
  41. {
  42. int result;
  43. result = pthread_cond_destroy(&m_cond);
  44. BX_CHECK(0 == result, "pthread_cond_destroy %d", result);
  45. result = pthread_mutex_destroy(&m_mutex);
  46. BX_CHECK(0 == result, "pthread_mutex_destroy %d", result);
  47. BX_UNUSED(result);
  48. }
  49. void post(uint32_t _count = 1)
  50. {
  51. int result = pthread_mutex_lock(&m_mutex);
  52. BX_CHECK(0 == result, "pthread_mutex_lock %d", result);
  53. for (uint32_t ii = 0; ii < _count; ++ii)
  54. {
  55. result = pthread_cond_signal(&m_cond);
  56. BX_CHECK(0 == result, "pthread_cond_signal %d", result);
  57. }
  58. m_count += _count;
  59. result = pthread_mutex_unlock(&m_mutex);
  60. BX_CHECK(0 == result, "pthread_mutex_unlock %d", result);
  61. BX_UNUSED(result);
  62. }
  63. bool wait(int32_t _msecs = -1)
  64. {
  65. int result = pthread_mutex_lock(&m_mutex);
  66. BX_CHECK(0 == result, "pthread_mutex_lock %d", result);
  67. # if BX_PLATFORM_NACL || BX_PLATFORM_OSX || BX_PLATFORM_IOS
  68. BX_UNUSED(_msecs);
  69. BX_CHECK(-1 == _msecs, "NaCl, iOS and OSX don't support pthread_cond_timedwait at this moment.");
  70. while (0 == result
  71. && 0 >= m_count)
  72. {
  73. result = pthread_cond_wait(&m_cond, &m_mutex);
  74. }
  75. # else
  76. timespec ts;
  77. clock_gettime(CLOCK_REALTIME, &ts);
  78. ts.tv_sec += _msecs/1000;
  79. ts.tv_nsec += (_msecs%1000)*1000;
  80. while (0 == result
  81. && 0 >= m_count)
  82. {
  83. result = pthread_cond_timedwait(&m_cond, &m_mutex, &ts);
  84. }
  85. # endif // BX_PLATFORM_NACL || BX_PLATFORM_OSX
  86. bool ok = 0 == result;
  87. if (ok)
  88. {
  89. --m_count;
  90. }
  91. result = pthread_mutex_unlock(&m_mutex);
  92. BX_CHECK(0 == result, "pthread_mutex_unlock %d", result);
  93. BX_UNUSED(result);
  94. return ok;
  95. }
  96. private:
  97. pthread_mutex_t m_mutex;
  98. pthread_cond_t m_cond;
  99. int32_t m_count;
  100. };
  101. # else
  102. class Semaphore
  103. {
  104. BX_CLASS(Semaphore
  105. , NO_COPY
  106. , NO_ASSIGNMENT
  107. );
  108. public:
  109. Semaphore()
  110. {
  111. int32_t result = sem_init(&m_handle, 0, 0);
  112. BX_CHECK(0 == result, "sem_init failed. errno %d", errno);
  113. BX_UNUSED(result);
  114. }
  115. ~Semaphore()
  116. {
  117. int32_t result = sem_destroy(&m_handle);
  118. BX_CHECK(0 == result, "sem_destroy failed. errno %d", errno);
  119. BX_UNUSED(result);
  120. }
  121. void post(uint32_t _count = 1)
  122. {
  123. int32_t result;
  124. for (uint32_t ii = 0; ii < _count; ++ii)
  125. {
  126. result = sem_post(&m_handle);
  127. BX_CHECK(0 == result, "sem_post failed. errno %d", errno);
  128. }
  129. BX_UNUSED(result);
  130. }
  131. bool wait(int32_t _msecs = -1)
  132. {
  133. # if BX_PLATFORM_NACL || BX_PLATFORM_OSX
  134. BX_CHECK(-1 == _msecs, "NaCl and OSX don't support sem_timedwait at this moment."); BX_UNUSED(_msecs);
  135. return 0 == sem_wait(&m_handle);
  136. # else
  137. if (0 > _msecs)
  138. {
  139. int32_t result;
  140. do
  141. {
  142. result = sem_wait(&m_handle);
  143. } // keep waiting when interrupted by a signal handler...
  144. while (-1 == result && EINTR == errno);
  145. BX_CHECK(0 == result, "sem_wait failed. errno %d", errno);
  146. return 0 == result;
  147. }
  148. timespec ts;
  149. clock_gettime(CLOCK_REALTIME, &ts);
  150. ts.tv_sec += _msecs/1000;
  151. ts.tv_nsec += (_msecs%1000)*1000;
  152. return 0 == sem_timedwait(&m_handle, &ts);
  153. # endif // BX_PLATFORM_
  154. }
  155. private:
  156. sem_t m_handle;
  157. };
  158. # endif // BX_CONFIG_SEMAPHORE_PTHREAD
  159. #elif BX_PLATFORM_WINDOWS || BX_PLATFORM_XBOX360 || BX_PLATFORM_WINRT
  160. class Semaphore
  161. {
  162. BX_CLASS(Semaphore
  163. , NO_COPY
  164. , NO_ASSIGNMENT
  165. );
  166. public:
  167. Semaphore()
  168. {
  169. #if BX_PLATFORM_WINRT
  170. m_handle = CreateSemaphoreEx(NULL, 0, LONG_MAX, NULL, 0, SEMAPHORE_ALL_ACCESS);
  171. #else
  172. m_handle = CreateSemaphore(NULL, 0, LONG_MAX, NULL);
  173. #endif
  174. BX_CHECK(NULL != m_handle, "Failed to create Semaphore!");
  175. }
  176. ~Semaphore()
  177. {
  178. CloseHandle(m_handle);
  179. }
  180. void post(uint32_t _count = 1) const
  181. {
  182. ReleaseSemaphore(m_handle, _count, NULL);
  183. }
  184. bool wait(int32_t _msecs = -1) const
  185. {
  186. DWORD milliseconds = (0 > _msecs) ? INFINITE : _msecs;
  187. #if BX_PLATFORM_WINRT
  188. return WAIT_OBJECT_0 == WaitForSingleObjectEx(m_handle, milliseconds, FALSE);
  189. #else
  190. return WAIT_OBJECT_0 == WaitForSingleObject(m_handle, milliseconds);
  191. #endif
  192. }
  193. private:
  194. HANDLE m_handle;
  195. };
  196. #endif // BX_PLATFORM_
  197. } // namespace bx
  198. #endif // BX_CONFIG_SUPPORTS_THREADING
  199. #endif // BX_SEM_H_HEADER_GUARD