annotate DEPENDENCIES/generic/include/boost/interprocess/sync/scoped_lock.hpp @ 133:4acb5d8d80b6 tip

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