mutex.hpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Stephen Cleary 2000
  4. // (C) Copyright Ion Gaztanaga 2015-2017.
  5. //
  6. // Distributed under the Boost
  7. // Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // See http://www.boost.org/libs/container for documentation.
  11. //
  12. //////////////////////////////////////////////////////////////////////////////
  13. #ifndef BOOST_CONTAINER_MUTEX_HPP
  14. #define BOOST_CONTAINER_MUTEX_HPP
  15. #ifndef BOOST_CONFIG_HPP
  16. # include <boost/config.hpp>
  17. #endif
  18. #if defined(BOOST_HAS_PRAGMA_ONCE)
  19. # pragma once
  20. #endif
  21. //#define BOOST_CONTAINER_NO_MT
  22. //#define BOOST_CONTAINER_NO_SPINLOCKS
  23. #include <boost/container/detail/config_begin.hpp>
  24. #include <boost/container/detail/workaround.hpp>
  25. // Extremely Light-Weight wrapper classes for OS thread synchronization
  26. #define BOOST_MUTEX_HELPER_NONE 0
  27. #define BOOST_MUTEX_HELPER_WIN32 1
  28. #define BOOST_MUTEX_HELPER_PTHREAD 2
  29. #define BOOST_MUTEX_HELPER_SPINLOCKS 3
  30. #if !defined(BOOST_HAS_THREADS) && !defined(BOOST_NO_MT)
  31. # define BOOST_NO_MT
  32. #endif
  33. #if defined(BOOST_NO_MT) || defined(BOOST_CONTAINER_NO_MT)
  34. // No multithreading -> make locks into no-ops
  35. #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_NONE
  36. #else
  37. //Taken from dlmalloc
  38. #if !defined(BOOST_CONTAINER_NO_SPINLOCKS) && \
  39. ((defined(__GNUC__) && \
  40. ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)) || \
  41. defined(__i386__) || defined(__x86_64__))) || \
  42. (defined(_MSC_VER) && _MSC_VER>=1310))
  43. #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_SPINLOCKS
  44. #elif defined(BOOST_HAS_UNISTD_H)
  45. #include <unistd.h>
  46. #if !defined(BOOST_MUTEX_HELPER) && (defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS))
  47. #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_PTHREAD
  48. #endif
  49. #endif
  50. #endif
  51. #ifndef BOOST_MUTEX_HELPER
  52. #error Unable to determine platform mutex type; #define BOOST_NO_MT to assume single-threaded
  53. #endif
  54. #if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE
  55. //...
  56. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_SPINLOCKS
  57. #if defined(_MSC_VER)
  58. #include <intrin.h>
  59. #define interlockedexchange _InterlockedExchange
  60. #elif defined(WIN32) && defined(__GNUC__)
  61. #define interlockedexchange __sync_lock_test_and_set
  62. #endif /* Win32 */
  63. /* First, define CAS_LOCK and CLEAR_LOCK on ints */
  64. /* Note CAS_LOCK defined to return 0 on success */
  65. #if defined(__GNUC__)&& (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
  66. #define BOOST_CONTAINER_CAS_LOCK(sl) __sync_lock_test_and_set(sl, 1)
  67. #define BOOST_CONTAINER_CLEAR_LOCK(sl) __sync_lock_release(sl)
  68. #elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
  69. /* Custom spin locks for older gcc on x86 */
  70. static inline int boost_container_x86_cas_lock(int *sl) {
  71. int ret;
  72. int val = 1;
  73. int cmp = 0;
  74. __asm__ __volatile__ ("lock; cmpxchgl %1, %2"
  75. : "=a" (ret)
  76. : "r" (val), "m" (*(sl)), "0"(cmp)
  77. : "memory", "cc");
  78. return ret;
  79. }
  80. static inline void boost_container_x86_clear_lock(int* sl) {
  81. assert(*sl != 0);
  82. int prev = 0;
  83. int ret;
  84. __asm__ __volatile__ ("lock; xchgl %0, %1"
  85. : "=r" (ret)
  86. : "m" (*(sl)), "0"(prev)
  87. : "memory");
  88. }
  89. #define BOOST_CONTAINER_CAS_LOCK(sl) boost_container_x86_cas_lock(sl)
  90. #define BOOST_CONTAINER_CLEAR_LOCK(sl) boost_container_x86_clear_lock(sl)
  91. #else /* Win32 MSC */
  92. #define BOOST_CONTAINER_CAS_LOCK(sl) interlockedexchange((long volatile*)sl, (long)1)
  93. #define BOOST_CONTAINER_CLEAR_LOCK(sl) interlockedexchange((long volatile*)sl, (long)0)
  94. #endif
  95. /* How to yield for a spin lock */
  96. #define SPINS_PER_YIELD 63
  97. #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  98. #if !defined( BOOST_USE_WINDOWS_H )
  99. #if defined (WIN32_PLATFORM_PSPC)
  100. #define BOOST_CONTAINERWINAPI_IMPORT BOOST_SYMBOL_IMPORT
  101. #elif defined (_WIN32_WCE)
  102. #define BOOST_CONTAINERWINAPI_IMPORT
  103. #else
  104. #define BOOST_CONTAINERWINAPI_IMPORT BOOST_SYMBOL_IMPORT
  105. #endif
  106. #if defined(WINAPI)
  107. #define BOOST_CONTAINERWINAPI_WINAPI_CC WINAPI
  108. #else
  109. #if defined(_M_IX86) || defined(__i386__)
  110. #define BOOST_CONTAINERWINAPI_DETAIL_STDCALL __stdcall
  111. #else
  112. #define BOOST_CONTAINERWINAPI_DETAIL_STDCALL
  113. #endif
  114. #define BOOST_CONTAINERWINAPI_WINAPI_CC BOOST_CONTAINERWINAPI_DETAIL_STDCALL
  115. #endif
  116. #if !defined(__LP64__)
  117. namespace boost {
  118. namespace container_winapi {
  119. typedef unsigned long DWORD_;
  120. #else
  121. typedef unsigned int DWORD_;
  122. #endif
  123. typedef int BOOL_;
  124. }}
  125. extern "C" {
  126. BOOST_CONTAINERWINAPI_IMPORT boost::container_winapi::DWORD_ BOOST_CONTAINERWINAPI_WINAPI_CC
  127. SleepEx(
  128. boost::container_winapi::DWORD_ dwMilliseconds,
  129. boost::container_winapi::BOOL_ bAlertable);
  130. } // extern "C"
  131. #endif
  132. namespace boost {
  133. namespace container_winapi {
  134. using ::SleepEx;
  135. }
  136. }
  137. #define SLEEP_EX_DURATION 50 /* delay for yield/sleep */
  138. #define SPIN_LOCK_YIELD boost::container_winapi::SleepEx(SLEEP_EX_DURATION, 0)
  139. #elif defined (__SVR4) && defined (__sun) /* solaris */
  140. #include <thread.h>
  141. #define SPIN_LOCK_YIELD thr_yield();
  142. #elif !defined(LACKS_SCHED_H)
  143. #include <sched.h>
  144. #define SPIN_LOCK_YIELD sched_yield();
  145. #else
  146. #define SPIN_LOCK_YIELD
  147. #endif /* ... yield ... */
  148. #define BOOST_CONTAINER_SPINS_PER_YIELD 63
  149. inline int boost_interprocess_spin_acquire_lock(int *sl) {
  150. int spins = 0;
  151. while (*(volatile int *)sl != 0 ||
  152. BOOST_CONTAINER_CAS_LOCK(sl)) {
  153. if ((++spins & BOOST_CONTAINER_SPINS_PER_YIELD) == 0) {
  154. SPIN_LOCK_YIELD;
  155. }
  156. }
  157. return 0;
  158. }
  159. #define BOOST_CONTAINER_MLOCK_T int
  160. #define BOOST_CONTAINER_TRY_LOCK(sl) !BOOST_CONTAINER_CAS_LOCK(sl)
  161. #define BOOST_CONTAINER_RELEASE_LOCK(sl) BOOST_CONTAINER_CLEAR_LOCK(sl)
  162. #define BOOST_CONTAINER_ACQUIRE_LOCK(sl) (BOOST_CONTAINER_CAS_LOCK(sl)? boost_interprocess_spin_acquire_lock(sl) : 0)
  163. #define BOOST_MOVE_INITIAL_LOCK(sl) (*sl = 0)
  164. #define BOOST_CONTAINER_DESTROY_LOCK(sl) (0)
  165. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32
  166. //
  167. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD
  168. #include <pthread.h>
  169. #endif
  170. namespace boost {
  171. namespace container {
  172. namespace dtl {
  173. #if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE
  174. class null_mutex
  175. {
  176. private:
  177. null_mutex(const null_mutex &);
  178. void operator=(const null_mutex &);
  179. public:
  180. null_mutex() { }
  181. static void lock() { }
  182. static void unlock() { }
  183. };
  184. typedef null_mutex default_mutex;
  185. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_SPINLOCKS
  186. class spin_mutex
  187. {
  188. private:
  189. BOOST_CONTAINER_MLOCK_T sl;
  190. spin_mutex(const spin_mutex &);
  191. void operator=(const spin_mutex &);
  192. public:
  193. spin_mutex() { BOOST_MOVE_INITIAL_LOCK(&sl); }
  194. void lock() { BOOST_CONTAINER_ACQUIRE_LOCK(&sl); }
  195. void unlock() { BOOST_CONTAINER_RELEASE_LOCK(&sl); }
  196. };
  197. typedef spin_mutex default_mutex;
  198. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32
  199. class mutex
  200. {
  201. private:
  202. CRITICAL_SECTION mtx;
  203. mutex(const mutex &);
  204. void operator=(const mutex &);
  205. public:
  206. mutex()
  207. { InitializeCriticalSection(&mtx); }
  208. ~mutex()
  209. { DeleteCriticalSection(&mtx); }
  210. void lock()
  211. { EnterCriticalSection(&mtx); }
  212. void unlock()
  213. { LeaveCriticalSection(&mtx); }
  214. };
  215. typedef mutex default_mutex;
  216. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD
  217. class mutex
  218. {
  219. private:
  220. pthread_mutex_t mtx;
  221. mutex(const mutex &);
  222. void operator=(const mutex &);
  223. public:
  224. mutex()
  225. { pthread_mutex_init(&mtx, 0); }
  226. ~mutex()
  227. { pthread_mutex_destroy(&mtx); }
  228. void lock()
  229. { pthread_mutex_lock(&mtx); }
  230. void unlock()
  231. { pthread_mutex_unlock(&mtx); }
  232. };
  233. typedef mutex default_mutex;
  234. #endif
  235. template<class Mutex>
  236. class scoped_lock
  237. {
  238. public:
  239. scoped_lock(Mutex &m)
  240. : m_(m)
  241. { m_.lock(); }
  242. ~scoped_lock()
  243. { m_.unlock(); }
  244. private:
  245. Mutex &m_;
  246. };
  247. } // namespace dtl
  248. } // namespace container
  249. } // namespace boost
  250. #undef BOOST_MUTEX_HELPER_WIN32
  251. #undef BOOST_MUTEX_HELPER_PTHREAD
  252. #undef BOOST_MUTEX_HELPER_NONE
  253. #undef BOOST_MUTEX_HELPER
  254. #undef BOOST_MUTEX_HELPER_SPINLOCKS
  255. #include <boost/container/detail/config_end.hpp>
  256. #endif