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++98, c++03, c++11
11 
12 // ALLOW_RETRIES: 2
13 
14 // shared_timed_mutex was introduced in macosx10.12
15 // UNSUPPORTED: with_system_cxx_lib=macosx10.11
16 // UNSUPPORTED: with_system_cxx_lib=macosx10.10
17 // UNSUPPORTED: with_system_cxx_lib=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 "test_macros.h"
31 
32 std::shared_timed_mutex m;
33 
34 typedef std::chrono::system_clock Clock;
35 typedef Clock::time_point time_point;
36 typedef Clock::duration duration;
37 typedef std::chrono::milliseconds ms;
38 typedef std::chrono::nanoseconds ns;
39 
40 void f()
41 {
42     time_point t0 = Clock::now();
43     assert(!m.try_lock());
44     assert(!m.try_lock());
45     assert(!m.try_lock());
46     while(!m.try_lock())
47         ;
48     time_point t1 = Clock::now();
49     m.unlock();
50     ns d = t1 - t0 - ms(250);
51     assert(d < ms(200));  // within 200ms
52 }
53 
54 int main(int, char**)
55 {
56     m.lock();
57     std::thread t(f);
58     std::this_thread::sleep_for(ms(250));
59     m.unlock();
60     t.join();
61 
62   return 0;
63 }
64