1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // UNSUPPORTED: libcpp-has-no-threads
10 
11 // FLAKY_TEST.
12 
13 // <mutex>
14 
15 // class recursive_timed_mutex;
16 
17 // void lock();
18 
19 #include <mutex>
20 #include <thread>
21 #include <cstdlib>
22 #include <cassert>
23 
24 std::recursive_timed_mutex m;
25 
26 typedef std::chrono::system_clock Clock;
27 typedef Clock::time_point time_point;
28 typedef Clock::duration duration;
29 typedef std::chrono::milliseconds ms;
30 typedef std::chrono::nanoseconds ns;
31 
32 void f()
33 {
34     time_point t0 = Clock::now();
35     m.lock();
36     time_point t1 = Clock::now();
37     m.lock();
38     m.unlock();
39     m.unlock();
40     ns d = t1 - t0 - ms(250);
41     assert(d < ms(50));  // within 50ms
42 }
43 
44 int main(int, char**)
45 {
46     m.lock();
47     std::thread t(f);
48     std::this_thread::sleep_for(ms(250));
49     m.unlock();
50     t.join();
51 
52   return 0;
53 }
54