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.pass.cpp
blob606a377e72c54894b02b56464363f9c03545fbc4
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();
22 #include <atomic>
23 #include <cassert>
24 #include <chrono>
25 #include <cstdlib>
26 #include <shared_mutex>
27 #include <thread>
29 #include "make_test_thread.h"
30 #include "test_macros.h"
32 std::shared_timed_mutex m;
34 typedef std::chrono::system_clock Clock;
35 typedef Clock::time_point time_point;
36 typedef Clock::duration duration;
37 typedef std::chrono::milliseconds ms;
38 typedef std::chrono::nanoseconds ns;
40 std::atomic<bool> ready(false);
41 time_point start;
43 ms WaitTime = ms(250);
45 void f()
47 ready.store(true);
48 m.lock();
49 time_point t0 = start;
50 time_point t1 = Clock::now();
51 m.unlock();
52 assert(t0.time_since_epoch() > ms(0));
53 assert(t1 - t0 >= WaitTime);
56 int main(int, char**)
58 m.lock();
59 std::thread t = support::make_test_thread(f);
60 while (!ready)
61 std::this_thread::yield();
62 start = Clock::now();
63 std::this_thread::sleep_for(WaitTime);
64 m.unlock();
65 t.join();
67 return 0;