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 future<R> 18 19 // template <class Clock, class Duration> 20 // future_status 21 // wait_until(const chrono::time_point<Clock, Duration>& abs_time) const; 22 23 #include <future> 24 #include <atomic> 25 #include <cassert> 26 27 #include "make_test_thread.h" 28 #include "test_macros.h" 29 30 enum class WorkerThreadState { Uninitialized, AllowedToRun, Exiting }; 31 typedef std::chrono::milliseconds ms; 32 33 std::atomic<WorkerThreadState> thread_state(WorkerThreadState::Uninitialized); 34 35 void set_worker_thread_state(WorkerThreadState state) 36 { 37 thread_state.store(state, std::memory_order_relaxed); 38 } 39 40 void wait_for_worker_thread_state(WorkerThreadState state) 41 { 42 while (thread_state.load(std::memory_order_relaxed) != state) 43 std::this_thread::yield(); 44 } 45 46 void func1(std::promise<int> p) 47 { 48 wait_for_worker_thread_state(WorkerThreadState::AllowedToRun); 49 p.set_value(3); 50 set_worker_thread_state(WorkerThreadState::Exiting); 51 } 52 53 int j = 0; 54 55 void func3(std::promise<int&> p) 56 { 57 wait_for_worker_thread_state(WorkerThreadState::AllowedToRun); 58 j = 5; 59 p.set_value(j); 60 set_worker_thread_state(WorkerThreadState::Exiting); 61 } 62 63 void func5(std::promise<void> p) 64 { 65 wait_for_worker_thread_state(WorkerThreadState::AllowedToRun); 66 p.set_value(); 67 set_worker_thread_state(WorkerThreadState::Exiting); 68 } 69 70 int main(int, char**) 71 { 72 typedef std::chrono::high_resolution_clock Clock; 73 { 74 typedef int T; 75 std::promise<T> p; 76 std::future<T> f = p.get_future(); 77 support::make_test_thread(func1, std::move(p)).detach(); 78 assert(f.valid()); 79 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout); 80 assert(f.valid()); 81 82 // allow the worker thread to produce the result and wait until the worker is done 83 set_worker_thread_state(WorkerThreadState::AllowedToRun); 84 wait_for_worker_thread_state(WorkerThreadState::Exiting); 85 86 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready); 87 assert(f.valid()); 88 f.wait(); 89 assert(f.valid()); 90 } 91 { 92 typedef int& T; 93 std::promise<T> p; 94 std::future<T> f = p.get_future(); 95 support::make_test_thread(func3, std::move(p)).detach(); 96 assert(f.valid()); 97 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout); 98 assert(f.valid()); 99 100 // allow the worker thread to produce the result and wait until the worker is done 101 set_worker_thread_state(WorkerThreadState::AllowedToRun); 102 wait_for_worker_thread_state(WorkerThreadState::Exiting); 103 104 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready); 105 assert(f.valid()); 106 f.wait(); 107 assert(f.valid()); 108 } 109 { 110 typedef void T; 111 std::promise<T> p; 112 std::future<T> f = p.get_future(); 113 support::make_test_thread(func5, std::move(p)).detach(); 114 assert(f.valid()); 115 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout); 116 assert(f.valid()); 117 118 // allow the worker thread to produce the result and wait until the worker is done 119 set_worker_thread_state(WorkerThreadState::AllowedToRun); 120 wait_for_worker_thread_state(WorkerThreadState::Exiting); 121 122 assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready); 123 assert(f.valid()); 124 f.wait(); 125 assert(f.valid()); 126 } 127 128 return 0; 129 } 130