1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2012-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_ANY_HPP
12 #define BOOST_INTERPROCESS_CONDITION_ANY_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/detail/posix_time_types_wrk.hpp>
28 #include <boost/interprocess/sync/interprocess_mutex.hpp>
29 #include <boost/interprocess/sync/interprocess_condition.hpp>
30 #include <boost/interprocess/exceptions.hpp>
31 #include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
33 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
36 //!Describes process-shared variables interprocess_condition_any class
40 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
45 #endif //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
47 namespace interprocess {
49 //!This class is a condition variable that can be placed in shared memory or
50 //!memory mapped files.
52 //!The interprocess_condition_any class is a generalization of interprocess_condition.
53 //!Whereas interprocess_condition works only on Locks with mutex_type == interprocess_mutex
54 //!interprocess_condition_any can operate on any user-defined lock that meets the BasicLockable
55 //!requirements (lock()/unlock() member functions).
57 //!Unlike std::condition_variable_any in C++11, it is NOT safe to invoke the destructor if all
58 //!threads have been only notified. It is required that they have exited their respective wait
60 class interprocess_condition_any
62 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
64 interprocess_condition_any(const interprocess_condition_any &);
65 interprocess_condition_any &operator=(const interprocess_condition_any &);
70 typedef interprocess_condition condvar_type;
71 typedef interprocess_mutex mutex_type;
73 condvar_type &get_condvar() { return m_cond; }
74 mutex_type &get_mutex() { return m_mut; }
81 ipcdetail::condition_any_wrapper<members> m_cond;
83 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
85 //!Constructs a interprocess_condition_any. On error throws interprocess_exception.
86 interprocess_condition_any(){}
89 //!liberating system resources.
90 ~interprocess_condition_any(){}
92 //!If there is a thread waiting on *this, change that
93 //!thread's state to ready. Otherwise there is no effect.
95 { m_cond.notify_one(); }
97 //!Change the state of all threads waiting on *this to ready.
98 //!If there are no waiting threads, notify_all() has no effect.
100 { m_cond.notify_all(); }
102 //!Releases the lock on the interprocess_mutex object associated with lock, blocks
103 //!the current thread of execution until readied by a call to
104 //!this->notify_one() or this->notify_all(), and then reacquires the lock.
105 template <typename L>
107 { m_cond.wait(lock); }
110 //!while (!pred()) wait(lock)
111 template <typename L, typename Pr>
112 void wait(L& lock, Pr pred)
113 { m_cond.wait(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)
122 { return m_cond.timed_wait(lock, abs_time); }
124 //!The same as: while (!pred()) {
125 //! if (!timed_wait(lock, abs_time)) return pred();
127 template <typename L, typename Pr>
128 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
129 { return m_cond.timed_wait(lock, abs_time, pred); }
132 } //namespace interprocess
135 #include <boost/interprocess/detail/config_end.hpp>
137 #endif // BOOST_INTERPROCESS_CONDITION_ANY_HPP