adaptive_mutex.hpp 6.2 KB

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