hmutex.h 8.1 KB

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