]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / condition_variable / members / 2.cc
1 // { dg-do run }
2 // { dg-options "-pthread" }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-effective-target pthread }
5 // { dg-require-gthreads "" }
6
7 // Copyright (C) 2008-2020 Free Software Foundation, Inc.
8 //
9 // This file is part of the GNU ISO C++ Library. This library is free
10 // software; you can redistribute it and/or modify it under the
11 // terms of the GNU General Public License as published by the
12 // Free Software Foundation; either version 3, or (at your option)
13 // any later version.
14
15 // This library is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19
20 // You should have received a copy of the GNU General Public License along
21 // with this library; see the file COPYING3. If not see
22 // <http://www.gnu.org/licenses/>.
23
24 #include <chrono>
25 #include <condition_variable>
26 #include <system_error>
27 #include <testsuite_hooks.h>
28 #include <slow_clock.h>
29
30 template <typename ClockType>
31 void test01()
32 {
33 try
34 {
35 std::chrono::microseconds ms(500);
36 std::condition_variable c1;
37 std::mutex m;
38 std::unique_lock<std::mutex> l(m);
39
40 auto then = ClockType::now();
41 std::cv_status result = c1.wait_until(l, then + ms);
42 VERIFY( result == std::cv_status::timeout );
43 VERIFY( (ClockType::now() - then) >= ms );
44 VERIFY( l.owns_lock() );
45 }
46 catch (const std::system_error& e)
47 {
48 VERIFY( false );
49 }
50 catch (...)
51 {
52 VERIFY( false );
53 }
54 }
55
56 void test01_alternate_clock()
57 {
58 try
59 {
60 std::condition_variable c1;
61 std::mutex m;
62 std::unique_lock<std::mutex> l(m);
63 auto const expire = slow_clock::now() + std::chrono::seconds(1);
64
65 while (slow_clock::now() < expire)
66 {
67 auto const result = c1.wait_until(l, expire);
68
69 // If wait_until returns before the timeout has expired when
70 // measured against the supplied clock, then wait_until must
71 // return no_timeout.
72 if (slow_clock::now() < expire)
73 VERIFY(result == std::cv_status::no_timeout);
74
75 // If wait_until returns timeout then the timeout must have
76 // expired.
77 if (result == std::cv_status::timeout)
78 VERIFY(slow_clock::now() >= expire);
79 }
80 }
81 catch (const std::system_error& e)
82 {
83 VERIFY( false );
84 }
85 catch (...)
86 {
87 VERIFY( false );
88 }
89 }
90
91 /* User defined clock that ticks in two-thousandths of a second
92 forty-two minutes ahead of steady_clock. */
93 struct user_defined_clock
94 {
95 typedef std::chrono::steady_clock::rep rep;
96 typedef std::ratio<1, 2000> period;
97 typedef std::chrono::duration<rep, period> duration;
98 typedef std::chrono::time_point<user_defined_clock> time_point;
99
100 static constexpr bool is_steady = true;
101
102 static time_point now() noexcept
103 {
104 using namespace std::chrono;
105 const auto steady_since_epoch = steady_clock::now().time_since_epoch();
106 const auto user_since_epoch = duration_cast<duration>(steady_since_epoch);
107 return time_point(user_since_epoch + minutes(42));
108 }
109 };
110
111 /*
112 It's not possible for this test to automatically ensure that the
113 system_clock test cases result in a wait on CLOCK_REALTIME and steady_clock
114 test cases result in a wait on CLOCK_MONOTONIC. It's recommended to run the
115 test under strace(1) and check whether the expected futex calls are made by
116 glibc. See https://gcc.gnu.org/ml/libstdc++/2019-09/msg00022.html for
117 instructions.
118 */
119
120 int main()
121 {
122 test01<std::chrono::steady_clock>();
123 test01<std::chrono::system_clock>();
124 test01<user_defined_clock>();
125 test01_alternate_clock();
126 }