Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / libcxx / test / std / thread / futures / futures.shared_future / wait_for.pass.cpp
blob80674387a38d182e3b9ebd602b4999fa6512230d
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: no-threads
10 // UNSUPPORTED: c++03
12 // <future>
14 // class shared_future<R>
16 // template <class Rep, class Period>
17 // future_status
18 // wait_for(const chrono::duration<Rep, Period>& rel_time) const;
20 #include <cassert>
21 #include <chrono>
22 #include <future>
24 #include "make_test_thread.h"
25 #include "test_macros.h"
27 typedef std::chrono::milliseconds ms;
29 static const ms sleepTime(500);
30 static const ms waitTime(5000);
32 void func1(std::promise<int> p)
34 std::this_thread::sleep_for(sleepTime);
35 p.set_value(3);
38 int j = 0;
40 void func3(std::promise<int&> p)
42 std::this_thread::sleep_for(sleepTime);
43 j = 5;
44 p.set_value(j);
47 void func5(std::promise<void> p)
49 std::this_thread::sleep_for(sleepTime);
50 p.set_value();
53 int main(int, char**)
55 typedef std::chrono::high_resolution_clock Clock;
58 typedef int T;
59 std::promise<T> p;
60 std::shared_future<T> f = p.get_future();
61 support::make_test_thread(func1, std::move(p)).detach();
62 assert(f.valid());
63 assert(f.wait_for(ms(1)) == std::future_status::timeout);
64 assert(f.valid());
65 assert(f.wait_for(waitTime) == std::future_status::ready);
66 assert(f.valid());
67 f.wait();
68 assert(f.valid());
71 typedef int& T;
72 std::promise<T> p;
73 std::shared_future<T> f = p.get_future();
74 support::make_test_thread(func3, std::move(p)).detach();
75 assert(f.valid());
76 assert(f.wait_for(ms(1)) == std::future_status::timeout);
77 assert(f.valid());
78 assert(f.wait_for(waitTime) == std::future_status::ready);
79 assert(f.valid());
80 f.wait();
81 assert(f.valid());
84 typedef void T;
85 std::promise<T> p;
86 std::shared_future<T> f = p.get_future();
87 support::make_test_thread(func5, std::move(p)).detach();
88 assert(f.valid());
89 assert(f.wait_for(ms(1)) == std::future_status::timeout);
90 assert(f.valid());
91 assert(f.wait_for(waitTime) == std::future_status::ready);
92 assert(f.valid());
93 f.wait();
94 assert(f.valid());
98 typedef int T;
99 std::promise<T> p;
100 std::shared_future<T> f = p.get_future();
101 Clock::time_point t0 = Clock::now();
102 support::make_test_thread(func1, std::move(p)).detach();
103 assert(f.valid());
104 assert(f.wait_for(ms(1)) == std::future_status::timeout);
105 assert(f.valid());
106 f.wait();
107 Clock::time_point t1 = Clock::now();
108 assert(f.valid());
109 assert(t1 - t0 >= sleepTime);
110 assert(f.wait_for(waitTime) == std::future_status::ready);
111 assert(f.valid());
114 typedef int& T;
115 std::promise<T> p;
116 std::shared_future<T> f = p.get_future();
117 Clock::time_point t0 = Clock::now();
118 support::make_test_thread(func3, std::move(p)).detach();
119 assert(f.valid());
120 assert(f.wait_for(ms(1)) == std::future_status::timeout);
121 assert(f.valid());
122 f.wait();
123 Clock::time_point t1 = Clock::now();
124 assert(f.valid());
125 assert(t1 - t0 >= sleepTime);
126 assert(f.wait_for(waitTime) == std::future_status::ready);
127 assert(f.valid());
130 typedef void T;
131 std::promise<T> p;
132 std::shared_future<T> f = p.get_future();
133 Clock::time_point t0 = Clock::now();
134 support::make_test_thread(func5, std::move(p)).detach();
135 assert(f.valid());
136 assert(f.wait_for(ms(1)) == std::future_status::timeout);
137 assert(f.valid());
138 f.wait();
139 Clock::time_point t1 = Clock::now();
140 assert(f.valid());
141 assert(t1 - t0 >= sleepTime);
142 assert(f.wait_for(waitTime) == std::future_status::ready);
143 assert(f.valid());
146 return 0;