Back to home page

EIC code displayed by LXR

 
 

    


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

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