]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | ////////////////////////////////////////////////////////////////////////////// |
2 | // | |
3 | // (C) Copyright Vicente J. Botet Escriba 2008-2009,2012. Distributed under the Boost | |
4 | // Software License, Version 1.0. (See accompanying file | |
5 | // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | // See http://www.boost.org/libs/thread for documentation. | |
8 | // | |
9 | ////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifndef BOOST_THREAD_LOCKABLE_ADAPTER_HPP | |
12 | #define BOOST_THREAD_LOCKABLE_ADAPTER_HPP | |
13 | ||
14 | #include <boost/thread/detail/delete.hpp> | |
15 | #include <boost/chrono/chrono.hpp> | |
16 | ||
17 | namespace boost | |
18 | { | |
19 | ||
20 | //[basic_lockable_adapter | |
21 | template <typename BasicLockable> | |
22 | class basic_lockable_adapter | |
23 | { | |
24 | public: | |
25 | typedef BasicLockable mutex_type; | |
26 | ||
27 | protected: | |
28 | mutex_type& lockable() const | |
29 | { | |
30 | return lockable_; | |
31 | } | |
32 | mutable mutex_type lockable_; /*< mutable so that it can be modified by const functions >*/ | |
33 | public: | |
34 | ||
35 | BOOST_THREAD_NO_COPYABLE( basic_lockable_adapter) /*< no copyable >*/ | |
36 | ||
37 | basic_lockable_adapter() | |
38 | {} | |
39 | ||
40 | void lock() const | |
41 | { | |
42 | lockable().lock(); | |
43 | } | |
44 | void unlock() const | |
45 | { | |
46 | lockable().unlock(); | |
47 | } | |
48 | ||
49 | }; | |
50 | //] | |
51 | ||
52 | //[lockable_adapter | |
53 | template <typename Lockable> | |
54 | class lockable_adapter : public basic_lockable_adapter<Lockable> | |
55 | { | |
56 | public: | |
57 | typedef Lockable mutex_type; | |
58 | ||
59 | bool try_lock() const | |
60 | { | |
61 | return this->lockable().try_lock(); | |
62 | } | |
63 | }; | |
64 | //] | |
65 | ||
66 | //[timed_lockable_adapter | |
67 | template <typename TimedLock> | |
68 | class timed_lockable_adapter: public lockable_adapter<TimedLock> | |
69 | { | |
70 | public: | |
71 | typedef TimedLock mutex_type; | |
72 | ||
73 | template <typename Clock, typename Duration> | |
74 | bool try_lock_until(chrono::time_point<Clock, Duration> const & abs_time) const | |
75 | { | |
76 | return this->lockable().try_lock_until(abs_time); | |
77 | } | |
78 | template <typename Rep, typename Period> | |
79 | bool try_lock_for(chrono::duration<Rep, Period> const & rel_time) const | |
80 | { | |
81 | return this->lockable().try_lock_for(rel_time); | |
82 | } | |
83 | ||
84 | }; | |
85 | //] | |
86 | ||
87 | //[shared_lockable_adapter | |
88 | template <typename SharableLock> | |
89 | class shared_lockable_adapter: public timed_lockable_adapter<SharableLock> | |
90 | { | |
91 | public: | |
92 | typedef SharableLock mutex_type; | |
93 | ||
94 | void lock_shared() const | |
95 | { | |
96 | this->lockable().lock_shared(); | |
97 | } | |
98 | bool try_lock_shared() const | |
99 | { | |
100 | return this->lockable().try_lock_shared(); | |
101 | } | |
102 | void unlock_shared() const | |
103 | { | |
104 | this->lockable().unlock_shared(); | |
105 | } | |
106 | ||
107 | template <typename Clock, typename Duration> | |
108 | bool try_lock_shared_until(chrono::time_point<Clock, Duration> const & abs_time) const | |
109 | { | |
110 | return this->lockable().try_lock_shared_until(abs_time); | |
111 | } | |
112 | template <typename Rep, typename Period> | |
113 | bool try_lock_shared_for(chrono::duration<Rep, Period> const & rel_time) const | |
114 | { | |
115 | return this->lockable().try_lock_shared_for(rel_time); | |
116 | } | |
117 | ||
118 | }; | |
119 | ||
120 | //] | |
121 | ||
122 | //[upgrade_lockable_adapter | |
123 | template <typename UpgradableLock> | |
124 | class upgrade_lockable_adapter: public shared_lockable_adapter<UpgradableLock> | |
125 | { | |
126 | public: | |
127 | typedef UpgradableLock mutex_type; | |
128 | ||
129 | void lock_upgrade() const | |
130 | { | |
131 | this->lockable().lock_upgrade(); | |
132 | } | |
133 | ||
134 | bool try_lock_upgrade() const | |
135 | { | |
136 | return this->lockable().try_lock_upgrade(); | |
137 | } | |
138 | ||
139 | void unlock_upgrade() const | |
140 | { | |
141 | this->lockable().unlock_upgrade(); | |
142 | } | |
143 | ||
144 | template <typename Clock, typename Duration> | |
145 | bool try_lock_upgrade_until(chrono::time_point<Clock, Duration> const & abs_time) const | |
146 | { | |
147 | return this->lockable().try_lock_upgrade_until(abs_time); | |
148 | } | |
149 | template <typename Rep, typename Period> | |
150 | bool try_lock_upgrade_for(chrono::duration<Rep, Period> const & rel_time) const | |
151 | { | |
152 | return this->lockable().try_lock_upgrade_for(rel_time); | |
153 | } | |
154 | ||
155 | bool try_unlock_shared_and_lock() const | |
156 | { | |
157 | return this->lockable().try_unlock_shared_and_lock(); | |
158 | } | |
159 | ||
160 | template <typename Clock, typename Duration> | |
161 | bool try_unlock_shared_and_lock_until(chrono::time_point<Clock, Duration> const & abs_time) const | |
162 | { | |
163 | return this->lockable().try_unlock_shared_and_lock_until(abs_time); | |
164 | } | |
165 | template <typename Rep, typename Period> | |
166 | bool try_unlock_shared_and_lock_for(chrono::duration<Rep, Period> const & rel_time) const | |
167 | { | |
168 | return this->lockable().try_unlock_shared_and_lock_for(rel_time); | |
169 | } | |
170 | ||
171 | void unlock_and_lock_shared() const | |
172 | { | |
173 | this->lockable().unlock_and_lock_shared(); | |
174 | } | |
175 | ||
176 | bool try_unlock_shared_and_lock_upgrade() const | |
177 | { | |
178 | return this->lockable().try_unlock_shared_and_lock_upgrade(); | |
179 | } | |
180 | ||
181 | template <typename Clock, typename Duration> | |
182 | bool try_unlock_shared_and_lock_upgrade_until(chrono::time_point<Clock, Duration> const & abs_time) const | |
183 | { | |
184 | return this->lockable().try_unlock_shared_and_lock_upgrade_until(abs_time); | |
185 | } | |
186 | template <typename Rep, typename Period> | |
187 | bool try_unlock_shared_and_lock_upgrade_for(chrono::duration<Rep, Period> const & rel_time) const | |
188 | { | |
189 | return this->lockable().try_unlock_shared_and_lock_upgrade_for(rel_time); | |
190 | } | |
191 | ||
192 | void unlock_and_lock_upgrade() const | |
193 | { | |
194 | this->lockable().unlock_and_lock_upgrade(); | |
195 | } | |
196 | ||
197 | void unlock_upgrade_and_lock() const | |
198 | { | |
199 | this->lockable().unlock_upgrade_and_lock(); | |
200 | } | |
201 | ||
202 | bool try_unlock_upgrade_and_lock() const | |
203 | { | |
204 | return this->lockable().try_unlock_upgrade_and_lock(); | |
205 | } | |
206 | template <typename Clock, typename Duration> | |
207 | bool try_unlock_upgrade_and_lock_until(chrono::time_point<Clock, Duration> const & abs_time) const | |
208 | { | |
209 | return this->lockable().try_unlock_upgrade_and_lock_until(abs_time); | |
210 | } | |
211 | template <typename Rep, typename Period> | |
212 | bool try_unlock_upgrade_and_lock_for(chrono::duration<Rep, Period> const & rel_time) const | |
213 | { | |
214 | return this->lockable().try_unlock_upgrade_and_lock_for(rel_time); | |
215 | } | |
216 | ||
217 | void unlock_upgrade_and_lock_shared() const | |
218 | { | |
219 | this->lockable().unlock_upgrade_and_lock_shared(); | |
220 | } | |
221 | ||
222 | }; | |
223 | //] | |
224 | ||
225 | } | |
226 | #endif |