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 // shared_timed_mutex was introduced in macosx10.12 13 // UNSUPPORTED: 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 shared_timed_mutex; 21 22 // void lock_shared(); 23 24 #include <thread> 25 26 #include <atomic> 27 #include <cassert> 28 #include <cstdlib> 29 #include <shared_mutex> 30 #include <vector> 31 32 #include "make_test_thread.h" 33 #include "test_macros.h" 34 35 std::shared_timed_mutex m; 36 37 typedef std::chrono::system_clock Clock; 38 typedef Clock::time_point time_point; 39 typedef Clock::duration duration; 40 typedef std::chrono::milliseconds ms; 41 typedef std::chrono::nanoseconds ns; 42 43 std::atomic<unsigned> countDown; 44 time_point readerStart; // Protected by the above mutex 'm' 45 time_point writerStart; // Protected by the above mutex 'm' 46 47 ms WaitTime = ms(250); 48 49 void readerMustWait() { 50 --countDown; 51 m.lock_shared(); 52 time_point t1 = Clock::now(); 53 time_point t0 = readerStart; 54 m.unlock_shared(); 55 assert(t0.time_since_epoch() > ms(0)); 56 assert(t1 - t0 >= WaitTime); 57 } 58 59 void reader() { 60 --countDown; 61 m.lock_shared(); 62 m.unlock_shared(); 63 } 64 65 void writerMustWait() { 66 --countDown; 67 m.lock(); 68 time_point t1 = Clock::now(); 69 time_point t0 = writerStart; 70 m.unlock(); 71 assert(t0.time_since_epoch() > ms(0)); 72 assert(t1 - t0 >= WaitTime); 73 } 74 75 int main(int, char**) 76 { 77 int threads = 5; 78 79 countDown.store(threads); 80 m.lock(); 81 std::vector<std::thread> v; 82 for (int i = 0; i < threads; ++i) 83 v.push_back(support::make_test_thread(readerMustWait)); 84 while (countDown > 0) 85 std::this_thread::yield(); 86 readerStart = Clock::now(); 87 std::this_thread::sleep_for(WaitTime); 88 m.unlock(); 89 for (auto& t : v) 90 t.join(); 91 92 countDown.store(threads + 1); 93 m.lock_shared(); 94 for (auto& t : v) 95 t = support::make_test_thread(reader); 96 std::thread q = support::make_test_thread(writerMustWait); 97 while (countDown > 0) 98 std::this_thread::yield(); 99 writerStart = Clock::now(); 100 std::this_thread::sleep_for(WaitTime); 101 m.unlock_shared(); 102 for (auto& t : v) 103 t.join(); 104 q.join(); 105 106 return 0; 107 } 108