Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-22 08:30:47

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