-
-
Notifications
You must be signed in to change notification settings - Fork 423
/
wait_all.hpp
411 lines (349 loc) · 14.7 KB
/
wait_all.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Copyright (c) 2007-2015 Hartmut Kaiser
// Copyright (c) 2013 Agustin Berge
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
/// \file lcos/wait_all.hpp
#if !defined(HPX_LCOS_WAIT_ALL_APR_19_2012_1140AM)
#define HPX_LCOS_WAIT_ALL_APR_19_2012_1140AM
#if defined(DOXYGEN)
namespace hpx
{
/// The function \a wait_all is a operator allowing to join on the result
/// of all given futures. It AND-composes all future objects given and
/// returns the same list of futures after they finished executing.
///
/// \param first The iterator pointing to the first element of a
/// sequence of \a future or \a shared_future objects for
/// which \a wait_all should wait.
/// \param last The iterator pointing to the last element of a
/// sequence of \a future or \a shared_future objects for
/// which \a wait_all should wait.
///
/// \note The function \a wait_all returns after all futures have become
/// ready. All input futures are still valid after \a wait_all
/// returns.
///
template <typename InputIter>
void wait_all(InputIter first, InputIter last);
/// The function \a wait_all is a operator allowing to join on the result
/// of all given futures. It AND-composes all future objects given and
/// returns the same list of futures after they finished executing.
///
/// \param futures A vector holding an arbitrary amount of \a future or
/// \a shared_future objects for which \a wait_all should
/// wait.
///
/// \note The function \a wait_all returns after all futures have become
/// ready. All input futures are still valid after \a wait_all
/// returns.
///
template <typename R>
void wait_all(std::vector<future<R>>&& futures);
/// The function \a wait_all is a operator allowing to join on the result
/// of all given futures. It AND-composes all future objects given and
/// returns the same list of futures after they finished executing.
///
/// \param futures An arbitrary number of \a future or \a shared_future
/// objects, possibly holding different types for which
/// \a wait_all should wait.
///
/// \note The function \a wait_all returns after all futures have become
/// ready. All input futures are still valid after \a wait_all
/// returns.
///
template <typename ...T>
void wait_all(T &&... futures);
/// The function \a wait_all_n is a operator allowing to join on the result
/// of all given futures. It AND-composes all future objects given and
/// returns the same list of futures after they finished executing.
///
/// \param begin The iterator pointing to the first element of a
/// sequence of \a future or \a shared_future objects for
/// which \a wait_all_n should wait.
/// \param count The number of elements in the sequence starting at
/// \a first.
///
/// \return The function \a wait_all_n will return an iterator
/// referring to the first element in the input sequence
/// after the last processed element.
///
/// \note The function \a wait_all_n returns after all futures have become
/// ready. All input futures are still valid after \a wait_all_n
/// returns.
///
template <typename InputIter>
InputIter wait_all_n(InputIter begin, std::size_t count);
}
#else // DOXYGEN
#include <hpx/hpx_fwd.hpp>
#include <hpx/lcos/future.hpp>
#include <hpx/lcos/wait_some.hpp>
#include <hpx/util/always_void.hpp>
#include <hpx/util/decay.hpp>
#include <hpx/util/move.hpp>
#include <hpx/util/tuple.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/fusion/include/begin.hpp>
#include <boost/fusion/include/end.hpp>
#include <boost/fusion/include/deref.hpp>
#include <boost/fusion/include/next.hpp>
#include <boost/range/functions.hpp>
#include <boost/ref.hpp>
#include <boost/type_traits/is_same.hpp>
#include <algorithm>
#include <iterator>
#include <vector>
///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace lcos
{
namespace detail
{
///////////////////////////////////////////////////////////////////////
template <typename Future, typename Enable = void>
struct is_future_or_shared_state
: traits::is_future<Future>
{};
template <typename R>
struct is_future_or_shared_state<
boost::intrusive_ptr<future_data<R> > >
: boost::mpl::true_
{};
///////////////////////////////////////////////////////////////////////
template <typename Range, typename Enable = void>
struct is_future_or_shared_state_range
: boost::mpl::false_
{};
template <typename T>
struct is_future_or_shared_state_range<std::vector<T> >
: is_future_or_shared_state<T>
{};
///////////////////////////////////////////////////////////////////////
template <typename Future, typename Enable = void>
struct future_or_shared_state_result;
template <typename Future>
struct future_or_shared_state_result<Future,
typename boost::enable_if<traits::is_future<Future> >::type>
: traits::future_traits<Future>
{};
template <typename R>
struct future_or_shared_state_result<
boost::intrusive_ptr<future_data<R> > >
{
typedef R type;
};
///////////////////////////////////////////////////////////////////////
template <typename Tuple>
struct wait_all_frame //-V690
: hpx::lcos::detail::future_data<void>
{
private:
// workaround gcc regression wrongly instantiating constructors
wait_all_frame();
wait_all_frame(wait_all_frame const&);
typedef typename boost::fusion::result_of::end<Tuple const>::type
end_type;
public:
wait_all_frame(Tuple const& t)
: t_(t)
{}
protected:
// End of the tuple is reached
template <typename TupleIter>
BOOST_FORCEINLINE
void await(TupleIter&&, boost::mpl::true_)
{
this->set_result(util::unused); // simply make ourself ready
}
// Current element is a range (vector) of futures
template <typename TupleIter, typename Iter>
void await_range(TupleIter iter, Iter next, Iter end)
{
typedef typename std::iterator_traits<Iter>::value_type
future_type;
typedef typename detail::future_or_shared_state_result<
future_type
>::type future_result_type;
void (wait_all_frame::*f)(TupleIter, Iter, Iter) =
&wait_all_frame::await_range;
for (/**/; next != end; ++next)
{
boost::intrusive_ptr<
lcos::detail::future_data<future_result_type>
> next_future_data = lcos::detail::get_shared_state(*next);
if (!next_future_data->is_ready())
{
next_future_data->execute_deferred();
// execute_deferred might have made the future ready
if (!next_future_data->is_ready())
{
// Attach a continuation to this future which will
// re-evaluate it and continue to the next element
// in the sequence (if any).
next_future_data->set_on_completed(
util::bind(
f, this, std::move(iter),
std::move(next), std::move(end)));
return;
}
}
}
// All elements of the sequence are ready now, proceed to the
// next argument.
typedef typename boost::fusion::result_of::next<TupleIter>::type
next_type;
typedef boost::is_same<next_type, end_type> pred;
await(boost::fusion::next(iter), pred());
}
template <typename TupleIter>
BOOST_FORCEINLINE
void await_next(TupleIter iter, boost::mpl::false_, boost::mpl::true_)
{
await_range(iter,
boost::begin(boost::unwrap_ref(boost::fusion::deref(iter))),
boost::end(boost::unwrap_ref(boost::fusion::deref(iter))));
}
// Current element is a simple future
template <typename TupleIter>
BOOST_FORCEINLINE
void await_next(TupleIter iter, boost::mpl::true_, boost::mpl::false_)
{
typedef typename util::decay_unwrap<
typename boost::fusion::result_of::deref<TupleIter>::type
>::type future_type;
typedef typename detail::future_or_shared_state_result<
future_type
>::type future_result_type;
using boost::mpl::false_;
using boost::mpl::true_;
boost::intrusive_ptr<
lcos::detail::future_data<future_result_type>
> next_future_data = lcos::detail::get_shared_state(
boost::fusion::deref(iter));
if (!next_future_data->is_ready())
{
next_future_data->execute_deferred();
// execute_deferred might have made the future ready
if (!next_future_data->is_ready())
{
// Attach a continuation to this future which will
// re-evaluate it and continue to the next argument
// (if any).
void (wait_all_frame::*f)(TupleIter, true_, false_) =
&wait_all_frame::await_next;
next_future_data->set_on_completed(hpx::util::bind(
f, this, std::move(iter), true_(), false_()));
}
}
else
{
typedef typename boost::fusion::result_of::next<TupleIter>::type
next_type;
typedef boost::is_same<next_type, end_type> pred;
await(boost::fusion::next(iter), pred());
}
}
template <typename TupleIter>
BOOST_FORCEINLINE
void await(TupleIter&& iter, boost::mpl::false_)
{
typedef typename util::decay_unwrap<
typename boost::fusion::result_of::deref<TupleIter>::type
>::type future_type;
typedef typename detail::is_future_or_shared_state<future_type>::type
is_future;
typedef typename detail::is_future_or_shared_state_range<future_type>::type
is_range;
await_next(std::forward<TupleIter>(iter), is_future(), is_range());
}
public:
void wait_all()
{
typedef typename boost::fusion::result_of::begin<Tuple const>::type
begin_type;
typedef boost::is_same<begin_type, end_type> pred;
await(boost::fusion::begin(t_), pred());
// If there are still futures which are not ready, suspend and
// wait.
if (!this->is_ready())
this->wait();
}
private:
Tuple const& t_;
};
}
///////////////////////////////////////////////////////////////////////////
template <typename Future>
void wait_all(std::vector<Future> const& values)
{
typedef hpx::util::tuple<std::vector<Future> const&> result_type;
typedef detail::wait_all_frame<result_type> frame_type;
result_type data(values);
frame_type frame(data);
frame.wait_all();
}
template <typename Future>
BOOST_FORCEINLINE void wait_all(std::vector<Future>& values)
{
lcos::wait_all(const_cast<std::vector<Future> const&>(values));
}
template <typename Future>
BOOST_FORCEINLINE void wait_all(std::vector<Future>&& values)
{
lcos::wait_all(const_cast<std::vector<Future> const&>(values));
}
template <typename Iterator>
typename util::always_void<
typename lcos::detail::future_iterator_traits<Iterator>::type
>::type
wait_all(Iterator begin, Iterator end)
{
typedef typename lcos::detail::future_iterator_traits<Iterator>::type
future_type;
typedef typename lcos::detail::shared_state_ptr_for<future_type>::type
shared_state_ptr;
typedef std::vector<shared_state_ptr> result_type;
result_type values;
std::transform(begin, end, std::back_inserter(values),
detail::wait_get_shared_state<future_type>());
lcos::wait_all(values);
}
template <typename Iterator>
Iterator wait_all_n(Iterator begin, std::size_t count)
{
typedef typename lcos::detail::future_iterator_traits<Iterator>::type
future_type;
typedef typename lcos::detail::shared_state_ptr_for<future_type>::type
shared_state_ptr;
typedef std::vector<shared_state_ptr> result_type;
result_type values;
values.reserve(count);
detail::wait_get_shared_state<future_type> func;
for (std::size_t i = 0; i != count; ++i)
values.push_back(func(*begin++));
lcos::wait_all(std::move(values));
return begin;
}
inline void wait_all()
{
}
///////////////////////////////////////////////////////////////////////////
template <typename... Ts>
void wait_all(Ts&&... ts)
{
typedef hpx::util::tuple<
typename lcos::detail::shared_state_ptr_for<Ts>::type...
> result_type;
typedef detail::wait_all_frame<result_type> frame_type;
result_type values = result_type(lcos::detail::get_shared_state(ts)...);
frame_type frame(values);
frame.wait_all();
}
}}
namespace hpx
{
using lcos::wait_all;
using lcos::wait_all_n;
}
#endif // DOXYGEN
#endif