Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-15 08:36:51

0001 //////////////////////////////////////////////////////////////////////////////
0002 //  Code based on Howard Hinnant's shared_mutex class
0003 //
0004 // (C) Copyright Howard Hinnant 2007-2010. Distributed under the Boost
0005 // Software License, Version 1.0. (see http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
0008 // Software License, Version 1.0. (See accompanying file
0009 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 // See http://www.boost.org/libs/interprocess for documentation.
0012 //
0013 //////////////////////////////////////////////////////////////////////////////
0014 
0015 #ifndef BOOST_INTERPROCESS_SHARABLE_MUTEX_HPP
0016 #define BOOST_INTERPROCESS_SHARABLE_MUTEX_HPP
0017 
0018 #ifndef BOOST_CONFIG_HPP
0019 #  include <boost/config.hpp>
0020 #endif
0021 #
0022 #if defined(BOOST_HAS_PRAGMA_ONCE)
0023 #  pragma once
0024 #endif
0025 
0026 #include <boost/interprocess/detail/config_begin.hpp>
0027 #include <boost/interprocess/detail/workaround.hpp>
0028 #include <boost/interprocess/sync/scoped_lock.hpp>
0029 #include <boost/interprocess/timed_utils.hpp>
0030 #include <boost/interprocess/sync/interprocess_mutex.hpp>
0031 #include <boost/interprocess/sync/interprocess_condition.hpp>
0032 #include <climits>
0033 
0034 
0035 //!\file
0036 //!Describes interprocess_sharable_mutex class
0037 
0038 namespace boost {
0039 namespace interprocess {
0040 
0041 //!Wraps a interprocess_sharable_mutex that can be placed in shared memory and can be
0042 //!shared between processes. Allows timed lock tries
0043 class interprocess_sharable_mutex
0044 {
0045    //Non-copyable
0046    interprocess_sharable_mutex(const interprocess_sharable_mutex &);
0047    interprocess_sharable_mutex &operator=(const interprocess_sharable_mutex &);
0048 
0049    friend class interprocess_condition;
0050    public:
0051 
0052    //!Constructs the sharable lock.
0053    //!Throws interprocess_exception on error.
0054    interprocess_sharable_mutex();
0055 
0056    //!Destroys the sharable lock.
0057    //!Does not throw.
0058    ~interprocess_sharable_mutex();
0059 
0060    //Exclusive locking
0061 
0062    //!Requires: The calling thread does not own the mutex.
0063    //!
0064    //!Effects: The calling thread tries to obtain exclusive ownership of the mutex,
0065    //!   and if another thread has exclusive or sharable ownership of
0066    //!   the mutex, it waits until it can obtain the ownership.
0067    //!Throws: interprocess_exception on error.
0068    //! 
0069    //!Note: A program may deadlock if the thread that has ownership calls 
0070    //!   this function. If the implementation can detect the deadlock,
0071    //!   an exception could be thrown.
0072    void lock();
0073 
0074    //!Requires: The calling thread does not own the mutex.
0075    //!
0076    //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
0077    //!   without waiting. If no other thread has exclusive or sharable
0078    //!   ownership of the mutex this succeeds.
0079    //!Returns: If it can acquire exclusive ownership immediately returns true.
0080    //!   If it has to wait, returns false.
0081    //!Throws: interprocess_exception on error.
0082    //! 
0083    //!Note: A program may deadlock if the thread that has ownership calls 
0084    //!   this function. If the implementation can detect the deadlock,
0085    //!   an exception could be thrown.
0086    bool try_lock();
0087 
0088    //!Requires: The calling thread does not own the mutex.
0089    //!
0090    //!Effects: The calling thread tries to acquire exclusive ownership of the mutex
0091    //!   waiting if necessary until no other thread has exclusive or sharable
0092    //!   ownership of the mutex or abs_time is reached.
0093    //!Returns: If acquires exclusive ownership, returns true. Otherwise returns false.
0094    //!Throws: interprocess_exception on error.
0095    //! 
0096    //!Note: A program may deadlock if the thread that has ownership calls 
0097    //!   this function. If the implementation can detect the deadlock,
0098    //!   an exception could be thrown.
0099    template<class TimePoint>
0100    bool timed_lock(const TimePoint &abs_time);
0101 
0102    //!Same as `timed_lock`, but this function is modeled after the
0103    //!standard library interface.
0104    template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0105    {  return this->timed_lock(abs_time);  }
0106 
0107    //!Same as `timed_lock`, but this function is modeled after the
0108    //!standard library interface.
0109    template<class Duration>  bool try_lock_for(const Duration &dur)
0110    {  return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
0111 
0112    //!Precondition: The thread must have exclusive ownership of the mutex.
0113    //!Effects: The calling thread releases the exclusive ownership of the mutex.
0114    //!Throws: An exception derived from interprocess_exception on error.
0115    void unlock();
0116 
0117    //Sharable locking
0118 
0119    //!Requires: The calling thread does not own the mutex.
0120    //!
0121    //!Effects: The calling thread tries to obtain sharable ownership of the mutex,
0122    //!   and if another thread has exclusive ownership of the mutex,
0123    //!   waits until it can obtain the ownership.
0124    //!Throws: interprocess_exception on error.
0125    //! 
0126    //!Note: A program may deadlock if the thread that has ownership calls 
0127    //!   this function. If the implementation can detect the deadlock,
0128    //!   an exception could be thrown.
0129    void lock_sharable();
0130 
0131    //!Same as `lock_sharable` but with a std-compatible interface
0132    //! 
0133    void lock_shared()
0134    {  this->lock_sharable();  }
0135 
0136    //!Requires: The calling thread does not own the mutex.
0137    //!
0138    //!Effects: The calling thread tries to acquire sharable ownership of the mutex
0139    //!   without waiting. If no other thread has exclusive ownership
0140    //!   of the mutex this succeeds.
0141    //!Returns: If it can acquire sharable ownership immediately returns true. If it
0142    //!   has to wait, returns false.
0143    //!Throws: interprocess_exception on error.
0144    //! 
0145    //!Note: A program may deadlock if the thread that has ownership calls 
0146    //!   this function. If the implementation can detect the deadlock,
0147    //!   an exception could be thrown.
0148    bool try_lock_sharable();
0149 
0150    //!Same as `try_lock_sharable` but with a std-compatible interface
0151    //! 
0152    bool try_lock_shared()
0153    {  return this->try_lock_sharable();  }
0154 
0155    //!Requires: The calling thread does not own the mutex.
0156    //!
0157    //!Effects: The calling thread tries to acquire sharable ownership of the mutex
0158    //!   waiting if necessary until no other thread has exclusive
0159    //!   ownership of the mutex or abs_time is reached.
0160    //!Returns: If acquires sharable ownership, returns true. Otherwise returns false.
0161    //!Throws: interprocess_exception on error.
0162    //! 
0163    //!Note: A program may deadlock if the thread that has ownership calls 
0164    //!   this function. If the implementation can detect the deadlock,
0165    //!   an exception could be thrown.
0166    template<class TimePoint>
0167    bool timed_lock_sharable(const TimePoint &abs_time);
0168 
0169    //!Same as `timed_lock_sharable`, but this function is modeled after the
0170    //!standard library interface.
0171    template<class TimePoint> bool try_lock_shared_until(const TimePoint &abs_time)
0172    {  return this->timed_lock_sharable(abs_time);  }
0173 
0174    //!Same as `timed_lock_sharable`, but this function is modeled after the
0175    //!standard library interface.
0176    template<class Duration>  bool try_lock_shared_for(const Duration &dur)
0177    {  return this->timed_lock_sharable(ipcdetail::duration_to_ustime(dur)); }
0178 
0179    //!Precondition: The thread must have sharable ownership of the mutex.
0180    //!Effects: The calling thread releases the sharable ownership of the mutex.
0181    //!Throws: An exception derived from interprocess_exception on error.
0182    void unlock_sharable();
0183 
0184    //!Same as `unlock_sharable` but with a std-compatible interface
0185    //! 
0186    void unlock_shared()
0187    {  this->unlock_sharable();  }
0188 
0189    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0190    private:
0191    typedef scoped_lock<interprocess_mutex> scoped_lock_t;
0192 
0193    //Pack all the control data in a word to be able
0194    //to use atomic instructions in the future
0195    struct control_word_t
0196    {
0197       unsigned exclusive_in   : 1;
0198       unsigned num_shared     : sizeof(unsigned)*CHAR_BIT-1;
0199    }                       m_ctrl;
0200 
0201    interprocess_mutex      m_mut;
0202    interprocess_condition  m_first_gate;
0203    interprocess_condition  m_second_gate;
0204 
0205    private:
0206    //Rollback structures for exceptions or failure return values
0207    struct exclusive_rollback
0208    {
0209       exclusive_rollback(control_word_t         &ctrl
0210                         ,interprocess_condition &first_gate)
0211          :  mp_ctrl(&ctrl), m_first_gate(first_gate)
0212       {}
0213 
0214       void release()
0215       {  mp_ctrl = 0;   }
0216 
0217       ~exclusive_rollback()
0218       {
0219          if(mp_ctrl){
0220             mp_ctrl->exclusive_in = 0;
0221             m_first_gate.notify_all();
0222          }
0223       }
0224       control_word_t          *mp_ctrl;
0225       interprocess_condition  &m_first_gate;
0226    };
0227 
0228    template<int Dummy>
0229    struct base_constants_t
0230    {
0231       static const unsigned max_readers
0232          = ~(unsigned(1) << (sizeof(unsigned)*CHAR_BIT-1));
0233    };
0234    typedef base_constants_t<0> constants;
0235    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0236 };
0237 
0238 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0239 
0240 template <int Dummy>
0241 const unsigned interprocess_sharable_mutex::base_constants_t<Dummy>::max_readers;
0242 
0243 inline interprocess_sharable_mutex::interprocess_sharable_mutex()
0244 {
0245    this->m_ctrl.exclusive_in  = 0;
0246    this->m_ctrl.num_shared   = 0;
0247 }
0248 
0249 inline interprocess_sharable_mutex::~interprocess_sharable_mutex()
0250 {}
0251 
0252 inline void interprocess_sharable_mutex::lock()
0253 {
0254    scoped_lock_t lck(m_mut);
0255 
0256    //The exclusive lock must block in the first gate
0257    //if an exclusive lock has been acquired
0258    while (this->m_ctrl.exclusive_in){
0259       this->m_first_gate.wait(lck);
0260    }
0261 
0262    //Mark that exclusive lock has been acquired
0263    this->m_ctrl.exclusive_in = 1;
0264 
0265    //Prepare rollback
0266    exclusive_rollback rollback(this->m_ctrl, this->m_first_gate);
0267 
0268    //Now wait until all readers are gone
0269    while (this->m_ctrl.num_shared){
0270       this->m_second_gate.wait(lck);
0271    }
0272    rollback.release();
0273 }
0274 
0275 inline bool interprocess_sharable_mutex::try_lock()
0276 {
0277    scoped_lock_t lck(m_mut, try_to_lock);
0278 
0279    //If we can't lock or any has there is any exclusive
0280    //or sharable mark return false;
0281    if(!lck.owns()
0282       || this->m_ctrl.exclusive_in
0283       || this->m_ctrl.num_shared){
0284       return false;
0285    }
0286    this->m_ctrl.exclusive_in = 1;
0287    return true;
0288 }
0289 
0290 template<class TimePoint>
0291 inline bool interprocess_sharable_mutex::timed_lock
0292    (const TimePoint &abs_time)
0293 {
0294    scoped_lock_t lck(m_mut, abs_time);
0295    if(!lck.owns())   return false;
0296 
0297    //The exclusive lock must block in the first gate
0298    //if an exclusive lock has been acquired
0299    while (this->m_ctrl.exclusive_in){
0300       //Mutexes and condvars handle just fine infinite abs_times
0301       //so avoid checking it here
0302       if(!this->m_first_gate.timed_wait(lck, abs_time)){
0303          if(this->m_ctrl.exclusive_in){
0304             return false;
0305          }
0306          break;
0307       }
0308    }
0309 
0310    //Mark that exclusive lock has been acquired
0311    this->m_ctrl.exclusive_in = 1;
0312 
0313    //Prepare rollback
0314    exclusive_rollback rollback(this->m_ctrl, this->m_first_gate);
0315 
0316    //Now wait until all readers are gone
0317    while (this->m_ctrl.num_shared){
0318       //Mutexes and condvars handle just fine infinite abs_times
0319       //so avoid checking it here
0320       if(!this->m_second_gate.timed_wait(lck, abs_time)){
0321          if(this->m_ctrl.num_shared){
0322             return false;
0323          }
0324          break;
0325       }
0326    }
0327    rollback.release();
0328    return true;
0329 }
0330 
0331 inline void interprocess_sharable_mutex::unlock()
0332 {
0333    scoped_lock_t lck(m_mut);
0334    this->m_ctrl.exclusive_in = 0;
0335    this->m_first_gate.notify_all();
0336 }
0337 
0338 //Sharable locking
0339 
0340 inline void interprocess_sharable_mutex::lock_sharable()
0341 {
0342    scoped_lock_t lck(m_mut);
0343 
0344    //The sharable lock must block in the first gate
0345    //if an exclusive lock has been acquired
0346    //or there are too many sharable locks
0347    while(this->m_ctrl.exclusive_in
0348         || this->m_ctrl.num_shared == constants::max_readers){
0349       this->m_first_gate.wait(lck);
0350    }
0351 
0352    //Increment sharable count
0353    ++this->m_ctrl.num_shared;
0354 }
0355 
0356 inline bool interprocess_sharable_mutex::try_lock_sharable()
0357 {
0358    scoped_lock_t lck(m_mut, try_to_lock);
0359 
0360    //The sharable lock must fail
0361    //if an exclusive lock has been acquired
0362    //or there are too many sharable locks
0363    if(!lck.owns()
0364       || this->m_ctrl.exclusive_in
0365       || this->m_ctrl.num_shared == constants::max_readers){
0366       return false;
0367    }
0368 
0369    //Increment sharable count
0370    ++this->m_ctrl.num_shared;
0371    return true;
0372 }
0373 
0374 template<class TimePoint>
0375 inline bool interprocess_sharable_mutex::timed_lock_sharable
0376    (const TimePoint &abs_time)
0377 {
0378    scoped_lock_t lck(m_mut, abs_time);
0379    if(!lck.owns())   return false;
0380 
0381    //The sharable lock must block in the first gate
0382    //if an exclusive lock has been acquired
0383    //or there are too many sharable locks
0384    while (this->m_ctrl.exclusive_in
0385          || this->m_ctrl.num_shared == constants::max_readers){
0386       //Mutexes and condvars handle just fine infinite abs_times
0387       //so avoid checking it here
0388       if(!this->m_first_gate.timed_wait(lck, abs_time)){
0389          if(this->m_ctrl.exclusive_in
0390                || this->m_ctrl.num_shared == constants::max_readers){
0391             return false;
0392          }
0393          break;
0394       }
0395    }
0396 
0397    //Increment sharable count
0398    ++this->m_ctrl.num_shared;
0399    return true;
0400 }
0401 
0402 inline void interprocess_sharable_mutex::unlock_sharable()
0403 {
0404    scoped_lock_t lck(m_mut);
0405    //Decrement sharable count
0406    --this->m_ctrl.num_shared;
0407    if (this->m_ctrl.num_shared == 0){
0408       this->m_second_gate.notify_one();
0409    }
0410    //Check if there are blocked sharables because of
0411    //there were too many sharables
0412    else if(this->m_ctrl.num_shared == (constants::max_readers-1)){
0413       this->m_first_gate.notify_all();
0414    }
0415 }
0416 
0417 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0418 
0419 }  //namespace interprocess {
0420 }  //namespace boost {
0421 
0422 #include <boost/interprocess/detail/config_end.hpp>
0423 
0424 #endif   //BOOST_INTERPROCESS_SHARABLE_MUTEX_HPP