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 // dylib support for shared_mutex was added in macosx10.12
13 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}
14 
15 // ALLOW_RETRIES: 2
16 
17 // <shared_mutex>
18 
19 // template <class Mutex> class shared_lock;
20 
21 // explicit shared_lock(mutex_type& m);
22 
23 // template<class _Mutex> shared_lock(shared_lock<_Mutex>)
24 //     -> shared_lock<_Mutex>;  // C++17
25 
26 #include <shared_mutex>
27 #include <thread>
28 #include <vector>
29 #include <cstdlib>
30 #include <cassert>
31 
32 #include "make_test_thread.h"
33 #include "test_macros.h"
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 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 !defined(TEST_HAS_SANITIZERS)
47 ms Tolerance = ms(50);
48 #else
49 ms Tolerance = ms(50 * 5);
50 #endif
51 
52 std::shared_timed_mutex m;
53 
54 void f()
55 {
56     time_point t0 = Clock::now();
57     time_point t1;
58     {
59     std::shared_lock<std::shared_timed_mutex> ul(m);
60     t1 = Clock::now();
61     }
62     ns d = t1 - t0 - WaitTime;
63     assert(d < Tolerance);  // within tolerance
64 }
65 
66 void g()
67 {
68     time_point t0 = Clock::now();
69     time_point t1;
70     {
71     std::shared_lock<std::shared_timed_mutex> ul(m);
72     t1 = Clock::now();
73     }
74     ns d = t1 - t0;
75     assert(d < Tolerance);  // within tolerance
76 }
77 
78 int main(int, char**)
79 {
80     std::vector<std::thread> v;
81     {
82         m.lock();
83         for (int i = 0; i < 5; ++i)
84             v.push_back(support::make_test_thread(f));
85         std::this_thread::sleep_for(WaitTime);
86         m.unlock();
87         for (auto& t : v)
88             t.join();
89     }
90     {
91         m.lock_shared();
92         for (auto& t : v)
93             t = support::make_test_thread(g);
94         std::thread q = support::make_test_thread(f);
95         std::this_thread::sleep_for(WaitTime);
96         m.unlock_shared();
97         for (auto& t : v)
98             t.join();
99         q.join();
100     }
101 
102 #if TEST_STD_VER >= 17
103     std::shared_lock sl(m);
104     static_assert((std::is_same<decltype(sl), std::shared_lock<decltype(m)>>::value), "" );
105 #endif
106 
107   return 0;
108 }
109