std_mutex.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #ifndef MUTEX_H_
  2. #define MUTEX_H_
  3. #define GCC_VER(x,y,z) ((x) * 10000 + (y) * 100 + (z))
  4. #define GCC_VERSION GCC_VER(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  5. #ifndef __has_include
  6. #define __has_include(s) 0
  7. #endif
  8. #if GCC_VERSION >= GCC_VER(4,4,0) && __GXX_EXPERIMENTAL_CXX0X__
  9. // GCC 4.4 provides <mutex>
  10. #include <mutex>
  11. #elif __has_include(<mutex>) && !ANDROID
  12. // Clang + libc++
  13. #include <mutex>
  14. #else
  15. // partial <mutex> implementation for win32/pthread
  16. #include <algorithm>
  17. #if defined(_WIN32)
  18. // WIN32
  19. #define WIN32_LEAN_AND_MEAN
  20. #include <Windows.h>
  21. #else
  22. // POSIX
  23. #include <pthread.h>
  24. #endif
  25. #if (_MSC_VER >= 1600) || (GCC_VERSION >= GCC_VER(4,3,0) && __GXX_EXPERIMENTAL_CXX0X__)
  26. #define USE_RVALUE_REFERENCES
  27. #endif
  28. #if defined(_WIN32) && defined(_M_X64)
  29. #define USE_SRWLOCKS
  30. #endif
  31. namespace std
  32. {
  33. class recursive_mutex
  34. {
  35. #ifdef _WIN32
  36. typedef CRITICAL_SECTION native_type;
  37. #else
  38. typedef pthread_mutex_t native_type;
  39. #endif
  40. public:
  41. typedef native_type* native_handle_type;
  42. recursive_mutex(const recursive_mutex&) /*= delete*/;
  43. recursive_mutex& operator=(const recursive_mutex&) /*= delete*/;
  44. recursive_mutex()
  45. {
  46. #ifdef _WIN32
  47. InitializeCriticalSection(&m_handle);
  48. #else
  49. pthread_mutexattr_t attr;
  50. pthread_mutexattr_init(&attr);
  51. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  52. pthread_mutex_init(&m_handle, &attr);
  53. #endif
  54. }
  55. ~recursive_mutex()
  56. {
  57. #ifdef _WIN32
  58. DeleteCriticalSection(&m_handle);
  59. #else
  60. pthread_mutex_destroy(&m_handle);
  61. #endif
  62. }
  63. void lock()
  64. {
  65. #ifdef _WIN32
  66. EnterCriticalSection(&m_handle);
  67. #else
  68. pthread_mutex_lock(&m_handle);
  69. #endif
  70. }
  71. void unlock()
  72. {
  73. #ifdef _WIN32
  74. LeaveCriticalSection(&m_handle);
  75. #else
  76. pthread_mutex_unlock(&m_handle);
  77. #endif
  78. }
  79. bool try_lock()
  80. {
  81. #ifdef _WIN32
  82. return (0 != TryEnterCriticalSection(&m_handle));
  83. #else
  84. return !pthread_mutex_trylock(&m_handle);
  85. #endif
  86. }
  87. native_handle_type native_handle()
  88. {
  89. return &m_handle;
  90. }
  91. private:
  92. native_type m_handle;
  93. };
  94. #if !defined(_WIN32) || defined(USE_SRWLOCKS)
  95. class mutex
  96. {
  97. #ifdef _WIN32
  98. typedef SRWLOCK native_type;
  99. #else
  100. typedef pthread_mutex_t native_type;
  101. #endif
  102. public:
  103. typedef native_type* native_handle_type;
  104. mutex(const mutex&) /*= delete*/;
  105. mutex& operator=(const mutex&) /*= delete*/;
  106. mutex()
  107. {
  108. #ifdef _WIN32
  109. InitializeSRWLock(&m_handle);
  110. #else
  111. pthread_mutex_init(&m_handle, NULL);
  112. #endif
  113. }
  114. ~mutex()
  115. {
  116. #ifdef _WIN32
  117. #else
  118. pthread_mutex_destroy(&m_handle);
  119. #endif
  120. }
  121. void lock()
  122. {
  123. #ifdef _WIN32
  124. AcquireSRWLockExclusive(&m_handle);
  125. #else
  126. pthread_mutex_lock(&m_handle);
  127. #endif
  128. }
  129. void unlock()
  130. {
  131. #ifdef _WIN32
  132. ReleaseSRWLockExclusive(&m_handle);
  133. #else
  134. pthread_mutex_unlock(&m_handle);
  135. #endif
  136. }
  137. bool try_lock()
  138. {
  139. #ifdef _WIN32
  140. // XXX TryAcquireSRWLockExclusive requires Windows 7!
  141. // return (0 != TryAcquireSRWLockExclusive(&m_handle));
  142. return false;
  143. #else
  144. return !pthread_mutex_trylock(&m_handle);
  145. #endif
  146. }
  147. native_handle_type native_handle()
  148. {
  149. return &m_handle;
  150. }
  151. private:
  152. native_type m_handle;
  153. };
  154. #else
  155. typedef recursive_mutex mutex; // just use CriticalSections
  156. #endif
  157. enum defer_lock_t { defer_lock };
  158. enum try_to_lock_t { try_to_lock };
  159. enum adopt_lock_t { adopt_lock };
  160. template <class Mutex>
  161. class lock_guard
  162. {
  163. public:
  164. typedef Mutex mutex_type;
  165. explicit lock_guard(mutex_type& m)
  166. : pm(m)
  167. {
  168. m.lock();
  169. }
  170. lock_guard(mutex_type& m, adopt_lock_t)
  171. : pm(m)
  172. {
  173. }
  174. ~lock_guard()
  175. {
  176. pm.unlock();
  177. }
  178. lock_guard(lock_guard const&) /*= delete*/;
  179. lock_guard& operator=(lock_guard const&) /*= delete*/;
  180. private:
  181. mutex_type& pm;
  182. };
  183. template <class Mutex>
  184. class unique_lock
  185. {
  186. public:
  187. typedef Mutex mutex_type;
  188. unique_lock()
  189. : pm(NULL), owns(false)
  190. {}
  191. /*explicit*/ unique_lock(mutex_type& m)
  192. : pm(&m), owns(true)
  193. {
  194. m.lock();
  195. }
  196. unique_lock(mutex_type& m, defer_lock_t)
  197. : pm(&m), owns(false)
  198. {}
  199. unique_lock(mutex_type& m, try_to_lock_t)
  200. : pm(&m), owns(m.try_lock())
  201. {}
  202. unique_lock(mutex_type& m, adopt_lock_t)
  203. : pm(&m), owns(true)
  204. {}
  205. //template <class Clock, class Duration>
  206. //unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
  207. //template <class Rep, class Period>
  208. //unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
  209. ~unique_lock()
  210. {
  211. if (owns_lock())
  212. mutex()->unlock();
  213. }
  214. #ifdef USE_RVALUE_REFERENCES
  215. unique_lock& operator=(const unique_lock&) /*= delete*/;
  216. unique_lock& operator=(unique_lock&& other)
  217. {
  218. #else
  219. unique_lock& operator=(const unique_lock& u)
  220. {
  221. // ugly const_cast to get around lack of rvalue references
  222. unique_lock& other = const_cast<unique_lock&>(u);
  223. #endif
  224. swap(other);
  225. return *this;
  226. }
  227. #ifdef USE_RVALUE_REFERENCES
  228. unique_lock(const unique_lock&) /*= delete*/;
  229. unique_lock(unique_lock&& other)
  230. : pm(NULL), owns(false)
  231. {
  232. #else
  233. unique_lock(const unique_lock& u)
  234. : pm(NULL), owns(false)
  235. {
  236. // ugly const_cast to get around lack of rvalue references
  237. unique_lock& other = const_cast<unique_lock&>(u);
  238. #endif
  239. swap(other);
  240. }
  241. void lock()
  242. {
  243. mutex()->lock();
  244. owns = true;
  245. }
  246. bool try_lock()
  247. {
  248. owns = mutex()->try_lock();
  249. return owns;
  250. }
  251. //template <class Rep, class Period>
  252. //bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
  253. //template <class Clock, class Duration>
  254. //bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
  255. void unlock()
  256. {
  257. mutex()->unlock();
  258. owns = false;
  259. }
  260. void swap(unique_lock& u)
  261. {
  262. std::swap(pm, u.pm);
  263. std::swap(owns, u.owns);
  264. }
  265. mutex_type* release()
  266. {
  267. auto const ret = mutex();
  268. pm = NULL;
  269. owns = false;
  270. return ret;
  271. }
  272. bool owns_lock() const
  273. {
  274. return owns;
  275. }
  276. //explicit operator bool () const
  277. //{
  278. // return owns_lock();
  279. //}
  280. mutex_type* mutex() const
  281. {
  282. return pm;
  283. }
  284. private:
  285. mutex_type* pm;
  286. bool owns;
  287. };
  288. template <class Mutex>
  289. void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y)
  290. {
  291. x.swap(y);
  292. }
  293. }
  294. #endif
  295. #endif