Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-04 08:22:18

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