comparison DEPENDENCIES/generic/include/boost/interprocess/sync/upgradable_lock.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-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/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 //
11 // This interface is inspired by Howard Hinnant's lock proposal.
12 // http://home.twcny.rr.com/hinnant/cpp_extensions/threads_move.html
13 //
14 //////////////////////////////////////////////////////////////////////////////
15
16 #ifndef BOOST_INTERPROCESS_UPGRADABLE_LOCK_HPP
17 #define BOOST_INTERPROCESS_UPGRADABLE_LOCK_HPP
18
19 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
20 # pragma once
21 #endif
22
23 #include <boost/interprocess/detail/config_begin.hpp>
24 #include <boost/interprocess/detail/workaround.hpp>
25 #include <boost/interprocess/interprocess_fwd.hpp>
26 #include <boost/interprocess/sync/lock_options.hpp>
27 #include <boost/interprocess/detail/mpl.hpp>
28 #include <boost/interprocess/detail/type_traits.hpp>
29
30 #include <boost/interprocess/exceptions.hpp>
31 #include <boost/move/move.hpp>
32 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
33
34 //!\file
35 //!Describes the upgradable_lock class that serves to acquire the upgradable
36 //!lock of a mutex.
37
38 namespace boost {
39 namespace interprocess {
40
41 //!upgradable_lock is meant to carry out the tasks for read-locking, unlocking,
42 //!try-read-locking and timed-read-locking (recursive or not) for the Mutex.
43 //!Additionally the upgradable_lock can transfer ownership to a scoped_lock
44 //!using transfer_lock syntax. The Mutex need not supply all of the functionality.
45 //!If the client of upgradable_lock<Mutex> does not use functionality which the
46 //!Mutex does not supply, no harm is done. Mutex ownership can be shared among
47 //!read_locks, and a single upgradable_lock. upgradable_lock does not support
48 //!copy semantics. However upgradable_lock supports ownership transfer from
49 //!a upgradable_locks or scoped_locks via transfer_lock syntax.
50 template <class UpgradableMutex>
51 class upgradable_lock
52 {
53 public:
54 typedef UpgradableMutex mutex_type;
55 /// @cond
56 private:
57 typedef upgradable_lock<UpgradableMutex> this_type;
58 explicit upgradable_lock(scoped_lock<mutex_type>&);
59 typedef bool this_type::*unspecified_bool_type;
60 BOOST_MOVABLE_BUT_NOT_COPYABLE(upgradable_lock)
61 /// @endcond
62 public:
63
64 //!Effects: Default constructs a upgradable_lock.
65 //!Postconditions: owns() == false and mutex() == 0.
66 upgradable_lock()
67 : mp_mutex(0), m_locked(false)
68 {}
69
70 explicit upgradable_lock(mutex_type& m)
71 : mp_mutex(&m), m_locked(false)
72 { mp_mutex->lock_upgradable(); m_locked = true; }
73
74 //!Postconditions: owns() == false, and mutex() == &m.
75 //!Notes: The constructor will not take ownership of the mutex. There is no effect
76 //! required on the referenced mutex.
77 upgradable_lock(mutex_type& m, defer_lock_type)
78 : mp_mutex(&m), m_locked(false)
79 {}
80
81 //!Postconditions: owns() == true, and mutex() == &m.
82 //!Notes: The constructor will suppose that the mutex is already upgradable
83 //! locked. There is no effect required on the referenced mutex.
84 upgradable_lock(mutex_type& m, accept_ownership_type)
85 : mp_mutex(&m), m_locked(true)
86 {}
87
88 //!Effects: m.try_lock_upgradable().
89 //!Postconditions: mutex() == &m. owns() == the return value of the
90 //! m.try_lock_upgradable() executed within the constructor.
91 //!Notes: The constructor will take upgradable-ownership of the mutex
92 //! if it can do so without waiting. Whether or not this constructor
93 //! handles recursive locking depends upon the mutex. If the mutex_type
94 //! does not support try_lock_upgradable, this constructor will fail at
95 //! compile time if instantiated, but otherwise have no effect.
96 upgradable_lock(mutex_type& m, try_to_lock_type)
97 : mp_mutex(&m), m_locked(false)
98 { m_locked = mp_mutex->try_lock_upgradable(); }
99
100 //!Effects: m.timed_lock_upgradable(abs_time)
101 //!Postconditions: mutex() == &m. owns() == the return value of the
102 //! m.timed_lock_upgradable() executed within the constructor.
103 //!Notes: The constructor will take upgradable-ownership of the mutex if it
104 //! can do so within the time specified. Whether or not this constructor
105 //! handles recursive locking depends upon the mutex. If the mutex_type
106 //! does not support timed_lock_upgradable, this constructor will fail
107 //! at compile time if instantiated, but otherwise have no effect.
108 upgradable_lock(mutex_type& m, const boost::posix_time::ptime& abs_time)
109 : mp_mutex(&m), m_locked(false)
110 { m_locked = mp_mutex->timed_lock_upgradable(abs_time); }
111
112 //!Effects: No effects on the underlying mutex.
113 //!Postconditions: mutex() == the value upgr.mutex() had before the
114 //! construction. upgr.mutex() == 0. owns() == upgr.owns() before the
115 //! construction. upgr.owns() == false.
116 //!Notes: If upgr is locked, this constructor will lock this upgradable_lock
117 //! while unlocking upgr. If upgr is unlocked, then this upgradable_lock will
118 //! be unlocked as well. Only a moved upgradable_lock's will match this
119 //! signature. An non-moved upgradable_lock can be moved with the
120 //! expression: "boost::move(lock);". This constructor does not alter the
121 //! state of the mutex, only potentially who owns it.
122 upgradable_lock(BOOST_RV_REF(upgradable_lock<mutex_type>) upgr)
123 : mp_mutex(0), m_locked(upgr.owns())
124 { mp_mutex = upgr.release(); }
125
126 //!Effects: If scop.owns(), m_.unlock_and_lock_upgradable().
127 //!Postconditions: mutex() == the value scop.mutex() had before the construction.
128 //! scop.mutex() == 0. owns() == scop.owns() before the constructor. After the
129 //! construction, scop.owns() == false.
130 //!Notes: If scop is locked, this constructor will transfer the exclusive-ownership
131 //! to an upgradable-ownership of this upgradable_lock.
132 //! Only a moved sharable_lock's will match this
133 //! signature. An non-moved sharable_lock can be moved with the
134 //! expression: "boost::move(lock);".
135 template<class T>
136 upgradable_lock(BOOST_RV_REF(scoped_lock<T>) scop
137 , typename ipcdetail::enable_if< ipcdetail::is_same<T, UpgradableMutex> >::type * = 0)
138 : mp_mutex(0), m_locked(false)
139 {
140 scoped_lock<mutex_type> &u_lock = scop;
141 if(u_lock.owns()){
142 u_lock.mutex()->unlock_and_lock_upgradable();
143 m_locked = true;
144 }
145 mp_mutex = u_lock.release();
146 }
147
148 //!Effects: If shar.owns() then calls try_unlock_sharable_and_lock_upgradable()
149 //! on the referenced mutex.
150 //! a)if try_unlock_sharable_and_lock_upgradable() returns true then mutex()
151 //! obtains the value from shar.release() and owns() is set to true.
152 //! b)if try_unlock_sharable_and_lock_upgradable() returns false then shar is
153 //! unaffected and this upgradable_lock construction has the same
154 //! effects as a default construction.
155 //! c)Else shar.owns() is false. mutex() obtains the value from shar.release()
156 //! and owns() is set to false.
157 //!Notes: This construction will not block. It will try to obtain mutex
158 //! ownership from shar immediately, while changing the lock type from a
159 //! "read lock" to an "upgradable lock". If the "read lock" isn't held
160 //! in the first place, the mutex merely changes type to an unlocked
161 //! "upgradable lock". If the "read lock" is held, then mutex transfer
162 //! occurs only if it can do so in a non-blocking manner.
163 template<class T>
164 upgradable_lock( BOOST_RV_REF(sharable_lock<T>) shar, try_to_lock_type
165 , typename ipcdetail::enable_if< ipcdetail::is_same<T, UpgradableMutex> >::type * = 0)
166 : mp_mutex(0), m_locked(false)
167 {
168 sharable_lock<mutex_type> &s_lock = shar;
169 if(s_lock.owns()){
170 if((m_locked = s_lock.mutex()->try_unlock_sharable_and_lock_upgradable()) == true){
171 mp_mutex = s_lock.release();
172 }
173 }
174 else{
175 s_lock.release();
176 }
177 }
178
179 //!Effects: if (owns()) m_->unlock_upgradable().
180 //!Notes: The destructor behavior ensures that the mutex lock is not leaked.
181 ~upgradable_lock()
182 {
183 try{
184 if(m_locked && mp_mutex) mp_mutex->unlock_upgradable();
185 }
186 catch(...){}
187 }
188
189 //!Effects: If owns(), then unlock_upgradable() is called on mutex().
190 //! *this gets the state of upgr and upgr gets set to a default constructed state.
191 //!Notes: With a recursive mutex it is possible that both this and upgr own the
192 //! mutex before the assignment. In this case, this will own the mutex
193 //! after the assignment (and upgr will not), but the mutex's upgradable lock
194 //! count will be decremented by one.
195 upgradable_lock &operator=(BOOST_RV_REF(upgradable_lock) upgr)
196 {
197 if(this->owns())
198 this->unlock();
199 m_locked = upgr.owns();
200 mp_mutex = upgr.release();
201 return *this;
202 }
203
204 //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
205 //! exception. Calls lock_upgradable() on the referenced mutex.
206 //!Postconditions: owns() == true.
207 //!Notes: The sharable_lock changes from a state of not owning the mutex,
208 //! to owning the mutex, blocking if necessary.
209 void lock()
210 {
211 if(!mp_mutex || m_locked)
212 throw lock_exception();
213 mp_mutex->lock_upgradable();
214 m_locked = true;
215 }
216
217 //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
218 //! exception. Calls try_lock_upgradable() on the referenced mutex.
219 //!Postconditions: owns() == the value returned from
220 //! mutex()->try_lock_upgradable().
221 //!Notes: The upgradable_lock changes from a state of not owning the mutex,
222 //! to owning the mutex, but only if blocking was not required. If the
223 //! mutex_type does not support try_lock_upgradable(), this function will
224 //! fail at compile time if instantiated, but otherwise have no effect.
225 bool try_lock()
226 {
227 if(!mp_mutex || m_locked)
228 throw lock_exception();
229 m_locked = mp_mutex->try_lock_upgradable();
230 return m_locked;
231 }
232
233 //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
234 //! exception. Calls timed_lock_upgradable(abs_time) on the referenced mutex.
235 //!Postconditions: owns() == the value returned from
236 //! mutex()->timed_lock_upgradable(abs_time).
237 //!Notes: The upgradable_lock changes from a state of not owning the mutex,
238 //! to owning the mutex, but only if it can obtain ownership within the
239 //! specified time. If the mutex_type does not support
240 //! timed_lock_upgradable(abs_time), this function will fail at compile
241 //! time if instantiated, but otherwise have no effect.
242 bool timed_lock(const boost::posix_time::ptime& abs_time)
243 {
244 if(!mp_mutex || m_locked)
245 throw lock_exception();
246 m_locked = mp_mutex->timed_lock_upgradable(abs_time);
247 return m_locked;
248 }
249
250 //!Effects: If mutex() == 0 or if not locked, throws a lock_exception()
251 //! exception. Calls unlock_upgradable() on the referenced mutex.
252 //!Postconditions: owns() == false.
253 //!Notes: The upgradable_lock changes from a state of owning the mutex,
254 //! to not owning the mutex.
255 void unlock()
256 {
257 if(!mp_mutex || !m_locked)
258 throw lock_exception();
259 mp_mutex->unlock_upgradable();
260 m_locked = false;
261 }
262
263 //!Effects: Returns true if this scoped_lock has acquired the
264 //!referenced mutex.
265 bool owns() const
266 { return m_locked && mp_mutex; }
267
268 //!Conversion to bool.
269 //!Returns owns().
270 operator unspecified_bool_type() const
271 { return m_locked? &this_type::m_locked : 0; }
272
273 //!Effects: Returns a pointer to the referenced mutex, or 0 if
274 //!there is no mutex to reference.
275 mutex_type* mutex() const
276 { return mp_mutex; }
277
278 //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no
279 //! mutex to reference.
280 //!Postconditions: mutex() == 0 and owns() == false.
281 mutex_type* release()
282 {
283 mutex_type *mut = mp_mutex;
284 mp_mutex = 0;
285 m_locked = false;
286 return mut;
287 }
288
289 //!Effects: Swaps state with moved lock.
290 //!Throws: Nothing.
291 void swap(upgradable_lock<mutex_type> &other)
292 {
293 std::swap(mp_mutex, other.mp_mutex);
294 std::swap(m_locked, other.m_locked);
295 }
296
297 /// @cond
298 private:
299 mutex_type *mp_mutex;
300 bool m_locked;
301 /// @endcond
302 };
303
304 } // namespace interprocess
305 } // namespace boost
306
307 #include <boost/interprocess/detail/config_end.hpp>
308
309 #endif // BOOST_INTERPROCESS_UPGRADABLE_LOCK_HPP