hmutex.h 9.0 KB

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