]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/30_threads/condition_variable/members/68519.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / condition_variable / members / 68519.cc
CommitLineData
83ffe9cd 1// Copyright (C) 2017-2023 Free Software Foundation, Inc.
83fd5e73
JW
2//
3// This file is part of the GNU ISO C++ Library. This library is free
4// software; you can redistribute it and/or modify it under the
5// terms of the GNU General Public License as published by the
6// Free Software Foundation; either version 3, or (at your option)
7// any later version.
8
9// This library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License along
15// with this library; see the file COPYING3. If not see
16// <http://www.gnu.org/licenses/>.
17
18// { dg-do run }
1e42d2f4 19// { dg-additional-options "-pthread" { target pthread } }
83fd5e73 20// { dg-require-effective-target c++11 }
83fd5e73
JW
21// { dg-require-gthreads "" }
22
23#include <condition_variable>
24#include <testsuite_hooks.h>
25
26// PR libstdc++/68519
27
83fd5e73 28void
e05ff300 29test_wait_for()
83fd5e73 30{
e05ff300
MC
31 std::mutex mx;
32 std::condition_variable cv;
33
83fd5e73
JW
34 for (int i = 0; i < 3; ++i)
35 {
36 std::unique_lock<std::mutex> l(mx);
37 auto start = std::chrono::system_clock::now();
e05ff300 38 cv.wait_for(l, std::chrono::duration<float>(1), [] { return false; });
83fd5e73
JW
39 auto t = std::chrono::system_clock::now();
40 VERIFY( (t - start) >= std::chrono::seconds(1) );
41 }
42}
43
e05ff300
MC
44// In order to ensure that the delta calculated in the arbitrary clock overload
45// of condition_variable::wait_until fits accurately in a float, but the result
46// of adding it to steady_clock with a float duration does not, this clock
47// needs to use a more recent epoch.
48struct recent_epoch_float_clock
49{
0a74a0e1
JW
50 using duration = std::chrono::duration<float>;
51 using rep = duration::rep;
52 using period = duration::period;
53 using time_point
54 = std::chrono::time_point<recent_epoch_float_clock, duration>;
e05ff300
MC
55 static constexpr bool is_steady = true;
56
57 static const std::chrono::steady_clock::time_point epoch;
58
59 static time_point now()
60 {
61 const auto steady = std::chrono::steady_clock::now();
62 return time_point{steady - epoch};
63 }
64};
65
66const std::chrono::steady_clock::time_point recent_epoch_float_clock::epoch =
67 std::chrono::steady_clock::now();
68
69void
70test_wait_until()
71{
72 using clock = recent_epoch_float_clock;
73
74 std::mutex mx;
75 std::condition_variable cv;
76
77 for (int i = 0; i < 3; ++i)
78 {
79 std::unique_lock<std::mutex> l(mx);
80 const auto start = clock::now();
81 const auto wait_time = start + std::chrono::duration<float>{1.0};
82
83 // In theory we could get a spurious wakeup, but in practice we won't.
84 const auto result = cv.wait_until(l, wait_time);
85
86 VERIFY( result == std::cv_status::timeout );
87 const auto elapsed = clock::now() - start;
88 VERIFY( elapsed >= std::chrono::seconds(1) );
89 }
90}
91
83fd5e73
JW
92int
93main()
94{
e05ff300
MC
95 test_wait_for();
96 test_wait_until();
83fd5e73 97}