• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 //   future_status
21 //   wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
22 
23 //#define BOOST_THREAD_VERSION 3
24 #define BOOST_THREAD_VERSION 4
25 //#define BOOST_THREAD_USES_LOG
26 #define BOOST_THREAD_USES_LOG_THREAD_ID
27 #include <boost/thread/detail/log.hpp>
28 
29 #include <boost/thread/future.hpp>
30 #include <boost/thread/thread.hpp>
31 #include <boost/chrono/chrono_io.hpp>
32 #include <boost/detail/lightweight_test.hpp>
33 #include "../../../timming.hpp"
34 
35 #if defined BOOST_THREAD_USES_CHRONO
36 
37 #ifdef BOOST_MSVC
38 #pragma warning(disable: 4127) // conditional expression is constant
39 #endif
40 
41 typedef boost::chrono::milliseconds ms;
42 typedef boost::chrono::nanoseconds ns;
43 
44 namespace boost
45 {
46   template <typename OStream>
operator <<(OStream & os,boost::future_status st)47   OStream& operator<<(OStream& os , boost::future_status st )
48   {
49     os << underlying_cast<int>(st) << " ";
50     return os;
51   }
52   template <typename T>
53   struct wrap
54   {
wrapboost::wrap55     wrap(T const& v) :
56       value(v)
57     {
58     }
59     T value;
60 
61   };
62 
63   template <typename T>
make_exception_ptr(T v)64   exception_ptr make_exception_ptr(T v)
65   {
66     return copy_exception(wrap<T> (v));
67   }
68 }
69 
func1(boost::promise<int> p)70 void func1(boost::promise<int> p)
71 {
72   boost::this_thread::sleep_for(ms(500));
73   p.set_value(3);
74 }
75 
76 int j = 0;
77 
func3(boost::promise<int &> p)78 void func3(boost::promise<int&> p)
79 {
80   boost::this_thread::sleep_for(ms(500));
81   j = 5;
82   p.set_value(j);
83 }
84 
func5(boost::promise<void> p)85 void func5(boost::promise<void> p)
86 {
87   boost::this_thread::sleep_for(ms(500));
88   p.set_value();
89 }
90 
91 const ms max_diff(BOOST_THREAD_TEST_TIME_MS);
92 
main()93 int main()
94 {
95   BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
96   {
97     typedef boost::chrono::high_resolution_clock Clock;
98     {
99       typedef int T;
100       boost::promise<T> p;
101       boost::future<T> f = p.get_future();
102 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
103       boost::thread(func1, boost::move(p)).detach();
104 #endif
105       BOOST_TEST(f.valid());
106       BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(250)) , boost::future_status::timeout);
107 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
108 #else
109       func1(boost::move(p));
110 #endif
111       BOOST_TEST(f.valid());
112       BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(750)) , boost::future_status::ready);
113       BOOST_TEST(f.valid());
114       Clock::time_point t0 = Clock::now();
115       f.wait();
116       Clock::time_point t1 = Clock::now();
117       BOOST_TEST(f.valid());
118       ns d = t1 - t0;
119       BOOST_THREAD_TEST_IT(d, ns(max_diff));
120     }
121     {
122       typedef int& T;
123       boost::promise<T> p;
124       boost::future<T> f = p.get_future();
125 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
126       boost::thread(func3, boost::move(p)).detach();
127 #endif
128       BOOST_TEST(f.valid());
129       BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(250)) , boost::future_status::timeout);
130       BOOST_TEST(f.valid());
131 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
132 #else
133       func3(boost::move(p));
134 #endif
135       BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(750)) , boost::future_status::ready);
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       ns d = t1 - t0;
142       BOOST_THREAD_TEST_IT(d, ns(max_diff));
143     }
144     {
145       typedef void T;
146       boost::promise<T> p;
147       boost::future<T> f = p.get_future();
148 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
149       boost::thread(func5, boost::move(p)).detach();
150 #endif
151       BOOST_TEST(f.valid());
152       BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(250)) , boost::future_status::timeout);
153       BOOST_TEST(f.valid());
154 #if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
155 #else
156       func5(boost::move(p));
157 #endif
158       BOOST_TEST_EQ(f.wait_until(Clock::now() + ms(750)) , boost::future_status::ready);
159       BOOST_TEST(f.valid());
160       Clock::time_point t0 = Clock::now();
161       f.wait();
162       Clock::time_point t1 = Clock::now();
163       BOOST_TEST(f.valid());
164       ns d = t1 - t0;
165       BOOST_THREAD_TEST_IT(d, ns(max_diff));
166     }
167   }
168   BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
169 
170   return boost::report_errors();
171 }
172 
173 #else
174 #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
175 #endif
176