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 // <mutex>
12 
13 // class recursive_timed_mutex;
14 
15 // bool try_lock();
16 
17 #include <mutex>
18 #include <thread>
19 #include <cstdlib>
20 #include <cassert>
21 
22 std::recursive_timed_mutex m;
23 
24 typedef std::chrono::system_clock Clock;
25 typedef Clock::time_point time_point;
26 typedef Clock::duration duration;
27 typedef std::chrono::milliseconds ms;
28 typedef std::chrono::nanoseconds ns;
29 
30 void f()
31 {
32     time_point t0 = Clock::now();
33     assert(!m.try_lock());
34     assert(!m.try_lock());
35     assert(!m.try_lock());
36     while(!m.try_lock())
37         ;
38     time_point t1 = Clock::now();
39     assert(m.try_lock());
40     m.unlock();
41     m.unlock();
42     ns d = t1 - t0 - ms(250);
43     assert(d < ms(200));  // within 200ms
44 }
45 
46 int main(int, char**)
47 {
48     m.lock();
49     std::thread t(f);
50     std::this_thread::sleep_for(ms(250));
51     m.unlock();
52     t.join();
53 
54   return 0;
55 }
56