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 // UNSUPPORTED: c++03, c++11
11 
12 // ALLOW_RETRIES: 2
13 
14 // shared_timed_mutex was introduced in macosx10.12
15 // UNSUPPORTED: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}
16 
17 // TODO(ldionne): This test fails on Ubuntu Focal on our CI nodes (and only there), in 32 bit mode.
18 // UNSUPPORTED: linux && 32bits-on-64bits
19 
20 // <shared_mutex>
21 
22 // class shared_timed_mutex;
23 
24 // bool try_lock();
25 
26 #include <shared_mutex>
27 #include <thread>
28 #include <cstdlib>
29 #include <cassert>
30 
31 #include "make_test_thread.h"
32 #include "test_macros.h"
33 
34 std::shared_timed_mutex m;
35 
36 typedef std::chrono::system_clock Clock;
37 typedef Clock::time_point time_point;
38 typedef Clock::duration duration;
39 typedef std::chrono::milliseconds ms;
40 typedef std::chrono::nanoseconds ns;
41 
42 void f()
43 {
44     time_point t0 = Clock::now();
45     assert(!m.try_lock());
46     assert(!m.try_lock());
47     assert(!m.try_lock());
48     while(!m.try_lock())
49         ;
50     time_point t1 = Clock::now();
51     m.unlock();
52     ns d = t1 - t0 - ms(250);
53     assert(d < ms(200));  // within 200ms
54 }
55 
56 int main(int, char**)
57 {
58     m.lock();
59     std::thread t = support::make_test_thread(f);
60     std::this_thread::sleep_for(ms(250));
61     m.unlock();
62     t.join();
63 
64   return 0;
65 }
66