Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2012-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_INTERPROCESS_CONDITION_ANY_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_CONDITION_ANY_HPP
|
Chris@16
|
13
|
Chris@101
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
15 # include <boost/config.hpp>
|
Chris@101
|
16 #endif
|
Chris@101
|
17 #
|
Chris@101
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
19 # pragma once
|
Chris@16
|
20 #endif
|
Chris@16
|
21
|
Chris@101
|
22 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
23
|
Chris@16
|
24 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
25 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
26
|
Chris@16
|
27 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
Chris@16
|
28 #include <boost/interprocess/sync/interprocess_mutex.hpp>
|
Chris@16
|
29 #include <boost/interprocess/sync/interprocess_condition.hpp>
|
Chris@16
|
30 #include <boost/interprocess/exceptions.hpp>
|
Chris@16
|
31 #include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
|
Chris@16
|
32
|
Chris@101
|
33 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
34
|
Chris@16
|
35 //!\file
|
Chris@16
|
36 //!Describes process-shared variables interprocess_condition_any class
|
Chris@16
|
37
|
Chris@16
|
38 namespace boost {
|
Chris@16
|
39
|
Chris@101
|
40 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@101
|
41
|
Chris@16
|
42 namespace posix_time
|
Chris@16
|
43 { class ptime; }
|
Chris@16
|
44
|
Chris@101
|
45 #endif //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@101
|
46
|
Chris@16
|
47 namespace interprocess {
|
Chris@16
|
48
|
Chris@16
|
49 //!This class is a condition variable that can be placed in shared memory or
|
Chris@16
|
50 //!memory mapped files.
|
Chris@16
|
51 //!
|
Chris@16
|
52 //!The interprocess_condition_any class is a generalization of interprocess_condition.
|
Chris@16
|
53 //!Whereas interprocess_condition works only on Locks with mutex_type == interprocess_mutex
|
Chris@16
|
54 //!interprocess_condition_any can operate on any user-defined lock that meets the BasicLockable
|
Chris@16
|
55 //!requirements (lock()/unlock() member functions).
|
Chris@16
|
56 //!
|
Chris@16
|
57 //!Unlike std::condition_variable_any in C++11, it is NOT safe to invoke the destructor if all
|
Chris@16
|
58 //!threads have been only notified. It is required that they have exited their respective wait
|
Chris@101
|
59 //!functions.
|
Chris@16
|
60 class interprocess_condition_any
|
Chris@16
|
61 {
|
Chris@101
|
62 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
63 //Non-copyable
|
Chris@16
|
64 interprocess_condition_any(const interprocess_condition_any &);
|
Chris@16
|
65 interprocess_condition_any &operator=(const interprocess_condition_any &);
|
Chris@16
|
66
|
Chris@16
|
67 class members
|
Chris@16
|
68 {
|
Chris@16
|
69 public:
|
Chris@16
|
70 typedef interprocess_condition condvar_type;
|
Chris@16
|
71 typedef interprocess_mutex mutex_type;
|
Chris@101
|
72
|
Chris@16
|
73 condvar_type &get_condvar() { return m_cond; }
|
Chris@16
|
74 mutex_type &get_mutex() { return m_mut; }
|
Chris@16
|
75
|
Chris@16
|
76 private:
|
Chris@16
|
77 condvar_type m_cond;
|
Chris@16
|
78 mutex_type m_mut;
|
Chris@16
|
79 };
|
Chris@16
|
80
|
Chris@16
|
81 ipcdetail::condition_any_wrapper<members> m_cond;
|
Chris@16
|
82
|
Chris@101
|
83 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
84 public:
|
Chris@16
|
85 //!Constructs a interprocess_condition_any. On error throws interprocess_exception.
|
Chris@16
|
86 interprocess_condition_any(){}
|
Chris@16
|
87
|
Chris@16
|
88 //!Destroys *this
|
Chris@16
|
89 //!liberating system resources.
|
Chris@16
|
90 ~interprocess_condition_any(){}
|
Chris@16
|
91
|
Chris@16
|
92 //!If there is a thread waiting on *this, change that
|
Chris@16
|
93 //!thread's state to ready. Otherwise there is no effect.
|
Chris@16
|
94 void notify_one()
|
Chris@16
|
95 { m_cond.notify_one(); }
|
Chris@16
|
96
|
Chris@16
|
97 //!Change the state of all threads waiting on *this to ready.
|
Chris@16
|
98 //!If there are no waiting threads, notify_all() has no effect.
|
Chris@16
|
99 void notify_all()
|
Chris@16
|
100 { m_cond.notify_all(); }
|
Chris@16
|
101
|
Chris@16
|
102 //!Releases the lock on the interprocess_mutex object associated with lock, blocks
|
Chris@16
|
103 //!the current thread of execution until readied by a call to
|
Chris@16
|
104 //!this->notify_one() or this->notify_all(), and then reacquires the lock.
|
Chris@16
|
105 template <typename L>
|
Chris@16
|
106 void wait(L& lock)
|
Chris@16
|
107 { m_cond.wait(lock); }
|
Chris@16
|
108
|
Chris@16
|
109 //!The same as:
|
Chris@16
|
110 //!while (!pred()) wait(lock)
|
Chris@16
|
111 template <typename L, typename Pr>
|
Chris@16
|
112 void wait(L& lock, Pr pred)
|
Chris@16
|
113 { m_cond.wait(lock, pred); }
|
Chris@16
|
114
|
Chris@16
|
115 //!Releases the lock on the interprocess_mutex object associated with lock, blocks
|
Chris@16
|
116 //!the current thread of execution until readied by a call to
|
Chris@16
|
117 //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
|
Chris@16
|
118 //!and then reacquires the lock.
|
Chris@16
|
119 //!Returns: false if time abs_time is reached, otherwise true.
|
Chris@16
|
120 template <typename L>
|
Chris@16
|
121 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
|
Chris@16
|
122 { return m_cond.timed_wait(lock, abs_time); }
|
Chris@16
|
123
|
Chris@16
|
124 //!The same as: while (!pred()) {
|
Chris@16
|
125 //! if (!timed_wait(lock, abs_time)) return pred();
|
Chris@16
|
126 //! } return true;
|
Chris@16
|
127 template <typename L, typename Pr>
|
Chris@16
|
128 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
|
Chris@16
|
129 { return m_cond.timed_wait(lock, abs_time, pred); }
|
Chris@16
|
130 };
|
Chris@16
|
131
|
Chris@16
|
132 } //namespace interprocess
|
Chris@16
|
133 } // namespace boost
|
Chris@16
|
134
|
Chris@16
|
135 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
136
|
Chris@16
|
137 #endif // BOOST_INTERPROCESS_CONDITION_ANY_HPP
|