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 #ifndef BOOST_INTERPROCESS_NAMED_CONDITION_HPP
12 #define BOOST_INTERPROCESS_NAMED_CONDITION_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
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
35 #include <boost/interprocess/sync/shm/named_condition.hpp>
39 //!Describes a named condition class for inter-process synchronization
42 namespace interprocess {
44 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
45 namespace ipcdetail{ class interprocess_tester; }
46 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
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.
53 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
56 named_condition(const named_condition &);
57 named_condition &operator=(const named_condition &);
58 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
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());
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, ... )
71 named_condition(open_or_create_t, const char *name, const permissions &perm = permissions());
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);
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.
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
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());
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, ... )
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());
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.
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);
109 #endif //#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
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
119 //!If there is a thread waiting on *this, change that
120 //!thread's state to ready. Otherwise there is no effect.*/
123 //!Change the state of all threads waiting on *this to ready.
124 //!If there are no waiting threads, notify_all() has no effect.
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>
134 //!while (!pred()) wait(lock)
135 template <typename L, typename Pr>
136 void wait(L& lock, Pr pred);
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);
146 //!The same as: while (!pred()) {
147 //! if (!timed_wait(lock, abs_time)) return pred();
149 template <typename L, class TimePoint, typename Pr>
150 bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred);
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; }
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); }
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)); }
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); }
176 //!Erases a named condition from the system.
177 //!Returns false on error. Never throws.
178 static bool remove(const char *name);
180 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
182 //!Erases a named condition from the system.
183 //!Returns false on error. Never throws.
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);
189 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
191 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
193 #if defined(BOOST_INTERPROCESS_NAMED_CONDITION_USE_WINAPI)
194 typedef ipcdetail::winapi_named_condition condition_type;
196 typedef ipcdetail::shm_named_condition condition_type;
198 condition_type m_cond;
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
206 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
208 inline named_condition::~named_condition()
211 inline named_condition::named_condition(create_only_t, const char *name, const permissions &perm)
212 : m_cond(create_only_t(), name, perm)
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)
219 inline named_condition::named_condition(open_only_t, const char *name)
220 : m_cond(open_only_t(), name)
223 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
225 inline named_condition::named_condition(create_only_t, const wchar_t *name, const permissions &perm)
226 : m_cond(create_only_t(), name, perm)
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)
233 inline named_condition::named_condition(open_only_t, const wchar_t *name)
234 : m_cond(open_only_t(), name)
237 #endif //#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
240 inline void named_condition::notify_one()
241 { m_cond.notify_one(); }
243 inline void named_condition::notify_all()
244 { m_cond.notify_all(); }
246 template <typename L>
247 inline void named_condition::wait(L& lock)
249 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
250 m_cond.wait(internal_lock);
253 template <typename L, typename Pr>
254 inline void named_condition::wait(L& lock, Pr pred)
256 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
257 m_cond.wait(internal_lock, pred);
260 template <typename L, typename TimePoint>
261 inline bool named_condition::timed_wait
262 (L& lock, const TimePoint &abs_time)
264 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
265 return m_cond.timed_wait(internal_lock, abs_time);
268 template <typename L, typename TimePoint, typename Pr>
269 inline bool named_condition::timed_wait
270 (L& lock, const TimePoint &abs_time, Pr pred)
272 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
273 return m_cond.timed_wait(internal_lock, abs_time, pred);
276 inline bool named_condition::remove(const char *name)
278 return condition_type::remove(name);
281 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
283 inline bool named_condition::remove(const wchar_t *name)
285 return condition_type::remove(name);
291 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
293 } //namespace interprocess
296 #include <boost/interprocess/detail/config_end.hpp>
298 #endif // BOOST_INTERPROCESS_NAMED_CONDITION_HPP