]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/condition_variable/members/2.cc
2 // { dg-options "-pthread" }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-effective-target pthread }
5 // { dg-require-gthreads "" }
7 // Copyright (C) 2008-2020 Free Software Foundation, Inc.
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)
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.
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/>.
25 #include <condition_variable>
26 #include <system_error>
27 #include <testsuite_hooks.h>
28 #include <slow_clock.h>
30 template <typename ClockType
>
35 std::chrono::microseconds
ms(500);
36 std::condition_variable c1
;
38 std::unique_lock
<std::mutex
> l(m
);
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() );
46 catch (const std::system_error
& e
)
56 void test01_alternate_clock()
60 std::condition_variable c1
;
62 std::unique_lock
<std::mutex
> l(m
);
63 auto const expire
= slow_clock::now() + std::chrono::seconds(1);
65 while (slow_clock::now() < expire
)
67 auto const result
= c1
.wait_until(l
, expire
);
69 // If wait_until returns before the timeout has expired when
70 // measured against the supplied clock, then wait_until must
72 if (slow_clock::now() < expire
)
73 VERIFY(result
== std::cv_status::no_timeout
);
75 // If wait_until returns timeout then the timeout must have
77 if (result
== std::cv_status::timeout
)
78 VERIFY(slow_clock::now() >= expire
);
81 catch (const std::system_error
& e
)
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
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
;
100 static constexpr bool is_steady
= true;
102 static time_point
now() noexcept
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));
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
122 test01
<std::chrono::steady_clock
>();
123 test01
<std::chrono::system_clock
>();
124 test01
<user_defined_clock
>();
125 test01_alternate_clock();