]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/include/boost/interprocess/sync/interprocess_mutex.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / interprocess / include / boost / interprocess / sync / interprocess_mutex.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10 //
11 // Parts of the pthread code come from Boost Threads code.
12 //
13 //////////////////////////////////////////////////////////////////////////////
14
15 #ifndef BOOST_INTERPROCESS_MUTEX_HPP
16 #define BOOST_INTERPROCESS_MUTEX_HPP
17
18 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
19
20 #ifndef BOOST_CONFIG_HPP
21 # include <boost/config.hpp>
22 #endif
23 #
24 #if defined(BOOST_HAS_PRAGMA_ONCE)
25 # pragma once
26 #endif
27
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>
33
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
37 //Experimental...
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
44
45 namespace boost {
46 namespace interprocess {
47 namespace ipcdetail{
48 namespace robust_emulation_helpers {
49
50 template<class T>
51 class mutex_traits;
52
53 }}}}
54
55 #endif
56
57 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
58
59 //!\file
60 //!Describes a mutex class that can be placed in memory shared by
61 //!several processes.
62
63 namespace boost {
64 namespace interprocess {
65
66 class interprocess_condition;
67
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
71 {
72 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
73 //Non-copyable
74 interprocess_mutex(const interprocess_mutex &);
75 interprocess_mutex &operator=(const interprocess_mutex &);
76 friend class interprocess_condition;
77
78 public:
79 #if defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
80 #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
81 typedef ipcdetail::spin_mutex internal_mutex_type;
82 private:
83 friend class ipcdetail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
84 void take_ownership(){ m_mutex.take_ownership(); }
85 public:
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;
92 #else
93 #error "Unknown platform for interprocess_mutex"
94 #endif
95
96 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
97 public:
98
99 //!Constructor.
100 //!Throws interprocess_exception on error.
101 interprocess_mutex();
102
103 //!Destructor. If any process uses the mutex after the destructor is called
104 //!the result is undefined. Does not throw.
105 ~interprocess_mutex();
106
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.
112 void lock();
113
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.
119 bool try_lock();
120
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);
129
130 //!Effects: The calling thread releases the exclusive ownership of the mutex.
131 //!Throws: interprocess_exception on error.
132 void unlock();
133
134 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
135 internal_mutex_type &internal_mutex()
136 { return m_mutex; }
137
138 const internal_mutex_type &internal_mutex() const
139 { return m_mutex; }
140
141 private:
142 internal_mutex_type m_mutex;
143 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
144 };
145
146 } //namespace interprocess {
147 } //namespace boost {
148
149
150 namespace boost {
151 namespace interprocess {
152
153 inline interprocess_mutex::interprocess_mutex(){}
154
155 inline interprocess_mutex::~interprocess_mutex(){}
156
157 inline void interprocess_mutex::lock()
158 {
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))
164 {
165 throw interprocess_exception(timeout_when_locking_error
166 , "Interprocess mutex timeout when locking. Possible deadlock: "
167 "owner died without unlocking?");
168 }
169 #else
170 m_mutex.lock();
171 #endif
172 }
173
174 inline bool interprocess_mutex::try_lock()
175 { return m_mutex.try_lock(); }
176
177 inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
178 { return m_mutex.timed_lock(abs_time); }
179
180 inline void interprocess_mutex::unlock()
181 { m_mutex.unlock(); }
182
183 } //namespace interprocess {
184 } //namespace boost {
185
186 #include <boost/interprocess/detail/config_end.hpp>
187
188 #endif //BOOST_INTERPROCESS_MUTEX_HPP