semaphore.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright 2010-2018 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
  4. */
  5. #include "bx_p.h"
  6. #include <bx/semaphore.h>
  7. #if BX_CONFIG_SUPPORTS_THREADING
  8. #if BX_CRT_NONE
  9. #elif BX_PLATFORM_OSX \
  10. || BX_PLATFORM_IOS
  11. # include <dispatch/dispatch.h>
  12. #elif BX_PLATFORM_POSIX
  13. # include <errno.h>
  14. # include <pthread.h>
  15. # include <semaphore.h>
  16. # include <time.h>
  17. #elif BX_PLATFORM_WINDOWS \
  18. || BX_PLATFORM_WINRT \
  19. || BX_PLATFORM_XBOXONE
  20. # include <windows.h>
  21. # include <limits.h>
  22. # if BX_PLATFORM_XBOXONE
  23. # include <synchapi.h>
  24. # endif // BX_PLATFORM_XBOXONE
  25. #endif // BX_PLATFORM_
  26. namespace bx
  27. {
  28. struct SemaphoreInternal
  29. {
  30. #if BX_CRT_NONE
  31. #elif BX_PLATFORM_OSX \
  32. || BX_PLATFORM_IOS
  33. dispatch_semaphore_t m_handle;
  34. #elif BX_PLATFORM_POSIX
  35. pthread_mutex_t m_mutex;
  36. pthread_cond_t m_cond;
  37. int32_t m_count;
  38. #elif BX_PLATFORM_WINDOWS \
  39. || BX_PLATFORM_WINRT \
  40. || BX_PLATFORM_XBOXONE
  41. HANDLE m_handle;
  42. #endif // BX_PLATFORM_
  43. };
  44. #if BX_CRT_NONE
  45. Semaphore::Semaphore()
  46. {
  47. BX_STATIC_ASSERT(sizeof(SemaphoreInternal) <= sizeof(m_internal) );
  48. }
  49. Semaphore::~Semaphore()
  50. {
  51. }
  52. void Semaphore::post(uint32_t _count)
  53. {
  54. BX_UNUSED(_count);
  55. }
  56. bool Semaphore::wait(int32_t _msecs)
  57. {
  58. BX_UNUSED(_msecs);
  59. return false;
  60. }
  61. #elif BX_PLATFORM_OSX \
  62. || BX_PLATFORM_IOS
  63. Semaphore::Semaphore()
  64. {
  65. BX_STATIC_ASSERT(sizeof(SemaphoreInternal) <= sizeof(m_internal) );
  66. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  67. si->m_handle = dispatch_semaphore_create(0);
  68. BX_CHECK(NULL != si->m_handle, "dispatch_semaphore_create failed.");
  69. }
  70. Semaphore::~Semaphore()
  71. {
  72. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  73. dispatch_release(si->m_handle);
  74. }
  75. void Semaphore::post(uint32_t _count)
  76. {
  77. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  78. for (uint32_t ii = 0; ii < _count; ++ii)
  79. {
  80. dispatch_semaphore_signal(si->m_handle);
  81. }
  82. }
  83. bool Semaphore::wait(int32_t _msecs)
  84. {
  85. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  86. dispatch_time_t dt = 0 > _msecs
  87. ? DISPATCH_TIME_FOREVER
  88. : dispatch_time(DISPATCH_TIME_NOW, int64_t(_msecs)*1000000)
  89. ;
  90. return !dispatch_semaphore_wait(si->m_handle, dt);
  91. }
  92. #elif BX_PLATFORM_POSIX
  93. uint64_t toNs(const timespec& _ts)
  94. {
  95. return _ts.tv_sec*UINT64_C(1000000000) + _ts.tv_nsec;
  96. }
  97. void toTimespecNs(timespec& _ts, uint64_t _nsecs)
  98. {
  99. _ts.tv_sec = _nsecs/UINT64_C(1000000000);
  100. _ts.tv_nsec = _nsecs%UINT64_C(1000000000);
  101. }
  102. void toTimespecMs(timespec& _ts, int32_t _msecs)
  103. {
  104. toTimespecNs(_ts, uint64_t(_msecs)*1000000);
  105. }
  106. void add(timespec& _ts, int32_t _msecs)
  107. {
  108. uint64_t ns = toNs(_ts);
  109. toTimespecNs(_ts, ns + uint64_t(_msecs)*1000000);
  110. }
  111. Semaphore::Semaphore()
  112. {
  113. BX_STATIC_ASSERT(sizeof(SemaphoreInternal) <= sizeof(m_internal) );
  114. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  115. si->m_count = 0;
  116. int result;
  117. result = pthread_mutex_init(&si->m_mutex, NULL);
  118. BX_CHECK(0 == result, "pthread_mutex_init %d", result);
  119. result = pthread_cond_init(&si->m_cond, NULL);
  120. BX_CHECK(0 == result, "pthread_cond_init %d", result);
  121. BX_UNUSED(result);
  122. }
  123. Semaphore::~Semaphore()
  124. {
  125. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  126. int result;
  127. result = pthread_cond_destroy(&si->m_cond);
  128. BX_CHECK(0 == result, "pthread_cond_destroy %d", result);
  129. result = pthread_mutex_destroy(&si->m_mutex);
  130. BX_CHECK(0 == result, "pthread_mutex_destroy %d", result);
  131. BX_UNUSED(result);
  132. }
  133. void Semaphore::post(uint32_t _count)
  134. {
  135. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  136. int result = pthread_mutex_lock(&si->m_mutex);
  137. BX_CHECK(0 == result, "pthread_mutex_lock %d", result);
  138. for (uint32_t ii = 0; ii < _count; ++ii)
  139. {
  140. result = pthread_cond_signal(&si->m_cond);
  141. BX_CHECK(0 == result, "pthread_cond_signal %d", result);
  142. }
  143. si->m_count += _count;
  144. result = pthread_mutex_unlock(&si->m_mutex);
  145. BX_CHECK(0 == result, "pthread_mutex_unlock %d", result);
  146. BX_UNUSED(result);
  147. }
  148. bool Semaphore::wait(int32_t _msecs)
  149. {
  150. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  151. int result = pthread_mutex_lock(&si->m_mutex);
  152. BX_CHECK(0 == result, "pthread_mutex_lock %d", result);
  153. if (-1 == _msecs)
  154. {
  155. while (0 == result
  156. && 0 >= si->m_count)
  157. {
  158. result = pthread_cond_wait(&si->m_cond, &si->m_mutex);
  159. }
  160. }
  161. else
  162. {
  163. timespec ts;
  164. clock_gettime(CLOCK_REALTIME, &ts);
  165. add(ts, _msecs);
  166. while (0 == result
  167. && 0 >= si->m_count)
  168. {
  169. result = pthread_cond_timedwait(&si->m_cond, &si->m_mutex, &ts);
  170. }
  171. }
  172. bool ok = 0 == result;
  173. if (ok)
  174. {
  175. --si->m_count;
  176. }
  177. result = pthread_mutex_unlock(&si->m_mutex);
  178. BX_CHECK(0 == result, "pthread_mutex_unlock %d", result);
  179. BX_UNUSED(result);
  180. return ok;
  181. }
  182. #elif BX_PLATFORM_WINDOWS \
  183. || BX_PLATFORM_WINRT \
  184. || BX_PLATFORM_XBOXONE
  185. Semaphore::Semaphore()
  186. {
  187. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  188. #if BX_PLATFORM_WINRT \
  189. || BX_PLATFORM_XBOXONE
  190. si->m_handle = CreateSemaphoreExW(NULL, 0, LONG_MAX, NULL, 0, SEMAPHORE_ALL_ACCESS);
  191. #else
  192. si->m_handle = CreateSemaphoreA(NULL, 0, LONG_MAX, NULL);
  193. #endif
  194. BX_CHECK(NULL != si->m_handle, "Failed to create Semaphore!");
  195. }
  196. Semaphore::~Semaphore()
  197. {
  198. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  199. CloseHandle(si->m_handle);
  200. }
  201. void Semaphore::post(uint32_t _count)
  202. {
  203. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  204. ReleaseSemaphore(si->m_handle, _count, NULL);
  205. }
  206. bool Semaphore::wait(int32_t _msecs)
  207. {
  208. SemaphoreInternal* si = (SemaphoreInternal*)m_internal;
  209. DWORD milliseconds = (0 > _msecs) ? INFINITE : _msecs;
  210. #if BX_PLATFORM_WINRT \
  211. || BX_PLATFORM_XBOXONE
  212. return WAIT_OBJECT_0 == WaitForSingleObjectEx(si->m_handle, milliseconds, FALSE);
  213. #else
  214. return WAIT_OBJECT_0 == WaitForSingleObject(si->m_handle, milliseconds);
  215. #endif
  216. }
  217. #endif // BX_PLATFORM_
  218. } // namespace bx
  219. #endif // BX_CONFIG_SUPPORTS_THREADING