1eb5cfb02SEric Fiselier //===----------------------------------------------------------------------===//
2eb5cfb02SEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eb5cfb02SEric Fiselier //
7eb5cfb02SEric Fiselier //===----------------------------------------------------------------------===//
8eb5cfb02SEric Fiselier //
9*a7f9895cSLouis Dionne // UNSUPPORTED: no-threads
1031cbe0f2SLouis Dionne // UNSUPPORTED: c++03
11eb5cfb02SEric Fiselier 
12eb5cfb02SEric Fiselier // <future>
13eb5cfb02SEric Fiselier 
14eb5cfb02SEric Fiselier // class packaged_task<R(ArgTypes...)>
15eb5cfb02SEric Fiselier 
16eb5cfb02SEric Fiselier // ~packaged_task();
17eb5cfb02SEric Fiselier 
18eb5cfb02SEric Fiselier #include <future>
19eb5cfb02SEric Fiselier #include <cassert>
20eb5cfb02SEric Fiselier 
2156462801SLouis Dionne #include "make_test_thread.h"
2208eb2148SAsiri Rathnayake #include "test_macros.h"
2308eb2148SAsiri Rathnayake 
24eb5cfb02SEric Fiselier class A
25eb5cfb02SEric Fiselier {
26eb5cfb02SEric Fiselier     long data_;
27eb5cfb02SEric Fiselier 
28eb5cfb02SEric Fiselier public:
A(long i)29eb5cfb02SEric Fiselier     explicit A(long i) : data_(i) {}
30eb5cfb02SEric Fiselier 
operator ()(long i,long j) const31eb5cfb02SEric Fiselier     long operator()(long i, long j) const {return data_ + i + j;}
32eb5cfb02SEric Fiselier };
33eb5cfb02SEric Fiselier 
func(std::packaged_task<double (int,char)>)34fd838227SEric Fiselier void func(std::packaged_task<double(int, char)>)
35eb5cfb02SEric Fiselier {
36eb5cfb02SEric Fiselier }
37eb5cfb02SEric Fiselier 
func2(std::packaged_task<double (int,char)> p)383f05377dSMuiez Ahmed void func2(std::packaged_task<double(int, char)> p) { p(3, 97); }
39eb5cfb02SEric Fiselier 
main(int,char **)402df59c50SJF Bastien int main(int, char**)
41eb5cfb02SEric Fiselier {
4208eb2148SAsiri Rathnayake #ifndef TEST_HAS_NO_EXCEPTIONS
43eb5cfb02SEric Fiselier     {
44eb5cfb02SEric Fiselier         std::packaged_task<double(int, char)> p(A(5));
45eb5cfb02SEric Fiselier         std::future<double> f = p.get_future();
4656462801SLouis Dionne         support::make_test_thread(func, std::move(p)).detach();
47eb5cfb02SEric Fiselier         try
48eb5cfb02SEric Fiselier         {
49eb5cfb02SEric Fiselier             double i = f.get();
504dc0ed83SStephan T. Lavavej             ((void)i); // Prevent unused warning
51eb5cfb02SEric Fiselier             assert(false);
52eb5cfb02SEric Fiselier         }
53eb5cfb02SEric Fiselier         catch (const std::future_error& e)
54eb5cfb02SEric Fiselier         {
55eb5cfb02SEric Fiselier             assert(e.code() == make_error_code(std::future_errc::broken_promise));
56eb5cfb02SEric Fiselier         }
57eb5cfb02SEric Fiselier     }
5808eb2148SAsiri Rathnayake #endif
59eb5cfb02SEric Fiselier     {
60eb5cfb02SEric Fiselier         std::packaged_task<double(int, char)> p(A(5));
61eb5cfb02SEric Fiselier         std::future<double> f = p.get_future();
6256462801SLouis Dionne         support::make_test_thread(func2, std::move(p)).detach();
63eb5cfb02SEric Fiselier         assert(f.get() == 105.0);
64eb5cfb02SEric Fiselier     }
652df59c50SJF Bastien 
662df59c50SJF Bastien   return 0;
67eb5cfb02SEric Fiselier }
68