/
async_implementations.hpp
300 lines (255 loc) · 10.1 KB
/
async_implementations.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
// Copyright (c) 2007-2015 Hartmut Kaiser
//
// 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)
#if !defined(HPX_LCOS_ASYNC_IMPLEMENTATIONS_APR_13_2015_0829AM)
#define HPX_LCOS_ASYNC_IMPLEMENTATIONS_APR_13_2015_0829AM
#include <hpx/config.hpp>
#include <hpx/traits/future_access.hpp>
#include <hpx/runtime/naming/address.hpp>
#include <hpx/runtime/naming/id_type.hpp>
#include <hpx/runtime/launch_policy.hpp>
#include <hpx/lcos/detail/async_implementations_fwd.hpp>
#include <hpx/lcos/packaged_action.hpp>
#include <hpx/util/move.hpp>
#include <boost/mpl/bool.hpp>
namespace hpx { namespace detail
{
/// \cond NOINTERNAL
HPX_FORCEINLINE bool has_async_policy(BOOST_SCOPED_ENUM(launch) policy)
{
return (static_cast<int>(policy) &
static_cast<int>(launch::async_policies)) ? true : false;
}
///////////////////////////////////////////////////////////////////////////
struct keep_id_alive
{
explicit keep_id_alive(naming::id_type const& gid)
: gid_(gid)
{}
void operator()() const {}
naming::id_type gid_;
};
///////////////////////////////////////////////////////////////////////////
template <typename Action, typename Result>
struct sync_local_invoke
{
template <typename ...Ts>
static lcos::future<Result>
call(naming::id_type const& id, naming::address && addr, Ts &&... vs)
{
bool target_is_managed = false;
naming::id_type id1;
if (id.get_management_type() == naming::id_type::managed)
{
id1 = naming::id_type(id.get_gid(), naming::id_type::unmanaged);
target_is_managed = true;
}
lcos::packaged_action<Action, Result> p;
p.apply(std::move(addr), target_is_managed ? id1 : id,
std::forward<Ts>(vs)...);
// keep id alive, if needed - this allows to send the destination
// as an unmanaged id
future<Result> f = p.get_future();
if (target_is_managed)
{
typedef typename traits::detail::shared_state_ptr_for<
future<Result>
>::type shared_state_ptr;
shared_state_ptr const& state = traits::detail::get_shared_state(f);
state->set_on_completed(hpx::detail::keep_id_alive(id));
}
return f;
}
};
template <typename Action, typename Result>
struct sync_local_invoke<Action, lcos::future<Result> >
{
template <typename ...Ts>
HPX_FORCEINLINE static lcos::future<Result>
call(naming::id_type const&, naming::address && addr, Ts &&... vs)
{
HPX_ASSERT(!!addr);
HPX_ASSERT(traits::component_type_is_compatible<
typename Action::component_type
>::call(addr));
return Action::execute_function(addr.address_,
std::forward<Ts>(vs)...);
}
};
///////////////////////////////////////////////////////////////////////////
template <typename Action, typename Result>
struct sync_local_invoke_cb
{
template <typename Callback, typename ...Ts>
static lcos::future<Result>
call(naming::id_type const& id, naming::address && addr, Callback && cb,
Ts &&... vs)
{
bool target_is_managed = false;
naming::id_type id1;
if (id.get_management_type() == naming::id_type::managed)
{
id1 = naming::id_type(id.get_gid(), naming::id_type::unmanaged);
target_is_managed = true;
}
lcos::packaged_action<Action, Result> p;
p.apply_cb(std::move(addr), target_is_managed ? id1 : id,
std::forward<Callback>(cb), std::forward<Ts>(vs)...);
// keep id alive, if needed - this allows to send the destination
// as an unmanaged id
future<Result> f = p.get_future();
if (target_is_managed)
{
typedef typename traits::detail::shared_state_ptr_for<
future<Result>
>::type shared_state_ptr;
shared_state_ptr const& state = traits::detail::get_shared_state(f);
state->set_on_completed(hpx::detail::keep_id_alive(id));
}
return f;
}
};
template <typename Action, typename Result>
struct sync_local_invoke_cb<Action, lcos::future<Result> >
{
template <typename Callback, typename ...Ts>
HPX_FORCEINLINE static lcos::future<Result>
call(naming::id_type const&, naming::address && addr, Callback && cb,
Ts &&... vs)
{
HPX_ASSERT(!!addr);
HPX_ASSERT(traits::component_type_is_compatible<
typename Action::component_type
>::call(addr));
lcos::future<Result> f = Action::execute_function(
addr.address_, std::forward<Ts>(vs)...);
// invoke callback
cb(boost::system::error_code(), parcelset::parcel());
return f;
}
};
///////////////////////////////////////////////////////////////////////////
template <typename Action, typename ...Ts>
hpx::future<
typename traits::promise_local_result<
typename hpx::actions::extract_action<Action>::remote_result_type
>::type>
async_impl(BOOST_SCOPED_ENUM(launch) policy, hpx::id_type const& id,
Ts&&... vs)
{
typedef typename hpx::actions::extract_action<Action>::type action_type;
typedef typename traits::promise_local_result<
typename action_type::remote_result_type
>::type result_type;
naming::address addr;
if (agas::is_local_address_cached(id, addr) && policy == launch::sync)
{
return hpx::detail::sync_local_invoke<action_type, result_type>::
call(id, std::move(addr), std::forward<Ts>(vs)...);
}
bool target_is_managed = false;
naming::id_type id1;
future<result_type> f;
if (id.get_management_type() == naming::id_type::managed)
{
id1 = naming::id_type(id.get_gid(), naming::id_type::unmanaged);
target_is_managed = true;
}
if (policy == launch::sync || hpx::detail::has_async_policy(policy))
{
lcos::packaged_action<action_type, result_type> p;
p.apply(std::move(addr), target_is_managed ? id1 : id,
std::forward<Ts>(vs)...);
f = p.get_future();
}
else if (policy == launch::deferred)
{
lcos::packaged_action<action_type, result_type> p;
p.apply_deferred(std::move(addr), target_is_managed ? id1 : id,
std::forward<Ts>(vs)...);
f = p.get_future();
}
else
{
HPX_THROW_EXCEPTION(bad_parameter,
"async_impl", "unknown launch policy");
return f;
}
// keep id alive, if needed - this allows to send the destination as an
// unmanaged id
if (target_is_managed)
{
typedef typename traits::detail::shared_state_ptr_for<
future<result_type>
>::type shared_state_ptr;
shared_state_ptr const& state = traits::detail::get_shared_state(f);
state->set_on_completed(hpx::detail::keep_id_alive(id));
}
return f;
}
/// \note This function is part of the invocation policy implemented by
/// this class
///
template <typename Action, typename Callback, typename ...Ts>
hpx::future<
typename traits::promise_local_result<
typename hpx::actions::extract_action<Action>::remote_result_type
>::type>
async_cb_impl(BOOST_SCOPED_ENUM(launch) policy, hpx::id_type const& id,
Callback&& cb, Ts&&... vs)
{
typedef typename hpx::actions::extract_action<Action>::type action_type;
typedef typename traits::promise_local_result<
typename action_type::remote_result_type
>::type result_type;
naming::address addr;
if (agas::is_local_address_cached(id, addr) && policy == launch::sync)
{
return hpx::detail::sync_local_invoke_cb<action_type, result_type>::
call(id, std::move(addr), std::forward<Callback>(cb),
std::forward<Ts>(vs)...);
}
future<result_type> f;
bool target_is_managed = false;
naming::id_type id1;
if (id.get_management_type() == naming::id_type::managed)
{
id1 = naming::id_type(id.get_gid(), naming::id_type::unmanaged);
target_is_managed = true;
}
if (policy == launch::sync || hpx::detail::has_async_policy(policy))
{
lcos::packaged_action<action_type, result_type> p;
p.apply_cb(std::move(addr), target_is_managed ? id1 : id,
std::forward<Callback>(cb), std::forward<Ts>(vs)...);
f = p.get_future();
}
else if (policy == launch::deferred)
{
lcos::packaged_action<action_type, result_type> p;
p.apply_deferred_cb(std::move(addr), target_is_managed ? id1 : id,
std::forward<Callback>(cb), std::forward<Ts>(vs)...);
f = p.get_future();
}
else
{
HPX_THROW_EXCEPTION(bad_parameter,
"async_cb_impl", "unknown launch policy");
return f;
}
// keep id alive, if needed - this allows to send the destination
// as an unmanaged id
if (target_is_managed)
{
typedef typename traits::detail::shared_state_ptr_for<
future<result_type>
>::type shared_state_ptr;
shared_state_ptr const& state = traits::detail::get_shared_state(f);
state->set_on_completed(detail::keep_id_alive(id));
}
return f;
}
/// \endcond
}}
#endif