strictest_lock.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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/mpl/integral_c.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 > > : mpl::integral_c< lock_access_mode, unlocked_access >
  62. {
  63. };
  64. #if !defined(BOOST_LOG_NO_THREADS)
  65. template< typename MutexT >
  66. struct thread_access_mode_of< lock_guard< MutexT > > : mpl::integral_c< lock_access_mode, exclusive_access >
  67. {
  68. };
  69. template< typename MutexT >
  70. struct thread_access_mode_of< shared_lock_guard< MutexT > > : mpl::integral_c< lock_access_mode, shared_access >
  71. {
  72. };
  73. template< typename MutexT >
  74. struct thread_access_mode_of< unique_lock< MutexT > > : mpl::integral_c< lock_access_mode, exclusive_access >
  75. {
  76. };
  77. template< typename MutexT >
  78. struct thread_access_mode_of< shared_lock< MutexT > > : mpl::integral_c< lock_access_mode, shared_access >
  79. {
  80. };
  81. template< typename MutexT >
  82. struct thread_access_mode_of< upgrade_lock< MutexT > > : mpl::integral_c< lock_access_mode, shared_access >
  83. {
  84. };
  85. template< typename MutexT >
  86. struct thread_access_mode_of< boost::log::aux::exclusive_lock_guard< MutexT > > : mpl::integral_c< lock_access_mode, exclusive_access >
  87. {
  88. };
  89. template< typename MutexT >
  90. struct thread_access_mode_of< boost::log::aux::shared_lock_guard< MutexT > > : mpl::integral_c< lock_access_mode, shared_access >
  91. {
  92. };
  93. template< typename MutexT1, typename MutexT2 >
  94. struct thread_access_mode_of< boost::log::aux::multiple_unique_lock2< MutexT1, MutexT2 > > : mpl::integral_c< lock_access_mode, exclusive_access >
  95. {
  96. };
  97. #endif // !defined(BOOST_LOG_NO_THREADS)
  98. namespace aux {
  99. //! The metafunction selects the most strict lock type of the two
  100. template<
  101. typename LeftLockT,
  102. typename RightLockT,
  103. #if !defined(BOOST_LOG_BROKEN_CONSTANT_EXPRESSIONS)
  104. bool CondV = (thread_access_mode_of< LeftLockT >::value < thread_access_mode_of< RightLockT >::value)
  105. #else
  106. bool CondV = mpl::less< thread_access_mode_of< LeftLockT >, thread_access_mode_of< RightLockT > >::value
  107. #endif
  108. >
  109. struct strictest_lock_impl
  110. {
  111. typedef RightLockT type;
  112. };
  113. template< typename LeftLockT, typename RightLockT >
  114. struct strictest_lock_impl< LeftLockT, RightLockT, false >
  115. {
  116. typedef LeftLockT type;
  117. };
  118. } // namespace aux
  119. #if defined(BOOST_LOG_DOXYGEN_PASS)
  120. /*!
  121. * \brief The metafunction selects the most strict lock type of the specified.
  122. *
  123. * The template supports all lock types provided by the Boost.Thread
  124. * library (except for \c upgrade_to_unique_lock), plus additional
  125. * pseudo-lock \c no_lock that indicates no locking at all.
  126. * Exclusive locks are considered the strictest, shared locks are weaker,
  127. * and \c no_lock is the weakest.
  128. */
  129. template< typename... LocksT >
  130. struct strictest_lock
  131. {
  132. typedef implementation_defined type;
  133. };
  134. #else // defined(BOOST_LOG_DOXYGEN_PASS)
  135. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  136. template< typename LockT, typename... LocksT >
  137. struct strictest_lock;
  138. template< typename LockT >
  139. struct strictest_lock< LockT >
  140. {
  141. typedef LockT type;
  142. };
  143. template< typename LeftLockT, typename RightLockT >
  144. struct strictest_lock< LeftLockT, RightLockT >
  145. {
  146. typedef typename aux::strictest_lock_impl< LeftLockT, RightLockT >::type type;
  147. };
  148. template< typename LeftLockT, typename RightLockT, typename... LocksT >
  149. struct strictest_lock< LeftLockT, RightLockT, LocksT... >
  150. {
  151. typedef typename strictest_lock<
  152. typename aux::strictest_lock_impl< LeftLockT, RightLockT >::type,
  153. LocksT...
  154. >::type type;
  155. };
  156. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  157. # define BOOST_LOG_TYPE_INTERNAL(z, i, data) BOOST_PP_CAT(T, BOOST_PP_INC(i))
  158. template<
  159. typename T,
  160. BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_PP_DEC(BOOST_LOG_STRICTEST_LOCK_LIMIT), typename T, void)
  161. >
  162. struct strictest_lock
  163. {
  164. typedef typename strictest_lock<
  165. typename boost::log::aux::strictest_lock_impl< T, T0 >::type
  166. BOOST_PP_ENUM_TRAILING(BOOST_PP_SUB(BOOST_LOG_STRICTEST_LOCK_LIMIT, 2), BOOST_LOG_TYPE_INTERNAL, ~)
  167. >::type type;
  168. };
  169. template< typename T >
  170. struct strictest_lock<
  171. T
  172. BOOST_PP_ENUM_TRAILING(BOOST_PP_DEC(BOOST_LOG_STRICTEST_LOCK_LIMIT), BOOST_LOG_PP_IDENTITY, void)
  173. >
  174. {
  175. typedef T type;
  176. };
  177. # undef BOOST_LOG_TYPE_INTERNAL
  178. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  179. #endif // defined(BOOST_LOG_DOXYGEN_PASS)
  180. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  181. } // namespace boost
  182. #include <boost/log/detail/footer.hpp>
  183. #endif // BOOST_LOG_UTILITY_STRICTEST_LOCK_HPP_INCLUDED_