hmutex.h 8.0 KB

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