-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathdispatch.hpp
More file actions
473 lines (415 loc) · 21.2 KB
/
Copy pathdispatch.hpp
File metadata and controls
473 lines (415 loc) · 21.2 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License
#ifndef __PROCESS_DISPATCH_HPP__
#define __PROCESS_DISPATCH_HPP__
#include <functional>
#include <memory>
#include <string>
#include <process/process.hpp>
#include <stout/lambda.hpp>
#include <stout/preprocessor.hpp>
#include <stout/result_of.hpp>
namespace process {
// The dispatch mechanism enables you to "schedule" a method to get
// invoked on a process. The result of that method invocation is
// accessible via the future that is returned by the dispatch method
// (note, however, that it might not be the _same_ future as the one
// returned from the method, if the method even returns a future, see
// below). Assuming some class 'Fibonacci' has a (visible) method
// named 'compute' that takes an integer, N (and returns the Nth
// fibonacci number) you might use dispatch like so:
//
// PID<Fibonacci> pid = spawn(new Fibonacci(), true); // Use the GC.
// Future<int> f = dispatch(pid, &Fibonacci::compute, 10);
//
// Because the pid argument is "typed" we can ensure that methods are
// only invoked on processes that are actually of that type. Providing
// this mechanism for varying numbers of function types and arguments
// requires support for variadic templates, slated to be released in
// C++11. Until then, we use the Boost preprocessor macros to
// accomplish the same thing (albeit less cleanly). See below for
// those definitions.
//
// Dispatching is done via a level of indirection. The dispatch
// routine itself creates a promise that is passed as an argument to a
// partially applied 'dispatcher' function (defined below). The
// dispatcher routines get passed to the actual process via an
// internal routine called, not surprisingly, 'dispatch', defined
// below:
namespace internal {
// The internal dispatch routine schedules a function to get invoked
// within the context of the process associated with the specified pid
// (first argument), unless that process is no longer valid. Note that
// this routine does not expect anything in particular about the
// specified function (second argument). The semantics are simple: the
// function gets applied/invoked with the process as its first
// argument.
void dispatch(
const UPID& pid,
std::unique_ptr<lambda::CallableOnce<void(ProcessBase*)>> f,
const Option<const std::type_info*>& functionType = None());
// NOTE: This struct is used by the public `dispatch(const UPID& pid, F&& f)`
// function. See comments there for the reason why we need this.
template <typename R>
struct Dispatch;
// Partial specialization for callable objects returning `void` to be dispatched
// on a process.
// NOTE: This struct is used by the public `dispatch(const UPID& pid, F&& f)`
// function. See comments there for the reason why we need this.
template <>
struct Dispatch<void>
{
template <typename F>
void operator()(const UPID& pid, F&& f)
{
std::unique_ptr<lambda::CallableOnce<void(ProcessBase*)>> f_(
new lambda::CallableOnce<void(ProcessBase*)>(
lambda::partial(
[](typename std::decay<F>::type&& f, ProcessBase*) {
std::move(f)();
},
std::forward<F>(f),
lambda::_1)));
internal::dispatch(pid, std::move(f_));
}
};
// Partial specialization for callable objects returning `Future<R>` to be
// dispatched on a process.
// NOTE: This struct is used by the public `dispatch(const UPID& pid, F&& f)`
// function. See comments there for the reason why we need this.
template <typename R>
struct Dispatch<Future<R>>
{
template <typename F>
Future<R> operator()(const UPID& pid, F&& f)
{
std::unique_ptr<Promise<R>> promise(new Promise<R>());
Future<R> future = promise->future();
std::unique_ptr<lambda::CallableOnce<void(ProcessBase*)>> f_(
new lambda::CallableOnce<void(ProcessBase*)>(
lambda::partial(
[](std::unique_ptr<Promise<R>> promise,
typename std::decay<F>::type&& f,
ProcessBase*) {
promise->associate(std::move(f)());
},
std::move(promise),
std::forward<F>(f),
lambda::_1)));
internal::dispatch(pid, std::move(f_));
return future;
}
};
// Dispatches a callable object returning `R` on a process.
// NOTE: This struct is used by the public `dispatch(const UPID& pid, F&& f)`
// function. See comments there for the reason why we need this.
template <typename R>
struct Dispatch
{
template <typename F>
Future<R> operator()(const UPID& pid, F&& f)
{
std::unique_ptr<Promise<R>> promise(new Promise<R>());
Future<R> future = promise->future();
std::unique_ptr<lambda::CallableOnce<void(ProcessBase*)>> f_(
new lambda::CallableOnce<void(ProcessBase*)>(
lambda::partial(
[](std::unique_ptr<Promise<R>> promise,
typename std::decay<F>::type&& f,
ProcessBase*) {
promise->set(std::move(f)());
},
std::move(promise),
std::forward<F>(f),
lambda::_1)));
internal::dispatch(pid, std::move(f_));
return future;
}
};
} // namespace internal {
// Okay, now for the definition of the dispatch routines
// themselves. For each routine we provide the version in C++11 using
// variadic templates so the reader can see what the Boost
// preprocessor macros are effectively providing. Using C++11 closures
// would shorten these definitions even more.
//
// First, definitions of dispatch for methods returning void:
template <typename T>
void dispatch(const PID<T>& pid, void (T::*method)())
{
std::unique_ptr<lambda::CallableOnce<void(ProcessBase*)>> f(
new lambda::CallableOnce<void(ProcessBase*)>(
[=](ProcessBase* process) {
assert(process != nullptr);
T* t = dynamic_cast<T*>(process);
assert(t != nullptr);
(t->*method)();
}));
internal::dispatch(pid, std::move(f), &typeid(method));
}
template <typename T>
void dispatch(const Process<T>& process, void (T::*method)())
{
dispatch(process.self(), method);
}
template <typename T>
void dispatch(const Process<T>* process, void (T::*method)())
{
dispatch(process->self(), method);
}
// Due to a bug (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=41933)
// with variadic templates and lambdas, we still need to do
// preprocessor expansions.
// The following assumes base names for type and variable are `A` and `a`.
#define FORWARD(Z, N, DATA) std::forward<A ## N>(a ## N)
#define MOVE(Z, N, DATA) std::move(a ## N)
#define DECL(Z, N, DATA) typename std::decay<A ## N>::type&& a ## N
#define TEMPLATE(Z, N, DATA) \
template <typename T, \
ENUM_PARAMS(N, typename P), \
ENUM_PARAMS(N, typename A)> \
void dispatch( \
const PID<T>& pid, \
void (T::*method)(ENUM_PARAMS(N, P)), \
ENUM_BINARY_PARAMS(N, A, &&a)) \
{ \
std::unique_ptr<lambda::CallableOnce<void(ProcessBase*)>> f( \
new lambda::CallableOnce<void(ProcessBase*)>( \
lambda::partial( \
[method](ENUM(N, DECL, _), ProcessBase* process) { \
assert(process != nullptr); \
T* t = dynamic_cast<T*>(process); \
assert(t != nullptr); \
(t->*method)(ENUM(N, MOVE, _)); \
}, \
ENUM(N, FORWARD, _), \
lambda::_1))); \
\
internal::dispatch(pid, std::move(f), &typeid(method)); \
} \
\
template <typename T, \
ENUM_PARAMS(N, typename P), \
ENUM_PARAMS(N, typename A)> \
void dispatch( \
const Process<T>& process, \
void (T::*method)(ENUM_PARAMS(N, P)), \
ENUM_BINARY_PARAMS(N, A, &&a)) \
{ \
dispatch(process.self(), method, ENUM(N, FORWARD, _)); \
} \
\
template <typename T, \
ENUM_PARAMS(N, typename P), \
ENUM_PARAMS(N, typename A)> \
void dispatch( \
const Process<T>* process, \
void (T::*method)(ENUM_PARAMS(N, P)), \
ENUM_BINARY_PARAMS(N, A, &&a)) \
{ \
dispatch(process->self(), method, ENUM(N, FORWARD, _)); \
}
REPEAT_FROM_TO(1, 13, TEMPLATE, _) // Args A0 -> A11.
#undef TEMPLATE
// Next, definitions of methods returning a future:
template <typename R, typename T>
Future<R> dispatch(const PID<T>& pid, Future<R> (T::*method)())
{
std::unique_ptr<Promise<R>> promise(new Promise<R>());
Future<R> future = promise->future();
std::unique_ptr<lambda::CallableOnce<void(ProcessBase*)>> f(
new lambda::CallableOnce<void(ProcessBase*)>(
lambda::partial(
[=](std::unique_ptr<Promise<R>> promise, ProcessBase* process) {
assert(process != nullptr);
T* t = dynamic_cast<T*>(process);
assert(t != nullptr);
promise->associate((t->*method)());
},
std::move(promise),
lambda::_1)));
internal::dispatch(pid, std::move(f), &typeid(method));
return future;
}
template <typename R, typename T>
Future<R> dispatch(const Process<T>& process, Future<R> (T::*method)())
{
return dispatch(process.self(), method);
}
template <typename R, typename T>
Future<R> dispatch(const Process<T>* process, Future<R> (T::*method)())
{
return dispatch(process->self(), method);
}
#define TEMPLATE(Z, N, DATA) \
template <typename R, \
typename T, \
ENUM_PARAMS(N, typename P), \
ENUM_PARAMS(N, typename A)> \
Future<R> dispatch( \
const PID<T>& pid, \
Future<R> (T::*method)(ENUM_PARAMS(N, P)), \
ENUM_BINARY_PARAMS(N, A, &&a)) \
{ \
std::unique_ptr<Promise<R>> promise(new Promise<R>()); \
Future<R> future = promise->future(); \
\
std::unique_ptr<lambda::CallableOnce<void(ProcessBase*)>> f( \
new lambda::CallableOnce<void(ProcessBase*)>( \
lambda::partial( \
[method](std::unique_ptr<Promise<R>> promise, \
ENUM(N, DECL, _), \
ProcessBase* process) { \
assert(process != nullptr); \
T* t = dynamic_cast<T*>(process); \
assert(t != nullptr); \
promise->associate( \
(t->*method)(ENUM(N, MOVE, _))); \
}, \
std::move(promise), \
ENUM(N, FORWARD, _), \
lambda::_1))); \
\
internal::dispatch(pid, std::move(f), &typeid(method)); \
\
return future; \
} \
\
template <typename R, \
typename T, \
ENUM_PARAMS(N, typename P), \
ENUM_PARAMS(N, typename A)> \
Future<R> dispatch( \
const Process<T>& process, \
Future<R> (T::*method)(ENUM_PARAMS(N, P)), \
ENUM_BINARY_PARAMS(N, A, &&a)) \
{ \
return dispatch(process.self(), method, ENUM(N, FORWARD, _)); \
} \
\
template <typename R, \
typename T, \
ENUM_PARAMS(N, typename P), \
ENUM_PARAMS(N, typename A)> \
Future<R> dispatch( \
const Process<T>* process, \
Future<R> (T::*method)(ENUM_PARAMS(N, P)), \
ENUM_BINARY_PARAMS(N, A, &&a)) \
{ \
return dispatch(process->self(), method, ENUM(N, FORWARD, _)); \
}
REPEAT_FROM_TO(1, 13, TEMPLATE, _) // Args A0 -> A11.
#undef TEMPLATE
// Next, definitions of methods returning a value.
template <typename R, typename T>
Future<R> dispatch(const PID<T>& pid, R (T::*method)())
{
std::unique_ptr<Promise<R>> promise(new Promise<R>());
Future<R> future = promise->future();
std::unique_ptr<lambda::CallableOnce<void(ProcessBase*)>> f(
new lambda::CallableOnce<void(ProcessBase*)>(
lambda::partial(
[=](std::unique_ptr<Promise<R>> promise, ProcessBase* process) {
assert(process != nullptr);
T* t = dynamic_cast<T*>(process);
assert(t != nullptr);
promise->set((t->*method)());
},
std::move(promise),
lambda::_1)));
internal::dispatch(pid, std::move(f), &typeid(method));
return future;
}
template <typename R, typename T>
Future<R> dispatch(const Process<T>& process, R (T::*method)())
{
return dispatch(process.self(), method);
}
template <typename R, typename T>
Future<R> dispatch(const Process<T>* process, R (T::*method)())
{
return dispatch(process->self(), method);
}
#define TEMPLATE(Z, N, DATA) \
template <typename R, \
typename T, \
ENUM_PARAMS(N, typename P), \
ENUM_PARAMS(N, typename A)> \
Future<R> dispatch( \
const PID<T>& pid, \
R (T::*method)(ENUM_PARAMS(N, P)), \
ENUM_BINARY_PARAMS(N, A, &&a)) \
{ \
std::unique_ptr<Promise<R>> promise(new Promise<R>()); \
Future<R> future = promise->future(); \
\
std::unique_ptr<lambda::CallableOnce<void(ProcessBase*)>> f( \
new lambda::CallableOnce<void(ProcessBase*)>( \
lambda::partial( \
[method](std::unique_ptr<Promise<R>> promise, \
ENUM(N, DECL, _), \
ProcessBase* process) { \
assert(process != nullptr); \
T* t = dynamic_cast<T*>(process); \
assert(t != nullptr); \
promise->set((t->*method)(ENUM(N, MOVE, _))); \
}, \
std::move(promise), \
ENUM(N, FORWARD, _), \
lambda::_1))); \
\
internal::dispatch(pid, std::move(f), &typeid(method)); \
\
return future; \
} \
\
template <typename R, \
typename T, \
ENUM_PARAMS(N, typename P), \
ENUM_PARAMS(N, typename A)> \
Future<R> dispatch( \
const Process<T>& process, \
R (T::*method)(ENUM_PARAMS(N, P)), \
ENUM_BINARY_PARAMS(N, A, &&a)) \
{ \
return dispatch(process.self(), method, ENUM(N, FORWARD, _)); \
} \
\
template <typename R, \
typename T, \
ENUM_PARAMS(N, typename P), \
ENUM_PARAMS(N, typename A)> \
Future<R> dispatch( \
const Process<T>* process, \
R (T::*method)(ENUM_PARAMS(N, P)), \
ENUM_BINARY_PARAMS(N, A, &&a)) \
{ \
return dispatch(process->self(), method, ENUM(N, FORWARD, _)); \
}
REPEAT_FROM_TO(1, 13, TEMPLATE, _) // Args A0 -> A11.
#undef TEMPLATE
#undef DECL
#undef MOVE
#undef FORWARD
// We use partial specialization of
// - internal::Dispatch<void> vs
// - internal::Dispatch<Future<R>> vs
// - internal::Dispatch
// in order to determine whether R is void, Future or other types.
template <typename F, typename R = typename result_of<F()>::type>
auto dispatch(const UPID& pid, F&& f)
-> decltype(internal::Dispatch<R>()(pid, std::forward<F>(f)))
{
return internal::Dispatch<R>()(pid, std::forward<F>(f));
}
} // namespace process {
#endif // __PROCESS_DISPATCH_HPP__