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 // UNSUPPORTED: c++03 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 // <future> 16 17 // class shared_future<R> 18 19 // template <class Rep, class Period> 20 // future_status 21 // wait_for(const chrono::duration<Rep, Period>& rel_time) const; 22 23 #include <future> 24 #include <cassert> 25 26 #include "make_test_thread.h" 27 #include "test_macros.h" 28 29 typedef std::chrono::milliseconds ms; 30 31 static const ms sleepTime(500); 32 static const ms waitTime(5000); 33 34 void func1(std::promise<int> p) 35 { 36 std::this_thread::sleep_for(sleepTime); 37 p.set_value(3); 38 } 39 40 int j = 0; 41 42 void func3(std::promise<int&> p) 43 { 44 std::this_thread::sleep_for(sleepTime); 45 j = 5; 46 p.set_value(j); 47 } 48 49 void func5(std::promise<void> p) 50 { 51 std::this_thread::sleep_for(sleepTime); 52 p.set_value(); 53 } 54 55 int main(int, char**) 56 { 57 typedef std::chrono::high_resolution_clock Clock; 58 59 { 60 typedef int T; 61 std::promise<T> p; 62 std::shared_future<T> f = p.get_future(); 63 support::make_test_thread(func1, std::move(p)).detach(); 64 assert(f.valid()); 65 assert(f.wait_for(ms(1)) == std::future_status::timeout); 66 assert(f.valid()); 67 assert(f.wait_for(waitTime) == std::future_status::ready); 68 assert(f.valid()); 69 f.wait(); 70 assert(f.valid()); 71 } 72 { 73 typedef int& T; 74 std::promise<T> p; 75 std::shared_future<T> f = p.get_future(); 76 support::make_test_thread(func3, std::move(p)).detach(); 77 assert(f.valid()); 78 assert(f.wait_for(ms(1)) == std::future_status::timeout); 79 assert(f.valid()); 80 assert(f.wait_for(waitTime) == std::future_status::ready); 81 assert(f.valid()); 82 f.wait(); 83 assert(f.valid()); 84 } 85 { 86 typedef void T; 87 std::promise<T> p; 88 std::shared_future<T> f = p.get_future(); 89 support::make_test_thread(func5, std::move(p)).detach(); 90 assert(f.valid()); 91 assert(f.wait_for(ms(1)) == std::future_status::timeout); 92 assert(f.valid()); 93 assert(f.wait_for(waitTime) == std::future_status::ready); 94 assert(f.valid()); 95 f.wait(); 96 assert(f.valid()); 97 } 98 99 { 100 typedef int T; 101 std::promise<T> p; 102 std::shared_future<T> f = p.get_future(); 103 Clock::time_point t0 = Clock::now(); 104 support::make_test_thread(func1, std::move(p)).detach(); 105 assert(f.valid()); 106 assert(f.wait_for(ms(1)) == std::future_status::timeout); 107 assert(f.valid()); 108 f.wait(); 109 Clock::time_point t1 = Clock::now(); 110 assert(f.valid()); 111 assert(t1 - t0 >= sleepTime); 112 assert(f.wait_for(waitTime) == std::future_status::ready); 113 assert(f.valid()); 114 } 115 { 116 typedef int& T; 117 std::promise<T> p; 118 std::shared_future<T> f = p.get_future(); 119 Clock::time_point t0 = Clock::now(); 120 support::make_test_thread(func3, std::move(p)).detach(); 121 assert(f.valid()); 122 assert(f.wait_for(ms(1)) == std::future_status::timeout); 123 assert(f.valid()); 124 f.wait(); 125 Clock::time_point t1 = Clock::now(); 126 assert(f.valid()); 127 assert(t1 - t0 >= sleepTime); 128 assert(f.wait_for(waitTime) == std::future_status::ready); 129 assert(f.valid()); 130 } 131 { 132 typedef void T; 133 std::promise<T> p; 134 std::shared_future<T> f = p.get_future(); 135 Clock::time_point t0 = Clock::now(); 136 support::make_test_thread(func5, std::move(p)).detach(); 137 assert(f.valid()); 138 assert(f.wait_for(ms(1)) == std::future_status::timeout); 139 assert(f.valid()); 140 f.wait(); 141 Clock::time_point t1 = Clock::now(); 142 assert(f.valid()); 143 assert(t1 - t0 >= sleepTime); 144 assert(f.wait_for(waitTime) == std::future_status::ready); 145 assert(f.valid()); 146 } 147 148 return 0; 149 } 150