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: 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 // template <class Rep, class Period>
24 //     bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
25 
26 #include <shared_mutex>
27 #include <thread>
28 #include <cstdlib>
29 #include <cassert>
30 
31 #include "test_macros.h"
32 
33 std::shared_timed_mutex m;
34 
35 typedef std::chrono::steady_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 
42 #if !TEST_SLOW_HOST()
43 ms WaitTime = ms(250);
44 #else
45 ms WaitTime = ms(750);
46 #endif
47 
48 // Thread sanitizer causes more overhead and will sometimes cause this test
49 // to fail. To prevent this we give Thread sanitizer more time to complete the
50 // test.
51 #if !defined(TEST_HAS_SANITIZERS) && !TEST_SLOW_HOST()
52 ms Tolerance = ms(50);
53 #else
54 ms Tolerance = ms(50 * 5);
55 #endif
56 
f1()57 void f1()
58 {
59     time_point t0 = Clock::now();
60     assert(m.try_lock_for(WaitTime + Tolerance) == true);
61     time_point t1 = Clock::now();
62     m.unlock();
63     ns d = t1 - t0 - WaitTime;
64     assert(d < Tolerance);  // within tolerance
65 }
66 
f2()67 void f2()
68 {
69     time_point t0 = Clock::now();
70     assert(m.try_lock_for(WaitTime) == false);
71     time_point t1 = Clock::now();
72     ns d = t1 - t0 - WaitTime;
73     assert(d < Tolerance);  // within tolerance
74 }
75 
main(int,char **)76 int main(int, char**)
77 {
78     {
79         m.lock();
80         std::thread t(f1);
81         std::this_thread::sleep_for(WaitTime);
82         m.unlock();
83         t.join();
84     }
85     {
86         m.lock();
87         std::thread t(f2);
88         std::this_thread::sleep_for(WaitTime + Tolerance);
89         m.unlock();
90         t.join();
91     }
92 
93   return 0;
94 }
95