Debian Codegraph @

libstdc++-v3/testsuite/30_threads/timed_mutex/try_lock_until/57641.cc

raw
1 // { dg-do run }
2 // { dg-additional-options "-pthread" { target pthread } }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-gthreads "" }
5
6 // Copyright (C) 2013-2025 Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
22
23 #include <mutex>
24 #include <chrono>
25 #include <thread>
26 #include <testsuite_hooks.h>
27
28 // PR libstdc++/57641
29
30 namespace C = std::chrono;
31
32 // custom clock with epoch 10s before system_clock's
33 struct clock
34 {
35 typedef C::system_clock::rep rep;
36 typedef C::system_clock::period period;
37 typedef C::system_clock::duration duration;
38 typedef C::time_point<clock> time_point;
39 static constexpr bool is_steady = C::system_clock::is_steady;
40
41 static time_point
42 now()
43 {
44 auto sys_time = C::system_clock::now().time_since_epoch();
45 return time_point(sys_time + C::seconds(10));
46 }
47 };
48
49 std::timed_mutex mx;
50 bool locked = false;
51
52 template <typename ClockType>
53 void f()
54 {
55 locked = mx.try_lock_until(ClockType::now() + C::milliseconds(1));
56 }
57
58 template <typename ClockType>
59 void test()
60 {
61 std::lock_guard<std::timed_mutex> l(mx);
62 auto start = ClockType::now();
63 std::thread t(f<ClockType>);
64 t.join();
65 auto stop = ClockType::now();
66 VERIFY( (stop - start) < C::seconds(9) );
67 VERIFY( !locked );
68 }
69
70 int main()
71 {
72 test<C::system_clock>();
73 test<C::steady_clock>();
74 }