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++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 // TODO(ldionne): This test fails on Ubuntu Focal on our CI nodes (and only there), in 32 bit mode. 16 // UNSUPPORTED: linux && 32bits-on-64bits 17 18 // <shared_mutex> 19 20 // class timed_mutex; 21 22 // template <class Rep, class Period> 23 // shared_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time); 24 25 #include <thread> 26 27 #include <atomic> 28 #include <cassert> 29 #include <cstdlib> 30 #include <shared_mutex> 31 #include <vector> 32 33 #include "make_test_thread.h" 34 #include "test_macros.h" 35 36 std::shared_timed_mutex m; 37 38 typedef std::chrono::steady_clock Clock; 39 typedef Clock::time_point time_point; 40 typedef Clock::duration duration; 41 typedef std::chrono::milliseconds ms; 42 typedef std::chrono::nanoseconds ns; 43 44 ms LongTime = ms(5000); 45 ms ShortTime = ms(50); 46 47 static const unsigned Threads = 5; 48 49 std::atomic<unsigned> CountDown(Threads); 50 51 void f1() 52 { 53 // Preemptive scheduling means that one cannot make assumptions about when 54 // code executes and therefore we cannot assume anthing about when the mutex 55 // starts waiting relative to code in the main thread. We can however prove 56 // that a timeout occured and that implies that this code is waiting. 57 // See f2() below. 58 // 59 // Nevertheless, we should at least try to ensure that the mutex waits and 60 // therefore we use an atomic variable to signal to the main thread that this 61 // code is just a few instructions away from waiting. 62 --CountDown; 63 std::shared_lock<std::shared_timed_mutex> lk(m, LongTime); 64 assert(lk.owns_lock() == true); 65 } 66 67 void f2() 68 { 69 time_point t0 = Clock::now(); 70 std::shared_lock<std::shared_timed_mutex> lk(m, ShortTime); 71 time_point t1 = Clock::now(); 72 assert(lk.owns_lock() == false); 73 assert(t1 - t0 >= ShortTime); 74 } 75 76 int main(int, char**) 77 { 78 { 79 m.lock(); 80 std::vector<std::thread> v; 81 for (unsigned i = 0; i < Threads; ++i) 82 v.push_back(support::make_test_thread(f1)); 83 while (CountDown > 0) 84 std::this_thread::yield(); 85 // Give one more chance for threads to block and wait for the mutex. 86 std::this_thread::yield(); 87 std::this_thread::sleep_for(ShortTime); 88 m.unlock(); 89 for (auto& t : v) 90 t.join(); 91 } 92 { 93 m.lock(); 94 std::vector<std::thread> v; 95 for (unsigned i = 0; i < Threads; ++i) 96 v.push_back(support::make_test_thread(f2)); 97 for (auto& t : v) 98 t.join(); 99 m.unlock(); 100 } 101 102 return 0; 103 } 104