]> 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
99dee823 1// Copyright (C) 2015-2021 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.
62 }
63 while (sleeping)
64 {
65 // The sleeping thread should finish eventually,
66 // even if continually interrupted after less than a second:
67 std::this_thread::sleep_for(std::chrono::milliseconds(500));
68 pthread_kill(t.native_handle(), SIGUSR1);
69 }
f55e699d
JW
70 t.join();
71 VERIFY( result );
72}
73
74struct slow_clock
75{
76 using rep = std::chrono::system_clock::rep;
77 using period = std::chrono::system_clock::period;
78 using duration = std::chrono::system_clock::duration;
79 using time_point = std::chrono::time_point<slow_clock, duration>;
80 static constexpr bool is_steady = false;
81
82 static time_point now()
83 {
84 auto real = std::chrono::system_clock::now();
85 return time_point{real.time_since_epoch() / 2};
86 }
87};
88
89void
90test03()
91{
f55e699d
JW
92 // test that this_thread::sleep_until() handles clock adjustments
93 auto when = slow_clock::now() + std::chrono::seconds(2);
94 std::this_thread::sleep_until(when);
95 VERIFY( slow_clock::now() >= when );
d1a74a28
JW
96}
97
98int
99main()
100{
101 test01();
f55e699d
JW
102 test02();
103 test03();
d1a74a28 104}