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
18 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
20 #include <boost/interprocess/detail/config_begin.hpp>
21 #include <boost/interprocess/detail/workaround.hpp>
23 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
24 #include <boost/interprocess/sync/interprocess_mutex.hpp>
25 #include <boost/interprocess/sync/detail/locks.hpp>
26 #include <boost/interprocess/exceptions.hpp>
27 #include <boost/limits.hpp>
28 #include <boost/assert.hpp>
30 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
31 #include <boost/interprocess/sync/posix/condition.hpp>
32 #define BOOST_INTERPROCESS_USE_POSIX
34 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
35 #include <boost/interprocess/sync/windows/condition.hpp>
36 #define BOOST_INTERPROCESS_USE_WINDOWS
37 #elif !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
38 #include <boost/interprocess/sync/spin/condition.hpp>
39 #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
42 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
45 //!Describes process-shared variables interprocess_condition class
49 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
54 #endif //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
56 namespace interprocess {
58 class named_condition;
60 //!This class is a condition variable that can be placed in shared memory or
61 //!memory mapped files.
62 //!Destroys the object of type std::condition_variable_any
64 //!Unlike std::condition_variable in C++11, it is NOT safe to invoke the destructor if all
65 //!threads have been only notified. It is required that they have exited their respective wait
67 class interprocess_condition
69 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
71 interprocess_condition(const interprocess_condition &);
72 interprocess_condition &operator=(const interprocess_condition &);
73 friend class named_condition;
74 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
77 //!Constructs a interprocess_condition. On error throws interprocess_exception.
78 interprocess_condition()
82 //!liberating system resources.
83 ~interprocess_condition()
86 //!If there is a thread waiting on *this, change that
87 //!thread's state to ready. Otherwise there is no effect.
89 { m_condition.notify_one(); }
91 //!Change the state of all threads waiting on *this to ready.
92 //!If there are no waiting threads, notify_all() has no effect.
94 { m_condition.notify_all(); }
96 //!Releases the lock on the interprocess_mutex object associated with lock, blocks
97 //!the current thread of execution until readied by a call to
98 //!this->notify_one() or this->notify_all(), and then reacquires the lock.
102 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
103 m_condition.wait(internal_lock);
107 //!while (!pred()) wait(lock)
108 template <typename L, typename Pr>
109 void wait(L& lock, Pr pred)
111 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
112 m_condition.wait(internal_lock, pred);
115 //!Releases the lock on the interprocess_mutex object associated with lock, blocks
116 //!the current thread of execution until readied by a call to
117 //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
118 //!and then reacquires the lock.
119 //!Returns: false if time abs_time is reached, otherwise true.
120 template <typename L>
121 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
123 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
124 return m_condition.timed_wait(internal_lock, abs_time);
127 //!The same as: while (!pred()) {
128 //! if (!timed_wait(lock, abs_time)) return pred();
130 template <typename L, typename Pr>
131 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
133 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
134 return m_condition.timed_wait(internal_lock, abs_time, pred);
137 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
140 #if defined (BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
141 #undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
142 ipcdetail::spin_condition m_condition;
143 #elif defined(BOOST_INTERPROCESS_USE_POSIX)
144 #undef BOOST_INTERPROCESS_USE_POSIX
145 ipcdetail::posix_condition m_condition;
146 #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
147 #undef BOOST_INTERPROCESS_USE_WINDOWS
148 ipcdetail::windows_condition m_condition;
150 #error "Unknown platform for interprocess_mutex"
152 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
155 } //namespace interprocess
158 #include <boost/interprocess/detail/config_end.hpp>
160 #endif // BOOST_INTERPROCESS_CONDITION_HPP