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 // ALLOW_RETRIES: 2 11 12 // TODO(ldionne): This test fails on Ubuntu Focal on our CI nodes (and only there), in 32 bit mode. 13 // UNSUPPORTED: linux && 32bits-on-64bits 14 15 // <condition_variable> 16 17 // class condition_variable_any; 18 19 // template <class Lock, class Rep, class Period> 20 // cv_status 21 // wait_for(Lock& lock, const chrono::duration<Rep, Period>& rel_time); 22 23 #include <condition_variable> 24 #include <mutex> 25 #include <thread> 26 #include <chrono> 27 #include <cassert> 28 29 #include "make_test_thread.h" 30 #include "test_macros.h" 31 32 std::condition_variable_any cv; 33 34 typedef std::timed_mutex L0; 35 typedef std::unique_lock<L0> L1; 36 37 L0 m0; 38 39 int test1 = 0; 40 int test2 = 0; 41 42 bool expect_timeout = false; 43 44 void f() 45 { 46 typedef std::chrono::system_clock Clock; 47 typedef std::chrono::milliseconds milliseconds; 48 L1 lk(m0); 49 assert(test2 == 0); 50 test1 = 1; 51 cv.notify_one(); 52 Clock::time_point t0 = Clock::now(); 53 Clock::time_point wait_end = t0 + milliseconds(250); 54 Clock::duration d; 55 do { 56 d = wait_end - Clock::now(); 57 if (d <= milliseconds(0)) break; 58 } while (test2 == 0 && cv.wait_for(lk, d) == std::cv_status::no_timeout); 59 Clock::time_point t1 = Clock::now(); 60 if (!expect_timeout) 61 { 62 assert(t1 - t0 < milliseconds(250)); 63 assert(test2 != 0); 64 } 65 else 66 { 67 assert(t1 - t0 - milliseconds(250) < milliseconds(50)); 68 assert(test2 == 0); 69 } 70 } 71 72 int main(int, char**) 73 { 74 { 75 L1 lk(m0); 76 std::thread t = support::make_test_thread(f); 77 assert(test1 == 0); 78 while (test1 == 0) 79 cv.wait(lk); 80 assert(test1 != 0); 81 test2 = 1; 82 lk.unlock(); 83 cv.notify_one(); 84 t.join(); 85 } 86 test1 = 0; 87 test2 = 0; 88 expect_timeout = true; 89 { 90 L1 lk(m0); 91 std::thread t = support::make_test_thread(f); 92 assert(test1 == 0); 93 while (test1 == 0) 94 cv.wait(lk); 95 assert(test1 != 0); 96 lk.unlock(); 97 t.join(); 98 } 99 100 return 0; 101 } 102