Debian Codegraph @

libstdc++-v3/testsuite/30_threads/async/async.cc

raw
1 // { dg-do run }
2 // { dg-additional-options "-pthread" { target pthread } }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-gthreads "" }
5
6 // Copyright (C) 2010-2025 Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
22
23 #include <iostream>
24
25 #include <future>
26 #include <thread>
27 #include <testsuite_hooks.h>
28
29 using namespace std;
30
31 void work(mutex& m)
32 {
33 unique_lock<mutex> l(m);
34 }
35
36 void test01()
37 {
38 mutex m;
39 unique_lock<mutex> l(m);
40 future<void> f1 = async(launch::async, &work, ref(m));
41 l.unlock(); // allow async thread to proceed
42 f1.get(); // wait for it to finish
43 }
44
45 void test02()
46 {
47 mutex m;
48 unique_lock<mutex> l(m);
49 future<void> f1 = async(launch::async, &work, ref(m));
50 std::future_status status;
51 status = f1.wait_for(std::chrono::milliseconds(1));
52 VERIFY( status == std::future_status::timeout );
53 status = f1.wait_until(std::chrono::system_clock::now());
54 VERIFY( status == std::future_status::timeout );
55 status = f1.wait_until(std::chrono::steady_clock::now());
56 VERIFY( status == std::future_status::timeout );
57 l.unlock(); // allow async thread to proceed
58 f1.wait(); // wait for it to finish
59 status = f1.wait_for(std::chrono::milliseconds(0));
60 VERIFY( status == std::future_status::ready );
61 status = f1.wait_until(std::chrono::system_clock::now());
62 VERIFY( status == std::future_status::ready );
63 status = f1.wait_until(std::chrono::steady_clock::now());
64 VERIFY( status == std::future_status::ready );
65 }
66
67 // This clock behaves exactly the same as steady_clock, but it is not
68 // steady_clock which means that the generic clock overload of
69 // future::wait_until is used.
70 struct steady_clock_copy
71 {
72 using rep = std::chrono::steady_clock::rep;
73 using period = std::chrono::steady_clock::period;
74 using duration = std::chrono::steady_clock::duration;
75 using time_point = std::chrono::time_point<steady_clock_copy, duration>;
76 static constexpr bool is_steady = true;
77
78 static time_point now()
79 {
80 const auto steady = std::chrono::steady_clock::now();
81 return time_point{steady.time_since_epoch()};
82 }
83 };
84
85 // This test is prone to failures if run on a loaded machine where the
86 // kernel decides not to schedule us for several seconds. It also
87 // assumes that no-one will warp CLOCK whilst the test is
88 // running when CLOCK is std::chrono::system_clock.
89 template<typename CLOCK>
90 void test03()
91 {
92 auto const start = CLOCK::now();
93 future<void> f1 = async(launch::async, []() {
94 std::this_thread::sleep_for(std::chrono::seconds(2));
95 });
96 std::future_status status;
97
98 status = f1.wait_for(std::chrono::milliseconds(500));
99 VERIFY( status == std::future_status::timeout );
100
101 status = f1.wait_until(start + std::chrono::seconds(1));
102 VERIFY( status == std::future_status::timeout );
103
104 status = f1.wait_until(start + std::chrono::seconds(5));
105 VERIFY( status == std::future_status::ready );
106
107 auto const elapsed = CLOCK::now() - start;
108 VERIFY( elapsed >= std::chrono::seconds(2) );
109 VERIFY( elapsed < std::chrono::seconds(5) );
110 }
111
112 // This clock is supposed to run at a tenth of normal speed, but we
113 // don't have to worry about rounding errors causing us to wake up
114 // slightly too early below if we actually run it at an eleventh of
115 // normal speed. It is used to exercise the
116 // __atomic_futex_unsigned::_M_load_when_equal_until overload that
117 // takes an arbitrary clock.
118 struct slow_clock
119 {
120 using rep = std::chrono::steady_clock::rep;
121 using period = std::chrono::steady_clock::period;
122 using duration = std::chrono::steady_clock::duration;
123 using time_point = std::chrono::time_point<slow_clock, duration>;
124 static constexpr bool is_steady = true;
125
126 static time_point now()
127 {
128 const auto steady = std::chrono::steady_clock::now();
129 return time_point{steady.time_since_epoch() / 11};
130 }
131 };
132
133 void test04()
134 {
135 using namespace std::chrono;
136
137 auto const steady_start = steady_clock::now();
138 auto const slow_start = slow_clock::now();
139 future<void> f1 = async(launch::async, []() {
140 std::this_thread::sleep_for(std::chrono::seconds(2));
141 });
142
143 // Wait for ~1s
144 {
145 auto const status = f1.wait_until(slow_start + milliseconds(100));
146 VERIFY(status == std::future_status::timeout);
147 auto const elapsed = steady_clock::now() - steady_start;
148 if (elapsed < seconds(1))
149 std::cout << elapsed.count () << "ns < 1s" << std::endl;
150 VERIFY(elapsed >= seconds(1));
151 VERIFY(elapsed < seconds(2));
152 }
153
154 // Wait for up to ~4s more, but since the async sleep completes, the
155 // actual wait may be shorter than 1s. Tolerate 3s because 2s
156 // hasn't been enough in some extreme cases.
157 {
158 auto const steady_begin = steady_clock::now();
159 auto const status = f1.wait_until(slow_start + milliseconds(500));
160 VERIFY(status == std::future_status::ready);
161 auto const elapsed = steady_clock::now() - steady_begin;
162 if (elapsed >= seconds(3))
163 std::cout << elapsed.count () << "ns > 2s" << std::endl;
164 VERIFY(elapsed < seconds(3));
165 }
166 }
167
168 void test_pr91486_wait_for()
169 {
170 future<void> f1 = async(launch::async, []() {
171 std::this_thread::sleep_for(std::chrono::seconds(1));
172 });
173
174 std::chrono::duration<float> const wait_time = std::chrono::seconds(1);
175 auto const start_steady = chrono::steady_clock::now();
176 auto status __attribute__ ((__unused__)) = f1.wait_for(wait_time);
177 auto const elapsed_steady = chrono::steady_clock::now() - start_steady;
178
179 VERIFY( elapsed_steady >= std::chrono::seconds(1) );
180 }
181
182 // This is a clock with a very recent epoch which ensures that the difference
183 // between now() and one second in the future is representable in a float so
184 // that when the generic clock version of
185 // __atomic_futex_unsigned::_M_load_when_equal_until calculates the delta it
186 // gets a duration of 1.0f. When chrono::steady_clock has moved sufficiently
187 // far from its epoch (about 208.5 days in my testing - about 2^54ns because
188 // there's a promotion to double happening too somewhere) adding 1.0f to the
189 // current time has no effect. Using this clock ensures that
190 // __atomic_futex_unsigned::_M_load_when_equal_until is using
191 // chrono::__detail::ceil correctly so that the function actually sleeps rather
192 // than spinning.
193 struct float_steady_clock
194 {
195 using duration = std::chrono::duration<float>;
196 using rep = typename duration::rep;
197 using period = typename duration::period;
198 using time_point = std::chrono::time_point<float_steady_clock, duration>;
199 static constexpr bool is_steady = true;
200
201 static chrono::steady_clock::time_point epoch;
202 static int call_count;
203
204 static time_point now()
205 {
206 ++call_count;
207 auto real = std::chrono::steady_clock::now();
208 return time_point{real - epoch};
209 }
210 };
211
212 chrono::steady_clock::time_point float_steady_clock::epoch;
213 int float_steady_clock::call_count = 0;
214
215 void test_pr91486_wait_until()
216 {
217 future<void> f1 = async(launch::async, []() {
218 std::this_thread::sleep_for(std::chrono::seconds(1));
219 });
220
221 // When we don't _GLIBCXX_HAVE_LINUX_FUTEX, we use
222 // condition_variables, whose wait_until converts times using
223 // deltas, and if too much time has elapsed since we set the epoch
224 // during program initialization, say if the other tests took over
225 // 8s and we're unlucky with the numbers, we may lose enough
226 // precision from the 1s delta that we don't sleep until the
227 // deadline, and then we may loop more times than expected. Each
228 // iteration will recompute the wait time from deadline -
229 // float_steady_clock::now(), and each such computation will bump
230 // float_steady_clock::call_count, so the call_count check below
231 // will fail spuriously. Setting the epoch just before running this
232 // test makes this failure mode far less likely.
233 float_steady_clock::epoch = chrono::steady_clock::now();
234 float_steady_clock::time_point const now = float_steady_clock::now();
235
236 std::chrono::duration<float> const wait_time = std::chrono::seconds(1);
237 float_steady_clock::time_point const expire = now + wait_time;
238 VERIFY( expire > now );
239
240 auto const start_steady = chrono::steady_clock::now();
241 auto status __attribute__ ((__unused__)) = f1.wait_until(expire);
242 auto const elapsed_steady = chrono::steady_clock::now() - start_steady;
243
244 // This checks that we didn't come back too soon
245 VERIFY( elapsed_steady >= std::chrono::seconds(1) );
246
247 // This checks that wait_until didn't busy wait checking the clock more times
248 // than necessary.
249 VERIFY( float_steady_clock::call_count <= 3 );
250 }
251
252 int main()
253 {
254 test01();
255 test02();
256 test03<std::chrono::system_clock>();
257 test03<std::chrono::steady_clock>();
258 test03<steady_clock_copy>();
259 test04();
260 test_pr91486_wait_for();
261 test_pr91486_wait_until();
262 return 0;
263 }