]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/30_threads/this_thread/60421.cc
Elide repeated RTL elements.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / this_thread / 60421.cc
CommitLineData
85ec4feb 1// Copyright (C) 2015-2018 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
JW
24// { dg-require-time "" }
25
26#include <thread>
27#include <chrono>
f55e699d
JW
28#include <atomic>
29#include <cstdint>
30#include <signal.h>
d1a74a28
JW
31#include <testsuite_hooks.h>
32
33void
34test01()
35{
36 std::this_thread::sleep_for(std::chrono::seconds(0));
37 std::this_thread::sleep_for(std::chrono::seconds(-1));
f55e699d
JW
38 std::this_thread::sleep_for(std::chrono::duration<std::uint64_t>::zero());
39}
40
41void
42test02()
43{
f55e699d
JW
44 // test interruption of this_thread::sleep_for() by a signal
45 struct sigaction sa{ };
46 sa.sa_handler = +[](int) { };
47 sigaction(SIGUSR1, &sa, 0);
48 bool result = false;
49 std::atomic<bool> sleeping{false};
50 std::thread t([&result, &sleeping] {
51 auto start = std::chrono::system_clock::now();
52 auto time = std::chrono::seconds(3);
53 sleeping = true;
54 std::this_thread::sleep_for(time);
55 result = std::chrono::system_clock::now() >= (start + time);
56 });
57 while (!sleeping) { }
58 std::this_thread::sleep_for(std::chrono::milliseconds(500));
59 pthread_kill(t.native_handle(), SIGUSR1);
60 t.join();
61 VERIFY( result );
62}
63
64struct slow_clock
65{
66 using rep = std::chrono::system_clock::rep;
67 using period = std::chrono::system_clock::period;
68 using duration = std::chrono::system_clock::duration;
69 using time_point = std::chrono::time_point<slow_clock, duration>;
70 static constexpr bool is_steady = false;
71
72 static time_point now()
73 {
74 auto real = std::chrono::system_clock::now();
75 return time_point{real.time_since_epoch() / 2};
76 }
77};
78
79void
80test03()
81{
f55e699d
JW
82 // test that this_thread::sleep_until() handles clock adjustments
83 auto when = slow_clock::now() + std::chrono::seconds(2);
84 std::this_thread::sleep_until(when);
85 VERIFY( slow_clock::now() >= when );
d1a74a28
JW
86}
87
88int
89main()
90{
91 test01();
f55e699d
JW
92 test02();
93 test03();
d1a74a28 94}