1
0

hmutex.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. #ifndef HV_MUTEX_H_
  2. #define HV_MUTEX_H_
  3. #include "hplatform.h"
  4. #ifdef _MSC_VER
  5. #define hmutex_t CRITICAL_SECTION
  6. #define hmutex_init InitializeCriticalSection
  7. #define hmutex_destroy DeleteCriticalSection
  8. #define hmutex_lock EnterCriticalSection
  9. #define hmutex_unlock LeaveCriticalSection
  10. #define HSPINLOCK_COUNT -1
  11. #define hspinlock_t CRITICAL_SECTION
  12. #define hspinlock_init(pspin) InitializeCriticalSectionAndSpinCount(pspin, HSPINLOCK_COUNT)
  13. #define hspinlock_destroy DeleteCriticalSection
  14. #define hspinlock_lock EnterCriticalSection
  15. #define hspinlock_unlock LeaveCriticalSection
  16. #define hrwlock_t SRWLOCK
  17. #define hrwlock_init InitializeSRWLock
  18. #define hrwlock_destroy(plock)
  19. #define hrwlock_rdlock AcquireSRWLockShared
  20. #define hrwlock_rdunlock ReleaseSRWLockShared
  21. #define hrwlock_wrlock AcquireSRWLockExclusive
  22. #define hrwlock_wrunlock ReleaseSRWLockExclusive
  23. #define htimed_mutex_t HANDLE
  24. #define htimed_mutex_init(pmutex) *(pmutex) = CreateMutex(NULL, FALSE, NULL)
  25. #define htimed_mutex_destroy(pmutex) CloseHandle(*(pmutex))
  26. #define htimed_mutex_lock(pmutex) WaitForSingleObject(*(pmutex), INFINITE)
  27. #define htimed_mutex_unlock(pmutex) ReleaseMutex(*(pmutex))
  28. // true: WAIT_OBJECT_0
  29. // false: WAIT_OBJECT_TIMEOUT
  30. #define htimed_mutex_lock_for(pmutex, ms) ( WaitForSingleObject(*(pmutex), ms) == WAIT_OBJECT_0 )
  31. #define hcondvar_t CONDITION_VARIABLE
  32. #define hcondvar_init InitializeConditionVariable
  33. #define hcondvar_destroy(pcond)
  34. #define hcondvar_wait(pcond, pmutex) SleepConditionVariableCS(pcond, pmutex, INFINITE)
  35. #define hcondvar_wait_for(pcond, pmutex, ms) SleepConditionVariableCS(pcond, pmutex, ms)
  36. #define hcondvar_signal WakeConditionVariable
  37. #define hcondvar_broadcast WakeAllConditionVariable
  38. #define honce_t INIT_ONCE
  39. #define HONCE_INIT INIT_ONCE_STATIC_INIT
  40. typedef void (*honce_fn)();
  41. static inline BOOL WINAPI s_once_func(INIT_ONCE* once, PVOID arg, PVOID* _) {
  42. honce_fn fn = (honce_fn)arg;
  43. fn();
  44. return TRUE;
  45. }
  46. static inline void honce(honce_t* once, honce_fn fn) {
  47. PVOID dummy = NULL;
  48. InitOnceExecuteOnce(once, s_once_func, (PVOID)fn, &dummy);
  49. }
  50. #define hsem_t HANDLE
  51. #define hsem_init(psem, value) *(psem) = CreateSemaphore(NULL, value, value+100000, NULL)
  52. #define hsem_destroy(psem) CloseHandle(*(psem))
  53. #define hsem_wait(psem) WaitForSingleObject(*(psem), INFINITE)
  54. #define hsem_post(psem) ReleaseSemaphore(*(psem))
  55. // true: WAIT_OBJECT_0
  56. // false: WAIT_OBJECT_TIMEOUT
  57. #define hsem_wait_for(psem, ms) ( WaitForSingleObject(*(psem), ms) == WAIT_OBJECT_0 )
  58. #else
  59. #define hmutex_t pthread_mutex_t
  60. #define hmutex_init(pmutex) pthread_mutex_init(pmutex, NULL)
  61. #define hmutex_destroy pthread_mutex_destroy
  62. #define hmutex_lock pthread_mutex_lock
  63. #define hmutex_unlock pthread_mutex_unlock
  64. #if HAVE_PTHREAD_SPIN_LOCK
  65. #define hspinlock_t pthread_spinlock_t
  66. #define hspinlock_init(pspin) pthread_spin_init(pspin, PTHREAD_PROCESS_PRIVATE)
  67. #define hspinlock_destroy pthread_spin_destroy
  68. #define hspinlock_lock pthread_spin_lock
  69. #define hspinlock_unlock pthread_spin_unlock
  70. #else
  71. #define hspinlock_t pthread_mutex_t
  72. #define hspinlock_init(pmutex) pthread_mutex_init(pmutex, NULL)
  73. #define hspinlock_destroy pthread_mutex_destroy
  74. #define hspinlock_lock pthread_mutex_lock
  75. #define hspinlock_unlock pthread_mutex_unlock
  76. #endif
  77. #define hrwlock_t pthread_rwlock_t
  78. #define hrwlock_init(prwlock) pthread_rwlock_init(prwlock, NULL)
  79. #define hrwlock_destroy pthread_rwlock_destroy
  80. #define hrwlock_rdlock pthread_rwlock_rdlock
  81. #define hrwlock_rdunlock pthread_rwlock_unlock
  82. #define hrwlock_wrlock pthread_rwlock_wrlock
  83. #define hrwlock_wrunlock pthread_rwlock_unlock
  84. #define htimed_mutex_t pthread_mutex_t
  85. #define htimed_mutex_init(pmutex) pthread_mutex_init(pmutex, NULL)
  86. #define htimed_mutex_destroy pthread_mutex_destroy
  87. #define htimed_mutex_lock pthread_mutex_lock
  88. #define htimed_mutex_unlock pthread_mutex_unlock
  89. // true: OK
  90. // false: ETIMEDOUT
  91. static inline int htimed_mutex_lock_for(htimed_mutex_t* mutex, unsigned int ms) {
  92. #if HAVE_PTHREAD_MUTEX_TIMEDLOCK
  93. struct timespec ts;
  94. struct timeval tv;
  95. gettimeofday(&tv, NULL);
  96. ts.tv_sec = tv.tv_sec + ms / 1000;
  97. ts.tv_nsec = tv.tv_usec * 1000 + ms % 1000 * 1000000;
  98. if (ts.tv_nsec >= 1000000000) {
  99. ts.tv_nsec -= 1000000000;
  100. ts.tv_sec += 1;
  101. }
  102. return pthread_mutex_timedlock(mutex, &ts) != ETIMEDOUT;
  103. #else
  104. int ret = 0;
  105. unsigned int end = gettick() + ms;
  106. while ((ret = pthread_mutex_trylock(mutex)) != 0) {
  107. if (gettick() >= end) {
  108. break;
  109. }
  110. msleep(1);
  111. }
  112. return ret == 0;
  113. #endif
  114. }
  115. #define hcondvar_t pthread_cond_t
  116. #define hcondvar_init(pcond) pthread_cond_init(pcond, NULL)
  117. #define hcondvar_destroy pthread_cond_destroy
  118. #define hcondvar_wait pthread_cond_wait
  119. #define hcondvar_signal pthread_cond_signal
  120. #define hcondvar_broadcast pthread_cond_broadcast
  121. // true: OK
  122. // false: ETIMEDOUT
  123. static inline int hcondvar_wait_for(hcondvar_t* cond, hmutex_t* mutex, unsigned int ms) {
  124. struct timespec ts;
  125. struct timeval tv;
  126. gettimeofday(&tv, NULL);
  127. ts.tv_sec = tv.tv_sec + ms / 1000;
  128. ts.tv_nsec = tv.tv_usec * 1000 + ms % 1000 * 1000000;
  129. if (ts.tv_nsec >= 1000000000) {
  130. ts.tv_nsec -= 1000000000;
  131. ts.tv_sec += 1;
  132. }
  133. return pthread_cond_timedwait(cond, mutex, &ts) != ETIMEDOUT;
  134. }
  135. #define honce_t pthread_once_t
  136. #define HONCE_INIT PTHREAD_ONCE_INIT
  137. #define honce pthread_once
  138. #include <semaphore.h>
  139. #define hsem_t sem_t
  140. #define hsem_init(psem, value) sem_init(psem, 0, value)
  141. #define hsem_destroy sem_destroy
  142. #define hsem_wait sem_wait
  143. #define hsem_post sem_post
  144. // true: OK
  145. // false: ETIMEDOUT
  146. static inline int hsem_wait_for(hsem_t* sem, unsigned int ms) {
  147. #if HAVE_SEM_TIMEDWAIT
  148. struct timespec ts;
  149. struct timeval tv;
  150. gettimeofday(&tv, NULL);
  151. ts.tv_sec = tv.tv_sec + ms / 1000;
  152. ts.tv_nsec = tv.tv_usec * 1000 + ms % 1000 * 1000000;
  153. if (ts.tv_nsec >= 1000000000) {
  154. ts.tv_nsec -= 1000000000;
  155. ts.tv_sec += 1;
  156. }
  157. return sem_timedwait(sem, &ts) != ETIMEDOUT;
  158. #else
  159. int ret = 0;
  160. unsigned int end = gettick() + ms;
  161. while ((ret = sem_trywait(sem)) != 0) {
  162. if (gettick() >= end) {
  163. break;
  164. }
  165. msleep(1);
  166. }
  167. return ret == 0;
  168. #endif
  169. }
  170. #endif
  171. #ifdef __cplusplus
  172. #include <mutex>
  173. #include <condition_variable>
  174. using std::mutex;
  175. // NOTE: test std::timed_mutex incorrect in some platforms, use htimed_mutex_t
  176. // using std::timed_mutex;
  177. using std::condition_variable;
  178. using std::lock_guard;
  179. using std::unique_lock;
  180. namespace hv {
  181. class MutexLock {
  182. public:
  183. MutexLock() { hmutex_init(&_mutex); }
  184. ~MutexLock() { hmutex_destroy(&_mutex); }
  185. void lock() { hmutex_lock(&_mutex); }
  186. void unlock() { hmutex_unlock(&_mutex); }
  187. protected:
  188. hmutex_t _mutex;
  189. };
  190. class SpinLock {
  191. public:
  192. SpinLock() { hspinlock_init(&_spin); }
  193. ~SpinLock() { hspinlock_destroy(&_spin); }
  194. void lock() { hspinlock_lock(&_spin); }
  195. void unlock() { hspinlock_unlock(&_spin); }
  196. protected:
  197. hspinlock_t _spin;
  198. };
  199. class RWLock {
  200. public:
  201. RWLock() { hrwlock_init(&_rwlock); }
  202. ~RWLock() { hrwlock_destroy(&_rwlock); }
  203. void rdlock() { hrwlock_rdlock(&_rwlock); }
  204. void rdunlock() { hrwlock_rdunlock(&_rwlock); }
  205. void wrlock() { hrwlock_wrlock(&_rwlock); }
  206. void wrunlock() { hrwlock_wrunlock(&_rwlock); }
  207. protected:
  208. hrwlock_t _rwlock;
  209. };
  210. } // end namespace
  211. #endif // __cplusplus
  212. #endif // HV_MUTEX_H_