annotate DEPENDENCIES/generic/include/boost/interprocess/sync/sharable_lock.hpp @ 125:34e428693f5d vext

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