Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-02 07:50:55

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