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 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   {
75     typedef int T;
76     std::promise<T> p;
77     std::shared_future<T> f = p.get_future();
78     support::make_test_thread(func1, std::move(p)).detach();
79     assert(f.valid());
80     assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout);
81     assert(f.valid());
82 
83     // allow the worker thread to produce the result and wait until the worker is done
84     set_worker_thread_state(WorkerThreadState::AllowedToRun);
85     wait_for_worker_thread_state(WorkerThreadState::Exiting);
86 
87     assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready);
88     assert(f.valid());
89     f.wait();
90     assert(f.valid());
91   }
92   {
93     typedef int& T;
94     std::promise<T> p;
95     std::shared_future<T> f = p.get_future();
96     support::make_test_thread(func3, std::move(p)).detach();
97     assert(f.valid());
98     assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout);
99     assert(f.valid());
100 
101     // allow the worker thread to produce the result and wait until the worker is done
102     set_worker_thread_state(WorkerThreadState::AllowedToRun);
103     wait_for_worker_thread_state(WorkerThreadState::Exiting);
104 
105     assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready);
106     assert(f.valid());
107     f.wait();
108     assert(f.valid());
109   }
110   {
111     typedef void T;
112     std::promise<T> p;
113     std::shared_future<T> f = p.get_future();
114     support::make_test_thread(func5, std::move(p)).detach();
115     assert(f.valid());
116     assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::timeout);
117     assert(f.valid());
118 
119     // allow the worker thread to produce the result and wait until the worker is done
120     set_worker_thread_state(WorkerThreadState::AllowedToRun);
121     wait_for_worker_thread_state(WorkerThreadState::Exiting);
122 
123     assert(f.wait_until(Clock::now() + ms(10)) == std::future_status::ready);
124     assert(f.valid());
125     f.wait();
126     assert(f.valid());
127   }
128 
129   return 0;
130 }
131