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