strictest_lock.hpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 utility/strictest_lock.hpp
  9. * \author Andrey Semashev
  10. * \date 30.05.2010
  11. *
  12. * The header contains definition of the \c strictest_lock metafunction that
  13. * allows to select a lock with the strictest access requirements.
  14. */
  15. #ifndef BOOST_LOG_UTILITY_STRICTEST_LOCK_HPP_INCLUDED_
  16. #define BOOST_LOG_UTILITY_STRICTEST_LOCK_HPP_INCLUDED_
  17. #include <boost/type_traits/integral_constant.hpp>
  18. #include <boost/log/detail/config.hpp>
  19. #include <boost/log/detail/locks.hpp>
  20. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  21. #include <boost/preprocessor/cat.hpp>
  22. #include <boost/preprocessor/arithmetic/sub.hpp>
  23. #include <boost/preprocessor/arithmetic/inc.hpp>
  24. #include <boost/preprocessor/arithmetic/dec.hpp>
  25. #include <boost/preprocessor/repetition/enum_trailing.hpp>
  26. #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
  27. #include <boost/log/detail/pp_identity.hpp>
  28. #endif
  29. #if defined(BOOST_LOG_BROKEN_CONSTANT_EXPRESSIONS)
  30. #include <boost/mpl/less.hpp>
  31. #endif
  32. #include <boost/log/detail/header.hpp>
  33. #ifdef BOOST_HAS_PRAGMA_ONCE
  34. #pragma once
  35. #endif
  36. #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  37. #if !defined(BOOST_LOG_STRICTEST_LOCK_LIMIT)
  38. /*!
  39. * The macro defines the maximum number of template arguments that the \c strictest_lock
  40. * metafunction accepts. Should not be less than 2.
  41. */
  42. #define BOOST_LOG_STRICTEST_LOCK_LIMIT 10
  43. #endif // BOOST_LOG_STRICTEST_LOCK_LIMIT
  44. #if BOOST_LOG_STRICTEST_LOCK_LIMIT < 2
  45. #error The BOOST_LOG_STRICTEST_LOCK_LIMIT macro should not be less than 2
  46. #endif
  47. #endif // defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  48. namespace boost {
  49. BOOST_LOG_OPEN_NAMESPACE
  50. //! Access modes for different types of locks
  51. enum lock_access_mode
  52. {
  53. unlocked_access, //!< A thread that owns this kind of lock doesn't restrict other threads in any way
  54. shared_access, //!< A thread that owns this kind of lock requires that no other thread modify the locked data
  55. exclusive_access //!< A thread that owns this kind of lock requires that no other thread has access to the locked data
  56. };
  57. //! The trait allows to select an access mode by the lock type
  58. template< typename LockT >
  59. struct thread_access_mode_of;
  60. template< typename MutexT >
  61. struct thread_access_mode_of< no_lock< MutexT > > : boost::integral_constant< lock_access_mode, unlocked_access >
  62. {
  63. };
  64. #if !defined(BOOST_LOG_NO_THREADS)
  65. #if !defined(BOOST_MSSTL_VERSION) || (BOOST_MSSTL_VERSION != 140)
  66. template< typename MutexT >
  67. struct thread_access_mode_of< std::lock_guard< MutexT > > : boost::integral_constant< lock_access_mode, exclusive_access >
  68. {
  69. };
  70. #else // !defined(BOOST_MSSTL_VERSION) || (BOOST_MSSTL_VERSION != 140)
  71. template< typename... MutexesT >
  72. struct thread_access_mode_of< std::lock_guard< MutexesT... > > : boost::integral_constant< lock_access_mode, exclusive_access >
  73. {
  74. };
  75. #endif // !defined(BOOST_MSSTL_VERSION) || (BOOST_MSSTL_VERSION != 140)
  76. template< typename MutexT >
  77. struct thread_access_mode_of< std::unique_lock< MutexT > > : boost::integral_constant< lock_access_mode, exclusive_access >
  78. {
  79. };
  80. #if !defined(BOOST_NO_CXX14_HDR_SHARED_MUTEX)
  81. template< typename MutexT >
  82. struct thread_access_mode_of< std::shared_lock< MutexT > > : boost::integral_constant< lock_access_mode, shared_access >
  83. {
  84. };
  85. #endif // !defined(BOOST_NO_CXX14_HDR_SHARED_MUTEX)
  86. #if defined(__cpp_lib_scoped_lock) && (__cpp_lib_scoped_lock >= 201703l)
  87. template< typename... MutexesT >
  88. struct thread_access_mode_of< std::scoped_lock< MutexesT... > > : boost::integral_constant< lock_access_mode, exclusive_access >
  89. {
  90. };
  91. #endif
  92. template< typename MutexT >
  93. struct thread_access_mode_of< lock_guard< MutexT > > : boost::integral_constant< lock_access_mode, exclusive_access >
  94. {
  95. };
  96. template< typename MutexT >
  97. struct thread_access_mode_of< shared_lock_guard< MutexT > > : boost::integral_constant< lock_access_mode, shared_access >
  98. {
  99. };
  100. template< typename MutexT >
  101. struct thread_access_mode_of< unique_lock< MutexT > > : boost::integral_constant< lock_access_mode, exclusive_access >
  102. {
  103. };
  104. template< typename MutexT >
  105. struct thread_access_mode_of< shared_lock< MutexT > > : boost::integral_constant< lock_access_mode, shared_access >
  106. {
  107. };
  108. template< typename MutexT >
  109. struct thread_access_mode_of< upgrade_lock< MutexT > > : boost::integral_constant< lock_access_mode, shared_access >
  110. {
  111. };
  112. template< typename MutexT >
  113. struct thread_access_mode_of< boost::log::aux::exclusive_lock_guard< MutexT > > : boost::integral_constant< lock_access_mode, exclusive_access >
  114. {
  115. };
  116. template< typename MutexT >
  117. struct thread_access_mode_of< boost::log::aux::shared_lock_guard< MutexT > > : boost::integral_constant< lock_access_mode, shared_access >
  118. {
  119. };
  120. template< typename MutexT1, typename MutexT2 >
  121. struct thread_access_mode_of< boost::log::aux::multiple_unique_lock2< MutexT1, MutexT2 > > : boost::integral_constant< lock_access_mode, exclusive_access >
  122. {
  123. };
  124. #endif // !defined(BOOST_LOG_NO_THREADS)
  125. namespace aux {
  126. //! The metafunction selects the most strict lock type of the two
  127. template<
  128. typename LeftLockT,
  129. typename RightLockT,
  130. #if !defined(BOOST_LOG_BROKEN_CONSTANT_EXPRESSIONS)
  131. bool CondV = (thread_access_mode_of< LeftLockT >::value < thread_access_mode_of< RightLockT >::value)
  132. #else
  133. bool CondV = mpl::less< thread_access_mode_of< LeftLockT >, thread_access_mode_of< RightLockT > >::value
  134. #endif
  135. >
  136. struct strictest_lock_impl
  137. {
  138. typedef RightLockT type;
  139. };
  140. template< typename LeftLockT, typename RightLockT >
  141. struct strictest_lock_impl< LeftLockT, RightLockT, false >
  142. {
  143. typedef LeftLockT type;
  144. };
  145. } // namespace aux
  146. #if defined(BOOST_LOG_DOXYGEN_PASS)
  147. /*!
  148. * \brief The metafunction selects the most strict lock type of the specified.
  149. *
  150. * The template supports all lock types provided by the Boost.Thread
  151. * library (except for \c upgrade_to_unique_lock), plus additional
  152. * pseudo-lock \c no_lock that indicates no locking at all.
  153. * Exclusive locks are considered the strictest, shared locks are weaker,
  154. * and \c no_lock is the weakest.
  155. */
  156. template< typename... LocksT >
  157. struct strictest_lock
  158. {
  159. typedef implementation_defined type;
  160. };
  161. #else // defined(BOOST_LOG_DOXYGEN_PASS)
  162. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  163. template< typename LockT, typename... LocksT >
  164. struct strictest_lock;
  165. template< typename LockT >
  166. struct strictest_lock< LockT >
  167. {
  168. typedef LockT type;
  169. };
  170. template< typename LeftLockT, typename RightLockT >
  171. struct strictest_lock< LeftLockT, RightLockT >
  172. {
  173. typedef typename aux::strictest_lock_impl< LeftLockT, RightLockT >::type type;
  174. };
  175. template< typename LeftLockT, typename RightLockT, typename... LocksT >
  176. struct strictest_lock< LeftLockT, RightLockT, LocksT... >
  177. {
  178. typedef typename strictest_lock<
  179. typename aux::strictest_lock_impl< LeftLockT, RightLockT >::type,
  180. LocksT...
  181. >::type type;
  182. };
  183. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  184. # define BOOST_LOG_TYPE_INTERNAL(z, i, data) BOOST_PP_CAT(T, BOOST_PP_INC(i))
  185. template<
  186. typename T,
  187. BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_PP_DEC(BOOST_LOG_STRICTEST_LOCK_LIMIT), typename T, void)
  188. >
  189. struct strictest_lock
  190. {
  191. typedef typename strictest_lock<
  192. typename boost::log::aux::strictest_lock_impl< T, T0 >::type
  193. BOOST_PP_ENUM_TRAILING(BOOST_PP_SUB(BOOST_LOG_STRICTEST_LOCK_LIMIT, 2), BOOST_LOG_TYPE_INTERNAL, ~)
  194. >::type type;
  195. };
  196. template< typename T >
  197. struct strictest_lock<
  198. T
  199. BOOST_PP_ENUM_TRAILING(BOOST_PP_DEC(BOOST_LOG_STRICTEST_LOCK_LIMIT), BOOST_LOG_PP_IDENTITY, void)
  200. >
  201. {
  202. typedef T type;
  203. };
  204. # undef BOOST_LOG_TYPE_INTERNAL
  205. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  206. #endif // defined(BOOST_LOG_DOXYGEN_PASS)
  207. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  208. } // namespace boost
  209. #include <boost/log/detail/footer.hpp>
  210. #endif // BOOST_LOG_UTILITY_STRICTEST_LOCK_HPP_INCLUDED_