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 // template <class Mutex> class shared_lock;
17 
18 // void lock();
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 ms WaitTime = ms(250);
37 
38 // Thread sanitizer causes more overhead and will sometimes cause this test
39 // to fail. To prevent this we give Thread sanitizer more time to complete the
40 // test.
41 #if !defined(TEST_HAS_SANITIZERS)
42 ms Tolerance = ms(25);
43 #else
44 ms Tolerance = ms(25 * 5);
45 #endif
46 
47 
48 void f()
49 {
50     std::shared_lock<std::shared_timed_mutex> lk(m, std::defer_lock);
51     time_point t0 = Clock::now();
52     lk.lock();
53     time_point t1 = Clock::now();
54     assert(lk.owns_lock() == true);
55     ns d = t1 - t0 - WaitTime;
56     assert(d < Tolerance);  // within tolerance
57 #ifndef TEST_HAS_NO_EXCEPTIONS
58     try
59     {
60         lk.lock();
61         assert(false);
62     }
63     catch (std::system_error& e)
64     {
65         assert(e.code().value() == EDEADLK);
66     }
67 #endif
68     lk.unlock();
69     lk.release();
70 #ifndef TEST_HAS_NO_EXCEPTIONS
71     try
72     {
73         lk.lock();
74         assert(false);
75     }
76     catch (std::system_error& e)
77     {
78         assert(e.code().value() == EPERM);
79     }
80 #endif
81 }
82 
83 int main()
84 {
85     m.lock();
86     std::vector<std::thread> v;
87     for (int i = 0; i < 5; ++i)
88         v.push_back(std::thread(f));
89     std::this_thread::sleep_for(WaitTime);
90     m.unlock();
91     for (auto& t : v)
92         t.join();
93 }
94