blob: c7a3906de326b636a3db55c2e5565fd154845843 [file] [log] [blame]
Howard Hinnant27f000e2010-08-30 18:46:21 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Howard Hinnant27f000e2010-08-30 18:46:21 +00006//
7//===----------------------------------------------------------------------===//
Jonathan Roelofsb3fcc672014-09-05 19:45:05 +00008//
9// UNSUPPORTED: libcpp-has-no-threads
Louis Dionne31cbe0f2020-06-01 10:38:23 -040010// UNSUPPORTED: c++03
Howard Hinnant27f000e2010-08-30 18:46:21 +000011
12// <future>
13
14// template <class F, class... Args>
15// future<typename result_of<F(Args...)>::type>
16// async(F&& f, Args&&... args);
17
18// template <class F, class... Args>
19// future<typename result_of<F(Args...)>::type>
20// async(launch policy, F&& f, Args&&... args);
21
Eric Fiselier382e9172016-04-29 04:07:45 +000022
Howard Hinnant27f000e2010-08-30 18:46:21 +000023#include <future>
Eric Fiselier382e9172016-04-29 04:07:45 +000024#include <atomic>
Howard Hinnant27f000e2010-08-30 18:46:21 +000025#include <memory>
26#include <cassert>
27
Eric Fiselier382e9172016-04-29 04:07:45 +000028#include "test_macros.h"
29
Howard Hinnant27f000e2010-08-30 18:46:21 +000030typedef std::chrono::high_resolution_clock Clock;
31typedef std::chrono::milliseconds ms;
32
Eric Fiselier382e9172016-04-29 04:07:45 +000033std::atomic_bool invoked = ATOMIC_VAR_INIT(false);
34
Howard Hinnant27f000e2010-08-30 18:46:21 +000035int f0()
36{
Eric Fiselier382e9172016-04-29 04:07:45 +000037 invoked = true;
Howard Hinnant27f000e2010-08-30 18:46:21 +000038 std::this_thread::sleep_for(ms(200));
39 return 3;
40}
41
42int i = 0;
43
44int& f1()
45{
Eric Fiselier382e9172016-04-29 04:07:45 +000046 invoked = true;
Howard Hinnant27f000e2010-08-30 18:46:21 +000047 std::this_thread::sleep_for(ms(200));
48 return i;
49}
50
51void f2()
52{
Eric Fiselier382e9172016-04-29 04:07:45 +000053 invoked = true;
Howard Hinnant27f000e2010-08-30 18:46:21 +000054 std::this_thread::sleep_for(ms(200));
55}
56
Eric Fiselier588e1db2016-04-28 02:00:52 +000057std::unique_ptr<int> f3(int j)
Howard Hinnant27f000e2010-08-30 18:46:21 +000058{
Eric Fiselier382e9172016-04-29 04:07:45 +000059 invoked = true;
Howard Hinnant27f000e2010-08-30 18:46:21 +000060 std::this_thread::sleep_for(ms(200));
Eric Fiselier588e1db2016-04-28 02:00:52 +000061 return std::unique_ptr<int>(new int(j));
Howard Hinnant27f000e2010-08-30 18:46:21 +000062}
63
64std::unique_ptr<int> f4(std::unique_ptr<int>&& p)
65{
Eric Fiselier382e9172016-04-29 04:07:45 +000066 invoked = true;
Howard Hinnant27f000e2010-08-30 18:46:21 +000067 std::this_thread::sleep_for(ms(200));
68 return std::move(p);
69}
70
Eric Fiselier588e1db2016-04-28 02:00:52 +000071void f5(int j)
Marshall Clow4fdb0702014-03-24 22:25:24 +000072{
73 std::this_thread::sleep_for(ms(200));
Eric Fiselierdee07a62016-12-24 04:34:33 +000074 ((void)j);
75 TEST_THROW(j);
Marshall Clow4fdb0702014-03-24 22:25:24 +000076}
77
Eric Fiselier382e9172016-04-29 04:07:45 +000078template <class Ret, class CheckLamdba, class ...Args>
79void test(CheckLamdba&& getAndCheckFn, bool IsDeferred, Args&&... args) {
80 // Reset global state.
81 invoked = false;
82
83 // Create the future and wait
84 std::future<Ret> f = std::async(std::forward<Args>(args)...);
85 std::this_thread::sleep_for(ms(300));
86
87 // Check that deferred async's have not invoked the function.
88 assert(invoked == !IsDeferred);
89
90 // Time the call to f.get() and check that the returned value matches
91 // what is expected.
92 Clock::time_point t0 = Clock::now();
93 assert(getAndCheckFn(f));
94 Clock::time_point t1 = Clock::now();
95
96 // If the async is deferred it should take more than 100ms, otherwise
97 // it should take less than 100ms.
98 if (IsDeferred) {
99 assert(t1-t0 > ms(100));
100 } else {
101 assert(t1-t0 < ms(100));
102 }
103}
104
JF Bastien2df59c52019-02-04 20:31:13 +0000105int main(int, char**)
Howard Hinnant27f000e2010-08-30 18:46:21 +0000106{
Eric Fiselier382e9172016-04-29 04:07:45 +0000107 // The default launch policy is implementation defined. libc++ defines
108 // it to be std::launch::async.
109 bool DefaultPolicyIsDeferred = false;
110 bool DPID = DefaultPolicyIsDeferred;
111
112 std::launch AnyPolicy = std::launch::async | std::launch::deferred;
113 LIBCPP_ASSERT(AnyPolicy == std::launch::any);
Howard Hinnant27f000e2010-08-30 18:46:21 +0000114
115 {
Eric Fiselier382e9172016-04-29 04:07:45 +0000116 auto checkInt = [](std::future<int>& f) { return f.get() == 3; };
117 test<int>(checkInt, DPID, f0);
118 test<int>(checkInt, false, std::launch::async, f0);
119 test<int>(checkInt, true, std::launch::deferred, f0);
120 test<int>(checkInt, DPID, AnyPolicy, f0);
Howard Hinnant27f000e2010-08-30 18:46:21 +0000121 }
122 {
Eric Fiselier382e9172016-04-29 04:07:45 +0000123 auto checkIntRef = [&](std::future<int&>& f) { return &f.get() == &i; };
124 test<int&>(checkIntRef, DPID, f1);
125 test<int&>(checkIntRef, false, std::launch::async, f1);
126 test<int&>(checkIntRef, true, std::launch::deferred, f1);
127 test<int&>(checkIntRef, DPID, AnyPolicy, f1);
Howard Hinnant27f000e2010-08-30 18:46:21 +0000128 }
129 {
Eric Fiselier382e9172016-04-29 04:07:45 +0000130 auto checkVoid = [](std::future<void>& f) { f.get(); return true; };
131 test<void>(checkVoid, DPID, f2);
132 test<void>(checkVoid, false, std::launch::async, f2);
133 test<void>(checkVoid, true, std::launch::deferred, f2);
Eric Fiselier53ca3c92016-04-30 02:30:18 +0000134 test<void>(checkVoid, DPID, AnyPolicy, f2);
Howard Hinnant27f000e2010-08-30 18:46:21 +0000135 }
136 {
Eric Fiselier382e9172016-04-29 04:07:45 +0000137 using Ret = std::unique_ptr<int>;
138 auto checkUPtr = [](std::future<Ret>& f) { return *f.get() == 3; };
139 test<Ret>(checkUPtr, DPID, f3, 3);
140 test<Ret>(checkUPtr, DPID, f4, std::unique_ptr<int>(new int(3)));
Howard Hinnant27f000e2010-08-30 18:46:21 +0000141 }
Asiri Rathnayake08eb2142016-10-06 11:15:41 +0000142#ifndef TEST_HAS_NO_EXCEPTIONS
Marshall Clow4fdb0702014-03-24 22:25:24 +0000143 {
144 std::future<void> f = std::async(f5, 3);
145 std::this_thread::sleep_for(ms(300));
Eric Fiselier53ca3c92016-04-30 02:30:18 +0000146 try { f.get(); assert (false); } catch ( int ) {}
Marshall Clow4fdb0702014-03-24 22:25:24 +0000147 }
Marshall Clow4fdb0702014-03-24 22:25:24 +0000148 {
149 std::future<void> f = std::async(std::launch::deferred, f5, 3);
150 std::this_thread::sleep_for(ms(300));
Eric Fiselier53ca3c92016-04-30 02:30:18 +0000151 try { f.get(); assert (false); } catch ( int ) {}
Marshall Clow4fdb0702014-03-24 22:25:24 +0000152 }
Asiri Rathnayake08eb2142016-10-06 11:15:41 +0000153#endif
JF Bastien2df59c52019-02-04 20:31:13 +0000154 return 0;
Howard Hinnantc2bf9e12011-05-18 00:47:00 +0000155}