Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:34

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 //
0011 // This interface is inspired by Howard Hinnant's lock proposal.
0012 // http://home.twcny.rr.com/hinnant/cpp_extensions/threads_move.html
0013 //
0014 //////////////////////////////////////////////////////////////////////////////
0015 
0016 #ifndef BOOST_INTERPROCESS_UPGRADABLE_LOCK_HPP
0017 #define BOOST_INTERPROCESS_UPGRADABLE_LOCK_HPP
0018 
0019 #ifndef BOOST_CONFIG_HPP
0020 #  include <boost/config.hpp>
0021 #endif
0022 #
0023 #if defined(BOOST_HAS_PRAGMA_ONCE)
0024 #  pragma once
0025 #endif
0026 
0027 #include <boost/interprocess/detail/config_begin.hpp>
0028 #include <boost/interprocess/detail/workaround.hpp>
0029 #include <boost/interprocess/interprocess_fwd.hpp>
0030 #include <boost/interprocess/sync/lock_options.hpp>
0031 #include <boost/interprocess/detail/mpl.hpp>
0032 #include <boost/interprocess/detail/type_traits.hpp>
0033 
0034 #include <boost/interprocess/exceptions.hpp>
0035 #include <boost/move/utility_core.hpp>
0036 
0037 //!\file
0038 //!Describes the upgradable_lock class that serves to acquire the upgradable
0039 //!lock of a mutex.
0040 
0041 namespace boost {
0042 namespace interprocess {
0043 
0044 //!upgradable_lock is meant to carry out the tasks for read-locking, unlocking,
0045 //!try-read-locking and timed-read-locking (recursive or not) for the Mutex.
0046 //!Additionally the upgradable_lock can transfer ownership to a scoped_lock
0047 //!using transfer_lock syntax. The Mutex need not supply all of the functionality.
0048 //!If the client of upgradable_lock<Mutex> does not use functionality which the
0049 //!Mutex does not supply, no harm is done. Mutex ownership can be shared among
0050 //!read_locks, and a single upgradable_lock. upgradable_lock does not support
0051 //!copy semantics. However upgradable_lock supports ownership transfer from
0052 //!a upgradable_locks or scoped_locks via transfer_lock syntax.
0053 template <class UpgradableMutex>
0054 class upgradable_lock
0055 {
0056    public:
0057    typedef UpgradableMutex mutex_type;
0058    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0059    private:
0060    typedef upgradable_lock<UpgradableMutex> this_type;
0061    explicit upgradable_lock(scoped_lock<mutex_type>&);
0062    typedef bool this_type::*unspecified_bool_type;
0063    BOOST_MOVABLE_BUT_NOT_COPYABLE(upgradable_lock)
0064    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0065    public:
0066 
0067    //!Effects: Default constructs a upgradable_lock.
0068    //!Postconditions: owns() == false and mutex() == 0.
0069    upgradable_lock() BOOST_NOEXCEPT
0070       : mp_mutex(0), m_locked(false)
0071    {}
0072 
0073    explicit upgradable_lock(mutex_type& m)
0074       : mp_mutex(&m), m_locked(false)
0075    {  mp_mutex->lock_upgradable();   m_locked = true;  }
0076 
0077    //!Postconditions: owns() == false, and mutex() == &m.
0078    //!Notes: The constructor will not take ownership of the mutex. There is no effect
0079    //!   required on the referenced mutex.
0080    upgradable_lock(mutex_type& m, defer_lock_type)
0081       : mp_mutex(&m), m_locked(false)
0082    {}
0083 
0084    //!Postconditions: owns() == true, and mutex() == &m.
0085    //!Notes: The constructor will suppose that the mutex is already upgradable
0086    //!   locked. There is no effect required on the referenced mutex.
0087    upgradable_lock(mutex_type& m, accept_ownership_type)
0088       : mp_mutex(&m), m_locked(true)
0089    {}
0090 
0091    //!Effects: m.try_lock_upgradable().
0092    //!Postconditions: mutex() == &m. owns() == the return value of the
0093    //!   m.try_lock_upgradable() executed within the constructor.
0094    //!Notes: The constructor will take upgradable-ownership of the mutex
0095    //!   if it can do so without waiting. Whether or not this constructor
0096    //!   handles recursive locking depends upon the mutex. If the mutex_type
0097    //!   does not support try_lock_upgradable, this constructor will fail at
0098    //!   compile time if instantiated, but otherwise have no effect.
0099    upgradable_lock(mutex_type& m, try_to_lock_type)
0100       : mp_mutex(&m), m_locked(false)
0101    {  m_locked = mp_mutex->try_lock_upgradable();   }
0102 
0103    //!Effects: m.timed_lock_upgradable(abs_time)
0104    //!Postconditions: mutex() == &m. owns() == the return value of the
0105    //!   m.timed_lock_upgradable() executed within the constructor.
0106    //!Notes: The constructor will take upgradable-ownership of the mutex if it
0107    //!   can do so within the time specified. Whether or not this constructor
0108    //!   handles recursive locking depends upon the mutex. If the mutex_type
0109    //!   does not support timed_lock_upgradable, this constructor will fail
0110    //!   at compile time if instantiated, but otherwise have no effect.
0111    template<class TimePoint>
0112    upgradable_lock(mutex_type& m, const TimePoint& abs_time)
0113       : mp_mutex(&m), m_locked(false)
0114    {  m_locked = mp_mutex->timed_lock_upgradable(abs_time);  }
0115 
0116    //!Effects: No effects on the underlying mutex.
0117    //!Postconditions: mutex() == the value upgr.mutex() had before the
0118    //!   construction. upgr.mutex() == 0. owns() == upgr.owns() before the
0119    //!   construction. upgr.owns() == false.
0120    //!Notes: If upgr is locked, this constructor will lock this upgradable_lock
0121    //!   while unlocking upgr. If upgr is unlocked, then this upgradable_lock will
0122    //!   be unlocked as well. Only a moved upgradable_lock's will match this
0123    //!   signature. An non-moved upgradable_lock can be moved with the
0124    //!   expression: "boost::move(lock);". This constructor does not alter the
0125    //!   state of the mutex, only potentially who owns it.
0126    upgradable_lock(BOOST_RV_REF(upgradable_lock<mutex_type>) upgr) BOOST_NOEXCEPT
0127       : mp_mutex(0), m_locked(upgr.owns())
0128    {  mp_mutex = upgr.release(); }
0129 
0130    //!Effects: If scop.owns(), m_.unlock_and_lock_upgradable().
0131    //!Postconditions: mutex() == the value scop.mutex() had before the construction.
0132    //!   scop.mutex() == 0. owns() == scop.owns() before the constructor. After the
0133    //!   construction, scop.owns() == false.
0134    //!Notes: If scop is locked, this constructor will transfer the exclusive-ownership
0135    //!   to an upgradable-ownership of this upgradable_lock.
0136    //!   Only a moved sharable_lock's will match this
0137    //!   signature. An non-moved sharable_lock can be moved with the
0138    //!   expression: "boost::move(lock);".
0139    template<class T>
0140    upgradable_lock(BOOST_RV_REF(scoped_lock<T>) scop
0141                   , typename ipcdetail::enable_if< ipcdetail::is_same<T, UpgradableMutex> >::type * = 0)
0142       : mp_mutex(0), m_locked(false)
0143    {
0144       scoped_lock<mutex_type> &u_lock = scop;
0145       if(u_lock.owns()){
0146          u_lock.mutex()->unlock_and_lock_upgradable();
0147          m_locked = true;
0148       }
0149       mp_mutex = u_lock.release();
0150    }
0151 
0152    //!Effects: If shar.owns() then calls try_unlock_sharable_and_lock_upgradable()
0153    //!   on the referenced mutex.
0154    //!   a)if try_unlock_sharable_and_lock_upgradable() returns true then mutex()
0155    //!      obtains the value from shar.release() and owns() is set to true.
0156    //!   b)if try_unlock_sharable_and_lock_upgradable() returns false then shar is
0157    //!      unaffected and this upgradable_lock construction has the same
0158    //!      effects as a default construction.
0159    //!   c)Else shar.owns() is false. mutex() obtains the value from shar.release()
0160    //!      and owns() is set to false.
0161    //!Notes: This construction will not block. It will try to obtain mutex
0162    //!   ownership from shar immediately, while changing the lock type from a
0163    //!   "read lock" to an "upgradable lock". If the "read lock" isn't held
0164    //!   in the first place, the mutex merely changes type to an unlocked
0165    //!   "upgradable lock". If the "read lock" is held, then mutex transfer
0166    //!   occurs only if it can do so in a non-blocking manner.
0167    template<class T>
0168    upgradable_lock( BOOST_RV_REF(sharable_lock<T>) shar, try_to_lock_type
0169                   , typename ipcdetail::enable_if< ipcdetail::is_same<T, UpgradableMutex> >::type * = 0)
0170       : mp_mutex(0), m_locked(false)
0171    {
0172       sharable_lock<mutex_type> &s_lock = shar;
0173       if(s_lock.owns()){
0174          if((m_locked = s_lock.mutex()->try_unlock_sharable_and_lock_upgradable()) == true){
0175             mp_mutex = s_lock.release();
0176          }
0177       }
0178       else{
0179          s_lock.release();
0180       }
0181    }
0182 
0183    //!Effects: if (owns()) m_->unlock_upgradable().
0184    //!Notes: The destructor behavior ensures that the mutex lock is not leaked.
0185    ~upgradable_lock()
0186    {
0187       BOOST_TRY{
0188          if(m_locked && mp_mutex)   mp_mutex->unlock_upgradable();
0189       }
0190       BOOST_CATCH(...){} BOOST_CATCH_END
0191    }
0192 
0193    //!Effects: If owns(), then unlock_upgradable() is called on mutex().
0194    //!   *this gets the state of upgr and upgr gets set to a default constructed state.
0195    //!Notes: With a recursive mutex it is possible that both this and upgr own the
0196    //!   mutex before the assignment. In this case, this will own the mutex
0197    //!   after the assignment (and upgr will not), but the mutex's upgradable lock
0198    //!   count will be decremented by one.
0199    upgradable_lock &operator=(BOOST_RV_REF(upgradable_lock) upgr)
0200    {
0201       if(this->owns())
0202          this->unlock();
0203       m_locked = upgr.owns();
0204       mp_mutex = upgr.release();
0205       return *this;
0206    }
0207 
0208    //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
0209    //!   exception. Calls lock_upgradable() on the referenced mutex.
0210    //!Postconditions: owns() == true.
0211    //!Notes: The sharable_lock changes from a state of not owning the mutex,
0212    //!   to owning the mutex, blocking if necessary.
0213    void lock()
0214    {
0215       if(!mp_mutex || m_locked)
0216          throw lock_exception();
0217       mp_mutex->lock_upgradable();
0218       m_locked = true;
0219    }
0220 
0221    //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
0222    //!   exception. Calls try_lock_upgradable() on the referenced mutex.
0223    //!Postconditions: owns() == the value returned from
0224    //!   mutex()->try_lock_upgradable().
0225    //!Notes: The upgradable_lock changes from a state of not owning the mutex,
0226    //!   to owning the mutex, but only if blocking was not required. If the
0227    //!   mutex_type does not support try_lock_upgradable(), this function will
0228    //!   fail at compile time if instantiated, but otherwise have no effect.
0229    bool try_lock()
0230    {
0231       if(!mp_mutex || m_locked)
0232          throw lock_exception();
0233       m_locked = mp_mutex->try_lock_upgradable();
0234       return m_locked;
0235    }
0236 
0237    //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()
0238    //!   exception. Calls timed_lock_upgradable(abs_time) on the referenced mutex.
0239    //!Postconditions: owns() == the value returned from
0240    //!   mutex()->timed_lock_upgradable(abs_time).
0241    //!Notes: The upgradable_lock changes from a state of not owning the mutex,
0242    //!   to owning the mutex, but only if it can obtain ownership within the
0243    //!   specified time. If the mutex_type does not support
0244    //!   timed_lock_upgradable(abs_time), this function will fail at compile
0245    //!   time if instantiated, but otherwise have no effect.
0246    template<class TimePoint>
0247    bool timed_lock(const TimePoint& abs_time)
0248    {
0249       if(!mp_mutex || m_locked)
0250          throw lock_exception();
0251       m_locked = mp_mutex->timed_lock_upgradable(abs_time);
0252       return m_locked;
0253    }
0254 
0255    //!Effects: If mutex() == 0 or if not locked, throws a lock_exception()
0256    //!   exception. Calls unlock_upgradable() on the referenced mutex.
0257    //!Postconditions: owns() == false.
0258    //!Notes: The upgradable_lock changes from a state of owning the mutex,
0259    //!   to not owning the mutex.
0260    void unlock()
0261    {
0262       if(!mp_mutex || !m_locked)
0263          throw lock_exception();
0264       mp_mutex->unlock_upgradable();
0265       m_locked = false;
0266    }
0267 
0268    //!Effects: Returns true if this scoped_lock has acquired the
0269    //!referenced mutex.
0270    bool owns() const BOOST_NOEXCEPT
0271    {  return m_locked && mp_mutex;  }
0272 
0273    //!Conversion to bool.
0274    //!Returns owns().
0275    operator unspecified_bool_type() const BOOST_NOEXCEPT
0276    {  return m_locked? &this_type::m_locked : 0;   }
0277 
0278    //!Effects: Returns a pointer to the referenced mutex, or 0 if
0279    //!there is no mutex to reference.
0280    mutex_type* mutex() const BOOST_NOEXCEPT
0281    {  return  mp_mutex;  }
0282 
0283    //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no
0284    //!   mutex to reference.
0285    //!Postconditions: mutex() == 0 and owns() == false.
0286    mutex_type* release() BOOST_NOEXCEPT
0287    {
0288       mutex_type *mut = mp_mutex;
0289       mp_mutex = 0;
0290       m_locked = false;
0291       return mut;
0292    }
0293 
0294    //!Effects: Swaps state with moved lock.
0295    //!Throws: Nothing.
0296    void swap(upgradable_lock<mutex_type> &other) BOOST_NOEXCEPT
0297    {
0298       (simple_swap)(mp_mutex, other.mp_mutex);
0299       (simple_swap)(m_locked, other.m_locked);
0300    }
0301 
0302    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0303    private:
0304    mutex_type *mp_mutex;
0305    bool        m_locked;
0306    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0307 };
0308 
0309 } // namespace interprocess
0310 } // namespace boost
0311 
0312 #include <boost/interprocess/detail/config_end.hpp>
0313 
0314 #endif // BOOST_INTERPROCESS_UPGRADABLE_LOCK_HPP