comparison DEPENDENCIES/generic/include/boost/interprocess/sync/sharable_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_SHARABLE_LOCK_HPP
17 #define BOOST_INTERPROCESS_SHARABLE_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/exceptions.hpp>
28 #include <boost/interprocess/detail/mpl.hpp>
29 #include <boost/interprocess/detail/type_traits.hpp>
30 #include <boost/move/move.hpp>
31 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
32
33 //!\file
34 //!Describes the upgradable_lock class that serves to acquire the upgradable
35 //!lock of a mutex.
36
37 namespace boost {
38 namespace interprocess {
39
40
41 //!sharable_lock is meant to carry out the tasks for sharable-locking
42 //!(such as read-locking), unlocking, try-sharable-locking and timed-sharable-locking
43 //!(recursive or not) for the Mutex. The Mutex need not supply all of this
44 //!functionality. If the client of sharable_lock<Mutex> does not use functionality which
45 //!the Mutex does not supply, no harm is done. Mutex ownership can be shared among
46 //!sharable_locks, and a single upgradable_lock. sharable_lock does not support
47 //!copy semantics. But sharable_lock supports ownership transfer from an sharable_lock,
48 //!upgradable_lock and scoped_lock via transfer_lock syntax.*/
49 template <class SharableMutex>
50 class sharable_lock
51 {
52 public:
53 typedef SharableMutex mutex_type;
54 /// @cond
55 private:
56 typedef sharable_lock<SharableMutex> this_type;
57 explicit sharable_lock(scoped_lock<mutex_type>&);
58 typedef bool this_type::*unspecified_bool_type;
59 BOOST_MOVABLE_BUT_NOT_COPYABLE(sharable_lock)
60 /// @endcond
61 public:
62
63 //!Effects: Default constructs a sharable_lock.
64 //!Postconditions: owns() == false and mutex() == 0.
65 sharable_lock()
66 : mp_mutex(0), m_locked(false)
67 {}
68
69 //!Effects: m.lock_sharable().
70 //!Postconditions: owns() == true and mutex() == &m.
71 //!Notes: The constructor will take sharable-ownership of the mutex. If
72 //! another thread already owns the mutex with exclusive ownership
73 //! (scoped_lock), this thread will block until the mutex is released.
74 //! If another thread owns the mutex with sharable or upgradable ownership,
75 //! then no blocking will occur. Whether or not this constructor handles
76 //! recursive locking depends upon the mutex.
77 explicit sharable_lock(mutex_type& m)
78 : mp_mutex(&m), m_locked(false)
79 { mp_mutex->lock_sharable(); m_locked = true; }
80
81 //!Postconditions: owns() == false, and mutex() == &m.
82 //!Notes: The constructor will not take ownership of the mutex. There is no effect
83 //! required on the referenced mutex.
84 sharable_lock(mutex_type& m, defer_lock_type)
85 : mp_mutex(&m), m_locked(false)
86 {}
87
88 //!Postconditions: owns() == true, and mutex() == &m.
89 //!Notes: The constructor will suppose that the mutex is already sharable
90 //! locked. There is no effect required on the referenced mutex.
91 sharable_lock(mutex_type& m, accept_ownership_type)
92 : mp_mutex(&m), m_locked(true)
93 {}
94
95 //!Effects: m.try_lock_sharable()
96 //!Postconditions: mutex() == &m. owns() == the return value of the
97 //! m.try_lock_sharable() executed within the constructor.
98 //!Notes: The constructor will take sharable-ownership of the mutex if it
99 //! can do so without waiting. Whether or not this constructor handles
100 //! recursive locking depends upon the mutex. If the mutex_type does not
101 //! support try_lock_sharable, this constructor will fail at compile
102 //! time if instantiated, but otherwise have no effect.
103 sharable_lock(mutex_type& m, try_to_lock_type)
104 : mp_mutex(&m), m_locked(false)
105 { m_locked = mp_mutex->try_lock_sharable(); }
106
107 //!Effects: m.timed_lock_sharable(abs_time)
108 //!Postconditions: mutex() == &m. owns() == the return value of the
109 //! m.timed_lock_sharable() executed within the constructor.
110 //!Notes: The constructor will take sharable-ownership of the mutex if it
111 //! can do so within the time specified. Whether or not this constructor
112 //! handles recursive locking depends upon the mutex. If the mutex_type
113 //! does not support timed_lock_sharable, this constructor will fail at
114 //! compile time if instantiated, but otherwise have no effect.
115 sharable_lock(mutex_type& m, const boost::posix_time::ptime& abs_time)
116 : mp_mutex(&m), m_locked(false)
117 { m_locked = mp_mutex->timed_lock_sharable(abs_time); }
118
119 //!Postconditions: mutex() == upgr.mutex(). owns() == the value of upgr.owns()
120 //! before the construction. upgr.owns() == false after the construction.
121 //!Notes: If the upgr sharable_lock owns the mutex, ownership is moved to this
122 //! sharable_lock with no blocking. If the upgr sharable_lock does not own the mutex, then
123 //! neither will this sharable_lock. Only a moved sharable_lock's will match this
124 //! signature. An non-moved sharable_lock can be moved with the expression:
125 //! "boost::move(lock);". This constructor does not alter the state of the mutex,
126 //! only potentially who owns it.
127 sharable_lock(BOOST_RV_REF(sharable_lock<mutex_type>) upgr)
128 : mp_mutex(0), m_locked(upgr.owns())
129 { mp_mutex = upgr.release(); }
130
131 //!Effects: If upgr.owns() then calls unlock_upgradable_and_lock_sharable() on the
132 //! referenced mutex.
133 //!Postconditions: mutex() == the value upgr.mutex() had before the construction.
134 //! upgr.mutex() == 0 owns() == the value of upgr.owns() before construction.
135 //! upgr.owns() == false after the construction.
136 //!Notes: If upgr is locked, this constructor will lock this sharable_lock while
137 //! unlocking upgr. Only a moved sharable_lock's will match this
138 //! signature. An non-moved upgradable_lock can be moved with the expression:
139 //! "boost::move(lock);".*/
140 template<class T>
141 sharable_lock(BOOST_RV_REF(upgradable_lock<T>) upgr
142 , typename ipcdetail::enable_if< ipcdetail::is_same<T, SharableMutex> >::type * = 0)
143 : mp_mutex(0), m_locked(false)
144 {
145 upgradable_lock<mutex_type> &u_lock = upgr;
146 if(u_lock.owns()){
147 u_lock.mutex()->unlock_upgradable_and_lock_sharable();
148 m_locked = true;
149 }
150 mp_mutex = u_lock.release();
151 }
152
153 //!Effects: If scop.owns() then calls unlock_and_lock_sharable() on the
154 //! referenced mutex.
155 //!Postconditions: mutex() == the value scop.mutex() had before the construction.
156 //! scop.mutex() == 0 owns() == scop.owns() before the constructor. After the
157 //! construction, scop.owns() == false.
158 //!Notes: If scop is locked, this constructor will transfer the exclusive ownership
159 //! to a sharable-ownership of this sharable_lock.
160 //! Only a moved scoped_lock's will match this
161 //! signature. An non-moved scoped_lock can be moved with the expression:
162 //! "boost::move(lock);".
163 template<class T>
164 sharable_lock(BOOST_RV_REF(scoped_lock<T>) scop
165 , typename ipcdetail::enable_if< ipcdetail::is_same<T, SharableMutex> >::type * = 0)
166 : mp_mutex(0), m_locked(false)
167 {
168 scoped_lock<mutex_type> &e_lock = scop;
169 if(e_lock.owns()){
170 e_lock.mutex()->unlock_and_lock_sharable();
171 m_locked = true;
172 }
173 mp_mutex = e_lock.release();
174 }
175
176 //!Effects: if (owns()) mp_mutex->unlock_sharable().
177 //!Notes: The destructor behavior ensures that the mutex lock is not leaked.
178 ~sharable_lock()
179 {
180 try{
181 if(m_locked && mp_mutex) mp_mutex->unlock_sharable();
182 }
183 catch(...){}
184 }
185
186 //!Effects: If owns() before the call, then unlock_sharable() is called on mutex().
187 //! *this gets the state of upgr and upgr gets set to a default constructed state.
188 //!Notes: With a recursive mutex it is possible that both this and upgr own the mutex
189 //! before the assignment. In this case, this will own the mutex after the assignment
190 //! (and upgr will not), but the mutex's lock count will be decremented by one.
191 sharable_lock &operator=(BOOST_RV_REF(sharable_lock<mutex_type>) upgr)
192 {
193 if(this->owns())
194 this->unlock();
195 m_locked = upgr.owns();
196 mp_mutex = upgr.release();
197 return *this;
198 }
199
200 //!Effects: If mutex() == 0 or already locked, throws a lock_exception()
201 //! exception. Calls lock_sharable() on the referenced mutex.
202 //!Postconditions: owns() == true.
203 //!Notes: The sharable_lock changes from a state of not owning the
204 //! mutex, to owning the mutex, blocking if necessary.
205 void lock()
206 {
207 if(!mp_mutex || m_locked)
208 throw lock_exception();
209 mp_mutex->lock_sharable();
210 m_locked = true;
211 }
212
213 //!Effects: If mutex() == 0 or already locked, throws a lock_exception()
214 //! exception. Calls try_lock_sharable() on the referenced mutex.
215 //!Postconditions: owns() == the value returned from
216 //! mutex()->try_lock_sharable().
217 //!Notes: The sharable_lock changes from a state of not owning the mutex,
218 //! to owning the mutex, but only if blocking was not required. If the
219 //! mutex_type does not support try_lock_sharable(), this function will
220 //! fail at compile time if instantiated, but otherwise have no effect.
221 bool try_lock()
222 {
223 if(!mp_mutex || m_locked)
224 throw lock_exception();
225 m_locked = mp_mutex->try_lock_sharable();
226 return m_locked;
227 }
228
229 //!Effects: If mutex() == 0 or already locked, throws a lock_exception()
230 //! exception. Calls timed_lock_sharable(abs_time) on the referenced mutex.
231 //!Postconditions: owns() == the value returned from
232 //! mutex()->timed_lock_sharable(elps_time).
233 //!Notes: The sharable_lock changes from a state of not owning the mutex,
234 //! to owning the mutex, but only if it can obtain ownership within the
235 //! specified time interval. If the mutex_type does not support
236 //! timed_lock_sharable(), this function will fail at compile time if
237 //! instantiated, but otherwise have no effect.
238 bool timed_lock(const boost::posix_time::ptime& abs_time)
239 {
240 if(!mp_mutex || m_locked)
241 throw lock_exception();
242 m_locked = mp_mutex->timed_lock_sharable(abs_time);
243 return m_locked;
244 }
245
246 //!Effects: If mutex() == 0 or not locked, throws a lock_exception() exception.
247 //! Calls unlock_sharable() on the referenced mutex.
248 //!Postconditions: owns() == false.
249 //!Notes: The sharable_lock changes from a state of owning the mutex, to
250 //! not owning the mutex.
251 void unlock()
252 {
253 if(!mp_mutex || !m_locked)
254 throw lock_exception();
255 mp_mutex->unlock_sharable();
256 m_locked = false;
257 }
258
259 //!Effects: Returns true if this scoped_lock has
260 //!acquired the referenced mutex.
261 bool owns() const
262 { return m_locked && mp_mutex; }
263
264 //!Conversion to bool.
265 //!Returns owns().
266 operator unspecified_bool_type() const
267 { return m_locked? &this_type::m_locked : 0; }
268
269 //!Effects: Returns a pointer to the referenced mutex, or 0 if
270 //!there is no mutex to reference.
271 mutex_type* mutex() const
272 { return mp_mutex; }
273
274 //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no
275 //! mutex to reference.
276 //!Postconditions: mutex() == 0 and owns() == false.
277 mutex_type* release()
278 {
279 mutex_type *mut = mp_mutex;
280 mp_mutex = 0;
281 m_locked = false;
282 return mut;
283 }
284
285 //!Effects: Swaps state with moved lock.
286 //!Throws: Nothing.
287 void swap(sharable_lock<mutex_type> &other)
288 {
289 std::swap(mp_mutex, other.mp_mutex);
290 std::swap(m_locked, other.m_locked);
291 }
292
293 /// @cond
294 private:
295 mutex_type *mp_mutex;
296 bool m_locked;
297 /// @endcond
298 };
299
300 } // namespace interprocess
301 } // namespace boost
302
303 #include <boost/interprocess/detail/config_end.hpp>
304
305 #endif // BOOST_INTERPROCESS_SHARABLE_LOCK_HPP