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