]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/thread/test/sync/futures/future/wait_for_pass.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / thread / test / sync / futures / future / wait_for_pass.cpp
CommitLineData
7c673cae
FG
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_for(const chrono::duration<Rep, Period>& rel_time) const;
22
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
35typedef boost::chrono::milliseconds ms;
36
37namespace 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
63void func1(boost::promise<int> p)
64{
65 boost::this_thread::sleep_for(ms(500));
66 p.set_value(3);
67}
68
69int j = 0;
70
71void func3(boost::promise<int&> p)
72{
73 boost::this_thread::sleep_for(ms(500));
74 j = 5;
75 p.set_value(j);
76}
77
78void func5(boost::promise<void> p)
79{
80 boost::this_thread::sleep_for(ms(500));
81 p.set_value();
82}
83
84int 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#endif
96 BOOST_TEST(f.valid());
97 BOOST_TEST_EQ(f.wait_for(ms(300)) , boost::future_status::timeout);
98#if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
99#else
100 func1(boost::move(p));
101#endif
102 BOOST_TEST(f.valid());
103 BOOST_TEST_EQ(f.wait_for(ms(300)) , boost::future_status::ready);
104 BOOST_TEST(f.valid());
105 Clock::time_point t0 = Clock::now();
106 f.wait();
107 Clock::time_point t1 = Clock::now();
108 BOOST_TEST(f.valid());
109 BOOST_TEST(t1 - t0 < ms(50));
110 }
111 {
112 typedef int& T;
113 boost::promise<T> p;
114 boost::future<T> f = p.get_future();
115#if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
116 boost::thread(func3, boost::move(p)).detach();
117#endif
118 BOOST_TEST(f.valid());
119 BOOST_TEST_EQ(f.wait_for(ms(300)) , boost::future_status::timeout);
120 BOOST_TEST(f.valid());
121#if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
122#else
123 func3(boost::move(p));
124#endif
125 BOOST_TEST_EQ(f.wait_for(ms(300)) , boost::future_status::ready);
126 BOOST_TEST(f.valid());
127 Clock::time_point t0 = Clock::now();
128 f.wait();
129 Clock::time_point t1 = Clock::now();
130 BOOST_TEST(f.valid());
131 BOOST_TEST(t1 - t0 < ms(50));
132 }
133 {
134 typedef void T;
135 boost::promise<T> p;
136 boost::future<T> f = p.get_future();
137#if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
138 boost::thread(func5, boost::move(p)).detach();
139#endif
140 BOOST_TEST(f.valid());
141 BOOST_TEST_EQ(f.wait_for(ms(300)) , boost::future_status::timeout);
142 BOOST_TEST(f.valid());
143#if defined BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK && defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD)
144#else
145 func5(boost::move(p));
146#endif
147 BOOST_TEST_EQ(f.wait_for(ms(300)) , boost::future_status::ready);
148 BOOST_TEST(f.valid());
149 Clock::time_point t0 = Clock::now();
150 f.wait();
151 Clock::time_point t1 = Clock::now();
152 BOOST_TEST(f.valid());
153 BOOST_TEST(t1 - t0 < ms(50));
154 }
155 }
156 BOOST_THREAD_LOG << BOOST_THREAD_END_LOG;
157
158 return boost::report_errors();
159}
160
161#else
162#error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported"
163#endif