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