1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 // Parts of the pthread code come from Boost Threads code.
13 //////////////////////////////////////////////////////////////////////////////
15 #ifndef BOOST_INTERPROCESS_MUTEX_HPP
16 #define BOOST_INTERPROCESS_MUTEX_HPP
18 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
24 #include <boost/interprocess/detail/config_begin.hpp>
25 #include <boost/interprocess/exceptions.hpp>
26 #include <boost/interprocess/detail/workaround.hpp>
27 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
28 #include <boost/assert.hpp>
30 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
31 #include <boost/interprocess/sync/posix/mutex.hpp>
32 #define BOOST_INTERPROCESS_USE_POSIX
34 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
35 #include <boost/interprocess/sync/windows/mutex.hpp>
36 #define BOOST_INTERPROCESS_USE_WINDOWS
37 #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
38 #include <boost/interprocess/sync/spin/mutex.hpp>
39 #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
42 namespace interprocess {
44 namespace robust_emulation_helpers {
53 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
56 //!Describes a mutex class that can be placed in memory shared by
60 namespace interprocess {
62 class interprocess_condition;
64 //!Wraps a interprocess_mutex that can be placed in shared memory and can be
65 //!shared between processes. Allows timed lock tries
66 class interprocess_mutex
68 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
70 interprocess_mutex(const interprocess_mutex &);
71 interprocess_mutex &operator=(const interprocess_mutex &);
72 friend class interprocess_condition;
75 #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
76 #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
77 typedef ipcdetail::spin_mutex internal_mutex_type;
79 friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
80 void take_ownership(){ m_mutex.take_ownership(); }
82 #elif defined(BOOST_INTERPROCESS_USE_POSIX)
83 #undef BOOST_INTERPROCESS_USE_POSIX
84 typedef ipcdetail::posix_mutex internal_mutex_type;
85 #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
86 #undef BOOST_INTERPROCESS_USE_WINDOWS
87 typedef ipcdetail::windows_mutex internal_mutex_type;
89 #error "Unknown platform for interprocess_mutex"
92 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
96 //!Throws interprocess_exception on error.
99 //!Destructor. If any process uses the mutex after the destructor is called
100 //!the result is undefined. Does not throw.
101 ~interprocess_mutex();
103 //!Effects: The calling thread tries to obtain ownership of the mutex, and
104 //! if another thread has ownership of the mutex, it waits until it can
105 //! obtain the ownership. If a thread takes ownership of the mutex the
106 //! mutex must be unlocked by the same mutex.
107 //!Throws: interprocess_exception on error.
110 //!Effects: The calling thread tries to obtain ownership of the mutex, and
111 //! if another thread has ownership of the mutex returns immediately.
112 //!Returns: If the thread acquires ownership of the mutex, returns true, if
113 //! the another thread has ownership of the mutex, returns false.
114 //!Throws: interprocess_exception on error.
117 //!Effects: The calling thread will try to obtain exclusive ownership of the
118 //! mutex if it can do so in until the specified time is reached. If the
119 //! mutex supports recursive locking, the mutex must be unlocked the same
120 //! number of times it is locked.
121 //!Returns: If the thread acquires ownership of the mutex, returns true, if
122 //! the timeout expires returns false.
123 //!Throws: interprocess_exception on error.
124 bool timed_lock(const boost::posix_time::ptime &abs_time);
126 //!Effects: The calling thread releases the exclusive ownership of the mutex.
127 //!Throws: interprocess_exception on error.
130 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
131 internal_mutex_type &internal_mutex()
134 const internal_mutex_type &internal_mutex() const
138 internal_mutex_type m_mutex;
139 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
142 } //namespace interprocess {
143 } //namespace boost {
147 namespace interprocess {
149 inline interprocess_mutex::interprocess_mutex(){}
151 inline interprocess_mutex::~interprocess_mutex(){}
153 inline void interprocess_mutex::lock()
155 #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
156 boost::posix_time::ptime wait_time
157 = boost::posix_time::microsec_clock::universal_time()
158 + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
159 if (!m_mutex.timed_lock(wait_time))
161 throw interprocess_exception(timeout_when_locking_error
162 , "Interprocess mutex timeout when locking. Possible deadlock: "
163 "owner died without unlocking?");
170 inline bool interprocess_mutex::try_lock()
171 { return m_mutex.try_lock(); }
173 inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
174 { return m_mutex.timed_lock(abs_time); }
176 inline void interprocess_mutex::unlock()
177 { m_mutex.unlock(); }
179 } //namespace interprocess {
180 } //namespace boost {
182 #include <boost/interprocess/detail/config_end.hpp>
184 #endif //BOOST_INTERPROCESS_MUTEX_HPP