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 && x86_64-apple-macosx10.11
16 // UNSUPPORTED: use_system_cxx_lib && x86_64-apple-macosx10.10
17 // UNSUPPORTED: use_system_cxx_lib && x86_64-apple-macosx10.9
18 
19 // <shared_mutex>
20 
21 // class shared_timed_mutex;
22 
23 // bool try_lock();
24 
25 #include <shared_mutex>
26 #include <thread>
27 #include <cstdlib>
28 #include <cassert>
29 
30 #include "make_test_thread.h"
31 #include "test_macros.h"
32 
33 std::shared_timed_mutex m;
34 
35 typedef std::chrono::system_clock Clock;
36 typedef Clock::time_point time_point;
37 typedef Clock::duration duration;
38 typedef std::chrono::milliseconds ms;
39 typedef std::chrono::nanoseconds ns;
40 
41 void f()
42 {
43     time_point t0 = Clock::now();
44     assert(!m.try_lock());
45     assert(!m.try_lock());
46     assert(!m.try_lock());
47     while(!m.try_lock())
48         ;
49     time_point t1 = Clock::now();
50     m.unlock();
51     ns d = t1 - t0 - ms(250);
52     assert(d < ms(200));  // within 200ms
53 }
54 
55 int main(int, char**)
56 {
57     m.lock();
58     std::thread t = support::make_test_thread(f);
59     std::this_thread::sleep_for(ms(250));
60     m.unlock();
61     t.join();
62 
63   return 0;
64 }
65