]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/interprocess/sync/named_condition.hpp
bump version to 18.2.4-pve3
[ceph.git] / ceph / src / boost / boost / interprocess / sync / named_condition.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 #ifndef BOOST_INTERPROCESS_NAMED_CONDITION_HPP
12 #define BOOST_INTERPROCESS_NAMED_CONDITION_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24
25 #include <boost/interprocess/sync/cv_status.hpp>
26 #include <boost/interprocess/creation_tags.hpp>
27 #include <boost/interprocess/exceptions.hpp>
28 #include <boost/interprocess/detail/interprocess_tester.hpp>
29 #include <boost/interprocess/permissions.hpp>
30 #include <boost/interprocess/sync/detail/locks.hpp>
31 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
32 #include <boost/interprocess/sync/windows/named_condition.hpp>
33 #define BOOST_INTERPROCESS_NAMED_CONDITION_USE_WINAPI
34 #else
35 #include <boost/interprocess/sync/shm/named_condition.hpp>
36 #endif
37
38 //!\file
39 //!Describes a named condition class for inter-process synchronization
40
41 namespace boost {
42 namespace interprocess {
43
44 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
45 namespace ipcdetail{ class interprocess_tester; }
46 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
47
48 //! A global condition variable that can be created by name.
49 //! This condition variable is designed to work with named_mutex and
50 //! can't be placed in shared memory or memory mapped files.
51 class named_condition
52 {
53 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
54 //Non-copyable
55 named_condition();
56 named_condition(const named_condition &);
57 named_condition &operator=(const named_condition &);
58 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
59 public:
60
61 //!Creates a global condition with a name.
62 //!If the condition can't be created throws interprocess_exception
63 named_condition(create_only_t, const char *name, const permissions &perm = permissions());
64
65 //!Opens or creates a global condition with a name.
66 //!If the condition is created, this call is equivalent to
67 //!named_condition(create_only_t, ... )
68 //!If the condition is already created, this call is equivalent
69 //!named_condition(open_only_t, ... )
70 //!Does not throw
71 named_condition(open_or_create_t, const char *name, const permissions &perm = permissions());
72
73 //!Opens a global condition with a name if that condition is previously
74 //!created. If it is not previously created this function throws
75 //!interprocess_exception.
76 named_condition(open_only_t, const char *name);
77
78 //!Opens a global condition with a name if that condition is previously
79 //!created. If it is not previously created this function throws
80 //!interprocess_exception.
81
82 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
83 //!Creates a global condition with a name.
84 //!If the condition can't be created throws interprocess_exception
85 //!
86 //!Note: This function is only available on operating systems with
87 //! native wchar_t APIs (e.g. Windows).
88 named_condition(create_only_t, const wchar_t *name, const permissions &perm = permissions());
89
90 //!Opens or creates a global condition with a name.
91 //!If the condition is created, this call is equivalent to
92 //!named_condition(create_only_t, ... )
93 //!If the condition is already created, this call is equivalent
94 //!named_condition(open_only_t, ... )
95 //!Does not throw
96 //!
97 //!Note: This function is only available on operating systems with
98 //! native wchar_t APIs (e.g. Windows).
99 named_condition(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
100
101 //!Opens a global condition with a name if that condition is previously
102 //!created. If it is not previously created this function throws
103 //!interprocess_exception.
104 //!
105 //!Note: This function is only available on operating systems with
106 //! native wchar_t APIs (e.g. Windows).
107 named_condition(open_only_t, const wchar_t *name);
108
109 #endif //#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
110
111 //!Destroys *this and indicates that the calling process is finished using
112 //!the resource. The destructor function will deallocate
113 //!any system resources allocated by the system for use by this process for
114 //!this resource. The resource can still be opened again calling
115 //!the open constructor overload. To erase the resource from the system
116 //!use remove().
117 ~named_condition();
118
119 //!If there is a thread waiting on *this, change that
120 //!thread's state to ready. Otherwise there is no effect.*/
121 void notify_one();
122
123 //!Change the state of all threads waiting on *this to ready.
124 //!If there are no waiting threads, notify_all() has no effect.
125 void notify_all();
126
127 //!Releases the lock on the named_mutex object associated with lock, blocks
128 //!the current thread of execution until readied by a call to
129 //!this->notify_one() or this->notify_all(), and then reacquires the lock.
130 template <typename L>
131 void wait(L& lock);
132
133 //!The same as:
134 //!while (!pred()) wait(lock)
135 template <typename L, typename Pr>
136 void wait(L& lock, Pr pred);
137
138 //!Releases the lock on the named_mutex object associated with lock, blocks
139 //!the current thread of execution until readied by a call to
140 //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
141 //!and then reacquires the lock.
142 //!Returns: false if time abs_time is reached, otherwise true.
143 template <typename L, class TimePoint>
144 bool timed_wait(L& lock, const TimePoint &abs_time);
145
146 //!The same as: while (!pred()) {
147 //! if (!timed_wait(lock, abs_time)) return pred();
148 //! } return true;
149 template <typename L, class TimePoint, typename Pr>
150 bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred);
151
152 //!Same as `timed_wait`, but this function is modeled after the
153 //!standard library interface.
154 template <typename L, class TimePoint>
155 cv_status wait_until(L& lock, const TimePoint &abs_time)
156 { return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
157
158 //!Same as `timed_wait`, but this function is modeled after the
159 //!standard library interface.
160 template <typename L, class TimePoint, typename Pr>
161 bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
162 { return this->timed_wait(lock, abs_time, pred); }
163
164 //!Same as `timed_wait`, but this function is modeled after the
165 //!standard library interface and uses relative timeouts.
166 template <typename L, class Duration>
167 cv_status wait_for(L& lock, const Duration &dur)
168 { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur)); }
169
170 //!Same as `timed_wait`, but this function is modeled after the
171 //!standard library interface and uses relative timeouts
172 template <typename L, class Duration, typename Pr>
173 bool wait_for(L& lock, const Duration &dur, Pr pred)
174 { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur), pred); }
175
176 //!Erases a named condition from the system.
177 //!Returns false on error. Never throws.
178 static bool remove(const char *name);
179
180 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
181
182 //!Erases a named condition from the system.
183 //!Returns false on error. Never throws.
184 //!
185 //!Note: This function is only available on operating systems with
186 //! native wchar_t APIs (e.g. Windows).
187 static bool remove(const wchar_t *name);
188
189 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
190
191 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
192 private:
193 #if defined(BOOST_INTERPROCESS_NAMED_CONDITION_USE_WINAPI)
194 typedef ipcdetail::winapi_named_condition condition_type;
195 #else
196 typedef ipcdetail::shm_named_condition condition_type;
197 #endif
198 condition_type m_cond;
199
200 friend class ipcdetail::interprocess_tester;
201 void dont_close_on_destruction()
202 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_cond); }
203 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
204 };
205
206 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
207
208 inline named_condition::~named_condition()
209 {}
210
211 inline named_condition::named_condition(create_only_t, const char *name, const permissions &perm)
212 : m_cond(create_only_t(), name, perm)
213 {}
214
215 inline named_condition::named_condition(open_or_create_t, const char *name, const permissions &perm)
216 : m_cond(open_or_create_t(), name, perm)
217 {}
218
219 inline named_condition::named_condition(open_only_t, const char *name)
220 : m_cond(open_only_t(), name)
221 {}
222
223 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
224
225 inline named_condition::named_condition(create_only_t, const wchar_t *name, const permissions &perm)
226 : m_cond(create_only_t(), name, perm)
227 {}
228
229 inline named_condition::named_condition(open_or_create_t, const wchar_t *name, const permissions &perm)
230 : m_cond(open_or_create_t(), name, perm)
231 {}
232
233 inline named_condition::named_condition(open_only_t, const wchar_t *name)
234 : m_cond(open_only_t(), name)
235 {}
236
237 #endif //#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
238
239
240 inline void named_condition::notify_one()
241 { m_cond.notify_one(); }
242
243 inline void named_condition::notify_all()
244 { m_cond.notify_all(); }
245
246 template <typename L>
247 inline void named_condition::wait(L& lock)
248 {
249 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
250 m_cond.wait(internal_lock);
251 }
252
253 template <typename L, typename Pr>
254 inline void named_condition::wait(L& lock, Pr pred)
255 {
256 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
257 m_cond.wait(internal_lock, pred);
258 }
259
260 template <typename L, typename TimePoint>
261 inline bool named_condition::timed_wait
262 (L& lock, const TimePoint &abs_time)
263 {
264 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
265 return m_cond.timed_wait(internal_lock, abs_time);
266 }
267
268 template <typename L, typename TimePoint, typename Pr>
269 inline bool named_condition::timed_wait
270 (L& lock, const TimePoint &abs_time, Pr pred)
271 {
272 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
273 return m_cond.timed_wait(internal_lock, abs_time, pred);
274 }
275
276 inline bool named_condition::remove(const char *name)
277 {
278 return condition_type::remove(name);
279 }
280
281 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
282
283 inline bool named_condition::remove(const wchar_t *name)
284 {
285 return condition_type::remove(name);
286 }
287
288 #endif
289
290
291 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
292
293 } //namespace interprocess
294 } //namespace boost
295
296 #include <boost/interprocess/detail/config_end.hpp>
297
298 #endif // BOOST_INTERPROCESS_NAMED_CONDITION_HPP