strictest_lock.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. #endif // !defined(BOOST_LOG_NO_THREADS)
  94. namespace aux {
  95. //! The metafunction selects the most strict lock type of the two
  96. template<
  97. typename LeftLockT,
  98. typename RightLockT,
  99. #if !defined(BOOST_LOG_BROKEN_CONSTANT_EXPRESSIONS)
  100. bool CondV = (thread_access_mode_of< LeftLockT >::value < thread_access_mode_of< RightLockT >::value)
  101. #else
  102. bool CondV = mpl::less< thread_access_mode_of< LeftLockT >, thread_access_mode_of< RightLockT > >::value
  103. #endif
  104. >
  105. struct strictest_lock_impl
  106. {
  107. typedef RightLockT type;
  108. };
  109. template< typename LeftLockT, typename RightLockT >
  110. struct strictest_lock_impl< LeftLockT, RightLockT, false >
  111. {
  112. typedef LeftLockT type;
  113. };
  114. } // namespace aux
  115. #if defined(BOOST_LOG_DOXYGEN_PASS)
  116. /*!
  117. * \brief The metafunction selects the most strict lock type of the specified.
  118. *
  119. * The template supports all lock types provided by the Boost.Thread
  120. * library (except for \c upgrade_to_unique_lock), plus additional
  121. * pseudo-lock \c no_lock that indicates no locking at all.
  122. * Exclusive locks are considered the strictest, shared locks are weaker,
  123. * and \c no_lock is the weakest.
  124. */
  125. template< typename... LocksT >
  126. struct strictest_lock
  127. {
  128. typedef implementation_defined type;
  129. };
  130. #else // defined(BOOST_LOG_DOXYGEN_PASS)
  131. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  132. template< typename LockT, typename... LocksT >
  133. struct strictest_lock;
  134. template< typename LockT >
  135. struct strictest_lock< LockT >
  136. {
  137. typedef LockT type;
  138. };
  139. template< typename LeftLockT, typename RightLockT >
  140. struct strictest_lock< LeftLockT, RightLockT >
  141. {
  142. typedef typename aux::strictest_lock_impl< LeftLockT, RightLockT >::type type;
  143. };
  144. template< typename LeftLockT, typename RightLockT, typename... LocksT >
  145. struct strictest_lock< LeftLockT, RightLockT, LocksT... >
  146. {
  147. typedef typename strictest_lock<
  148. typename aux::strictest_lock_impl< LeftLockT, RightLockT >::type,
  149. LocksT...
  150. >::type type;
  151. };
  152. #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  153. # define BOOST_LOG_TYPE_INTERNAL(z, i, data) BOOST_PP_CAT(T, BOOST_PP_INC(i))
  154. template<
  155. typename T,
  156. BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_PP_DEC(BOOST_LOG_STRICTEST_LOCK_LIMIT), typename T, void)
  157. >
  158. struct strictest_lock
  159. {
  160. typedef typename strictest_lock<
  161. typename boost::log::aux::strictest_lock_impl< T, T0 >::type
  162. BOOST_PP_ENUM_TRAILING(BOOST_PP_SUB(BOOST_LOG_STRICTEST_LOCK_LIMIT, 2), BOOST_LOG_TYPE_INTERNAL, ~)
  163. >::type type;
  164. };
  165. template< typename T >
  166. struct strictest_lock<
  167. T
  168. BOOST_PP_ENUM_TRAILING(BOOST_PP_DEC(BOOST_LOG_STRICTEST_LOCK_LIMIT), BOOST_LOG_PP_IDENTITY, void)
  169. >
  170. {
  171. typedef T type;
  172. };
  173. # undef BOOST_LOG_TYPE_INTERNAL
  174. #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  175. #endif // defined(BOOST_LOG_DOXYGEN_PASS)
  176. BOOST_LOG_CLOSE_NAMESPACE // namespace log
  177. } // namespace boost
  178. #include <boost/log/detail/footer.hpp>
  179. #endif // BOOST_LOG_UTILITY_STRICTEST_LOCK_HPP_INCLUDED_