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