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++03, c++11 11 // ALLOW_RETRIES: 2 12 13 // dylib support for shared_mutex was added in macosx10.12 14 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}} 15 16 // <shared_mutex> 17 18 // template <class Mutex> class shared_lock; 19 20 // explicit shared_lock(mutex_type& m); 21 22 // template<class _Mutex> shared_lock(shared_lock<_Mutex>) 23 // -> shared_lock<_Mutex>; // C++17 24 25 #include <shared_mutex> 26 #include <thread> 27 #include <vector> 28 #include <cstdlib> 29 #include <cassert> 30 31 #include "make_test_thread.h" 32 #include "test_macros.h" 33 34 typedef std::chrono::system_clock Clock; 35 typedef Clock::time_point time_point; 36 typedef Clock::duration duration; 37 typedef std::chrono::milliseconds ms; 38 typedef std::chrono::nanoseconds ns; 39 40 ms WaitTime = ms(250); 41 42 // Thread sanitizer causes more overhead and will sometimes cause this test 43 // to fail. To prevent this we give Thread sanitizer more time to complete the 44 // test. 45 #if !defined(TEST_HAS_SANITIZERS) 46 ms Tolerance = ms(50); 47 #else 48 ms Tolerance = ms(50 * 5); 49 #endif 50 51 std::shared_timed_mutex m; 52 53 void f() 54 { 55 time_point t0 = Clock::now(); 56 time_point t1; 57 { 58 std::shared_lock<std::shared_timed_mutex> ul(m); 59 t1 = Clock::now(); 60 } 61 ns d = t1 - t0 - WaitTime; 62 assert(d < Tolerance); // within tolerance 63 } 64 65 void g() 66 { 67 time_point t0 = Clock::now(); 68 time_point t1; 69 { 70 std::shared_lock<std::shared_timed_mutex> ul(m); 71 t1 = Clock::now(); 72 } 73 ns d = t1 - t0; 74 assert(d < Tolerance); // within tolerance 75 } 76 77 int main(int, char**) 78 { 79 std::vector<std::thread> v; 80 { 81 m.lock(); 82 for (int i = 0; i < 5; ++i) 83 v.push_back(support::make_test_thread(f)); 84 std::this_thread::sleep_for(WaitTime); 85 m.unlock(); 86 for (auto& t : v) 87 t.join(); 88 } 89 { 90 m.lock_shared(); 91 for (auto& t : v) 92 t = support::make_test_thread(g); 93 std::thread q = support::make_test_thread(f); 94 std::this_thread::sleep_for(WaitTime); 95 m.unlock_shared(); 96 for (auto& t : v) 97 t.join(); 98 q.join(); 99 } 100 101 #if TEST_STD_VER >= 17 102 std::shared_lock sl(m); 103 static_assert((std::is_same<decltype(sl), std::shared_lock<decltype(m)>>::value), "" ); 104 #endif 105 106 return 0; 107 } 108