Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.mutex / thread.mutex.requirements / thread.sharedtimedmutex.requirements / thread.sharedtimedmutex.class / lock_shared.pass.cpp
blob20d7347b21cdc95414c3352156d8bd1f22e261b4
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
12 // UNSUPPORTED: availability-shared_mutex-missing
14 // ALLOW_RETRIES: 3
16 // <shared_mutex>
18 // class shared_timed_mutex;
20 // void lock_shared();
23 #include <atomic>
24 #include <cassert>
25 #include <chrono>
26 #include <cstdlib>
27 #include <shared_mutex>
28 #include <thread>
29 #include <vector>
31 #include "make_test_thread.h"
32 #include "test_macros.h"
34 std::shared_timed_mutex m;
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;
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'
46 ms WaitTime = ms(250);
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);
58 void reader() {
59 --countDown;
60 m.lock_shared();
61 m.unlock_shared();
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);
74 int main(int, char**)
76 int threads = 5;
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();
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();
105 return 0;