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