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