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