| 1 |
// Copyright (C) 2015-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-options "-pthread" } |
| 20 |
// { dg-require-effective-target c++11 } |
| 21 |
// { dg-require-effective-target pthread } |
| 22 |
// { dg-require-cstdint "" } |
| 23 |
// { dg-require-gthreads "" } |
| 24 |
// { dg-require-time "" } |
| 25 |
// { dg-require-sleep "" } |
| 26 |
|
| 27 |
#include <thread> |
| 28 |
#include <chrono> |
| 29 |
#include <atomic> |
| 30 |
#include <cstdint> |
| 31 |
#include <signal.h> |
| 32 |
#include <testsuite_hooks.h> |
| 33 |
|
| 34 |
void |
| 35 |
test01() |
| 36 |
{ |
| 37 |
std::this_thread::sleep_for(std::chrono::seconds(0)); |
| 38 |
std::this_thread::sleep_for(std::chrono::seconds(-1)); |
| 39 |
std::this_thread::sleep_for(std::chrono::duration<std::uint64_t>::zero()); |
| 40 |
} |
| 41 |
|
| 42 |
void |
| 43 |
test02() |
| 44 |
{ |
| 45 |
// test interruption of this_thread::sleep_for() by a signal |
| 46 |
struct sigaction sa{ }; |
| 47 |
sa.sa_handler = +[](int) { }; |
| 48 |
sigaction(SIGUSR1, &sa, 0); |
| 49 |
bool result = false; |
| 50 |
std::atomic<bool> sleeping{false}; |
| 51 |
std::thread t([&result, &sleeping] { |
| 52 |
auto start = std::chrono::system_clock::now(); |
| 53 |
auto time = std::chrono::seconds(3); |
| 54 |
sleeping = true; |
| 55 |
std::this_thread::sleep_for(time); |
| 56 |
result = std::chrono::system_clock::now() >= (start + time); |
| 57 |
sleeping = false; |
| 58 |
}); |
| 59 |
while (!sleeping) |
| 60 |
{ |
| 61 |
// Wait for the thread to start sleeping. |
| 62 |
sched_yield (); |
| 63 |
} |
| 64 |
while (sleeping) |
| 65 |
{ |
| 66 |
// The sleeping thread should finish eventually, |
| 67 |
// even if continually interrupted after less than a second: |
| 68 |
std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 69 |
pthread_kill(t.native_handle(), SIGUSR1); |
| 70 |
} |
| 71 |
t.join(); |
| 72 |
VERIFY( result ); |
| 73 |
} |
| 74 |
|
| 75 |
struct slow_clock |
| 76 |
{ |
| 77 |
using rep = std::chrono::system_clock::rep; |
| 78 |
using period = std::chrono::system_clock::period; |
| 79 |
using duration = std::chrono::system_clock::duration; |
| 80 |
using time_point = std::chrono::time_point<slow_clock, duration>; |
| 81 |
static constexpr bool is_steady = false; |
| 82 |
|
| 83 |
static time_point now() |
| 84 |
{ |
| 85 |
auto real = std::chrono::system_clock::now(); |
| 86 |
return time_point{real.time_since_epoch() / 2}; |
| 87 |
} |
| 88 |
}; |
| 89 |
|
| 90 |
void |
| 91 |
test03() |
| 92 |
{ |
| 93 |
// test that this_thread::sleep_until() handles clock adjustments |
| 94 |
auto when = slow_clock::now() + std::chrono::seconds(2); |
| 95 |
std::this_thread::sleep_until(when); |
| 96 |
VERIFY( slow_clock::now() >= when ); |
| 97 |
} |
| 98 |
|
| 99 |
int |
| 100 |
main() |
| 101 |
{ |
| 102 |
test01(); |
| 103 |
test02(); |
| 104 |
test03(); |
| 105 |
} |