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 / try_lock_until.pass.cpp
blob7aab2353e4eb01fddc987de4a1413ffbc8986d73
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 // ALLOW_RETRIES: 2
14 // UNSUPPORTED: availability-shared_mutex-missing
16 // <shared_mutex>
18 // class shared_timed_mutex;
20 // template <class Clock, class Duration>
21 // bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
23 #include <shared_mutex>
24 #include <thread>
25 #include <cstdlib>
26 #include <cassert>
27 #include <chrono>
29 #include "make_test_thread.h"
30 #include "test_macros.h"
32 std::shared_timed_mutex m;
34 typedef std::chrono::steady_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;
41 ms WaitTime = ms(250);
43 // Thread sanitizer causes more overhead and will sometimes cause this test
44 // to fail. To prevent this we give Thread sanitizer more time to complete the
45 // test.
46 #if !defined(TEST_IS_EXECUTED_IN_A_SLOW_ENVIRONMENT)
47 ms Tolerance = ms(50);
48 #else
49 ms Tolerance = ms(50 * 5);
50 #endif
52 void f1()
54 time_point t0 = Clock::now();
55 assert(m.try_lock_until(Clock::now() + WaitTime + Tolerance) == true);
56 time_point t1 = Clock::now();
57 m.unlock();
58 ns d = t1 - t0 - WaitTime;
59 assert(d < Tolerance); // within tolerance
62 void f2()
64 time_point t0 = Clock::now();
65 assert(m.try_lock_until(Clock::now() + WaitTime) == false);
66 time_point t1 = Clock::now();
67 ns d = t1 - t0 - WaitTime;
68 assert(d < Tolerance); // within tolerance
71 int main(int, char**)
74 m.lock();
75 std::thread t = support::make_test_thread(f1);
76 std::this_thread::sleep_for(WaitTime);
77 m.unlock();
78 t.join();
81 m.lock();
82 std::thread t = support::make_test_thread(f2);
83 std::this_thread::sleep_for(WaitTime + Tolerance);
84 m.unlock();
85 t.join();
88 return 0;