]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/30_threads/this_thread/60421.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / this_thread / 60421.cc
CommitLineData
a945c346 1// Copyright (C) 2015-2024 Free Software Foundation, Inc.
d1a74a28
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
d1236680
RO
18// { dg-do run }
19// { dg-options "-pthread" }
71c54f8e 20// { dg-require-effective-target c++11 }
d1236680 21// { dg-require-effective-target pthread }
d1a74a28 22// { dg-require-cstdint "" }
f55e699d 23// { dg-require-gthreads "" }
d1a74a28 24// { dg-require-time "" }
5bbb1f30 25// { dg-require-sleep "" }
d1a74a28
JW
26
27#include <thread>
28#include <chrono>
f55e699d
JW
29#include <atomic>
30#include <cstdint>
31#include <signal.h>
d1a74a28
JW
32#include <testsuite_hooks.h>
33
34void
35test01()
36{
37 std::this_thread::sleep_for(std::chrono::seconds(0));
38 std::this_thread::sleep_for(std::chrono::seconds(-1));
f55e699d
JW
39 std::this_thread::sleep_for(std::chrono::duration<std::uint64_t>::zero());
40}
41
42void
43test02()
44{
f55e699d
JW
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);
cfef4c32 57 sleeping = false;
f55e699d 58 });
cfef4c32
JW
59 while (!sleeping)
60 {
61 // Wait for the thread to start sleeping.
f129628e 62 sched_yield ();
cfef4c32
JW
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 }
f55e699d
JW
71 t.join();
72 VERIFY( result );
73}
74
75struct 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
90void
91test03()
92{
f55e699d
JW
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 );
d1a74a28
JW
97}
98
99int
100main()
101{
102 test01();
f55e699d
JW
103 test02();
104 test03();
d1a74a28 105}