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 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 16 17 // ALLOW_RETRIES: 2 18 19 // <shared_mutex> 20 21 // template <class Mutex> class shared_lock; 22 23 // void lock(); 24 25 #include <shared_mutex> 26 #include <thread> 27 #include <vector> 28 #include <cstdlib> 29 #include <cassert> 30 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_HAS_SANITIZERS) 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(std::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 } 101