spin_mutex.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2013.
  3. * Distributed under the Boost Software License, Version 1.0.
  4. * (See accompanying file LICENSE_1_0.txt or copy at
  5. * http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. /*!
  8. * \file spin_mutex.hpp
  9. * \author Andrey Semashev
  10. * \date 01.08.2010
  11. *
  12. * \brief This header is the Boost.Log library implementation, see the library documentation
  13. * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
  14. */
  15. #ifndef BOOST_LOG_DETAIL_SPIN_MUTEX_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_SPIN_MUTEX_HPP_INCLUDED_
  17. #include <boost/log/detail/config.hpp>
  18. #ifdef BOOST_HAS_PRAGMA_ONCE
  19. #pragma once
  20. #endif
  21. #ifndef BOOST_LOG_NO_THREADS
  22. #include <boost/throw_exception.hpp>
  23. #include <boost/thread/exceptions.hpp>
  24. #if defined(BOOST_THREAD_POSIX) // This one can be defined by users, so it should go first
  25. #define BOOST_LOG_SPIN_MUTEX_USE_PTHREAD
  26. #elif defined(BOOST_WINDOWS)
  27. #define BOOST_LOG_SPIN_MUTEX_USE_WINAPI
  28. #elif defined(BOOST_HAS_PTHREADS)
  29. #define BOOST_LOG_SPIN_MUTEX_USE_PTHREAD
  30. #endif
  31. #if defined(BOOST_LOG_SPIN_MUTEX_USE_WINAPI)
  32. #include <boost/detail/interlocked.hpp>
  33. #if defined(BOOST_USE_WINDOWS_H)
  34. #ifndef _WIN32_WINNT
  35. #define _WIN32_WINNT 0x0500
  36. #endif
  37. #include <windows.h>
  38. #else // defined(BOOST_USE_WINDOWS_H)
  39. namespace boost {
  40. BOOST_LOG_OPEN_NAMESPACE
  41. namespace aux {
  42. extern "C" {
  43. __declspec(dllimport) int __stdcall SwitchToThread();
  44. } // extern "C"
  45. } // namespace aux
  46. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  47. } // namespace boost
  48. #endif // BOOST_USE_WINDOWS_H
  49. #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
  50. # if defined(_M_IX86)
  51. # define BOOST_LOG_PAUSE_OP __asm { pause }
  52. # elif defined(_M_AMD64)
  53. extern "C" void _mm_pause(void);
  54. #pragma intrinsic(_mm_pause)
  55. # define BOOST_LOG_PAUSE_OP _mm_pause()
  56. # endif
  57. # if defined(__INTEL_COMPILER)
  58. # define BOOST_LOG_COMPILER_BARRIER __memory_barrier()
  59. # else
  60. extern "C" void _ReadWriteBarrier(void);
  61. #pragma intrinsic(_ReadWriteBarrier)
  62. # define BOOST_LOG_COMPILER_BARRIER _ReadWriteBarrier()
  63. # endif
  64. #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  65. # define BOOST_LOG_PAUSE_OP __asm__ __volatile__("pause;")
  66. # define BOOST_LOG_COMPILER_BARRIER __asm__ __volatile__("" : : : "memory")
  67. #endif
  68. #include <boost/log/detail/header.hpp>
  69. namespace boost {
  70. BOOST_LOG_OPEN_NAMESPACE
  71. namespace aux {
  72. //! A simple spinning mutex
  73. class spin_mutex
  74. {
  75. private:
  76. enum state
  77. {
  78. initial_pause = 2,
  79. max_pause = 16
  80. };
  81. long m_State;
  82. public:
  83. spin_mutex() : m_State(0) {}
  84. bool try_lock()
  85. {
  86. return (BOOST_INTERLOCKED_COMPARE_EXCHANGE(&m_State, 1L, 0L) == 0L);
  87. }
  88. void lock()
  89. {
  90. #if defined(BOOST_LOG_PAUSE_OP)
  91. register unsigned int pause_count = initial_pause;
  92. #endif
  93. while (!try_lock())
  94. {
  95. #if defined(BOOST_LOG_PAUSE_OP)
  96. if (pause_count < max_pause)
  97. {
  98. for (register unsigned int i = 0; i < pause_count; ++i)
  99. {
  100. BOOST_LOG_PAUSE_OP;
  101. }
  102. pause_count += pause_count;
  103. }
  104. else
  105. {
  106. // Restart spinning after waking up this thread
  107. pause_count = initial_pause;
  108. SwitchToThread();
  109. }
  110. #else
  111. SwitchToThread();
  112. #endif
  113. }
  114. }
  115. void unlock()
  116. {
  117. #if (defined(_M_IX86) || defined(_M_AMD64)) && defined(BOOST_LOG_COMPILER_BARRIER)
  118. BOOST_LOG_COMPILER_BARRIER;
  119. m_State = 0L;
  120. BOOST_LOG_COMPILER_BARRIER;
  121. #else
  122. BOOST_INTERLOCKED_EXCHANGE(&m_State, 0L);
  123. #endif
  124. }
  125. // Non-copyable
  126. BOOST_DELETED_FUNCTION(spin_mutex(spin_mutex const&))
  127. BOOST_DELETED_FUNCTION(spin_mutex& operator= (spin_mutex const&))
  128. };
  129. #undef BOOST_LOG_PAUSE_OP
  130. #undef BOOST_LOG_COMPILER_BARRIER
  131. } // namespace aux
  132. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  133. } // namespace boost
  134. #include <boost/log/detail/footer.hpp>
  135. #elif defined(BOOST_LOG_SPIN_MUTEX_USE_PTHREAD)
  136. #include <pthread.h>
  137. #include <boost/assert.hpp>
  138. #include <boost/log/detail/header.hpp>
  139. namespace boost {
  140. BOOST_LOG_OPEN_NAMESPACE
  141. namespace aux {
  142. #if defined(_POSIX_SPIN_LOCKS) && _POSIX_SPIN_LOCKS > 0
  143. //! A simple spinning mutex
  144. class spin_mutex
  145. {
  146. private:
  147. pthread_spinlock_t m_State;
  148. public:
  149. spin_mutex()
  150. {
  151. const int err = pthread_spin_init(&m_State, PTHREAD_PROCESS_PRIVATE);
  152. if (err != 0)
  153. throw_exception< thread_resource_error >(err, "failed to initialize a spin mutex", "spin_mutex::spin_mutex()", __FILE__, __LINE__);
  154. }
  155. ~spin_mutex()
  156. {
  157. BOOST_VERIFY(pthread_spin_destroy(&m_State) == 0);
  158. }
  159. bool try_lock()
  160. {
  161. const int err = pthread_spin_trylock(&m_State);
  162. if (err == 0)
  163. return true;
  164. if (err != EBUSY)
  165. throw_exception< lock_error >(err, "failed to lock a spin mutex", "spin_mutex::try_lock()", __FILE__, __LINE__);
  166. return false;
  167. }
  168. void lock()
  169. {
  170. const int err = pthread_spin_lock(&m_State);
  171. if (err != 0)
  172. throw_exception< lock_error >(err, "failed to lock a spin mutex", "spin_mutex::lock()", __FILE__, __LINE__);
  173. }
  174. void unlock()
  175. {
  176. BOOST_VERIFY(pthread_spin_unlock(&m_State) == 0);
  177. }
  178. // Non-copyable
  179. BOOST_DELETED_FUNCTION(spin_mutex(spin_mutex const&))
  180. BOOST_DELETED_FUNCTION(spin_mutex& operator= (spin_mutex const&))
  181. private:
  182. template< typename ExceptionT >
  183. static BOOST_NOINLINE BOOST_LOG_NORETURN void throw_exception(int err, const char* descr, const char* func, const char* file, int line)
  184. {
  185. #if !defined(BOOST_EXCEPTION_DISABLE)
  186. boost::exception_detail::throw_exception_(ExceptionT(err, descr), func, file, line);
  187. #else
  188. boost::throw_exception(ExceptionT(err, descr));
  189. #endif
  190. }
  191. };
  192. #else // defined(_POSIX_SPIN_LOCKS)
  193. //! Backup implementation in case if pthreads don't support spin locks
  194. class spin_mutex
  195. {
  196. private:
  197. pthread_mutex_t m_State;
  198. public:
  199. spin_mutex()
  200. {
  201. const int err = pthread_mutex_init(&m_State, NULL);
  202. if (err != 0)
  203. throw_exception< thread_resource_error >(err, "failed to initialize a spin mutex", "spin_mutex::spin_mutex()", __FILE__, __LINE__);
  204. }
  205. ~spin_mutex()
  206. {
  207. BOOST_VERIFY(pthread_mutex_destroy(&m_State) == 0);
  208. }
  209. bool try_lock()
  210. {
  211. const int err = pthread_mutex_trylock(&m_State);
  212. if (err == 0)
  213. return true;
  214. if (err != EBUSY)
  215. throw_exception< lock_error >(err, "failed to lock a spin mutex", "spin_mutex::try_lock()", __FILE__, __LINE__);
  216. return false;
  217. }
  218. void lock()
  219. {
  220. const int err = pthread_mutex_lock(&m_State);
  221. if (err != 0)
  222. throw_exception< lock_error >(err, "failed to lock a spin mutex", "spin_mutex::lock()", __FILE__, __LINE__);
  223. }
  224. void unlock()
  225. {
  226. BOOST_VERIFY(pthread_mutex_unlock(&m_State) == 0);
  227. }
  228. // Non-copyable
  229. BOOST_DELETED_FUNCTION(spin_mutex(spin_mutex const&))
  230. BOOST_DELETED_FUNCTION(spin_mutex& operator= (spin_mutex const&))
  231. private:
  232. template< typename ExceptionT >
  233. static BOOST_NOINLINE BOOST_LOG_NORETURN void throw_exception(int err, const char* descr, const char* func, const char* file, int line)
  234. {
  235. #if !defined(BOOST_EXCEPTION_DISABLE)
  236. boost::exception_detail::throw_exception_(ExceptionT(err, descr), func, file, line);
  237. #else
  238. boost::throw_exception(ExceptionT(err, descr));
  239. #endif
  240. }
  241. };
  242. #endif // defined(_POSIX_SPIN_LOCKS)
  243. } // namespace aux
  244. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  245. } // namespace boost
  246. #include <boost/log/detail/footer.hpp>
  247. #endif
  248. #endif // BOOST_LOG_NO_THREADS
  249. #endif // BOOST_LOG_DETAIL_SPIN_MUTEX_HPP_INCLUDED_