Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-05 09:22:01

0001 /*
0002  *          Copyright Andrey Semashev 2007 - 2015.
0003  * Distributed under the Boost Software License, Version 1.0.
0004  *    (See accompanying file LICENSE_1_0.txt or copy at
0005  *          http://www.boost.org/LICENSE_1_0.txt)
0006  */
0007 /*!
0008  * \file   utility/strictest_lock.hpp
0009  * \author Andrey Semashev
0010  * \date   30.05.2010
0011  *
0012  * The header contains definition of the \c strictest_lock metafunction that
0013  * allows to select a lock with the strictest access requirements.
0014  */
0015 
0016 #ifndef BOOST_LOG_UTILITY_STRICTEST_LOCK_HPP_INCLUDED_
0017 #define BOOST_LOG_UTILITY_STRICTEST_LOCK_HPP_INCLUDED_
0018 
0019 #include <boost/type_traits/integral_constant.hpp>
0020 #include <boost/log/detail/config.hpp>
0021 #include <boost/log/detail/locks.hpp>
0022 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
0023 #include <boost/preprocessor/cat.hpp>
0024 #include <boost/preprocessor/arithmetic/sub.hpp>
0025 #include <boost/preprocessor/arithmetic/inc.hpp>
0026 #include <boost/preprocessor/arithmetic/dec.hpp>
0027 #include <boost/preprocessor/repetition/enum_trailing.hpp>
0028 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
0029 #include <boost/log/detail/pp_identity.hpp>
0030 #endif
0031 #if defined(BOOST_LOG_BROKEN_CONSTANT_EXPRESSIONS)
0032 #include <boost/mpl/less.hpp>
0033 #endif
0034 #include <boost/log/detail/header.hpp>
0035 
0036 #ifdef BOOST_HAS_PRAGMA_ONCE
0037 #pragma once
0038 #endif
0039 
0040 #if defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
0041 #if !defined(BOOST_LOG_STRICTEST_LOCK_LIMIT)
0042 /*!
0043  * The macro defines the maximum number of template arguments that the \c strictest_lock
0044  * metafunction accepts. Should not be less than 2.
0045  */
0046 #define BOOST_LOG_STRICTEST_LOCK_LIMIT 10
0047 #endif // BOOST_LOG_STRICTEST_LOCK_LIMIT
0048 #if BOOST_LOG_STRICTEST_LOCK_LIMIT < 2
0049 #error The BOOST_LOG_STRICTEST_LOCK_LIMIT macro should not be less than 2
0050 #endif
0051 #endif // defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
0052 
0053 namespace boost {
0054 
0055 BOOST_LOG_OPEN_NAMESPACE
0056 
0057 //! Access modes for different types of locks
0058 enum lock_access_mode
0059 {
0060     unlocked_access,    //!< A thread that owns this kind of lock doesn't restrict other threads in any way
0061     shared_access,      //!< A thread that owns this kind of lock requires that no other thread modify the locked data
0062     exclusive_access    //!< A thread that owns this kind of lock requires that no other thread has access to the locked data
0063 };
0064 
0065 //! The trait allows to select an access mode by the lock type
0066 template< typename LockT >
0067 struct thread_access_mode_of;
0068 
0069 template< typename MutexT >
0070 struct thread_access_mode_of< no_lock< MutexT > > : boost::integral_constant< lock_access_mode, unlocked_access >
0071 {
0072 };
0073 
0074 #if !defined(BOOST_LOG_NO_THREADS)
0075 
0076 #if !defined(BOOST_MSSTL_VERSION) || (BOOST_MSSTL_VERSION != 140)
0077 template< typename MutexT >
0078 struct thread_access_mode_of< std::lock_guard< MutexT > > : boost::integral_constant< lock_access_mode, exclusive_access >
0079 {
0080 };
0081 #else // !defined(BOOST_MSSTL_VERSION) || (BOOST_MSSTL_VERSION != 140)
0082 template< typename... MutexesT >
0083 struct thread_access_mode_of< std::lock_guard< MutexesT... > > : boost::integral_constant< lock_access_mode, exclusive_access >
0084 {
0085 };
0086 #endif // !defined(BOOST_MSSTL_VERSION) || (BOOST_MSSTL_VERSION != 140)
0087 
0088 template< typename MutexT >
0089 struct thread_access_mode_of< std::unique_lock< MutexT > > : boost::integral_constant< lock_access_mode, exclusive_access >
0090 {
0091 };
0092 
0093 #if !defined(BOOST_NO_CXX14_HDR_SHARED_MUTEX)
0094 template< typename MutexT >
0095 struct thread_access_mode_of< std::shared_lock< MutexT > > : boost::integral_constant< lock_access_mode, shared_access >
0096 {
0097 };
0098 #endif // !defined(BOOST_NO_CXX14_HDR_SHARED_MUTEX)
0099 
0100 #if defined(__cpp_lib_scoped_lock) && (__cpp_lib_scoped_lock >= 201703l)
0101 template< typename... MutexesT >
0102 struct thread_access_mode_of< std::scoped_lock< MutexesT... > > : boost::integral_constant< lock_access_mode, exclusive_access >
0103 {
0104 };
0105 #endif
0106 
0107 template< typename MutexT >
0108 struct thread_access_mode_of< lock_guard< MutexT > > : boost::integral_constant< lock_access_mode, exclusive_access >
0109 {
0110 };
0111 
0112 template< typename MutexT >
0113 struct thread_access_mode_of< shared_lock_guard< MutexT > > : boost::integral_constant< lock_access_mode, shared_access >
0114 {
0115 };
0116 
0117 template< typename MutexT >
0118 struct thread_access_mode_of< unique_lock< MutexT > > : boost::integral_constant< lock_access_mode, exclusive_access >
0119 {
0120 };
0121 
0122 template< typename MutexT >
0123 struct thread_access_mode_of< shared_lock< MutexT > > : boost::integral_constant< lock_access_mode, shared_access >
0124 {
0125 };
0126 
0127 template< typename MutexT >
0128 struct thread_access_mode_of< upgrade_lock< MutexT > > : boost::integral_constant< lock_access_mode, shared_access >
0129 {
0130 };
0131 
0132 template< typename MutexT >
0133 struct thread_access_mode_of< boost::log::aux::exclusive_lock_guard< MutexT > > : boost::integral_constant< lock_access_mode, exclusive_access >
0134 {
0135 };
0136 
0137 template< typename MutexT >
0138 struct thread_access_mode_of< boost::log::aux::shared_lock_guard< MutexT > > : boost::integral_constant< lock_access_mode, shared_access >
0139 {
0140 };
0141 
0142 template< typename MutexT1, typename MutexT2 >
0143 struct thread_access_mode_of< boost::log::aux::multiple_unique_lock2< MutexT1, MutexT2 > > : boost::integral_constant< lock_access_mode, exclusive_access >
0144 {
0145 };
0146 
0147 #endif // !defined(BOOST_LOG_NO_THREADS)
0148 
0149 namespace aux {
0150 
0151 //! The metafunction selects the most strict lock type of the two
0152 template<
0153     typename LeftLockT,
0154     typename RightLockT,
0155 #if !defined(BOOST_LOG_BROKEN_CONSTANT_EXPRESSIONS)
0156     bool CondV = (thread_access_mode_of< LeftLockT >::value < thread_access_mode_of< RightLockT >::value)
0157 #else
0158     bool CondV = mpl::less< thread_access_mode_of< LeftLockT >, thread_access_mode_of< RightLockT > >::value
0159 #endif
0160 >
0161 struct strictest_lock_impl
0162 {
0163     typedef RightLockT type;
0164 };
0165 template< typename LeftLockT, typename RightLockT >
0166 struct strictest_lock_impl< LeftLockT, RightLockT, false >
0167 {
0168     typedef LeftLockT type;
0169 };
0170 
0171 } // namespace aux
0172 
0173 #if defined(BOOST_LOG_DOXYGEN_PASS)
0174 
0175 /*!
0176  * \brief The metafunction selects the most strict lock type of the specified.
0177  *
0178  * The template supports all lock types provided by the Boost.Thread
0179  * library (except for \c upgrade_to_unique_lock), plus additional
0180  * pseudo-lock \c no_lock that indicates no locking at all.
0181  * Exclusive locks are considered the strictest, shared locks are weaker,
0182  * and \c no_lock is the weakest.
0183  */
0184 template< typename... LocksT >
0185 struct strictest_lock
0186 {
0187     typedef implementation_defined type;
0188 };
0189 
0190 #else // defined(BOOST_LOG_DOXYGEN_PASS)
0191 
0192 #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
0193 
0194 template< typename LockT, typename... LocksT >
0195 struct strictest_lock;
0196 
0197 template< typename LockT >
0198 struct strictest_lock< LockT >
0199 {
0200     typedef LockT type;
0201 };
0202 
0203 template< typename LeftLockT, typename RightLockT >
0204 struct strictest_lock< LeftLockT, RightLockT >
0205 {
0206     typedef typename aux::strictest_lock_impl< LeftLockT, RightLockT >::type type;
0207 };
0208 
0209 template< typename LeftLockT, typename RightLockT, typename... LocksT >
0210 struct strictest_lock< LeftLockT, RightLockT, LocksT... >
0211 {
0212     typedef typename strictest_lock<
0213         typename aux::strictest_lock_impl< LeftLockT, RightLockT >::type,
0214         LocksT...
0215     >::type type;
0216 };
0217 
0218 #else // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
0219 
0220 #   define BOOST_LOG_TYPE_INTERNAL(z, i, data) BOOST_PP_CAT(T, BOOST_PP_INC(i))
0221 
0222 template<
0223     typename T,
0224     BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_PP_DEC(BOOST_LOG_STRICTEST_LOCK_LIMIT), typename T, void)
0225 >
0226 struct strictest_lock
0227 {
0228     typedef typename strictest_lock<
0229         typename boost::log::aux::strictest_lock_impl< T, T0 >::type
0230         BOOST_PP_ENUM_TRAILING(BOOST_PP_SUB(BOOST_LOG_STRICTEST_LOCK_LIMIT, 2), BOOST_LOG_TYPE_INTERNAL, ~)
0231     >::type type;
0232 };
0233 
0234 template< typename T >
0235 struct strictest_lock<
0236     T
0237     BOOST_PP_ENUM_TRAILING(BOOST_PP_DEC(BOOST_LOG_STRICTEST_LOCK_LIMIT), BOOST_LOG_PP_IDENTITY, void)
0238 >
0239 {
0240     typedef T type;
0241 };
0242 
0243 #   undef BOOST_LOG_TYPE_INTERNAL
0244 
0245 #endif // !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
0246 
0247 #endif // defined(BOOST_LOG_DOXYGEN_PASS)
0248 
0249 BOOST_LOG_CLOSE_NAMESPACE // namespace log
0250 
0251 } // namespace boost
0252 
0253 #include <boost/log/detail/footer.hpp>
0254 
0255 #endif // BOOST_LOG_UTILITY_STRICTEST_LOCK_HPP_INCLUDED_