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 12 // dylib support for shared_mutex was added in macosx10.12 13 // XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}} 14 15 // <shared_mutex> 16 17 // class shared_timed_mutex; 18 19 // template <class Clock, class Duration> 20 // shared_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); 21 22 #include <thread> 23 24 #include <atomic> 25 #include <cassert> 26 #include <cstdlib> 27 #include <shared_mutex> 28 #include <vector> 29 30 #include "make_test_thread.h" 31 #include "test_macros.h" 32 33 std::shared_timed_mutex m; 34 35 typedef std::chrono::steady_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 LongTime = ms(5000); 42 ms ShortTime = ms(50); 43 44 static constexpr unsigned Threads = 5; 45 46 std::atomic<unsigned> CountDown(Threads); 47 f1()48void f1() 49 { 50 --CountDown; 51 time_point t0 = Clock::now(); 52 std::shared_lock<std::shared_timed_mutex> lk(m, t0 + LongTime); 53 time_point t1 = Clock::now(); 54 assert(lk.owns_lock() == true); 55 assert(t1 - t0 <= LongTime); 56 } 57 f2()58void f2() 59 { 60 time_point t0 = Clock::now(); 61 std::shared_lock<std::shared_timed_mutex> lk(m, t0 + ShortTime); 62 time_point t1 = Clock::now(); 63 assert(lk.owns_lock() == false); 64 assert(t1 - t0 >= ShortTime); 65 } 66 main(int,char **)67int main(int, char**) 68 { 69 { 70 m.lock(); 71 std::vector<std::thread> v; 72 for (unsigned i = 0; i < Threads; ++i) 73 v.push_back(support::make_test_thread(f1)); 74 while (CountDown > 0) 75 std::this_thread::yield(); 76 std::this_thread::sleep_for(ShortTime); 77 m.unlock(); 78 for (auto& t : v) 79 t.join(); 80 } 81 { 82 m.lock(); 83 std::vector<std::thread> v; 84 for (unsigned i = 0; i < Threads; ++i) 85 v.push_back(support::make_test_thread(f2)); 86 for (auto& t : v) 87 t.join(); 88 m.unlock(); 89 } 90 91 return 0; 92 } 93