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++98, c++03, c++11 11 12 // ALLOW_RETRIES: 2 13 14 // shared_timed_mutex was introduced in macosx10.12 15 // UNSUPPORTED: with_system_cxx_lib=macosx10.11 16 // UNSUPPORTED: with_system_cxx_lib=macosx10.10 17 // UNSUPPORTED: with_system_cxx_lib=macosx10.9 18 19 // <shared_mutex> 20 21 // class shared_timed_mutex; 22 23 // void lock_shared(); 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 42 ms WaitTime = ms(250); 43 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 46 // test. 47 #if !defined(TEST_HAS_SANITIZERS) 48 ms Tolerance = ms(50); 49 #else 50 ms Tolerance = ms(50 * 5); 51 #endif 52 53 54 void f() 55 { 56 time_point t0 = Clock::now(); 57 m.lock_shared(); 58 time_point t1 = Clock::now(); 59 m.unlock_shared(); 60 ns d = t1 - t0 - WaitTime; 61 assert(d < Tolerance); // within tolerance 62 } 63 64 void g() 65 { 66 time_point t0 = Clock::now(); 67 m.lock_shared(); 68 time_point t1 = Clock::now(); 69 m.unlock_shared(); 70 ns d = t1 - t0; 71 assert(d < Tolerance); // within tolerance 72 } 73 74 75 int main(int, char**) 76 { 77 m.lock(); 78 std::vector<std::thread> v; 79 for (int i = 0; i < 5; ++i) 80 v.push_back(std::thread(f)); 81 std::this_thread::sleep_for(WaitTime); 82 m.unlock(); 83 for (auto& t : v) 84 t.join(); 85 m.lock_shared(); 86 for (auto& t : v) 87 t = std::thread(g); 88 std::thread q(f); 89 std::this_thread::sleep_for(WaitTime); 90 m.unlock_shared(); 91 for (auto& t : v) 92 t.join(); 93 q.join(); 94 95 return 0; 96 } 97