hmutex.h 8.1 KB

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