Back to home page

EIC code displayed by LXR

 
 

    


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

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