1 // { dg-do run }
2 // { dg-options "-pthread"  }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-effective-target pthread }
5 
6 // Copyright (C) 2013-2019 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
nowclock42   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 
f()52 void f()
53 {
54   locked = mx.try_lock_until(clock::now() + C::milliseconds(1));
55 }
56 
main()57 int main()
58 {
59   std::lock_guard<std::timed_mutex> l(mx);
60   auto start = C::system_clock::now();
61   std::thread t(f);
62   t.join();
63   auto stop = C::system_clock::now();
64   VERIFY( (stop - start) < C::seconds(9) );
65   VERIFY( !locked );
66 }
67