scoped_lock.hpp

来自「Boost provides free peer-reviewed portab」· HPP 代码 · 共 470 行 · 第 1/2 页

HPP
470
字号
   #ifndef BOOST_INTERPROCESS_RVALUE_REFERENCE   scoped_lock(detail::moved_object<upgradable_lock<Mutex> > upgr              ,boost::posix_time::ptime &abs_time)      : mp_mutex(0), m_locked(false)   {      upgradable_lock<mutex_type> &u_lock = upgr.get();      if(u_lock.owns()){         if((m_locked = u_lock.mutex()->timed_unlock_upgradable_and_lock(abs_time)) == true){            mp_mutex = u_lock.release();         }      }      else{         u_lock.release();      }   }   #else   scoped_lock(upgradable_lock<Mutex> &&upgr              ,boost::posix_time::ptime &abs_time)      : mp_mutex(0), m_locked(false)   {      upgradable_lock<mutex_type> &u_lock = upgr;      if(u_lock.owns()){         if((m_locked = u_lock.mutex()->timed_unlock_upgradable_and_lock(abs_time)) == true){            mp_mutex = u_lock.release();         }      }      else{         u_lock.release();      }   }   #endif   //!Effects: If shar.owns() then calls try_unlock_sharable_and_lock() on the   //!referenced mutex.    //!   a)if try_unlock_sharable_and_lock() returns true then mutex() obtains   //!      the value from shar.release() and owns() is set to true.    //!   b)if try_unlock_sharable_and_lock() returns false then shar is   //!      unaffected and this scoped_lock construction has the same   //!      effects as a default construction.    //!   c)Else shar.owns() is false. mutex() obtains the value from   //!      shar.release() and owns() is set to false    //!Notes: This construction will not block. It will try to obtain mutex   //!   ownership from shar immediately, while changing the lock type from a   //!   "read lock" to a "write lock". If the "read lock" isn't held in the   //!   first place, the mutex merely changes type to an unlocked "write lock".   //!   If the "read lock" is held, then mutex transfer occurs only if it can   //!   do so in a non-blocking manner.   #ifndef BOOST_INTERPROCESS_RVALUE_REFERENCE   scoped_lock(detail::moved_object<sharable_lock<Mutex> > shar              ,detail::try_to_lock_type)      : mp_mutex(0), m_locked(false)   {      sharable_lock<mutex_type> &s_lock = shar.get();      if(s_lock.owns()){         if((m_locked = s_lock.mutex()->try_unlock_sharable_and_lock()) == true){            mp_mutex = s_lock.release();         }      }      else{         s_lock.release();      }   }   #else   scoped_lock(sharable_lock<Mutex> &&shar              ,detail::try_to_lock_type)      : mp_mutex(0), m_locked(false)   {      sharable_lock<mutex_type> &s_lock = shar;      if(s_lock.owns()){         if((m_locked = s_lock.mutex()->try_unlock_sharable_and_lock()) == true){            mp_mutex = s_lock.release();         }      }      else{         s_lock.release();      }   }   #endif   //!Effects: if (owns()) mp_mutex->unlock().   //!Notes: The destructor behavior ensures that the mutex lock is not leaked.*/   ~scoped_lock()   {      try{  if(m_locked && mp_mutex)   mp_mutex->unlock();  }      catch(...){}   }   //!Effects: If owns() before the call, then unlock() is called on mutex().   //!   *this gets the state of scop and scop gets set to a default constructed state.    //!Notes: With a recursive mutex it is possible that both this and scop own   //!   the same mutex before the assignment. In this case, this will own the   //!   mutex after the assignment (and scop will not), but the mutex's lock   //!   count will be decremented by one.   #ifndef BOOST_INTERPROCESS_RVALUE_REFERENCE   scoped_lock &operator=(detail::moved_object<scoped_lock> scop)   {        if(this->owns())         this->unlock();      m_locked = scop.get().owns();      mp_mutex = scop.get().release();      return *this;   }   #else   scoped_lock &operator=(scoped_lock &&scop)   {        if(this->owns())         this->unlock();      m_locked = scop.owns();      mp_mutex = scop.release();      return *this;   }   #endif   //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()   //!   exception. Calls lock() on the referenced mutex.   //!Postconditions: owns() == true.   //!Notes: The scoped_lock changes from a state of not owning the mutex, to   //!   owning the mutex, blocking if necessary.   void lock()   {      if(!mp_mutex || m_locked)         throw lock_exception();      mp_mutex->lock();      m_locked = true;   }   //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()   //!   exception. Calls try_lock() on the referenced mutex.    //!Postconditions: owns() == the value returned from mutex()->try_lock().   //!Notes: The scoped_lock changes from a state of not owning the mutex, to   //!   owning the mutex, but only if blocking was not required. If the   //!   mutex_type does not support try_lock(), this function will fail at   //!   compile time if instantiated, but otherwise have no effect.*/   bool try_lock()   {      if(!mp_mutex || m_locked)         throw lock_exception();      m_locked = mp_mutex->try_lock();      return m_locked;   }   //!Effects: If mutex() == 0 or if already locked, throws a lock_exception()   //!   exception. Calls timed_lock(abs_time) on the referenced mutex.   //!Postconditions: owns() == the value returned from mutex()-> timed_lock(abs_time).   //!Notes: The scoped_lock changes from a state of not owning the mutex, to   //!   owning the mutex, but only if it can obtain ownership by the specified   //!   time. If the mutex_type does not support timed_lock (), this function   //!   will fail at compile time if instantiated, but otherwise have no effect.*/   bool timed_lock(const boost::posix_time::ptime& abs_time)   {      if(!mp_mutex || m_locked)         throw lock_exception();      m_locked = mp_mutex->timed_lock(abs_time);      return m_locked;   }   //!Effects: If mutex() == 0 or if not locked, throws a lock_exception()   //!   exception. Calls unlock() on the referenced mutex.   //!Postconditions: owns() == false.   //!Notes: The scoped_lock changes from a state of owning the mutex, to not   //!   owning the mutex.*/   void unlock()   {      if(!mp_mutex || !m_locked)         throw lock_exception();      mp_mutex->unlock();      m_locked = false;   }   //!Effects: Returns true if this scoped_lock has acquired   //!the referenced mutex.   bool owns() const   {  return m_locked && mp_mutex;  }   //!Conversion to bool.   //!Returns owns().   operator unspecified_bool_type() const   {  return m_locked? &this_type::m_locked : 0;   }   //!Effects: Returns a pointer to the referenced mutex, or 0 if   //!there is no mutex to reference.   mutex_type* mutex() const   {  return  mp_mutex;  }   //!Effects: Returns a pointer to the referenced mutex, or 0 if there is no   //!   mutex to reference.   //!Postconditions: mutex() == 0 and owns() == false.   mutex_type* release()   {      mutex_type *mut = mp_mutex;      mp_mutex = 0;      m_locked = false;      return mut;   }    //!Effects: Swaps state with moved lock.    //!Throws: Nothing.   #ifndef BOOST_INTERPROCESS_RVALUE_REFERENCE   void swap(detail::moved_object<scoped_lock<mutex_type> > other)   {      std::swap(mp_mutex, other.get().mp_mutex);      std::swap(m_locked, other.get().m_locked);   }   #else   void swap(scoped_lock<mutex_type> &&other)   {      std::swap(mp_mutex, other.mp_mutex);      std::swap(m_locked, other.m_locked);   }   #endif   /// @cond   private:   mutex_type *mp_mutex;    bool        m_locked;   /// @endcond};/// @cond//!This class is movabletemplate <class M>struct is_movable<scoped_lock<M> >{   enum {   value = true };};/// @endcond} // namespace interprocess} // namespace boost#include <boost/interprocess/detail/config_end.hpp>#endif // BOOST_INTERPROCESS_SCOPED_LOCK_HPP

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?