1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // UNSUPPORTED: libcpp-has-no-threads 11 // UNSUPPORTED: c++98, c++03, c++11 12 13 // <shared_mutex> 14 15 // class timed_mutex; 16 17 // template <class Rep, class Period> 18 // shared_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); 19 20 #include <shared_mutex> 21 #include <thread> 22 #include <vector> 23 #include <cstdlib> 24 #include <cassert> 25 26 #include "test_macros.h" 27 28 std::shared_timed_mutex m; 29 30 typedef std::chrono::steady_clock Clock; 31 typedef Clock::time_point time_point; 32 typedef Clock::duration duration; 33 typedef std::chrono::milliseconds ms; 34 typedef std::chrono::nanoseconds ns; 35 36 ms WaitTime = ms(250); 37 38 // Thread sanitizer causes more overhead and will sometimes cause this test 39 // to fail. To prevent this we give Thread sanitizer more time to complete the 40 // test. 41 #if !TEST_HAS_FEATURE(thread_sanitizer) 42 ms Tolerance = ms(50); 43 #else 44 ms Tolerance = ms(100); 45 #endif 46 47 48 void f1() 49 { 50 time_point t0 = Clock::now(); 51 std::shared_lock<std::shared_timed_mutex> lk(m, WaitTime + Tolerance); 52 assert(lk.owns_lock() == true); 53 time_point t1 = Clock::now(); 54 ns d = t1 - t0 - WaitTime; 55 assert(d < Tolerance); // within 50ms 56 } 57 58 void f2() 59 { 60 time_point t0 = Clock::now(); 61 std::shared_lock<std::shared_timed_mutex> lk(m, WaitTime); 62 assert(lk.owns_lock() == false); 63 time_point t1 = Clock::now(); 64 ns d = t1 - t0 - WaitTime; 65 assert(d < Tolerance); // within 50ms 66 } 67 68 int main() 69 { 70 { 71 m.lock(); 72 std::vector<std::thread> v; 73 for (int i = 0; i < 5; ++i) 74 v.push_back(std::thread(f1)); 75 std::this_thread::sleep_for(WaitTime); 76 m.unlock(); 77 for (auto& t : v) 78 t.join(); 79 } 80 { 81 m.lock(); 82 std::vector<std::thread> v; 83 for (int i = 0; i < 5; ++i) 84 v.push_back(std::thread(f2)); 85 std::this_thread::sleep_for(WaitTime + Tolerance); 86 m.unlock(); 87 for (auto& t : v) 88 t.join(); 89 } 90 } 91