source: mainline/uspace/lib/cpp/include/__bits/thread/condition_variable.hpp@ b57a3ee

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b57a3ee was b57a3ee, checked in by Dzejrou <dzejrou@…>, 7 years ago

cpp: refactored the library layout, everything from the impl directory was moved to the bits directory for the sake of consistency, updated copyright notices and include guards

  • Property mode set to 100644
File size: 7.4 KB
Line 
1/*
2 * Copyright (c) 2018 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_BITS_THREAD_CONDITION_VARIABLE
30#define LIBCPP_BITS_THREAD_CONDITION_VARIABLE
31
32#include <__bits/thread/threading.hpp>
33#include <mutex>
34
35namespace std
36{
37 extern "C" {
38 #include <fibril.h>
39 #include <fibril_synch.h>
40 }
41
42 enum class cv_status
43 {
44 no_timeout,
45 timeout
46 };
47
48 namespace aux
49 {
50 template<class Clock, class Duration>
51 aux::time_unit_t time_until(const chrono::time_point<Clock, Duration>& abs_time)
52 {
53 return aux::threading::time::convert(abs_time - Clock::now());
54 }
55 }
56
57 /**
58 * 30.5.1, class condition_variable:
59 */
60
61 class condition_variable
62 {
63 public:
64 condition_variable();
65 ~condition_variable();
66
67 condition_variable(const condition_variable&) = delete;
68 condition_variable& operator=(const condition_variable&) = delete;
69
70 void notify_one() noexcept;
71 void notify_all() noexcept;
72
73 void wait(unique_lock<mutex>&);
74
75 template<class Predicate>
76 void wait(unique_lock<mutex>& lock, Predicate pred)
77 {
78 /**
79 * Note: lock is supposed to be locked here,
80 * so no need to lock it.
81 */
82 while (!pred())
83 wait(lock);
84 }
85
86 template<class Clock, class Duration>
87 cv_status wait_until(unique_lock<mutex>& lock,
88 const chrono::time_point<Clock, Duration>& abs_time)
89 {
90 auto ret = aux::threading::condvar::wait_for(
91 cv_, *lock.mutex()->native_handle(), aux::time_until(abs_time)
92 );
93
94 if (ret == EOK)
95 return cv_status::no_timeout;
96 else
97 return cv_status::timeout;
98 }
99
100 template<class Clock, class Duration, class Predicate>
101 bool wait_until(unique_lock<mutex>& lock,
102 const chrono::time_point<Clock, Duration>& abs_time,
103 Predicate pred)
104 {
105 while (!pred())
106 {
107 if (wait_until(lock, abs_time) == cv_status::timeout)
108 return pred();
109 }
110
111 return true;
112 }
113
114 template<class Rep, class Period>
115 cv_status wait_for(unique_lock<mutex>& lock,
116 const chrono::duration<Rep, Period>& rel_time)
117 {
118 return wait_until(
119 lock, chrono::steady_clock::now() + rel_time
120 );
121 }
122
123 template<class Rep, class Period, class Predicate>
124 bool wait_for(unique_lock<mutex>& lock,
125 const chrono::duration<Rep, Period>& rel_time,
126 Predicate pred)
127 {
128 return wait_until(
129 lock, chrono::steady_clock::now() + rel_time,
130 move(pred)
131 );
132 }
133
134 using native_handle_type = aux::condvar_t*;
135 native_handle_type native_handle();
136
137 private:
138 aux::condvar_t cv_;
139 };
140
141 /**
142 * 30.5.2, class condition_variable_any:
143 */
144
145 class condition_variable_any
146 {
147 public:
148 condition_variable_any();
149 ~condition_variable_any();
150
151 condition_variable_any(const condition_variable_any&) = delete;
152 condition_variable_any& operator=(const condition_variable_any&) = delete;
153
154 void notify_one() noexcept;
155 void notify_all() noexcept;
156
157 template<class Lock>
158 void wait(Lock& lock)
159 {
160 aux::threading::condvar::wait(cv_, *lock.native_handle());
161 }
162
163 template<class Lock, class Predicate>
164 void wait(Lock& lock, Predicate pred)
165 {
166 while (!pred())
167 wait(lock);
168 }
169
170 template<class Lock, class Clock, class Duration>
171 cv_status wait_until(Lock& lock,
172 const chrono::time_point<Clock, Duration>& abs_time)
173 {
174 auto ret = aux::threading::condvar::wait_for(
175 cv_, *lock.mutex()->native_handle(), aux::time_until(abs_time)
176 );
177
178 if (ret == EOK)
179 return cv_status::no_timeout;
180 else
181 return cv_status::timeout;
182 }
183
184 template<class Lock, class Clock, class Duration, class Predicate>
185 bool wait_until(Lock& lock,
186 const chrono::time_point<Clock, Duration>& abs_time,
187 Predicate pred)
188 {
189 while (!pred())
190 {
191 if (wait_until(lock, abs_time) == cv_status::timeout)
192 return pred();
193 }
194
195 return true;
196 }
197
198 template<class Lock, class Rep, class Period>
199 cv_status wait_for(Lock& lock,
200 const chrono::duration<Rep, Period>& rel_time)
201 {
202 return wait_until(
203 lock, chrono::steady_clock::now() + rel_time
204 );
205 }
206
207 template<class Lock, class Rep, class Period, class Predicate>
208 bool wait_for(Lock& lock,
209 const chrono::duration<Rep, Period>& rel_time,
210 Predicate pred)
211 {
212 return wait_until(
213 lock, chrono::steady_clock::now() + rel_time,
214 move(pred)
215 );
216 }
217
218 using native_handle_type = aux::condvar_t*;
219 native_handle_type native_handle();
220
221 private:
222 aux::condvar_t cv_;
223 };
224
225 void notify_all_at_thread_exit(condition_variable&, unique_lock<mutex>&);
226}
227
228#endif
Note: See TracBrowser for help on using the repository browser.