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 timed_mutex; 18 19 // template <class Rep, class Period> 20 // shared_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_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 const unsigned Threads = 5; 45 46 std::atomic<unsigned> CountDown(Threads); 47 48 void f1() 49 { 50 // Preemptive scheduling means that one cannot make assumptions about when 51 // code executes and therefore we cannot assume anthing about when the mutex 52 // starts waiting relative to code in the main thread. We can however prove 53 // that a timeout occured and that implies that this code is waiting. 54 // See f2() below. 55 // 56 // Nevertheless, we should at least try to ensure that the mutex waits and 57 // therefore we use an atomic variable to signal to the main thread that this 58 // code is just a few instructions away from waiting. 59 --CountDown; 60 std::shared_lock<std::shared_timed_mutex> lk(m, LongTime); 61 assert(lk.owns_lock() == true); 62 } 63 64 void f2() 65 { 66 time_point t0 = Clock::now(); 67 std::shared_lock<std::shared_timed_mutex> lk(m, ShortTime); 68 time_point t1 = Clock::now(); 69 assert(lk.owns_lock() == false); 70 assert(t1 - t0 >= ShortTime); 71 } 72 73 int main(int, char**) 74 { 75 { 76 m.lock(); 77 std::vector<std::thread> v; 78 for (unsigned i = 0; i < Threads; ++i) 79 v.push_back(support::make_test_thread(f1)); 80 while (CountDown > 0) 81 std::this_thread::yield(); 82 // Give one more chance for threads to block and wait for the mutex. 83 std::this_thread::yield(); 84 std::this_thread::sleep_for(ShortTime); 85 m.unlock(); 86 for (auto& t : v) 87 t.join(); 88 } 89 { 90 m.lock(); 91 std::vector<std::thread> v; 92 for (unsigned i = 0; i < Threads; ++i) 93 v.push_back(support::make_test_thread(f2)); 94 for (auto& t : v) 95 t.join(); 96 m.unlock(); 97 } 98 99 return 0; 100 } 101