blob: 5cb824d48224077b28f263c0fb9b224cf230acb2 [file] [log] [blame]
Howard Hinnant27f000e2010-08-30 18:46:21 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Howard Hinnant412dbeb2010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnant27f000e2010-08-30 18:46:21 +00007//
8//===----------------------------------------------------------------------===//
Jonathan Roelofsb3fcc672014-09-05 19:45:05 +00009//
Asiri Rathnayakef520c142015-11-10 11:41:22 +000010// XFAIL: libcpp-no-exceptions
Jonathan Roelofsb3fcc672014-09-05 19:45:05 +000011// UNSUPPORTED: libcpp-has-no-threads
Eric Fiselier10e51f82015-07-28 07:49:15 +000012// UNSUPPORTED: c++98, c++03
Howard Hinnant27f000e2010-08-30 18:46:21 +000013
14// <future>
15
16// template <class F, class... Args>
17// future<typename result_of<F(Args...)>::type>
18// async(F&& f, Args&&... args);
19
20// template <class F, class... Args>
21// future<typename result_of<F(Args...)>::type>
22// async(launch policy, F&& f, Args&&... args);
23
Eric Fiselier382e9172016-04-29 04:07:45 +000024
Howard Hinnant27f000e2010-08-30 18:46:21 +000025#include <future>
Eric Fiselier382e9172016-04-29 04:07:45 +000026#include <atomic>
Howard Hinnant27f000e2010-08-30 18:46:21 +000027#include <memory>
28#include <cassert>
29
Eric Fiselier382e9172016-04-29 04:07:45 +000030#include "test_macros.h"
31
Howard Hinnant27f000e2010-08-30 18:46:21 +000032typedef std::chrono::high_resolution_clock Clock;
33typedef std::chrono::milliseconds ms;
34
Eric Fiselier382e9172016-04-29 04:07:45 +000035std::atomic_bool invoked = ATOMIC_VAR_INIT(false);
36
Howard Hinnant27f000e2010-08-30 18:46:21 +000037int f0()
38{
Eric Fiselier382e9172016-04-29 04:07:45 +000039 invoked = true;
Howard Hinnant27f000e2010-08-30 18:46:21 +000040 std::this_thread::sleep_for(ms(200));
41 return 3;
42}
43
44int i = 0;
45
46int& f1()
47{
Eric Fiselier382e9172016-04-29 04:07:45 +000048 invoked = true;
Howard Hinnant27f000e2010-08-30 18:46:21 +000049 std::this_thread::sleep_for(ms(200));
50 return i;
51}
52
53void f2()
54{
Eric Fiselier382e9172016-04-29 04:07:45 +000055 invoked = true;
Howard Hinnant27f000e2010-08-30 18:46:21 +000056 std::this_thread::sleep_for(ms(200));
57}
58
Eric Fiselier588e1db2016-04-28 02:00:52 +000059std::unique_ptr<int> f3(int j)
Howard Hinnant27f000e2010-08-30 18:46:21 +000060{
Eric Fiselier382e9172016-04-29 04:07:45 +000061 invoked = true;
Howard Hinnant27f000e2010-08-30 18:46:21 +000062 std::this_thread::sleep_for(ms(200));
Eric Fiselier588e1db2016-04-28 02:00:52 +000063 return std::unique_ptr<int>(new int(j));
Howard Hinnant27f000e2010-08-30 18:46:21 +000064}
65
66std::unique_ptr<int> f4(std::unique_ptr<int>&& p)
67{
Eric Fiselier382e9172016-04-29 04:07:45 +000068 invoked = true;
Howard Hinnant27f000e2010-08-30 18:46:21 +000069 std::this_thread::sleep_for(ms(200));
70 return std::move(p);
71}
72
Eric Fiselier588e1db2016-04-28 02:00:52 +000073void f5(int j)
Marshall Clow4fdb0702014-03-24 22:25:24 +000074{
75 std::this_thread::sleep_for(ms(200));
Eric Fiselier588e1db2016-04-28 02:00:52 +000076 throw j;
Marshall Clow4fdb0702014-03-24 22:25:24 +000077}
78
Eric Fiselier382e9172016-04-29 04:07:45 +000079template <class Ret, class CheckLamdba, class ...Args>
80void test(CheckLamdba&& getAndCheckFn, bool IsDeferred, Args&&... args) {
81 // Reset global state.
82 invoked = false;
83
84 // Create the future and wait
85 std::future<Ret> f = std::async(std::forward<Args>(args)...);
86 std::this_thread::sleep_for(ms(300));
87
88 // Check that deferred async's have not invoked the function.
89 assert(invoked == !IsDeferred);
90
91 // Time the call to f.get() and check that the returned value matches
92 // what is expected.
93 Clock::time_point t0 = Clock::now();
94 assert(getAndCheckFn(f));
95 Clock::time_point t1 = Clock::now();
96
97 // If the async is deferred it should take more than 100ms, otherwise
98 // it should take less than 100ms.
99 if (IsDeferred) {
100 assert(t1-t0 > ms(100));
101 } else {
102 assert(t1-t0 < ms(100));
103 }
104}
105
Howard Hinnant27f000e2010-08-30 18:46:21 +0000106int main()
107{
Eric Fiselier382e9172016-04-29 04:07:45 +0000108 // The default launch policy is implementation defined. libc++ defines
109 // it to be std::launch::async.
110 bool DefaultPolicyIsDeferred = false;
111 bool DPID = DefaultPolicyIsDeferred;
112
113 std::launch AnyPolicy = std::launch::async | std::launch::deferred;
114 LIBCPP_ASSERT(AnyPolicy == std::launch::any);
Howard Hinnant27f000e2010-08-30 18:46:21 +0000115
116 {
Eric Fiselier382e9172016-04-29 04:07:45 +0000117 auto checkInt = [](std::future<int>& f) { return f.get() == 3; };
118 test<int>(checkInt, DPID, f0);
119 test<int>(checkInt, false, std::launch::async, f0);
120 test<int>(checkInt, true, std::launch::deferred, f0);
121 test<int>(checkInt, DPID, AnyPolicy, f0);
Howard Hinnant27f000e2010-08-30 18:46:21 +0000122 }
123 {
Eric Fiselier382e9172016-04-29 04:07:45 +0000124 auto checkIntRef = [&](std::future<int&>& f) { return &f.get() == &i; };
125 test<int&>(checkIntRef, DPID, f1);
126 test<int&>(checkIntRef, false, std::launch::async, f1);
127 test<int&>(checkIntRef, true, std::launch::deferred, f1);
128 test<int&>(checkIntRef, DPID, AnyPolicy, f1);
Howard Hinnant27f000e2010-08-30 18:46:21 +0000129 }
130 {
Eric Fiselier382e9172016-04-29 04:07:45 +0000131 auto checkVoid = [](std::future<void>& f) { f.get(); return true; };
132 test<void>(checkVoid, DPID, f2);
133 test<void>(checkVoid, false, std::launch::async, f2);
134 test<void>(checkVoid, true, std::launch::deferred, f2);
Eric Fiselier53ca3c92016-04-30 02:30:18 +0000135 test<void>(checkVoid, DPID, AnyPolicy, f2);
Howard Hinnant27f000e2010-08-30 18:46:21 +0000136 }
137 {
Eric Fiselier382e9172016-04-29 04:07:45 +0000138 using Ret = std::unique_ptr<int>;
139 auto checkUPtr = [](std::future<Ret>& f) { return *f.get() == 3; };
140 test<Ret>(checkUPtr, DPID, f3, 3);
141 test<Ret>(checkUPtr, DPID, f4, std::unique_ptr<int>(new int(3)));
Howard Hinnant27f000e2010-08-30 18:46:21 +0000142 }
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 }
Howard Hinnantc2bf9e12011-05-18 00:47:00 +0000153}