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_CONDITION_HPP
12 #define BOOST_INTERPROCESS_CONDITION_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
24 #include <boost/interprocess/detail/config_begin.hpp>
25 #include <boost/interprocess/detail/workaround.hpp>
27 #include <boost/interprocess/sync/cv_status.hpp>
28 #include <boost/interprocess/sync/interprocess_mutex.hpp>
29 #include <boost/interprocess/sync/detail/locks.hpp>
30 #include <boost/interprocess/exceptions.hpp>
31 #include <boost/limits.hpp>
32 #include <boost/assert.hpp>
34 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
35 #include <boost/interprocess/sync/posix/condition.hpp>
36 #define BOOST_INTERPROCESS_CONDITION_USE_POSIX
38 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
39 #include <boost/interprocess/sync/windows/condition.hpp>
40 #define BOOST_INTERPROCESS_CONDITION_USE_WINAPI
42 //spin_condition is used
43 #include <boost/interprocess/sync/spin/condition.hpp>
46 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
49 //!Describes process-shared variables interprocess_condition class
52 namespace interprocess {
54 class named_condition;
56 //!This class is a condition variable that can be placed in shared memory or
57 //!memory mapped files.
58 //!Destroys the object of type std::condition_variable_any
60 //!Unlike std::condition_variable in C++11, it is NOT safe to invoke the destructor if all
61 //!threads have been only notified. It is required that they have exited their respective wait
63 class interprocess_condition
65 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
67 interprocess_condition(const interprocess_condition &);
68 interprocess_condition &operator=(const interprocess_condition &);
69 friend class named_condition;
70 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
73 //!Constructs a interprocess_condition. On error throws interprocess_exception.
74 interprocess_condition()
78 //!liberating system resources.
79 ~interprocess_condition()
82 //!If there is a thread waiting on *this, change that
83 //!thread's state to ready. Otherwise there is no effect.
85 { m_condition.notify_one(); }
87 //!Change the state of all threads waiting on *this to ready.
88 //!If there are no waiting threads, notify_all() has no effect.
90 { m_condition.notify_all(); }
92 //!Releases the lock on the interprocess_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.
98 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
99 m_condition.wait(internal_lock);
103 //!while (!pred()) wait(lock)
104 template <typename L, typename Pr>
105 void wait(L& lock, Pr pred)
107 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
108 m_condition.wait(internal_lock, pred);
111 //!Releases the lock on the interprocess_mutex object associated with lock, blocks
112 //!the current thread of execution until readied by a call to
113 //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
114 //!and then reacquires the lock.
115 //!Returns: false if time abs_time is reached, otherwise true.
116 template <typename L, class TimePoint>
117 bool timed_wait(L& lock, const TimePoint &abs_time)
119 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
120 return m_condition.timed_wait(internal_lock, abs_time);
123 //!The same as: while (!pred()) {
124 //! if (!timed_wait(lock, abs_time)) return pred();
126 template <typename L, class TimePoint, typename Pr>
127 bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
129 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
130 return m_condition.timed_wait(internal_lock, abs_time, pred);
133 //!Same as `timed_wait`, but this function is modeled after the
134 //!standard library interface.
135 template <typename L, class TimePoint>
136 cv_status wait_until(L& lock, const TimePoint &abs_time)
137 { return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
139 //!Same as `timed_wait`, but this function is modeled after the
140 //!standard library interface.
141 template <typename L, class TimePoint, typename Pr>
142 bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
143 { return this->timed_wait(lock, abs_time, pred); }
145 //!Same as `timed_wait`, but this function is modeled after the
146 //!standard library interface and uses relative timeouts.
147 template <typename L, class Duration>
148 cv_status wait_for(L& lock, const Duration &dur)
149 { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur)); }
151 //!Same as `timed_wait`, but this function is modeled after the
152 //!standard library interface and uses relative timeouts
153 template <typename L, class Duration, typename Pr>
154 bool wait_for(L& lock, const Duration &dur, Pr pred)
155 { return this->wait_until(lock, ipcdetail::duration_to_ustime(dur), pred); }
157 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
160 #if defined(BOOST_INTERPROCESS_CONDITION_USE_POSIX)
161 ipcdetail::posix_condition m_condition;
162 #elif defined(BOOST_INTERPROCESS_CONDITION_USE_WINAPI)
163 ipcdetail::winapi_condition m_condition;
165 ipcdetail::spin_condition m_condition;
168 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
171 } //namespace interprocess
174 #include <boost/interprocess/detail/config_end.hpp>
176 #endif // BOOST_INTERPROCESS_CONDITION_HPP