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 // void 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 
41 ms WaitTime = ms(250);
42 
43 // Thread sanitizer causes more overhead and will sometimes cause this test
44 // to fail. To prevent this we give Thread sanitizer more time to complete the
45 // test.
46 #if !TEST_HAS_FEATURE(thread_sanitizer)
47 ms Tolerance = ms(50);
48 #else
49 ms Tolerance = ms(100);
50 #endif
51 
52 
53 void f()
54 {
55     time_point t0 = Clock::now();
56     m.lock();
57     time_point t1 = Clock::now();
58     m.unlock();
59     ns d = t1 - t0 - ms(250);
60     assert(d < ms(50));  // within 50ms
61 }
62 
63 int main(int, char**)
64 {
65     m.lock();
66     std::thread t(f);
67     std::this_thread::sleep_for(ms(250));
68     m.unlock();
69     t.join();
70 
71   return 0;
72 }
73