mutex.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #endif
  45. #if defined(BOOST_WINDOWS)
  46. #include <windows.h>
  47. #ifndef BOOST_MUTEX_HELPER
  48. #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_WIN32
  49. #endif
  50. #elif defined(BOOST_HAS_UNISTD_H)
  51. #include <unistd.h>
  52. #if !defined(BOOST_MUTEX_HELPER) && (defined(_POSIX_THREADS) || defined(BOOST_HAS_PTHREADS))
  53. #define BOOST_MUTEX_HELPER BOOST_MUTEX_HELPER_PTHREAD
  54. #endif
  55. #endif
  56. #endif
  57. #ifndef BOOST_MUTEX_HELPER
  58. #error Unable to determine platform mutex type; #define BOOST_NO_MT to assume single-threaded
  59. #endif
  60. #if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE
  61. //...
  62. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_SPINLOCKS
  63. #if defined(_MSC_VER)
  64. #ifndef _M_AMD64
  65. /* These are already defined on AMD64 builds */
  66. #ifdef __cplusplus
  67. extern "C" {
  68. #endif /* __cplusplus */
  69. long __cdecl _InterlockedCompareExchange(long volatile *Dest, long Exchange, long Comp);
  70. long __cdecl _InterlockedExchange(long volatile *Target, long Value);
  71. #ifdef __cplusplus
  72. }
  73. #endif /* __cplusplus */
  74. #endif /* _M_AMD64 */
  75. #pragma intrinsic (_InterlockedCompareExchange)
  76. #pragma intrinsic (_InterlockedExchange)
  77. #define interlockedcompareexchange _InterlockedCompareExchange
  78. #define interlockedexchange _InterlockedExchange
  79. #elif defined(WIN32) && defined(__GNUC__)
  80. #define interlockedcompareexchange(a, b, c) __sync_val_compare_and_swap(a, c, b)
  81. #define interlockedexchange __sync_lock_test_and_set
  82. #endif /* Win32 */
  83. /* First, define CAS_LOCK and CLEAR_LOCK on ints */
  84. /* Note CAS_LOCK defined to return 0 on success */
  85. #if defined(__GNUC__)&& (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
  86. #define BOOST_CONTAINER_CAS_LOCK(sl) __sync_lock_test_and_set(sl, 1)
  87. #define BOOST_CONTAINER_CLEAR_LOCK(sl) __sync_lock_release(sl)
  88. #elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
  89. /* Custom spin locks for older gcc on x86 */
  90. static inline int boost_container_x86_cas_lock(int *sl) {
  91. int ret;
  92. int val = 1;
  93. int cmp = 0;
  94. __asm__ __volatile__ ("lock; cmpxchgl %1, %2"
  95. : "=a" (ret)
  96. : "r" (val), "m" (*(sl)), "0"(cmp)
  97. : "memory", "cc");
  98. return ret;
  99. }
  100. static inline void boost_container_x86_clear_lock(int* sl) {
  101. assert(*sl != 0);
  102. int prev = 0;
  103. int ret;
  104. __asm__ __volatile__ ("lock; xchgl %0, %1"
  105. : "=r" (ret)
  106. : "m" (*(sl)), "0"(prev)
  107. : "memory");
  108. }
  109. #define BOOST_CONTAINER_CAS_LOCK(sl) boost_container_x86_cas_lock(sl)
  110. #define BOOST_CONTAINER_CLEAR_LOCK(sl) boost_container_x86_clear_lock(sl)
  111. #else /* Win32 MSC */
  112. #define BOOST_CONTAINER_CAS_LOCK(sl) interlockedexchange((long volatile*)sl, (long)1)
  113. #define BOOST_CONTAINER_CLEAR_LOCK(sl) interlockedexchange((long volatile*)sl, (long)0)
  114. #endif
  115. /* How to yield for a spin lock */
  116. #define SPINS_PER_YIELD 63
  117. #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
  118. #define SLEEP_EX_DURATION 50 /* delay for yield/sleep */
  119. #define SPIN_LOCK_YIELD SleepEx(SLEEP_EX_DURATION, FALSE)
  120. #elif defined (__SVR4) && defined (__sun) /* solaris */
  121. #include <thread.h>
  122. #define SPIN_LOCK_YIELD thr_yield();
  123. #elif !defined(LACKS_SCHED_H)
  124. #include <sched.h>
  125. #define SPIN_LOCK_YIELD sched_yield();
  126. #else
  127. #define SPIN_LOCK_YIELD
  128. #endif /* ... yield ... */
  129. #define BOOST_CONTAINER_SPINS_PER_YIELD 63
  130. inline int boost_interprocess_spin_acquire_lock(int *sl) {
  131. int spins = 0;
  132. while (*(volatile int *)sl != 0 ||
  133. BOOST_CONTAINER_CAS_LOCK(sl)) {
  134. if ((++spins & BOOST_CONTAINER_SPINS_PER_YIELD) == 0) {
  135. SPIN_LOCK_YIELD;
  136. }
  137. }
  138. return 0;
  139. }
  140. #define BOOST_CONTAINER_MLOCK_T int
  141. #define BOOST_CONTAINER_TRY_LOCK(sl) !BOOST_CONTAINER_CAS_LOCK(sl)
  142. #define BOOST_CONTAINER_RELEASE_LOCK(sl) BOOST_CONTAINER_CLEAR_LOCK(sl)
  143. #define BOOST_CONTAINER_ACQUIRE_LOCK(sl) (BOOST_CONTAINER_CAS_LOCK(sl)? boost_interprocess_spin_acquire_lock(sl) : 0)
  144. #define BOOST_MOVE_INITIAL_LOCK(sl) (*sl = 0)
  145. #define BOOST_CONTAINER_DESTROY_LOCK(sl) (0)
  146. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32
  147. //
  148. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD
  149. #include <pthread.h>
  150. #endif
  151. namespace boost {
  152. namespace container {
  153. namespace dtl {
  154. #if BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_NONE
  155. class null_mutex
  156. {
  157. private:
  158. null_mutex(const null_mutex &);
  159. void operator=(const null_mutex &);
  160. public:
  161. null_mutex() { }
  162. static void lock() { }
  163. static void unlock() { }
  164. };
  165. typedef null_mutex default_mutex;
  166. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_SPINLOCKS
  167. class spin_mutex
  168. {
  169. private:
  170. BOOST_CONTAINER_MLOCK_T sl;
  171. spin_mutex(const spin_mutex &);
  172. void operator=(const spin_mutex &);
  173. public:
  174. spin_mutex() { BOOST_MOVE_INITIAL_LOCK(&sl); }
  175. void lock() { BOOST_CONTAINER_ACQUIRE_LOCK(&sl); }
  176. void unlock() { BOOST_CONTAINER_RELEASE_LOCK(&sl); }
  177. };
  178. typedef spin_mutex default_mutex;
  179. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_WIN32
  180. class mutex
  181. {
  182. private:
  183. CRITICAL_SECTION mtx;
  184. mutex(const mutex &);
  185. void operator=(const mutex &);
  186. public:
  187. mutex()
  188. { InitializeCriticalSection(&mtx); }
  189. ~mutex()
  190. { DeleteCriticalSection(&mtx); }
  191. void lock()
  192. { EnterCriticalSection(&mtx); }
  193. void unlock()
  194. { LeaveCriticalSection(&mtx); }
  195. };
  196. typedef mutex default_mutex;
  197. #elif BOOST_MUTEX_HELPER == BOOST_MUTEX_HELPER_PTHREAD
  198. class mutex
  199. {
  200. private:
  201. pthread_mutex_t mtx;
  202. mutex(const mutex &);
  203. void operator=(const mutex &);
  204. public:
  205. mutex()
  206. { pthread_mutex_init(&mtx, 0); }
  207. ~mutex()
  208. { pthread_mutex_destroy(&mtx); }
  209. void lock()
  210. { pthread_mutex_lock(&mtx); }
  211. void unlock()
  212. { pthread_mutex_unlock(&mtx); }
  213. };
  214. typedef mutex default_mutex;
  215. #endif
  216. template<class Mutex>
  217. class scoped_lock
  218. {
  219. public:
  220. scoped_lock(Mutex &m)
  221. : m_(m)
  222. { m_.lock(); }
  223. ~scoped_lock()
  224. { m_.unlock(); }
  225. private:
  226. Mutex &m_;
  227. };
  228. } // namespace dtl
  229. } // namespace container
  230. } // namespace boost
  231. #undef BOOST_MUTEX_HELPER_WIN32
  232. #undef BOOST_MUTEX_HELPER_PTHREAD
  233. #undef BOOST_MUTEX_HELPER_NONE
  234. #undef BOOST_MUTEX_HELPER
  235. #undef BOOST_MUTEX_HELPER_SPINLOCKS
  236. #include <boost/container/detail/config_end.hpp>
  237. #endif