1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // UNSUPPORTED: libcpp-has-no-threads
11 // UNSUPPORTED: c++98, c++03
12 
13 // <future>
14 
15 // class packaged_task<R(ArgTypes...)>
16 
17 // void operator()(ArgTypes... args);
18 
19 #include <future>
20 #include <cassert>
21 
22 #include "test_macros.h"
23 
24 class A
25 {
26     long data_;
27 
28 public:
29     explicit A(long i) : data_(i) {}
30 
31     long operator()(long i, long j) const
32     {
33         if (j == 'z')
34             TEST_THROW(A(6));
35         return data_ + i + j;
36     }
37 };
38 
39 void func0(std::packaged_task<double(int, char)> p)
40 {
41     std::this_thread::sleep_for(std::chrono::milliseconds(500));
42     p(3, 'a');
43 }
44 
45 void func1(std::packaged_task<double(int, char)> p)
46 {
47     std::this_thread::sleep_for(std::chrono::milliseconds(500));
48     p(3, 'z');
49 }
50 
51 void func2(std::packaged_task<double(int, char)> p)
52 {
53 #ifndef TEST_HAS_NO_EXCEPTIONS
54     p(3, 'a');
55     try
56     {
57         p(3, 'c');
58     }
59     catch (const std::future_error& e)
60     {
61         assert(e.code() == make_error_code(std::future_errc::promise_already_satisfied));
62     }
63 #endif
64 }
65 
66 void func3(std::packaged_task<double(int, char)> p)
67 {
68 #ifndef TEST_HAS_NO_EXCEPTIONS
69     try
70     {
71         p(3, 'a');
72     }
73     catch (const std::future_error& e)
74     {
75         assert(e.code() == make_error_code(std::future_errc::no_state));
76     }
77 #endif
78 }
79 
80 int main()
81 {
82     {
83         std::packaged_task<double(int, char)> p(A(5));
84         std::future<double> f = p.get_future();
85         std::thread(func0, std::move(p)).detach();
86         assert(f.get() == 105.0);
87     }
88 #ifndef TEST_HAS_NO_EXCEPTIONS
89     {
90         std::packaged_task<double(int, char)> p(A(5));
91         std::future<double> f = p.get_future();
92         std::thread(func1, std::move(p)).detach();
93         try
94         {
95             f.get();
96             assert(false);
97         }
98         catch (const A& e)
99         {
100             assert(e(3, 'a') == 106);
101         }
102     }
103     {
104         std::packaged_task<double(int, char)> p(A(5));
105         std::future<double> f = p.get_future();
106         std::thread t(func2, std::move(p));
107         assert(f.get() == 105.0);
108         t.join();
109     }
110     {
111         std::packaged_task<double(int, char)> p;
112         std::thread t(func3, std::move(p));
113         t.join();
114     }
115 #endif
116 }
117