Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / thread / test / sync / futures / future / wait_pass.cpp
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 // Copyright (C) 2013 Vicente J. Botet Escriba
11 //
12 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
13 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
14
15 // <boost/thread/future.hpp>
16
17 // class future<R>
18
19 // template <class Rep, class Period>
20 // void wait() const;
21
22 //#define BOOST_THREAD_VERSION 3
23 #define BOOST_THREAD_VERSION 4
24 //#define BOOST_THREAD_USES_LOG
25 #define BOOST_THREAD_USES_LOG_THREAD_ID
26 #include <boost/thread/detail/log.hpp>
27
28 #include <boost/thread/future.hpp>
29 #include <boost/thread/thread.hpp>
30 #include <boost/chrono/chrono_io.hpp>
31 #include <boost/detail/lightweight_test.hpp>
32
33 #if defined BOOST_THREAD_USES_CHRONO
34
35 typedef boost::chrono::milliseconds ms;
36
37 namespace boost
38 {
39   template <typename OStream>
40   OStream& operator<<(OStream& os , boost::future_status st )
41   {
42     os << underlying_cast<int>(st) << " ";
43     return os;
44   }
45   template <typename T>
46   struct wrap
47   {
48     wrap(T const& v) :
49       value(v)
50     {
51     }
52     T value;
53
54   };
55
56   template <typename T>
57   exception_ptr make_exception_ptr(T v)
58   {
59     return copy_exception(wrap<T> (v));
60   }
61 }
62
63 void func1(boost::promise<int> p)
64 {
65   boost::this_thread::sleep_for(ms(500));
66   p.set_value(3);
67 }
68
69 int j = 0;
70
71 void func3(boost::promise<int&> p)
72 {
73   boost::this_thread::sleep_for(ms(500));
74   j = 5;
75   p.set_value(j);
76 }
77
78 void func5(boost::promise<void> p)
79 {
80   boost::this_thread::sleep_for(ms(500));
81   p.set_value();
82 }
83
84 int main()
85 {
86   BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
87   {
88     typedef boost::chrono::high_resolution_clock Clock;
89     {
90       typedef int T;
91       boost::promise<T> p;
92       boost::future<T> f = p.get_future();
93 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
94       boost::thread(func1, boost::move(p)).detach();
95 #else
96       func1(boost::move(p));
97 #endif
98       BOOST_TEST(f.valid());
99       f.wait();
100       BOOST_TEST(f.valid());
101       Clock::time_point t0 = Clock::now();
102       f.wait();
103       Clock::time_point t1 = Clock::now();
104       BOOST_TEST(f.valid());
105       BOOST_TEST(t1 - t0 < ms(50));
106     }
107     {
108       typedef int& T;
109       boost::promise<T> p;
110       boost::future<T> f = p.get_future();
111 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
112       boost::thread(func3, boost::move(p)).detach();
113 #else
114       func3(boost::move(p));
115 #endif
116       BOOST_TEST(f.valid());
117       f.wait();
118       BOOST_TEST(f.valid());
119       Clock::time_point t0 = Clock::now();
120       f.wait();
121       Clock::time_point t1 = Clock::now();
122       BOOST_TEST(f.valid());
123       BOOST_TEST(t1 - t0 < ms(50));
124     }
125     {
126       typedef void T;
127       boost::promise<T> p;
128       boost::future<T> f = p.get_future();
129 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
130       boost::thread(func5, boost::move(p)).detach();
131 #else
132       func5(boost::move(p));
133 #endif
134       BOOST_TEST(f.valid());
135       f.wait();
136       BOOST_TEST(f.valid());
137       Clock::time_point t0 = Clock::now();
138       f.wait();
139       Clock::time_point t1 = Clock::now();
140       BOOST_TEST(f.valid());
141       BOOST_TEST(t1 - t0 < ms(50));
142     }
143   }
144   BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
145
146   return boost::report_errors();
147 }
148
149 #else
150 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
151 #endif