]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/thread/test/sync/futures/async/async_executor_pass.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / thread / test / sync / futures / async / async_executor_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) 2011 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 // template <class Executor, class F, class... Args>
18 // future<typename result_of<F(Args...)>::type>
19 // async(Executor& ex, F&& f, Args&&... args);
20
21 #define BOOST_THREAD_VERSION 4
22 #define BOOST_THREAD_PROVIDES_EXECUTORS
23 #include <boost/config.hpp>
24 #if ! defined BOOST_NO_CXX11_DECLTYPE
25 #define BOOST_RESULT_OF_USE_DECLTYPE
26 #endif
27 #include <iostream>
28 #include <boost/thread/future.hpp>
29 #include <boost/thread/thread.hpp>
30 #include <boost/thread/detail/memory.hpp>
31 #include <boost/thread/csbl/memory/unique_ptr.hpp>
32 #include <memory>
33 #include <boost/detail/lightweight_test.hpp>
34 #include <boost/thread/executors/basic_thread_pool.hpp>
35 #include <boost/thread/executor.hpp>
36
37 typedef boost::chrono::high_resolution_clock Clock;
38 typedef boost::chrono::milliseconds ms;
39
40 class A
41 {
42 long data_;
43
44 public:
45 typedef long result_type;
46
47 explicit A(long i) :
48 data_(i)
49 {
50 }
51
52 long doit() const
53 {
54 boost::this_thread::sleep_for(ms(200));
55 return data_;
56 }
57 long operator()() const
58 {
59 boost::this_thread::sleep_for(ms(200));
60 return data_;
61 }
62 };
63
64 class MoveOnly
65 {
66 public:
67 typedef int result_type;
68
69 int value;
70
71 BOOST_THREAD_MOVABLE_ONLY(MoveOnly)
72 MoveOnly()
73 {
74 value = 0;
75 }
76 MoveOnly( BOOST_THREAD_RV_REF(MoveOnly))
77 {
78 value = 1;
79 }
80 MoveOnly& operator=(BOOST_THREAD_RV_REF(MoveOnly))
81 {
82 value = 2;
83 return *this;
84 }
85
86 int operator()()
87 {
88 boost::this_thread::sleep_for(ms(200));
89 return 3;
90 }
91 template <typename OS>
92 friend OS& operator<<(OS& os, MoveOnly const& v)
93 {
94 os << v.value;
95 return os;
96 }
97 };
98
99 namespace boost
100 {
101 BOOST_THREAD_DCL_MOVABLE (MoveOnly)
102 }
103
104 int f0()
105 {
106 boost::this_thread::sleep_for(ms(200));
107 return 3;
108 }
109
110 int i = 0;
111
112 int& f1()
113 {
114 boost::this_thread::sleep_for(ms(200));
115 return i;
116 }
117
118 void f2()
119 {
120 boost::this_thread::sleep_for(ms(200));
121 }
122
123 boost::csbl::unique_ptr<int> f3_0()
124 {
125 boost::this_thread::sleep_for(ms(200));
126 boost::csbl::unique_ptr<int> r( (new int(3)));
127 return boost::move(r);
128 }
129 MoveOnly f3_1()
130 {
131 boost::this_thread::sleep_for(ms(200));
132 MoveOnly r;
133 return boost::move(r);
134 }
135
136 boost::csbl::unique_ptr<int> f3(int i)
137 {
138 boost::this_thread::sleep_for(ms(200));
139 return boost::csbl::unique_ptr<int>(new int(i));
140 }
141
142 boost::csbl::unique_ptr<int> f4(
143 BOOST_THREAD_RV_REF_BEG boost::csbl::unique_ptr<int> BOOST_THREAD_RV_REF_END p
144 )
145 {
146 boost::this_thread::sleep_for(ms(200));
147 return boost::move(p);
148 }
149
150 struct check_timer {
151 boost::chrono::nanoseconds delay;
152 Clock::time_point start;
153 check_timer(boost::chrono::nanoseconds delay)
154 : delay(delay)
155 , start(Clock::now())
156 {
157 }
158 ~check_timer() {
159 Clock::time_point now = Clock::now();
160 BOOST_TEST(now - start < delay);
161 std::cout << __FILE__ << "[" << __LINE__ << "] " << (now - start).count() << std::endl;
162 }
163
164 };
165
166 int main()
167 {
168 std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
169 #if defined BOOST_THREAD_PROVIDES_EXECUTORS
170 {
171 try
172 {
173 boost::executor_adaptor<boost::basic_thread_pool> ex(1);
174 boost::future<int> f = boost::async(ex, &f0);
175 boost::this_thread::sleep_for(ms(300));
176 int res;
177 {
178 check_timer timer(ms(500));
179 res = f.get();
180 }
181 BOOST_TEST(res == 3);
182 }
183 catch (std::exception& ex)
184 {
185 std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
186 BOOST_TEST(false && "exception thrown");
187 }
188 catch (...)
189 {
190 BOOST_TEST(false && "exception thrown");
191 }
192 }
193 #endif
194 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) && defined BOOST_THREAD_PROVIDES_EXECUTORS
195 std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
196 {
197 try
198 {
199 boost::executor_adaptor<boost::basic_thread_pool> ex(1);
200 boost::future<long> f = boost::async(ex, A(3));
201 boost::this_thread::sleep_for(ms(300));
202 int res;
203 {
204 check_timer timer(ms(500));
205 res = f.get();
206 }
207 BOOST_TEST(res == 3);
208 }
209 catch (std::exception& ex)
210 {
211 std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
212 BOOST_TEST(false && "exception thrown");
213 }
214 catch (...)
215 {
216 BOOST_TEST(false && "exception thrown");
217 }
218
219 }
220 #endif
221 #if defined(BOOST_THREAD_PROVIDES_VARIADIC_THREAD) && defined BOOST_THREAD_PROVIDES_EXECUTORS
222 std::cout << __FILE__ << "[" << __LINE__ << "]" << std::endl;
223 {
224 try
225 {
226 boost::executor_adaptor<boost::basic_thread_pool> ex(1);
227 MoveOnly mo;
228 boost::future<int> f = boost::async(ex, boost::move(mo));
229 //boost::future<int> f = boost::async(ex, MoveOnly());
230 boost::this_thread::sleep_for(ms(300));
231 int res;
232 {
233 check_timer timer(ms(500));
234 res = f.get();
235 }
236 BOOST_TEST(res == 3);
237 }
238 catch (std::exception& ex)
239 {
240 std::cout << __FILE__ << "[" << __LINE__ << "]" << ex.what() << std::endl;
241 BOOST_TEST(false && "exception thrown");
242 }
243 catch (...)
244 {
245 BOOST_TEST(false && "exception thrown");
246 }
247 }
248 #endif
249
250 return boost::report_errors();
251 }