Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10 //
|
Chris@16
|
11 // Parts of the pthread code come from Boost Threads code.
|
Chris@16
|
12 //
|
Chris@16
|
13 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
14
|
Chris@16
|
15 #ifndef BOOST_INTERPROCESS_MUTEX_HPP
|
Chris@16
|
16 #define BOOST_INTERPROCESS_MUTEX_HPP
|
Chris@16
|
17
|
Chris@101
|
18 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
19
|
Chris@101
|
20 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
21 # include <boost/config.hpp>
|
Chris@101
|
22 #endif
|
Chris@101
|
23 #
|
Chris@101
|
24 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
25 # pragma once
|
Chris@16
|
26 #endif
|
Chris@16
|
27
|
Chris@16
|
28 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
29 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
30 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
31 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
32 #include <boost/assert.hpp>
|
Chris@16
|
33
|
Chris@16
|
34 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
|
Chris@16
|
35 #include <boost/interprocess/sync/posix/mutex.hpp>
|
Chris@16
|
36 #define BOOST_INTERPROCESS_USE_POSIX
|
Chris@16
|
37 //Experimental...
|
Chris@16
|
38 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
|
Chris@16
|
39 #include <boost/interprocess/sync/windows/mutex.hpp>
|
Chris@16
|
40 #define BOOST_INTERPROCESS_USE_WINDOWS
|
Chris@16
|
41 #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
42 #include <boost/interprocess/sync/spin/mutex.hpp>
|
Chris@16
|
43 #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
|
Chris@16
|
44
|
Chris@16
|
45 namespace boost {
|
Chris@16
|
46 namespace interprocess {
|
Chris@16
|
47 namespace ipcdetail{
|
Chris@16
|
48 namespace robust_emulation_helpers {
|
Chris@16
|
49
|
Chris@16
|
50 template<class T>
|
Chris@16
|
51 class mutex_traits;
|
Chris@16
|
52
|
Chris@16
|
53 }}}}
|
Chris@16
|
54
|
Chris@16
|
55 #endif
|
Chris@16
|
56
|
Chris@101
|
57 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
58
|
Chris@16
|
59 //!\file
|
Chris@16
|
60 //!Describes a mutex class that can be placed in memory shared by
|
Chris@16
|
61 //!several processes.
|
Chris@16
|
62
|
Chris@16
|
63 namespace boost {
|
Chris@16
|
64 namespace interprocess {
|
Chris@16
|
65
|
Chris@16
|
66 class interprocess_condition;
|
Chris@16
|
67
|
Chris@16
|
68 //!Wraps a interprocess_mutex that can be placed in shared memory and can be
|
Chris@16
|
69 //!shared between processes. Allows timed lock tries
|
Chris@16
|
70 class interprocess_mutex
|
Chris@16
|
71 {
|
Chris@101
|
72 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
73 //Non-copyable
|
Chris@16
|
74 interprocess_mutex(const interprocess_mutex &);
|
Chris@16
|
75 interprocess_mutex &operator=(const interprocess_mutex &);
|
Chris@16
|
76 friend class interprocess_condition;
|
Chris@16
|
77
|
Chris@16
|
78 public:
|
Chris@16
|
79 #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
|
Chris@16
|
80 #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
|
Chris@16
|
81 typedef ipcdetail::spin_mutex internal_mutex_type;
|
Chris@16
|
82 private:
|
Chris@16
|
83 friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
|
Chris@16
|
84 void take_ownership(){ m_mutex.take_ownership(); }
|
Chris@16
|
85 public:
|
Chris@16
|
86 #elif defined(BOOST_INTERPROCESS_USE_POSIX)
|
Chris@16
|
87 #undef BOOST_INTERPROCESS_USE_POSIX
|
Chris@16
|
88 typedef ipcdetail::posix_mutex internal_mutex_type;
|
Chris@16
|
89 #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
|
Chris@16
|
90 #undef BOOST_INTERPROCESS_USE_WINDOWS
|
Chris@16
|
91 typedef ipcdetail::windows_mutex internal_mutex_type;
|
Chris@16
|
92 #else
|
Chris@16
|
93 #error "Unknown platform for interprocess_mutex"
|
Chris@16
|
94 #endif
|
Chris@16
|
95
|
Chris@101
|
96 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
97 public:
|
Chris@16
|
98
|
Chris@16
|
99 //!Constructor.
|
Chris@16
|
100 //!Throws interprocess_exception on error.
|
Chris@16
|
101 interprocess_mutex();
|
Chris@16
|
102
|
Chris@16
|
103 //!Destructor. If any process uses the mutex after the destructor is called
|
Chris@16
|
104 //!the result is undefined. Does not throw.
|
Chris@16
|
105 ~interprocess_mutex();
|
Chris@16
|
106
|
Chris@16
|
107 //!Effects: The calling thread tries to obtain ownership of the mutex, and
|
Chris@16
|
108 //! if another thread has ownership of the mutex, it waits until it can
|
Chris@16
|
109 //! obtain the ownership. If a thread takes ownership of the mutex the
|
Chris@16
|
110 //! mutex must be unlocked by the same mutex.
|
Chris@16
|
111 //!Throws: interprocess_exception on error.
|
Chris@16
|
112 void lock();
|
Chris@16
|
113
|
Chris@16
|
114 //!Effects: The calling thread tries to obtain ownership of the mutex, and
|
Chris@16
|
115 //! if another thread has ownership of the mutex returns immediately.
|
Chris@16
|
116 //!Returns: If the thread acquires ownership of the mutex, returns true, if
|
Chris@16
|
117 //! the another thread has ownership of the mutex, returns false.
|
Chris@16
|
118 //!Throws: interprocess_exception on error.
|
Chris@16
|
119 bool try_lock();
|
Chris@16
|
120
|
Chris@16
|
121 //!Effects: The calling thread will try to obtain exclusive ownership of the
|
Chris@16
|
122 //! mutex if it can do so in until the specified time is reached. If the
|
Chris@16
|
123 //! mutex supports recursive locking, the mutex must be unlocked the same
|
Chris@16
|
124 //! number of times it is locked.
|
Chris@16
|
125 //!Returns: If the thread acquires ownership of the mutex, returns true, if
|
Chris@16
|
126 //! the timeout expires returns false.
|
Chris@16
|
127 //!Throws: interprocess_exception on error.
|
Chris@16
|
128 bool timed_lock(const boost::posix_time::ptime &abs_time);
|
Chris@16
|
129
|
Chris@16
|
130 //!Effects: The calling thread releases the exclusive ownership of the mutex.
|
Chris@16
|
131 //!Throws: interprocess_exception on error.
|
Chris@16
|
132 void unlock();
|
Chris@16
|
133
|
Chris@101
|
134 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
135 internal_mutex_type &internal_mutex()
|
Chris@16
|
136 { return m_mutex; }
|
Chris@16
|
137
|
Chris@16
|
138 const internal_mutex_type &internal_mutex() const
|
Chris@16
|
139 { return m_mutex; }
|
Chris@16
|
140
|
Chris@16
|
141 private:
|
Chris@16
|
142 internal_mutex_type m_mutex;
|
Chris@101
|
143 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
144 };
|
Chris@16
|
145
|
Chris@16
|
146 } //namespace interprocess {
|
Chris@16
|
147 } //namespace boost {
|
Chris@16
|
148
|
Chris@16
|
149
|
Chris@16
|
150 namespace boost {
|
Chris@16
|
151 namespace interprocess {
|
Chris@16
|
152
|
Chris@16
|
153 inline interprocess_mutex::interprocess_mutex(){}
|
Chris@16
|
154
|
Chris@16
|
155 inline interprocess_mutex::~interprocess_mutex(){}
|
Chris@16
|
156
|
Chris@16
|
157 inline void interprocess_mutex::lock()
|
Chris@16
|
158 {
|
Chris@16
|
159 #ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
|
Chris@16
|
160 boost::posix_time::ptime wait_time
|
Chris@16
|
161 = boost::posix_time::microsec_clock::universal_time()
|
Chris@16
|
162 + boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
|
Chris@16
|
163 if (!m_mutex.timed_lock(wait_time))
|
Chris@16
|
164 {
|
Chris@16
|
165 throw interprocess_exception(timeout_when_locking_error
|
Chris@16
|
166 , "Interprocess mutex timeout when locking. Possible deadlock: "
|
Chris@16
|
167 "owner died without unlocking?");
|
Chris@16
|
168 }
|
Chris@16
|
169 #else
|
Chris@16
|
170 m_mutex.lock();
|
Chris@16
|
171 #endif
|
Chris@16
|
172 }
|
Chris@16
|
173
|
Chris@16
|
174 inline bool interprocess_mutex::try_lock()
|
Chris@16
|
175 { return m_mutex.try_lock(); }
|
Chris@16
|
176
|
Chris@16
|
177 inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
|
Chris@16
|
178 { return m_mutex.timed_lock(abs_time); }
|
Chris@16
|
179
|
Chris@16
|
180 inline void interprocess_mutex::unlock()
|
Chris@16
|
181 { m_mutex.unlock(); }
|
Chris@16
|
182
|
Chris@16
|
183 } //namespace interprocess {
|
Chris@16
|
184 } //namespace boost {
|
Chris@16
|
185
|
Chris@16
|
186 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
187
|
Chris@16
|
188 #endif //BOOST_INTERPROCESS_MUTEX_HPP
|