1
0

hmutex.h 8.9 KB

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