strictest_lock.hpp 6.3 KB

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