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: no-threads 10 // UNSUPPORTED: c++03, c++11 11 // ALLOW_RETRIES: 2 12 13 // shared_timed_mutex was introduced in macosx10.12 14 // UNSUPPORTED: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}} 15 16 // <shared_mutex> 17 18 // class shared_timed_mutex; 19 20 // template <class Rep, class Period> 21 // bool try_lock_for(const chrono::duration<Rep, Period>& rel_time); 22 23 #include <shared_mutex> 24 #include <thread> 25 #include <cstdlib> 26 #include <cassert> 27 28 #include "make_test_thread.h" 29 #include "test_macros.h" 30 31 std::shared_timed_mutex m; 32 33 typedef std::chrono::steady_clock Clock; 34 typedef Clock::time_point time_point; 35 typedef Clock::duration duration; 36 typedef std::chrono::milliseconds ms; 37 typedef std::chrono::nanoseconds ns; 38 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 f1()51void f1() 52 { 53 time_point t0 = Clock::now(); 54 assert(m.try_lock_for(WaitTime + Tolerance) == true); 55 time_point t1 = Clock::now(); 56 m.unlock(); 57 ns d = t1 - t0 - WaitTime; 58 assert(d < Tolerance); // within tolerance 59 } 60 f2()61void f2() 62 { 63 time_point t0 = Clock::now(); 64 assert(m.try_lock_for(WaitTime) == false); 65 time_point t1 = Clock::now(); 66 ns d = t1 - t0 - WaitTime; 67 assert(d < Tolerance); // within tolerance 68 } 69 main(int,char **)70int main(int, char**) 71 { 72 { 73 m.lock(); 74 std::thread t = support::make_test_thread(f1); 75 std::this_thread::sleep_for(WaitTime); 76 m.unlock(); 77 t.join(); 78 } 79 { 80 m.lock(); 81 std::thread t = support::make_test_thread(f2); 82 std::this_thread::sleep_for(WaitTime + Tolerance); 83 m.unlock(); 84 t.join(); 85 } 86 87 return 0; 88 } 89