Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Howard Hinnant | 412dbeb | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | b3fcc67 | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // UNSUPPORTED: libcpp-has-no-threads |
Eric Fiselier | 10e51f8 | 2015-07-28 07:49:15 +0000 | [diff] [blame] | 11 | // UNSUPPORTED: c++98, c++03 |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 12 | |
| 13 | // <future> |
| 14 | |
| 15 | // template <class F, class... Args> |
| 16 | // future<typename result_of<F(Args...)>::type> |
| 17 | // async(F&& f, Args&&... args); |
| 18 | |
| 19 | // template <class F, class... Args> |
| 20 | // future<typename result_of<F(Args...)>::type> |
| 21 | // async(launch policy, F&& f, Args&&... args); |
| 22 | |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 23 | |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 24 | #include <future> |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 25 | #include <atomic> |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 26 | #include <memory> |
| 27 | #include <cassert> |
| 28 | |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 29 | #include "test_macros.h" |
| 30 | |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 31 | typedef std::chrono::high_resolution_clock Clock; |
| 32 | typedef std::chrono::milliseconds ms; |
| 33 | |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 34 | std::atomic_bool invoked = ATOMIC_VAR_INIT(false); |
| 35 | |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 36 | int f0() |
| 37 | { |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 38 | invoked = true; |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 39 | std::this_thread::sleep_for(ms(200)); |
| 40 | return 3; |
| 41 | } |
| 42 | |
| 43 | int i = 0; |
| 44 | |
| 45 | int& f1() |
| 46 | { |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 47 | invoked = true; |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 48 | std::this_thread::sleep_for(ms(200)); |
| 49 | return i; |
| 50 | } |
| 51 | |
| 52 | void f2() |
| 53 | { |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 54 | invoked = true; |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 55 | std::this_thread::sleep_for(ms(200)); |
| 56 | } |
| 57 | |
Eric Fiselier | 588e1db | 2016-04-28 02:00:52 +0000 | [diff] [blame] | 58 | std::unique_ptr<int> f3(int j) |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 59 | { |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 60 | invoked = true; |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 61 | std::this_thread::sleep_for(ms(200)); |
Eric Fiselier | 588e1db | 2016-04-28 02:00:52 +0000 | [diff] [blame] | 62 | return std::unique_ptr<int>(new int(j)); |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | std::unique_ptr<int> f4(std::unique_ptr<int>&& p) |
| 66 | { |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 67 | invoked = true; |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 68 | std::this_thread::sleep_for(ms(200)); |
| 69 | return std::move(p); |
| 70 | } |
| 71 | |
Eric Fiselier | 588e1db | 2016-04-28 02:00:52 +0000 | [diff] [blame] | 72 | void f5(int j) |
Marshall Clow | 4fdb070 | 2014-03-24 22:25:24 +0000 | [diff] [blame] | 73 | { |
| 74 | std::this_thread::sleep_for(ms(200)); |
Eric Fiselier | dee07a6 | 2016-12-24 04:34:33 +0000 | [diff] [blame^] | 75 | ((void)j); |
| 76 | TEST_THROW(j); |
Marshall Clow | 4fdb070 | 2014-03-24 22:25:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 79 | template <class Ret, class CheckLamdba, class ...Args> |
| 80 | void 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 Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 106 | int main() |
| 107 | { |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 108 | // 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 Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 115 | |
| 116 | { |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 117 | 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 Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 122 | } |
| 123 | { |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 124 | 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 Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 129 | } |
| 130 | { |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 131 | 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 Fiselier | 53ca3c9 | 2016-04-30 02:30:18 +0000 | [diff] [blame] | 135 | test<void>(checkVoid, DPID, AnyPolicy, f2); |
Howard Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 136 | } |
| 137 | { |
Eric Fiselier | 382e917 | 2016-04-29 04:07:45 +0000 | [diff] [blame] | 138 | 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 Hinnant | 27f000e | 2010-08-30 18:46:21 +0000 | [diff] [blame] | 142 | } |
Asiri Rathnayake | 08eb214 | 2016-10-06 11:15:41 +0000 | [diff] [blame] | 143 | #ifndef TEST_HAS_NO_EXCEPTIONS |
Marshall Clow | 4fdb070 | 2014-03-24 22:25:24 +0000 | [diff] [blame] | 144 | { |
| 145 | std::future<void> f = std::async(f5, 3); |
| 146 | std::this_thread::sleep_for(ms(300)); |
Eric Fiselier | 53ca3c9 | 2016-04-30 02:30:18 +0000 | [diff] [blame] | 147 | try { f.get(); assert (false); } catch ( int ) {} |
Marshall Clow | 4fdb070 | 2014-03-24 22:25:24 +0000 | [diff] [blame] | 148 | } |
Marshall Clow | 4fdb070 | 2014-03-24 22:25:24 +0000 | [diff] [blame] | 149 | { |
| 150 | std::future<void> f = std::async(std::launch::deferred, f5, 3); |
| 151 | std::this_thread::sleep_for(ms(300)); |
Eric Fiselier | 53ca3c9 | 2016-04-30 02:30:18 +0000 | [diff] [blame] | 152 | try { f.get(); assert (false); } catch ( int ) {} |
Marshall Clow | 4fdb070 | 2014-03-24 22:25:24 +0000 | [diff] [blame] | 153 | } |
Asiri Rathnayake | 08eb214 | 2016-10-06 11:15:41 +0000 | [diff] [blame] | 154 | #endif |
Howard Hinnant | c2bf9e1 | 2011-05-18 00:47:00 +0000 | [diff] [blame] | 155 | } |