Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:35:57

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 // Parts of the pthread code come from Boost Threads code.
0012 //
0013 //////////////////////////////////////////////////////////////////////////////
0014 
0015 #ifndef BOOST_INTERPROCESS_MUTEX_HPP
0016 #define BOOST_INTERPROCESS_MUTEX_HPP
0017 
0018 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0019 
0020 #ifndef BOOST_CONFIG_HPP
0021 #  include <boost/config.hpp>
0022 #endif
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/exceptions.hpp>
0030 #include <boost/interprocess/detail/workaround.hpp>
0031 #include <boost/interprocess/timed_utils.hpp>
0032 #include <boost/assert.hpp>
0033 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
0034 
0035 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
0036    #include <boost/interprocess/sync/posix/mutex.hpp>
0037    #define BOOST_INTERPROCESS_MUTEX_USE_POSIX
0038 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
0039    //Experimental...
0040    #define BOOST_INTERPROCESS_MUTEX_USE_WINAPI
0041    #include <boost/interprocess/sync/windows/mutex.hpp>
0042 #else
0043    //spin_mutex is used
0044    #include <boost/interprocess/sync/spin/mutex.hpp>
0045    namespace boost {
0046    namespace interprocess {
0047    namespace ipcdetail{
0048    namespace robust_emulation_helpers {
0049 
0050    template<class T>
0051    class mutex_traits;
0052 
0053    }}}}
0054 #endif
0055 
0056 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0057 
0058 //!\file
0059 //!Describes a mutex class that can be placed in memory shared by
0060 //!several processes.
0061 
0062 namespace boost {
0063 namespace interprocess {
0064 
0065 class interprocess_condition;
0066 
0067 //!Wraps a interprocess_mutex that can be placed in shared memory and can be
0068 //!shared between processes. Allows timed lock tries
0069 class interprocess_mutex
0070 {
0071    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0072    //Non-copyable
0073    interprocess_mutex(const interprocess_mutex &);
0074    interprocess_mutex &operator=(const interprocess_mutex &);
0075    friend class interprocess_condition;
0076 
0077    public:
0078    #if defined(BOOST_INTERPROCESS_MUTEX_USE_POSIX)
0079       typedef ipcdetail::posix_mutex internal_mutex_type;
0080    #elif defined(BOOST_INTERPROCESS_MUTEX_USE_WINAPI)
0081       typedef ipcdetail::winapi_mutex internal_mutex_type;
0082    #else
0083       typedef ipcdetail::spin_mutex internal_mutex_type;
0084       private:
0085       friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
0086       void take_ownership(){ m_mutex.take_ownership(); }
0087       public:
0088    #endif
0089 
0090    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0091    public:
0092 
0093    //!Constructor.
0094    //!Throws interprocess_exception on error.
0095    interprocess_mutex();
0096 
0097    //!Destructor. If any process uses the mutex after the destructor is called
0098    //!the result is undefined. Does not throw.
0099    ~interprocess_mutex();
0100 
0101    //!Requires: The calling thread does not own the mutex.
0102    //!
0103    //!Effects: The calling thread tries to obtain ownership of the mutex, and
0104    //!   if another thread has ownership of the mutex, it waits until it can
0105    //!   obtain the ownership. If a thread takes ownership of the mutex the
0106    //!   mutex must be unlocked by the same mutex.
0107    //!Throws: interprocess_exception on error.
0108    //! 
0109    //!Note: A program may deadlock if the thread that has ownership calls 
0110    //!   this function. If the implementation can detect the deadlock,
0111    //!   an exception could be thrown.
0112    void lock();
0113 
0114    //!Requires: The calling thread does not own the mutex.
0115    //!
0116    //!Effects: The calling thread tries to obtain ownership of the mutex, and
0117    //!   if another thread has ownership of the mutex returns immediately.
0118    //!Returns: If the thread acquires ownership of the mutex, returns true, if
0119    //!   the another thread has ownership of the mutex, returns false.
0120    //!Throws: interprocess_exception on error.
0121    //! 
0122    //!Note: A program may deadlock if the thread that has ownership calls 
0123    //!   this function. If the implementation can detect the deadlock,
0124    //!   an exception could be thrown.
0125    bool try_lock();
0126 
0127    //!Requires: The calling thread does not own the mutex.
0128    //!
0129    //!Effects: The calling thread will try to obtain exclusive ownership of the
0130    //!   mutex if it can do so in until the specified time is reached. If the
0131    //!   mutex supports recursive locking, the mutex must be unlocked the same
0132    //!   number of times it is locked.
0133    //!Returns: If the thread acquires ownership of the mutex, returns true, if
0134    //!   the timeout expires returns false.
0135    //!Throws: interprocess_exception on error.
0136    //! 
0137    //!Note: A program may deadlock if the thread that has ownership calls 
0138    //!   this function. If the implementation can detect the deadlock,
0139    //!   an exception could be thrown.
0140    template<class TimePoint>
0141    bool timed_lock(const TimePoint &abs_time);
0142 
0143    //!Same as `timed_lock`, but this function is modeled after the
0144    //!standard library interface.
0145    template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
0146    {  return this->timed_lock(abs_time);  }
0147 
0148    //!Same as `timed_lock`, but this function is modeled after the
0149    //!standard library interface.
0150    template<class Duration>  bool try_lock_for(const Duration &dur)
0151    {  return this->timed_lock(ipcdetail::duration_to_ustime(dur)); }
0152 
0153    //!Effects: The calling thread releases the exclusive ownership of the mutex.
0154    //!Throws: interprocess_exception on error.
0155    void unlock();
0156 
0157    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0158    internal_mutex_type &internal_mutex()
0159    {  return m_mutex;   }
0160 
0161    const internal_mutex_type &internal_mutex() const
0162    {  return m_mutex;   }
0163 
0164    private:
0165    internal_mutex_type m_mutex;
0166    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0167 };
0168 
0169 }  //namespace interprocess {
0170 }  //namespace boost {
0171 
0172 
0173 namespace boost {
0174 namespace interprocess {
0175 
0176 inline interprocess_mutex::interprocess_mutex(){}
0177 
0178 inline interprocess_mutex::~interprocess_mutex(){}
0179 
0180 inline void interprocess_mutex::lock()
0181 {  ipcdetail::timeout_when_locking_aware_lock(m_mutex);  }
0182 
0183 inline bool interprocess_mutex::try_lock()
0184 { return m_mutex.try_lock(); }
0185 
0186 template <class TimePoint>
0187 inline bool interprocess_mutex::timed_lock(const TimePoint &abs_time)
0188 { return m_mutex.timed_lock(abs_time); }
0189 
0190 inline void interprocess_mutex::unlock()
0191 { m_mutex.unlock(); }
0192 
0193 }  //namespace interprocess {
0194 }  //namespace boost {
0195 
0196 #include <boost/interprocess/detail/config_end.hpp>
0197 
0198 #endif   //BOOST_INTERPROCESS_MUTEX_HPP