annotate DEPENDENCIES/generic/include/boost/interprocess/sync/scoped_lock.hpp @ 16:2665513ce2d3

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