semaphore.cpp 6.0 KB

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