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