1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: no-threads
10 // UNSUPPORTED: c++03, c++11
13 // XFAIL: availability-shared_mutex-missing
17 // template <class Mutex> class shared_lock;
25 #include <shared_mutex>
26 #include <system_error>
30 #include "make_test_thread.h"
31 #include "test_macros.h"
33 std::shared_timed_mutex m;
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;
41 ms WaitTime = ms(250);
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
46 #if !defined(TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT)
47 ms Tolerance = ms(25);
49 ms Tolerance = ms(25 * 5);
55 std::shared_lock<std::shared_timed_mutex> lk(m, std::defer_lock);
56 time_point t0 = Clock::now();
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
68 catch (std::system_error& e)
70 assert(e.code().value() == EDEADLK);
75 #ifndef TEST_HAS_NO_EXCEPTIONS
81 catch (std::system_error& e)
83 assert(e.code().value() == EPERM);
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);