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: no-threads
10 // UNSUPPORTED: c++03, c++11, c++14
11 
12 // ALLOW_RETRIES: 2
13 
14 // shared_mutex was introduced in macosx10.12
15 // UNSUPPORTED: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}
16 
17 // <shared_mutex>
18 
19 // class shared_mutex;
20 
21 // void lock_shared();
22 
23 #include <shared_mutex>
24 #include <thread>
25 #include <vector>
26 #include <cstdlib>
27 #include <cassert>
28 
29 #include "make_test_thread.h"
30 #include "test_macros.h"
31 
32 std::shared_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 ms WaitTime = ms(250);
41 
42 // Thread sanitizer causes more overhead and will sometimes cause this test
43 // to fail. To prevent this we give Thread sanitizer more time to complete the
44 // test.
45 #if !defined(TEST_HAS_SANITIZERS)
46 ms Tolerance = ms(50);
47 #else
48 ms Tolerance = ms(50 * 5);
49 #endif
50 
f()51 void f()
52 {
53     time_point t0 = Clock::now();
54     m.lock_shared();
55     time_point t1 = Clock::now();
56     m.unlock_shared();
57     ns d = t1 - t0 - WaitTime;
58     assert(d < Tolerance);  // within tolerance
59 }
60 
g()61 void g()
62 {
63     time_point t0 = Clock::now();
64     m.lock_shared();
65     time_point t1 = Clock::now();
66     m.unlock_shared();
67     ns d = t1 - t0;
68     assert(d < Tolerance);  // within tolerance
69 }
70 
71 
main(int,char **)72 int main(int, char**)
73 {
74     m.lock();
75     std::vector<std::thread> v;
76     for (int i = 0; i < 5; ++i)
77         v.push_back(support::make_test_thread(f));
78     std::this_thread::sleep_for(WaitTime);
79     m.unlock();
80     for (auto& t : v)
81         t.join();
82     m.lock_shared();
83     for (auto& t : v)
84         t = support::make_test_thread(g);
85     std::thread q = support::make_test_thread(f);
86     std::this_thread::sleep_for(WaitTime);
87     m.unlock_shared();
88     for (auto& t : v)
89         t.join();
90     q.join();
91 
92   return 0;
93 }
94