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 locks.hpp
|
Chris@16
|
9 * \author Andrey Semashev
|
Chris@16
|
10 * \date 30.05.2010
|
Chris@16
|
11 *
|
Chris@16
|
12 * \brief This header is the Boost.Log library implementation, see the library documentation
|
Chris@16
|
13 * at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
|
Chris@16
|
14 */
|
Chris@16
|
15
|
Chris@16
|
16 #ifndef BOOST_LOG_DETAIL_LOCKS_HPP_INCLUDED_
|
Chris@16
|
17 #define BOOST_LOG_DETAIL_LOCKS_HPP_INCLUDED_
|
Chris@16
|
18
|
Chris@16
|
19 #include <boost/log/detail/config.hpp>
|
Chris@16
|
20 #include <boost/log/detail/header.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 #ifdef BOOST_HAS_PRAGMA_ONCE
|
Chris@16
|
23 #pragma once
|
Chris@16
|
24 #endif
|
Chris@16
|
25
|
Chris@16
|
26 namespace boost {
|
Chris@16
|
27
|
Chris@16
|
28 #ifndef BOOST_LOG_NO_THREADS
|
Chris@16
|
29
|
Chris@16
|
30 // Forward declaration of Boost.Thread locks. Specified here to avoid including Boost.Thread,
|
Chris@16
|
31 // which would bring in many dependent headers, including a great deal of Boost.DateTime.
|
Chris@16
|
32 template< typename >
|
Chris@16
|
33 class lock_guard;
|
Chris@16
|
34 template< typename >
|
Chris@16
|
35 class shared_lock;
|
Chris@16
|
36 template< typename >
|
Chris@16
|
37 class upgrade_lock;
|
Chris@16
|
38 template< typename >
|
Chris@16
|
39 class unique_lock;
|
Chris@16
|
40
|
Chris@16
|
41 template< typename >
|
Chris@16
|
42 struct is_mutex_type;
|
Chris@16
|
43
|
Chris@16
|
44 #endif // BOOST_LOG_NO_THREADS
|
Chris@16
|
45
|
Chris@16
|
46 BOOST_LOG_OPEN_NAMESPACE
|
Chris@16
|
47
|
Chris@16
|
48 //! An auxiliary pseudo-lock to express no locking requirements in logger features
|
Chris@16
|
49 template< typename MutexT >
|
Chris@16
|
50 class no_lock
|
Chris@16
|
51 {
|
Chris@16
|
52 public:
|
Chris@16
|
53 /*!
|
Chris@16
|
54 * Constructs the pseudo-lock. The mutex is not affected during the construction.
|
Chris@16
|
55 */
|
Chris@16
|
56 explicit no_lock(MutexT&) {}
|
Chris@16
|
57
|
Chris@16
|
58 private:
|
Chris@16
|
59 no_lock(no_lock const&);
|
Chris@16
|
60 no_lock& operator= (no_lock const&);
|
Chris@16
|
61 };
|
Chris@16
|
62
|
Chris@16
|
63 namespace aux {
|
Chris@16
|
64
|
Chris@16
|
65 #ifndef BOOST_LOG_NO_THREADS
|
Chris@16
|
66
|
Chris@16
|
67 //! A trait to detect if the mutex supports exclusive locking
|
Chris@16
|
68 template< typename MutexT >
|
Chris@16
|
69 struct is_exclusively_lockable
|
Chris@16
|
70 {
|
Chris@16
|
71 typedef char true_type;
|
Chris@16
|
72 struct false_type { char t[2]; };
|
Chris@16
|
73
|
Chris@16
|
74 template< typename T >
|
Chris@16
|
75 static true_type check_lockable(T*, void (T::*)() = &T::lock, void (T::*)() = &T::unlock);
|
Chris@16
|
76 static false_type check_lockable(void*);
|
Chris@16
|
77
|
Chris@16
|
78 enum value_t { value = sizeof(check_lockable((MutexT*)NULL)) == sizeof(true_type) };
|
Chris@16
|
79 };
|
Chris@16
|
80
|
Chris@16
|
81 //! A trait to detect if the mutex supports shared locking
|
Chris@16
|
82 template< typename MutexT >
|
Chris@16
|
83 struct is_shared_lockable
|
Chris@16
|
84 {
|
Chris@16
|
85 typedef char true_type;
|
Chris@16
|
86 struct false_type { char t[2]; };
|
Chris@16
|
87
|
Chris@16
|
88 template< typename T >
|
Chris@16
|
89 static true_type check_shared_lockable(T*, void (T::*)() = &T::lock_shared, void (T::*)() = &T::unlock_shared);
|
Chris@16
|
90 static false_type check_shared_lockable(void*);
|
Chris@16
|
91
|
Chris@16
|
92 enum value_t { value = sizeof(check_shared_lockable((MutexT*)NULL)) == sizeof(true_type) };
|
Chris@16
|
93 };
|
Chris@16
|
94
|
Chris@16
|
95 //! An analogue to the minimalistic \c lock_guard template. Defined here to avoid including Boost.Thread.
|
Chris@16
|
96 template< typename MutexT >
|
Chris@16
|
97 struct exclusive_lock_guard
|
Chris@16
|
98 {
|
Chris@16
|
99 explicit exclusive_lock_guard(MutexT& m) : m_Mutex(m)
|
Chris@16
|
100 {
|
Chris@16
|
101 m.lock();
|
Chris@16
|
102 }
|
Chris@16
|
103 ~exclusive_lock_guard()
|
Chris@16
|
104 {
|
Chris@16
|
105 m_Mutex.unlock();
|
Chris@16
|
106 }
|
Chris@16
|
107
|
Chris@16
|
108 private:
|
Chris@16
|
109 exclusive_lock_guard(exclusive_lock_guard const&);
|
Chris@16
|
110 exclusive_lock_guard& operator= (exclusive_lock_guard const&);
|
Chris@16
|
111
|
Chris@16
|
112 private:
|
Chris@16
|
113 MutexT& m_Mutex;
|
Chris@16
|
114 };
|
Chris@16
|
115
|
Chris@16
|
116 //! An analogue to the minimalistic \c lock_guard template that locks \c shared_mutex with shared ownership.
|
Chris@16
|
117 template< typename MutexT >
|
Chris@16
|
118 struct shared_lock_guard
|
Chris@16
|
119 {
|
Chris@16
|
120 explicit shared_lock_guard(MutexT& m) : m_Mutex(m)
|
Chris@16
|
121 {
|
Chris@16
|
122 m.lock_shared();
|
Chris@16
|
123 }
|
Chris@16
|
124 ~shared_lock_guard()
|
Chris@16
|
125 {
|
Chris@16
|
126 m_Mutex.unlock_shared();
|
Chris@16
|
127 }
|
Chris@16
|
128
|
Chris@16
|
129 private:
|
Chris@16
|
130 shared_lock_guard(shared_lock_guard const&);
|
Chris@16
|
131 shared_lock_guard& operator= (shared_lock_guard const&);
|
Chris@16
|
132
|
Chris@16
|
133 private:
|
Chris@16
|
134 MutexT& m_Mutex;
|
Chris@16
|
135 };
|
Chris@16
|
136
|
Chris@16
|
137 //! A deadlock-safe lock type that exclusively locks two mutexes
|
Chris@16
|
138 template< typename MutexT1, typename MutexT2 >
|
Chris@16
|
139 class multiple_unique_lock2
|
Chris@16
|
140 {
|
Chris@16
|
141 public:
|
Chris@16
|
142 multiple_unique_lock2(MutexT1& m1, MutexT2& m2) :
|
Chris@16
|
143 m_p1(&m1),
|
Chris@16
|
144 m_p2(&m2)
|
Chris@16
|
145 {
|
Chris@16
|
146 // Yes, it's not conforming, but it works
|
Chris@16
|
147 // and it doesn't require to #include <functional>
|
Chris@16
|
148 if (static_cast< void* >(m_p1) < static_cast< void* >(m_p2))
|
Chris@16
|
149 {
|
Chris@16
|
150 m_p1->lock();
|
Chris@16
|
151 m_p2->lock();
|
Chris@16
|
152 }
|
Chris@16
|
153 else
|
Chris@16
|
154 {
|
Chris@16
|
155 m_p2->lock();
|
Chris@16
|
156 m_p1->lock();
|
Chris@16
|
157 }
|
Chris@16
|
158 }
|
Chris@16
|
159 ~multiple_unique_lock2()
|
Chris@16
|
160 {
|
Chris@16
|
161 m_p2->unlock();
|
Chris@16
|
162 m_p1->unlock();
|
Chris@16
|
163 }
|
Chris@16
|
164
|
Chris@16
|
165 private:
|
Chris@16
|
166 MutexT1* m_p1;
|
Chris@16
|
167 MutexT2* m_p2;
|
Chris@16
|
168 };
|
Chris@16
|
169
|
Chris@16
|
170 #endif // BOOST_LOG_NO_THREADS
|
Chris@16
|
171
|
Chris@16
|
172 } // namespace aux
|
Chris@16
|
173
|
Chris@16
|
174 BOOST_LOG_CLOSE_NAMESPACE // namespace log
|
Chris@16
|
175
|
Chris@16
|
176 } // namespace boost
|
Chris@16
|
177
|
Chris@16
|
178 #include <boost/log/detail/footer.hpp>
|
Chris@16
|
179
|
Chris@16
|
180 #endif // BOOST_LOG_DETAIL_LOCKS_HPP_INCLUDED_
|