mutex.hpp 8.7 KB

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