Chris@16
|
1 // (C) Copyright 2012 Vicente Botet
|
Chris@16
|
2 //
|
Chris@16
|
3 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
5
|
Chris@16
|
6 #ifndef BOOST_THREAD_LOCK_CONCEPTS_HPP
|
Chris@16
|
7 #define BOOST_THREAD_LOCK_CONCEPTS_HPP
|
Chris@16
|
8
|
Chris@16
|
9 #include <boost/thread/lock_traits.hpp>
|
Chris@16
|
10 #include <boost/thread/lock_options.hpp>
|
Chris@16
|
11 #include <boost/thread/lockable_concepts.hpp>
|
Chris@16
|
12 #include <boost/thread/exceptions.hpp>
|
Chris@16
|
13 #include <boost/thread/detail/move.hpp>
|
Chris@16
|
14
|
Chris@16
|
15 #include <boost/chrono/chrono.hpp>
|
Chris@16
|
16 #include <boost/concept_check.hpp>
|
Chris@16
|
17 #include <boost/static_assert.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 namespace boost
|
Chris@16
|
20 {
|
Chris@16
|
21
|
Chris@16
|
22 /**
|
Chris@16
|
23 * BasicLock object supports the basic features
|
Chris@16
|
24 * required to delimit a critical region
|
Chris@16
|
25 * Supports the basic lock, unlock and try_lock functions and
|
Chris@16
|
26 * defines the lock traits
|
Chris@16
|
27 */
|
Chris@16
|
28
|
Chris@16
|
29 template <typename Lk>
|
Chris@16
|
30 struct BasicLock
|
Chris@16
|
31 {
|
Chris@16
|
32 typedef typename Lk::mutex_type mutex_type;
|
Chris@16
|
33 void cvt_mutex_ptr(mutex_type*) {}
|
Chris@16
|
34 BOOST_CONCEPT_ASSERT(( BasicLockable<mutex_type> ));
|
Chris@16
|
35
|
Chris@16
|
36 BOOST_CONCEPT_USAGE(BasicLock)
|
Chris@16
|
37 {
|
Chris@16
|
38 const Lk l1(mtx);
|
Chris@16
|
39 Lk l2(mtx, defer_lock);
|
Chris@16
|
40 Lk l3(mtx, adopt_lock);
|
Chris@16
|
41 Lk l4(( Lk()));
|
Chris@16
|
42 Lk l5(( boost::move(l2)));
|
Chris@16
|
43 cvt_mutex_ptr(l1.mutex());
|
Chris@16
|
44 if (l1.owns_lock()) return;
|
Chris@16
|
45 if (l1) return;
|
Chris@16
|
46 if (!l1) return;
|
Chris@16
|
47
|
Chris@16
|
48 l2.lock();
|
Chris@16
|
49 l2.unlock();
|
Chris@16
|
50 l2.release();
|
Chris@16
|
51
|
Chris@16
|
52 }
|
Chris@16
|
53 BasicLock() :
|
Chris@16
|
54 mtx(*static_cast<mutex_type*>(0))
|
Chris@16
|
55 {}
|
Chris@16
|
56 private:
|
Chris@16
|
57 BasicLock operator=(BasicLock const&);
|
Chris@16
|
58 mutex_type& mtx;
|
Chris@16
|
59 }
|
Chris@16
|
60 ;
|
Chris@16
|
61
|
Chris@16
|
62 template <typename Lk>
|
Chris@16
|
63 struct Lock
|
Chris@16
|
64 {
|
Chris@16
|
65 BOOST_CONCEPT_ASSERT(( BasicLock<Lk> ));
|
Chris@16
|
66 typedef typename Lk::mutex_type mutex_type;
|
Chris@16
|
67 BOOST_CONCEPT_ASSERT(( Lockable<mutex_type> ));
|
Chris@16
|
68
|
Chris@16
|
69 BOOST_CONCEPT_USAGE(Lock)
|
Chris@16
|
70 {
|
Chris@16
|
71 Lk l1(mtx, try_to_lock);
|
Chris@16
|
72 if (l1.try_lock()) return;
|
Chris@16
|
73 }
|
Chris@16
|
74 Lock() :
|
Chris@16
|
75 mtx(*static_cast<mutex_type*>(0))
|
Chris@16
|
76 {}
|
Chris@16
|
77 private:
|
Chris@16
|
78 Lock operator=(Lock const&);
|
Chris@16
|
79 mutex_type& mtx;
|
Chris@16
|
80 };
|
Chris@16
|
81
|
Chris@16
|
82 template <typename Lk>
|
Chris@16
|
83 struct TimedLock
|
Chris@16
|
84 {
|
Chris@16
|
85 BOOST_CONCEPT_ASSERT(( Lock<Lk> ));
|
Chris@16
|
86 typedef typename Lk::mutex_type mutex_type;
|
Chris@16
|
87 BOOST_CONCEPT_ASSERT(( TimedLockable<mutex_type> ));
|
Chris@16
|
88
|
Chris@16
|
89 BOOST_CONCEPT_USAGE(TimedLock)
|
Chris@16
|
90 {
|
Chris@16
|
91 const Lk l1(mtx, t);
|
Chris@16
|
92 Lk l2(mtx, d);
|
Chris@16
|
93 if (l1.try_lock_until(t)) return;
|
Chris@16
|
94 if (l1.try_lock_for(d)) return;
|
Chris@16
|
95 }
|
Chris@16
|
96 TimedLock() :
|
Chris@16
|
97 mtx(*static_cast<mutex_type*>(0))
|
Chris@16
|
98 {}
|
Chris@16
|
99 private:
|
Chris@16
|
100 TimedLock operator=(TimedLock const&);
|
Chris@16
|
101 mutex_type& mtx;
|
Chris@16
|
102 boost::chrono::system_clock::time_point t;
|
Chris@16
|
103 boost::chrono::system_clock::duration d;
|
Chris@16
|
104 };
|
Chris@16
|
105
|
Chris@16
|
106 template <typename Lk>
|
Chris@16
|
107 struct UniqueLock
|
Chris@16
|
108 {
|
Chris@16
|
109 BOOST_CONCEPT_ASSERT(( TimedLock<Lk> ));
|
Chris@16
|
110 typedef typename Lk::mutex_type mutex_type;
|
Chris@16
|
111
|
Chris@16
|
112 BOOST_CONCEPT_USAGE(UniqueLock)
|
Chris@16
|
113 {
|
Chris@16
|
114
|
Chris@16
|
115 }
|
Chris@16
|
116 UniqueLock() :
|
Chris@16
|
117 mtx(*static_cast<mutex_type*>(0))
|
Chris@16
|
118 {}
|
Chris@16
|
119 private:
|
Chris@16
|
120 UniqueLock operator=(UniqueLock const&);
|
Chris@16
|
121 mutex_type& mtx;
|
Chris@16
|
122 };
|
Chris@16
|
123
|
Chris@16
|
124 template <typename Lk>
|
Chris@16
|
125 struct SharedLock
|
Chris@16
|
126 {
|
Chris@16
|
127 BOOST_CONCEPT_ASSERT(( TimedLock<Lk> ));
|
Chris@16
|
128 typedef typename Lk::mutex_type mutex_type;
|
Chris@16
|
129
|
Chris@16
|
130 BOOST_CONCEPT_USAGE(SharedLock)
|
Chris@16
|
131 {
|
Chris@16
|
132 }
|
Chris@16
|
133 SharedLock() :
|
Chris@16
|
134 mtx(*static_cast<mutex_type*>(0))
|
Chris@16
|
135 {}
|
Chris@16
|
136 private:
|
Chris@16
|
137 SharedLock operator=(SharedLock const&);
|
Chris@16
|
138 mutex_type& mtx;
|
Chris@16
|
139
|
Chris@16
|
140 };
|
Chris@16
|
141
|
Chris@16
|
142 template <typename Lk>
|
Chris@16
|
143 struct UpgradeLock
|
Chris@16
|
144 {
|
Chris@16
|
145 BOOST_CONCEPT_ASSERT(( SharedLock<Lk> ));
|
Chris@16
|
146 typedef typename Lk::mutex_type mutex_type;
|
Chris@16
|
147
|
Chris@16
|
148 BOOST_CONCEPT_USAGE(UpgradeLock)
|
Chris@16
|
149 {
|
Chris@16
|
150 }
|
Chris@16
|
151 UpgradeLock() :
|
Chris@16
|
152 mtx(*static_cast<mutex_type*>(0))
|
Chris@16
|
153 {}
|
Chris@16
|
154 private:
|
Chris@16
|
155 UpgradeLock operator=(UpgradeLock const&);
|
Chris@16
|
156 mutex_type& mtx;
|
Chris@16
|
157 };
|
Chris@16
|
158
|
Chris@16
|
159 /**
|
Chris@16
|
160 * An StrictLock is a scoped lock guard ensuring the mutex is locked on the
|
Chris@16
|
161 * scope of the lock, by locking the mutex on construction and unlocking it on
|
Chris@16
|
162 * destruction.
|
Chris@16
|
163 *
|
Chris@16
|
164 * Essentially, a StrictLock's role is only to live on the stack as an
|
Chris@16
|
165 * automatic variable. strict_lock must adhere to a non-copy and non-alias
|
Chris@16
|
166 * policy. StrictLock disables copying by making the copy constructor and the
|
Chris@16
|
167 * assignment operator private. While we're at it, let's disable operator new
|
Chris@16
|
168 * and operator delete; strict locks are not intended to be allocated on the
|
Chris@16
|
169 * heap. StrictLock avoids aliasing by using a slightly less orthodox and
|
Chris@16
|
170 * less well-known technique: disable address taking.
|
Chris@16
|
171 */
|
Chris@16
|
172
|
Chris@16
|
173 template <typename Lk>
|
Chris@16
|
174 struct StrictLock
|
Chris@16
|
175 {
|
Chris@16
|
176 typedef typename Lk::mutex_type mutex_type;
|
Chris@16
|
177 BOOST_CONCEPT_ASSERT(( BasicLockable<mutex_type> ));
|
Chris@16
|
178 BOOST_STATIC_ASSERT(( is_strict_lock<Lk>::value ));
|
Chris@16
|
179
|
Chris@16
|
180 BOOST_CONCEPT_USAGE( StrictLock)
|
Chris@16
|
181 {
|
Chris@16
|
182 if (l1.owns_lock(&mtx)) return;
|
Chris@16
|
183 }
|
Chris@16
|
184 StrictLock() :
|
Chris@16
|
185 l1(*static_cast<Lk*>(0)),
|
Chris@16
|
186 mtx(*static_cast<mutex_type*>(0))
|
Chris@16
|
187 {}
|
Chris@16
|
188 private:
|
Chris@16
|
189 StrictLock operator=(StrictLock const&);
|
Chris@16
|
190
|
Chris@16
|
191 Lk const& l1;
|
Chris@16
|
192 mutex_type const& mtx;
|
Chris@16
|
193
|
Chris@16
|
194 };
|
Chris@16
|
195
|
Chris@16
|
196 }
|
Chris@16
|
197 #endif
|