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