]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/interprocess/sync/interprocess_condition.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / interprocess / sync / interprocess_condition.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_CONDITION_HPP
12 #define BOOST_INTERPROCESS_CONDITION_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
22 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
23
24 #include <boost/interprocess/detail/config_begin.hpp>
25 #include <boost/interprocess/detail/workaround.hpp>
26
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>
33
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
37 //Experimental...
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
41 #else
42 //spin_condition is used
43 #include <boost/interprocess/sync/spin/condition.hpp>
44 #endif
45
46 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
47
48 //!\file
49 //!Describes process-shared variables interprocess_condition class
50
51 namespace boost {
52 namespace interprocess {
53
54 class named_condition;
55
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
59 //!
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
62 //!functions.
63 class interprocess_condition
64 {
65 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
66 //Non-copyable
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
71
72 public:
73 //!Constructs a interprocess_condition. On error throws interprocess_exception.
74 interprocess_condition()
75 {}
76
77 //!Destroys *this
78 //!liberating system resources.
79 ~interprocess_condition()
80 {}
81
82 //!If there is a thread waiting on *this, change that
83 //!thread's state to ready. Otherwise there is no effect.
84 void notify_one()
85 { m_condition.notify_one(); }
86
87 //!Change the state of all threads waiting on *this to ready.
88 //!If there are no waiting threads, notify_all() has no effect.
89 void notify_all()
90 { m_condition.notify_all(); }
91
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.
95 template <typename L>
96 void wait(L& lock)
97 {
98 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
99 m_condition.wait(internal_lock);
100 }
101
102 //!The same as:
103 //!while (!pred()) wait(lock)
104 template <typename L, typename Pr>
105 void wait(L& lock, Pr pred)
106 {
107 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
108 m_condition.wait(internal_lock, pred);
109 }
110
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)
118 {
119 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
120 return m_condition.timed_wait(internal_lock, abs_time);
121 }
122
123 //!The same as: while (!pred()) {
124 //! if (!timed_wait(lock, abs_time)) return pred();
125 //! } return true;
126 template <typename L, class TimePoint, typename Pr>
127 bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
128 {
129 ipcdetail::internal_mutex_lock<L> internal_lock(lock);
130 return m_condition.timed_wait(internal_lock, abs_time, pred);
131 }
132
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; }
138
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); }
144
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)); }
150
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); }
156
157 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
158
159 private:
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;
164 #else
165 ipcdetail::spin_condition m_condition;
166 #endif
167
168 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
169 };
170
171 } //namespace interprocess
172 } // namespace boost
173
174 #include <boost/interprocess/detail/config_end.hpp>
175
176 #endif // BOOST_INTERPROCESS_CONDITION_HPP