Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // This interface is inspired by Howard Hinnant's lock proposal. Chris@16: // http://home.twcny.rr.com/hinnant/cpp_extensions/threads_move.html Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_SHARABLE_LOCK_HPP Chris@16: #define BOOST_INTERPROCESS_SHARABLE_LOCK_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@101: #include Chris@101: #include Chris@16: #include Chris@16: Chris@16: //!\file Chris@16: //!Describes the upgradable_lock class that serves to acquire the upgradable Chris@16: //!lock of a mutex. Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: Chris@16: Chris@16: //!sharable_lock is meant to carry out the tasks for sharable-locking Chris@16: //!(such as read-locking), unlocking, try-sharable-locking and timed-sharable-locking Chris@16: //!(recursive or not) for the Mutex. The Mutex need not supply all of this Chris@16: //!functionality. If the client of sharable_lock does not use functionality which Chris@16: //!the Mutex does not supply, no harm is done. Mutex ownership can be shared among Chris@16: //!sharable_locks, and a single upgradable_lock. sharable_lock does not support Chris@16: //!copy semantics. But sharable_lock supports ownership transfer from an sharable_lock, Chris@16: //!upgradable_lock and scoped_lock via transfer_lock syntax.*/ Chris@16: template Chris@16: class sharable_lock Chris@16: { Chris@16: public: Chris@16: typedef SharableMutex mutex_type; Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: typedef sharable_lock this_type; Chris@16: explicit sharable_lock(scoped_lock&); Chris@16: typedef bool this_type::*unspecified_bool_type; Chris@16: BOOST_MOVABLE_BUT_NOT_COPYABLE(sharable_lock) Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: public: Chris@16: Chris@16: //!Effects: Default constructs a sharable_lock. Chris@16: //!Postconditions: owns() == false and mutex() == 0. Chris@16: sharable_lock() Chris@16: : mp_mutex(0), m_locked(false) Chris@16: {} Chris@16: Chris@16: //!Effects: m.lock_sharable(). Chris@16: //!Postconditions: owns() == true and mutex() == &m. Chris@16: //!Notes: The constructor will take sharable-ownership of the mutex. If Chris@16: //! another thread already owns the mutex with exclusive ownership Chris@16: //! (scoped_lock), this thread will block until the mutex is released. Chris@16: //! If another thread owns the mutex with sharable or upgradable ownership, Chris@16: //! then no blocking will occur. Whether or not this constructor handles Chris@16: //! recursive locking depends upon the mutex. Chris@16: explicit sharable_lock(mutex_type& m) Chris@16: : mp_mutex(&m), m_locked(false) Chris@16: { mp_mutex->lock_sharable(); m_locked = true; } Chris@16: Chris@16: //!Postconditions: owns() == false, and mutex() == &m. Chris@16: //!Notes: The constructor will not take ownership of the mutex. There is no effect Chris@16: //! required on the referenced mutex. Chris@16: sharable_lock(mutex_type& m, defer_lock_type) Chris@16: : mp_mutex(&m), m_locked(false) Chris@16: {} Chris@16: Chris@16: //!Postconditions: owns() == true, and mutex() == &m. Chris@16: //!Notes: The constructor will suppose that the mutex is already sharable Chris@16: //! locked. There is no effect required on the referenced mutex. Chris@16: sharable_lock(mutex_type& m, accept_ownership_type) Chris@16: : mp_mutex(&m), m_locked(true) Chris@16: {} Chris@16: Chris@16: //!Effects: m.try_lock_sharable() Chris@16: //!Postconditions: mutex() == &m. owns() == the return value of the Chris@16: //! m.try_lock_sharable() executed within the constructor. Chris@16: //!Notes: The constructor will take sharable-ownership of the mutex if it Chris@16: //! can do so without waiting. Whether or not this constructor handles Chris@16: //! recursive locking depends upon the mutex. If the mutex_type does not Chris@16: //! support try_lock_sharable, this constructor will fail at compile Chris@16: //! time if instantiated, but otherwise have no effect. Chris@16: sharable_lock(mutex_type& m, try_to_lock_type) Chris@16: : mp_mutex(&m), m_locked(false) Chris@16: { m_locked = mp_mutex->try_lock_sharable(); } Chris@16: Chris@16: //!Effects: m.timed_lock_sharable(abs_time) Chris@16: //!Postconditions: mutex() == &m. owns() == the return value of the Chris@16: //! m.timed_lock_sharable() executed within the constructor. Chris@16: //!Notes: The constructor will take sharable-ownership of the mutex if it Chris@16: //! can do so within the time specified. Whether or not this constructor Chris@16: //! handles recursive locking depends upon the mutex. If the mutex_type Chris@16: //! does not support timed_lock_sharable, this constructor will fail at Chris@16: //! compile time if instantiated, but otherwise have no effect. Chris@16: sharable_lock(mutex_type& m, const boost::posix_time::ptime& abs_time) Chris@16: : mp_mutex(&m), m_locked(false) Chris@16: { m_locked = mp_mutex->timed_lock_sharable(abs_time); } Chris@16: Chris@16: //!Postconditions: mutex() == upgr.mutex(). owns() == the value of upgr.owns() Chris@16: //! before the construction. upgr.owns() == false after the construction. Chris@16: //!Notes: If the upgr sharable_lock owns the mutex, ownership is moved to this Chris@16: //! sharable_lock with no blocking. If the upgr sharable_lock does not own the mutex, then Chris@16: //! neither will this sharable_lock. Only a moved sharable_lock's will match this Chris@16: //! signature. An non-moved sharable_lock can be moved with the expression: Chris@16: //! "boost::move(lock);". This constructor does not alter the state of the mutex, Chris@16: //! only potentially who owns it. Chris@16: sharable_lock(BOOST_RV_REF(sharable_lock) upgr) Chris@16: : mp_mutex(0), m_locked(upgr.owns()) Chris@16: { mp_mutex = upgr.release(); } Chris@16: Chris@16: //!Effects: If upgr.owns() then calls unlock_upgradable_and_lock_sharable() on the Chris@16: //! referenced mutex. Chris@16: //!Postconditions: mutex() == the value upgr.mutex() had before the construction. Chris@16: //! upgr.mutex() == 0 owns() == the value of upgr.owns() before construction. Chris@16: //! upgr.owns() == false after the construction. Chris@16: //!Notes: If upgr is locked, this constructor will lock this sharable_lock while Chris@16: //! unlocking upgr. Only a moved sharable_lock's will match this Chris@16: //! signature. An non-moved upgradable_lock can be moved with the expression: Chris@16: //! "boost::move(lock);".*/ Chris@16: template Chris@16: sharable_lock(BOOST_RV_REF(upgradable_lock) upgr Chris@16: , typename ipcdetail::enable_if< ipcdetail::is_same >::type * = 0) Chris@16: : mp_mutex(0), m_locked(false) Chris@16: { Chris@16: upgradable_lock &u_lock = upgr; Chris@16: if(u_lock.owns()){ Chris@16: u_lock.mutex()->unlock_upgradable_and_lock_sharable(); Chris@16: m_locked = true; Chris@16: } Chris@16: mp_mutex = u_lock.release(); Chris@16: } Chris@16: Chris@16: //!Effects: If scop.owns() then calls unlock_and_lock_sharable() on the Chris@16: //! referenced mutex. Chris@16: //!Postconditions: mutex() == the value scop.mutex() had before the construction. Chris@16: //! scop.mutex() == 0 owns() == scop.owns() before the constructor. After the Chris@16: //! construction, scop.owns() == false. Chris@16: //!Notes: If scop is locked, this constructor will transfer the exclusive ownership Chris@16: //! to a sharable-ownership of this sharable_lock. Chris@16: //! Only a moved scoped_lock's will match this Chris@16: //! signature. An non-moved scoped_lock can be moved with the expression: Chris@16: //! "boost::move(lock);". Chris@16: template Chris@16: sharable_lock(BOOST_RV_REF(scoped_lock) scop Chris@16: , typename ipcdetail::enable_if< ipcdetail::is_same >::type * = 0) Chris@16: : mp_mutex(0), m_locked(false) Chris@16: { Chris@16: scoped_lock &e_lock = scop; Chris@16: if(e_lock.owns()){ Chris@16: e_lock.mutex()->unlock_and_lock_sharable(); Chris@16: m_locked = true; Chris@16: } Chris@16: mp_mutex = e_lock.release(); Chris@16: } Chris@16: Chris@16: //!Effects: if (owns()) mp_mutex->unlock_sharable(). Chris@16: //!Notes: The destructor behavior ensures that the mutex lock is not leaked. Chris@16: ~sharable_lock() Chris@16: { Chris@16: try{ Chris@16: if(m_locked && mp_mutex) mp_mutex->unlock_sharable(); Chris@16: } Chris@16: catch(...){} Chris@16: } Chris@16: Chris@16: //!Effects: If owns() before the call, then unlock_sharable() is called on mutex(). Chris@16: //! *this gets the state of upgr and upgr gets set to a default constructed state. Chris@16: //!Notes: With a recursive mutex it is possible that both this and upgr own the mutex Chris@16: //! before the assignment. In this case, this will own the mutex after the assignment Chris@16: //! (and upgr will not), but the mutex's lock count will be decremented by one. Chris@16: sharable_lock &operator=(BOOST_RV_REF(sharable_lock) upgr) Chris@16: { Chris@16: if(this->owns()) Chris@16: this->unlock(); Chris@16: m_locked = upgr.owns(); Chris@16: mp_mutex = upgr.release(); Chris@16: return *this; Chris@16: } Chris@16: Chris@16: //!Effects: If mutex() == 0 or already locked, throws a lock_exception() Chris@16: //! exception. Calls lock_sharable() on the referenced mutex. Chris@16: //!Postconditions: owns() == true. Chris@16: //!Notes: The sharable_lock changes from a state of not owning the Chris@16: //! mutex, to owning the mutex, blocking if necessary. Chris@16: void lock() Chris@16: { Chris@16: if(!mp_mutex || m_locked) Chris@16: throw lock_exception(); Chris@16: mp_mutex->lock_sharable(); Chris@16: m_locked = true; Chris@16: } Chris@16: Chris@16: //!Effects: If mutex() == 0 or already locked, throws a lock_exception() Chris@16: //! exception. Calls try_lock_sharable() on the referenced mutex. Chris@16: //!Postconditions: owns() == the value returned from Chris@16: //! mutex()->try_lock_sharable(). Chris@16: //!Notes: The sharable_lock changes from a state of not owning the mutex, Chris@16: //! to owning the mutex, but only if blocking was not required. If the Chris@16: //! mutex_type does not support try_lock_sharable(), this function will Chris@16: //! fail at compile time if instantiated, but otherwise have no effect. Chris@16: bool try_lock() Chris@16: { Chris@16: if(!mp_mutex || m_locked) Chris@16: throw lock_exception(); Chris@16: m_locked = mp_mutex->try_lock_sharable(); Chris@16: return m_locked; Chris@16: } Chris@16: Chris@16: //!Effects: If mutex() == 0 or already locked, throws a lock_exception() Chris@16: //! exception. Calls timed_lock_sharable(abs_time) on the referenced mutex. Chris@16: //!Postconditions: owns() == the value returned from Chris@16: //! mutex()->timed_lock_sharable(elps_time). Chris@16: //!Notes: The sharable_lock changes from a state of not owning the mutex, Chris@16: //! to owning the mutex, but only if it can obtain ownership within the Chris@16: //! specified time interval. If the mutex_type does not support Chris@16: //! timed_lock_sharable(), this function will fail at compile time if Chris@16: //! instantiated, but otherwise have no effect. Chris@16: bool timed_lock(const boost::posix_time::ptime& abs_time) Chris@16: { Chris@16: if(!mp_mutex || m_locked) Chris@16: throw lock_exception(); Chris@16: m_locked = mp_mutex->timed_lock_sharable(abs_time); Chris@16: return m_locked; Chris@16: } Chris@16: Chris@16: //!Effects: If mutex() == 0 or not locked, throws a lock_exception() exception. Chris@16: //! Calls unlock_sharable() on the referenced mutex. Chris@16: //!Postconditions: owns() == false. Chris@16: //!Notes: The sharable_lock changes from a state of owning the mutex, to Chris@16: //! not owning the mutex. Chris@16: void unlock() Chris@16: { Chris@16: if(!mp_mutex || !m_locked) Chris@16: throw lock_exception(); Chris@16: mp_mutex->unlock_sharable(); Chris@16: m_locked = false; Chris@16: } Chris@16: Chris@16: //!Effects: Returns true if this scoped_lock has Chris@16: //!acquired the referenced mutex. Chris@16: bool owns() const Chris@16: { return m_locked && mp_mutex; } Chris@16: Chris@16: //!Conversion to bool. Chris@16: //!Returns owns(). Chris@16: operator unspecified_bool_type() const Chris@16: { return m_locked? &this_type::m_locked : 0; } Chris@16: Chris@16: //!Effects: Returns a pointer to the referenced mutex, or 0 if Chris@16: //!there is no mutex to reference. Chris@16: mutex_type* mutex() const Chris@16: { return mp_mutex; } Chris@16: Chris@16: //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no Chris@16: //! mutex to reference. Chris@16: //!Postconditions: mutex() == 0 and owns() == false. Chris@16: mutex_type* release() Chris@16: { Chris@16: mutex_type *mut = mp_mutex; Chris@16: mp_mutex = 0; Chris@16: m_locked = false; Chris@16: return mut; Chris@16: } Chris@16: Chris@16: //!Effects: Swaps state with moved lock. Chris@16: //!Throws: Nothing. Chris@16: void swap(sharable_lock &other) Chris@16: { Chris@101: (simple_swap)(mp_mutex, other.mp_mutex); Chris@101: (simple_swap)(m_locked, other.m_locked); Chris@16: } Chris@16: Chris@101: #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) Chris@16: private: Chris@16: mutex_type *mp_mutex; Chris@16: bool m_locked; Chris@101: #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED Chris@16: }; Chris@16: Chris@16: } // namespace interprocess Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // BOOST_INTERPROCESS_SHARABLE_LOCK_HPP