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