| 1 | // Copyright (C) 2017-2025 Free Software Foundation, Inc. |
| 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 } |
| 19 | // { dg-additional-options "-pthread" { target pthread } } |
| 20 | // { dg-require-effective-target c++11 } |
| 21 | // { dg-require-gthreads "" } |
| 22 | |
| 23 | #include <condition_variable> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | // PR libstdc++/68519 |
| 27 | |
| 28 | void |
| 29 | test_wait_for() |
| 30 | { |
| 31 | std::mutex mx; |
| 32 | std::condition_variable cv; |
| 33 | |
| 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(); |
| 38 | cv.wait_for(l, std::chrono::duration<float>(1), [] { return false; }); |
| 39 | auto t = std::chrono::system_clock::now(); |
| 40 | VERIFY( (t - start) >= std::chrono::seconds(1) ); |
| 41 | } |
| 42 | } |
| 43 | |
| 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. |
| 48 | struct recent_epoch_float_clock |
| 49 | { |
| 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>; |
| 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 | |
| 66 | const std::chrono::steady_clock::time_point recent_epoch_float_clock::epoch = |
| 67 | std::chrono::steady_clock::now(); |
| 68 | |
| 69 | void |
| 70 | test_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 | |
| 92 | int |
| 93 | main() |
| 94 | { |
| 95 | test_wait_for(); |
| 96 | test_wait_until(); |
| 97 | } |