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>
24 #include <boost/interprocess/creation_tags.hpp>
25 #include <boost/interprocess/exceptions.hpp>
26 #include <boost/interprocess/detail/interprocess_tester.hpp>
27 #include <boost/interprocess/permissions.hpp>
28 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
29 #include <boost/interprocess/sync/detail/locks.hpp>
30 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
31 #include <boost/interprocess/sync/windows/named_condition.hpp>
32 #define BOOST_INTERPROCESS_USE_WINDOWS
34 #include <boost/interprocess/sync/shm/named_condition.hpp>
38 //!Describes a named condition class for inter-process synchronization
41 namespace interprocess {
43 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
44 namespace ipcdetail{ class interprocess_tester; }
45 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
47 //! A global condition variable that can be created by name.
48 //! This condition variable is designed to work with named_mutex and
49 //! can't be placed in shared memory or memory mapped files.
52 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
55 named_condition(const named_condition &);
56 named_condition &operator=(const named_condition &);
57 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
59 //!Creates a global condition with a name.
60 //!If the condition can't be created throws interprocess_exception
61 named_condition(create_only_t create_only, const char *name, const permissions &perm = permissions());
63 //!Opens or creates a global condition with a name.
64 //!If the condition is created, this call is equivalent to
65 //!named_condition(create_only_t, ... )
66 //!If the condition is already created, this call is equivalent
67 //!named_condition(open_only_t, ... )
69 named_condition(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
71 //!Opens a global condition with a name if that condition is previously
72 //!created. If it is not previously created this function throws
73 //!interprocess_exception.
74 named_condition(open_only_t open_only, const char *name);
76 //!Destroys *this and indicates that the calling process is finished using
77 //!the resource. The destructor function will deallocate
78 //!any system resources allocated by the system for use by this process for
79 //!this resource. The resource can still be opened again calling
80 //!the open constructor overload. To erase the resource from the system
84 //!If there is a thread waiting on *this, change that
85 //!thread's state to ready. Otherwise there is no effect.*/
88 //!Change the state of all threads waiting on *this to ready.
89 //!If there are no waiting threads, notify_all() has no effect.
92 //!Releases the lock on the named_mutex object associated with lock, blocks
93 //!the current thread of execution until readied by a call to
94 //!this->notify_one() or this->notify_all(), and then reacquires the lock.
99 //!while (!pred()) wait(lock)
100 template <typename L, typename Pr>
101 void wait(L& lock, Pr pred);
103 //!Releases the lock on the named_mutex object associated with lock, blocks
104 //!the current thread of execution until readied by a call to
105 //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
106 //!and then reacquires the lock.
107 //!Returns: false if time abs_time is reached, otherwise true.
108 template <typename L>
109 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time);
111 //!The same as: while (!pred()) {
112 //! if (!timed_wait(lock, abs_time)) return pred();
114 template <typename L, typename Pr>
115 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred);
117 //!Erases a named condition from the system.
118 //!Returns false on error. Never throws.
119 static bool remove(const char *name);
121 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
123 #if defined(BOOST_INTERPROCESS_USE_WINDOWS)
124 typedef ipcdetail::windows_named_condition condition_type;
126 typedef ipcdetail::shm_named_condition condition_type;
128 condition_type m_cond;
130 friend class ipcdetail::interprocess_tester;
131 void dont_close_on_destruction()
132 { ipcdetail::interprocess_tester::dont_close_on_destruction(m_cond); }
133 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
136 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
138 inline named_condition::~named_condition()
141 inline named_condition::named_condition(create_only_t, const char *name, const permissions &perm)
142 : m_cond(create_only_t(), name, perm)
145 inline named_condition::named_condition(open_or_create_t, const char *name, const permissions &perm)
146 : m_cond(open_or_create_t(), name, perm)
149 inline named_condition::named_condition(open_only_t, const char *name)
150 : m_cond(open_only_t(), name)
153 inline void named_condition::notify_one()
154 { m_cond.notify_one(); }
156 inline void named_condition::notify_all()
157 { m_cond.notify_all(); }
159 template <typename L>
160 inline void named_condition::wait(L& lock)
162 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
163 m_cond.wait(internal_lock);
166 template <typename L, typename Pr>
167 inline void named_condition::wait(L& lock, Pr pred)
169 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
170 m_cond.wait(internal_lock, pred);
173 template <typename L>
174 inline bool named_condition::timed_wait
175 (L& lock, const boost::posix_time::ptime &abs_time)
177 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
178 return m_cond.timed_wait(internal_lock, abs_time);
181 template <typename L, typename Pr>
182 inline bool named_condition::timed_wait
183 (L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
185 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
186 return m_cond.timed_wait(internal_lock, abs_time, pred);
189 inline bool named_condition::remove(const char *name)
191 return condition_type::remove(name);
194 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
196 } //namespace interprocess
199 #include <boost/interprocess/detail/config_end.hpp>
201 #endif // BOOST_INTERPROCESS_NAMED_CONDITION_HPP