adaptive_mutex.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright Andrey Semashev 2007 - 2015.
  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 adaptive_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_ADAPTIVE_MUTEX_HPP_INCLUDED_
  16. #define BOOST_LOG_DETAIL_ADAPTIVE_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_ADAPTIVE_MUTEX_USE_PTHREAD
  26. #elif defined(BOOST_WINDOWS)
  27. #define BOOST_LOG_ADAPTIVE_MUTEX_USE_WINAPI
  28. #elif defined(BOOST_HAS_PTHREADS)
  29. #define BOOST_LOG_ADAPTIVE_MUTEX_USE_PTHREAD
  30. #endif
  31. #if defined(BOOST_LOG_ADAPTIVE_MUTEX_USE_WINAPI)
  32. #include <boost/log/detail/pause.hpp>
  33. #include <boost/winapi/thread.hpp>
  34. #include <boost/detail/interlocked.hpp>
  35. #if defined(__INTEL_COMPILER) || defined(_MSC_VER)
  36. # if defined(__INTEL_COMPILER)
  37. # define BOOST_LOG_COMPILER_BARRIER __memory_barrier()
  38. # elif defined(__clang__) // clang-win also defines _MSC_VER
  39. # define BOOST_LOG_COMPILER_BARRIER __atomic_signal_fence(__ATOMIC_SEQ_CST)
  40. # else
  41. extern "C" void _ReadWriteBarrier(void);
  42. # if defined(BOOST_MSVC)
  43. # pragma intrinsic(_ReadWriteBarrier)
  44. # endif
  45. # define BOOST_LOG_COMPILER_BARRIER _ReadWriteBarrier()
  46. # endif
  47. #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
  48. # define BOOST_LOG_COMPILER_BARRIER __asm__ __volatile__("" : : : "memory")
  49. #endif
  50. #include <boost/log/detail/header.hpp>
  51. namespace boost {
  52. BOOST_LOG_OPEN_NAMESPACE
  53. namespace aux {
  54. //! A mutex that performs spinning or thread yielding in case of contention
  55. class adaptive_mutex
  56. {
  57. private:
  58. enum state
  59. {
  60. initial_pause = 2,
  61. max_pause = 16
  62. };
  63. long m_State;
  64. public:
  65. adaptive_mutex() : m_State(0) {}
  66. bool try_lock()
  67. {
  68. return (BOOST_INTERLOCKED_COMPARE_EXCHANGE(&m_State, 1L, 0L) == 0L);
  69. }
  70. void lock()
  71. {
  72. #if defined(BOOST_LOG_AUX_PAUSE)
  73. unsigned int pause_count = initial_pause;
  74. #endif
  75. while (!try_lock())
  76. {
  77. #if defined(BOOST_LOG_AUX_PAUSE)
  78. if (pause_count < max_pause)
  79. {
  80. for (unsigned int i = 0; i < pause_count; ++i)
  81. {
  82. BOOST_LOG_AUX_PAUSE;
  83. }
  84. pause_count += pause_count;
  85. }
  86. else
  87. {
  88. // Restart spinning after waking up this thread
  89. pause_count = initial_pause;
  90. boost::winapi::SwitchToThread();
  91. }
  92. #else
  93. boost::winapi::SwitchToThread();
  94. #endif
  95. }
  96. }
  97. void unlock()
  98. {
  99. #if (defined(_M_IX86) || defined(_M_AMD64)) && defined(BOOST_LOG_COMPILER_BARRIER)
  100. BOOST_LOG_COMPILER_BARRIER;
  101. m_State = 0L;
  102. BOOST_LOG_COMPILER_BARRIER;
  103. #else
  104. BOOST_INTERLOCKED_EXCHANGE(&m_State, 0L);
  105. #endif
  106. }
  107. // Non-copyable
  108. BOOST_DELETED_FUNCTION(adaptive_mutex(adaptive_mutex const&))
  109. BOOST_DELETED_FUNCTION(adaptive_mutex& operator= (adaptive_mutex const&))
  110. };
  111. #undef BOOST_LOG_AUX_PAUSE
  112. #undef BOOST_LOG_COMPILER_BARRIER
  113. } // namespace aux
  114. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  115. } // namespace boost
  116. #include <boost/log/detail/footer.hpp>
  117. #elif defined(BOOST_LOG_ADAPTIVE_MUTEX_USE_PTHREAD)
  118. #include <pthread.h>
  119. #include <boost/assert.hpp>
  120. #include <boost/log/detail/header.hpp>
  121. #if defined(PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP)
  122. #define BOOST_LOG_ADAPTIVE_MUTEX_USE_PTHREAD_MUTEX_ADAPTIVE_NP
  123. #endif
  124. namespace boost {
  125. BOOST_LOG_OPEN_NAMESPACE
  126. namespace aux {
  127. //! A mutex that performs spinning or thread yielding in case of contention
  128. class adaptive_mutex
  129. {
  130. private:
  131. pthread_mutex_t m_State;
  132. public:
  133. adaptive_mutex()
  134. {
  135. #if defined(BOOST_LOG_ADAPTIVE_MUTEX_USE_PTHREAD_MUTEX_ADAPTIVE_NP)
  136. pthread_mutexattr_t attrs;
  137. pthread_mutexattr_init(&attrs);
  138. pthread_mutexattr_settype(&attrs, PTHREAD_MUTEX_ADAPTIVE_NP);
  139. const int err = pthread_mutex_init(&m_State, &attrs);
  140. pthread_mutexattr_destroy(&attrs);
  141. #else
  142. const int err = pthread_mutex_init(&m_State, NULL);
  143. #endif
  144. if (BOOST_UNLIKELY(err != 0))
  145. throw_exception< thread_resource_error >(err, "Failed to initialize an adaptive mutex", "adaptive_mutex::adaptive_mutex()", __FILE__, __LINE__);
  146. }
  147. ~adaptive_mutex()
  148. {
  149. BOOST_VERIFY(pthread_mutex_destroy(&m_State) == 0);
  150. }
  151. bool try_lock()
  152. {
  153. const int err = pthread_mutex_trylock(&m_State);
  154. if (err == 0)
  155. return true;
  156. if (BOOST_UNLIKELY(err != EBUSY))
  157. throw_exception< lock_error >(err, "Failed to lock an adaptive mutex", "adaptive_mutex::try_lock()", __FILE__, __LINE__);
  158. return false;
  159. }
  160. void lock()
  161. {
  162. const int err = pthread_mutex_lock(&m_State);
  163. if (BOOST_UNLIKELY(err != 0))
  164. throw_exception< lock_error >(err, "Failed to lock an adaptive mutex", "adaptive_mutex::lock()", __FILE__, __LINE__);
  165. }
  166. void unlock()
  167. {
  168. BOOST_VERIFY(pthread_mutex_unlock(&m_State) == 0);
  169. }
  170. // Non-copyable
  171. BOOST_DELETED_FUNCTION(adaptive_mutex(adaptive_mutex const&))
  172. BOOST_DELETED_FUNCTION(adaptive_mutex& operator= (adaptive_mutex const&))
  173. private:
  174. template< typename ExceptionT >
  175. static BOOST_NOINLINE BOOST_LOG_NORETURN void throw_exception(int err, const char* descr, const char* func, const char* file, int line)
  176. {
  177. #if !defined(BOOST_EXCEPTION_DISABLE)
  178. boost::exception_detail::throw_exception_(ExceptionT(err, descr), func, file, line);
  179. #else
  180. boost::throw_exception(ExceptionT(err, descr));
  181. #endif
  182. }
  183. };
  184. } // namespace aux
  185. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  186. } // namespace boost
  187. #include <boost/log/detail/footer.hpp>
  188. #endif
  189. #endif // BOOST_LOG_NO_THREADS
  190. #endif // BOOST_LOG_DETAIL_ADAPTIVE_MUTEX_HPP_INCLUDED_