adaptive_mutex.hpp 6.1 KB

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