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: libcpp-has-no-threads
10 // UNSUPPORTED: c++03, c++11
12 // dylib support for shared_mutex was added in macosx10.12
13 // XFAIL: with_system_cxx_lib=macosx10.11
14 // XFAIL: with_system_cxx_lib=macosx10.10
15 // XFAIL: with_system_cxx_lib=macosx10.9
21 // template <class Mutex> class shared_lock;
25 #include <shared_mutex>
31 #include "make_test_thread.h"
32 #include "test_macros.h"
34 std::shared_timed_mutex m;
36 typedef std::chrono::system_clock Clock;
37 typedef Clock::time_point time_point;
38 typedef Clock::duration duration;
39 typedef std::chrono::milliseconds ms;
40 typedef std::chrono::nanoseconds ns;
42 ms WaitTime = ms(250);
44 // Thread sanitizer causes more overhead and will sometimes cause this test
45 // to fail. To prevent this we give Thread sanitizer more time to complete the
47 #if !defined(TEST_HAS_SANITIZERS)
48 ms Tolerance = ms(25);
50 ms Tolerance = ms(25 * 5);
56 std::shared_lock<std::shared_timed_mutex> lk(m, std::defer_lock);
57 time_point t0 = Clock::now();
59 time_point t1 = Clock::now();
60 assert(lk.owns_lock() == true);
61 ns d = t1 - t0 - WaitTime;
62 assert(d < Tolerance); // within tolerance
63 #ifndef TEST_HAS_NO_EXCEPTIONS
69 catch (std::system_error& e)
71 assert(e.code().value() == EDEADLK);
76 #ifndef TEST_HAS_NO_EXCEPTIONS
82 catch (std::system_error& e)
84 assert(e.code().value() == EPERM);
92 std::vector<std::thread> v;
93 for (int i = 0; i < 5; ++i)
94 v.push_back(support::make_test_thread(f));
95 std::this_thread::sleep_for(WaitTime);