e7d24621e02254fbb4dfa8ed577a8c8dcbfc8cef
[llvm-toolchain-19.git] /
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
11 // ALLOW_RETRIES: 2
12
13 // XFAIL: availability-shared_mutex-missing
14
15 // <shared_mutex>
16
17 // template <class Mutex> class shared_lock;
18
19 // void lock();
20
21 #include <cassert>
22 #include <chrono>
23 #include <cstdlib>
24 #include <mutex>
25 #include <shared_mutex>
26 #include <system_error>
27 #include <thread>
28 #include <vector>
29
30 #include "make_test_thread.h"
31 #include "test_macros.h"
32
33 std::shared_timed_mutex m;
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_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT)
47 ms Tolerance = ms(25);
48 #else
49 ms Tolerance = ms(25 * 5);
50 #endif
51
52
53 void f()
54 {
55     std::shared_lock<std::shared_timed_mutex> lk(m, std::defer_lock);
56     time_point t0 = Clock::now();
57     lk.lock();
58     time_point t1 = Clock::now();
59     assert(lk.owns_lock() == true);
60     ns d = t1 - t0 - WaitTime;
61     assert(d < Tolerance);  // within tolerance
62 #ifndef TEST_HAS_NO_EXCEPTIONS
63     try
64     {
65         lk.lock();
66         assert(false);
67     }
68     catch (std::system_error& e)
69     {
70         assert(e.code().value() == EDEADLK);
71     }
72 #endif
73     lk.unlock();
74     lk.release();
75 #ifndef TEST_HAS_NO_EXCEPTIONS
76     try
77     {
78         lk.lock();
79         assert(false);
80     }
81     catch (std::system_error& e)
82     {
83         assert(e.code().value() == EPERM);
84     }
85 #endif
86 }
87
88 int main(int, char**)
89 {
90     m.lock();
91     std::vector<std::thread> v;
92     for (int i = 0; i < 5; ++i)
93         v.push_back(support::make_test_thread(f));
94     std::this_thread::sleep_for(WaitTime);
95     m.unlock();
96     for (auto& t : v)
97         t.join();
98
99   return 0;
100 }