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)
20 #ifndef BOOST_CONFIG_HPP
21 # include <boost/config.hpp>
24 #if defined(BOOST_HAS_PRAGMA_ONCE)
28 #include <boost/interprocess/detail/config_begin.hpp>
29 #include <boost/interprocess/exceptions.hpp>
30 #include <boost/interprocess/detail/workaround.hpp>
31 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
32 #include <boost/assert.hpp>
34 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
35 #include <boost/interprocess/sync/posix/mutex.hpp>
36 #define BOOST_INTERPROCESS_USE_POSIX
38 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
39 #include <boost/interprocess/sync/windows/mutex.hpp>
40 #define BOOST_INTERPROCESS_USE_WINDOWS
41 #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
42 #include <boost/interprocess/sync/spin/mutex.hpp>
43 #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
46 namespace interprocess {
48 namespace robust_emulation_helpers {
57 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
60 //!Describes a mutex class that can be placed in memory shared by
64 namespace interprocess {
66 class interprocess_condition;
68 //!Wraps a interprocess_mutex that can be placed in shared memory and can be
69 //!shared between processes. Allows timed lock tries
70 class interprocess_mutex
72 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
74 interprocess_mutex(const interprocess_mutex &);
75 interprocess_mutex &operator=(const interprocess_mutex &);
76 friend class interprocess_condition;
79 #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
80 #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
81 typedef ipcdetail::spin_mutex internal_mutex_type;
83 friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
84 void take_ownership(){ m_mutex.take_ownership(); }
86 #elif defined(BOOST_INTERPROCESS_USE_POSIX)
87 #undef BOOST_INTERPROCESS_USE_POSIX
88 typedef ipcdetail::posix_mutex internal_mutex_type;
89 #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
90 #undef BOOST_INTERPROCESS_USE_WINDOWS
91 typedef ipcdetail::windows_mutex internal_mutex_type;
93 #error "Unknown platform for interprocess_mutex"
96 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
100 //!Throws interprocess_exception on error.
101 interprocess_mutex();
103 //!Destructor. If any process uses the mutex after the destructor is called
104 //!the result is undefined. Does not throw.
105 ~interprocess_mutex();
107 //!Effects: The calling thread tries to obtain ownership of the mutex, and
108 //! if another thread has ownership of the mutex, it waits until it can
109 //! obtain the ownership. If a thread takes ownership of the mutex the
110 //! mutex must be unlocked by the same mutex.
111 //!Throws: interprocess_exception on error.
114 //!Effects: The calling thread tries to obtain ownership of the mutex, and
115 //! if another thread has ownership of the mutex returns immediately.
116 //!Returns: If the thread acquires ownership of the mutex, returns true, if
117 //! the another thread has ownership of the mutex, returns false.
118 //!Throws: interprocess_exception on error.
121 //!Effects: The calling thread will try to obtain exclusive ownership of the
122 //! mutex if it can do so in until the specified time is reached. If the
123 //! mutex supports recursive locking, the mutex must be unlocked the same
124 //! number of times it is locked.
125 //!Returns: If the thread acquires ownership of the mutex, returns true, if
126 //! the timeout expires returns false.
127 //!Throws: interprocess_exception on error.
128 bool timed_lock(const boost::posix_time::ptime &abs_time);
130 //!Effects: The calling thread releases the exclusive ownership of the mutex.
131 //!Throws: interprocess_exception on error.
134 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
135 internal_mutex_type &internal_mutex()
138 const internal_mutex_type &internal_mutex() const
142 internal_mutex_type m_mutex;
143 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
146 } //namespace interprocess {
147 } //namespace boost {
151 namespace interprocess {
153 inline interprocess_mutex::interprocess_mutex(){}
155 inline interprocess_mutex::~interprocess_mutex(){}
157 inline void interprocess_mutex::lock()
159 #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
160 boost::posix_time::ptime wait_time
161 = microsec_clock::universal_time()
162 + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
163 if (!m_mutex.timed_lock(wait_time))
165 throw interprocess_exception(timeout_when_locking_error
166 , "Interprocess mutex timeout when locking. Possible deadlock: "
167 "owner died without unlocking?");
174 inline bool interprocess_mutex::try_lock()
175 { return m_mutex.try_lock(); }
177 inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
178 { return m_mutex.timed_lock(abs_time); }
180 inline void interprocess_mutex::unlock()
181 { m_mutex.unlock(); }
183 } //namespace interprocess {
184 } //namespace boost {
186 #include <boost/interprocess/detail/config_end.hpp>
188 #endif //BOOST_INTERPROCESS_MUTEX_HPP