/
action_support.hpp
1355 lines (1201 loc) · 55.1 KB
/
action_support.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
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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2007-2014 Hartmut Kaiser
// Copyright (c) 2011 Bryce Lelbach
// Copyright (c) 2011 Thomas Heller
//
// 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 action_support.hpp
#if !defined(HPX_RUNTIME_ACTIONS_ACTION_SUPPORT_NOV_14_2008_0711PM)
#define HPX_RUNTIME_ACTIONS_ACTION_SUPPORT_NOV_14_2008_0711PM
#include <hpx/hpx_fwd.hpp>
#include <hpx/config.hpp>
#include <hpx/config/bind.hpp>
#include <hpx/config/tuple.hpp>
#include <hpx/config/function.hpp>
#include <hpx/util/move.hpp>
#include <hpx/util/void_guard.hpp>
#include <hpx/traits/action_priority.hpp>
#include <hpx/traits/action_stacksize.hpp>
#include <hpx/traits/action_serialization_filter.hpp>
#include <hpx/traits/action_message_handler.hpp>
#include <hpx/traits/action_may_require_id_splitting.hpp>
#include <hpx/traits/action_does_termination_detection.hpp>
#include <hpx/traits/action_is_target_valid.hpp>
#include <hpx/traits/action_decorate_function.hpp>
#include <hpx/traits/action_decorate_continuation.hpp>
#include <hpx/traits/action_schedule_thread.hpp>
#include <hpx/traits/future_traits.hpp>
#include <hpx/traits/is_future.hpp>
#include <hpx/traits/type_size.hpp>
#include <hpx/runtime/get_lva.hpp>
#include <hpx/runtime/threads/thread_helpers.hpp>
#include <hpx/runtime/threads/thread_init_data.hpp>
#include <hpx/runtime/actions/continuation.hpp>
#include <hpx/util/polymorphic_factory.hpp>
#include <hpx/util/serialize_sequence.hpp>
#include <hpx/util/serialize_exception.hpp>
#include <hpx/util/demangle_helper.hpp>
#include <hpx/util/register_locks.hpp>
#include <hpx/util/decay.hpp>
#include <hpx/util/detail/count_num_args.hpp>
#include <hpx/util/detail/serialization_registration.hpp>
#include <hpx/util/static.hpp>
#include <hpx/lcos/async_fwd.hpp>
#include <hpx/lcos/future.hpp>
#if defined(HPX_HAVE_SECURITY)
#include <hpx/traits/action_capability_provider.hpp>
#endif
#include <boost/version.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <boost/fusion/include/for_each.hpp>
#include <boost/fusion/include/size.hpp>
#include <boost/fusion/include/transform_view.hpp>
#include <boost/ref.hpp>
#include <boost/foreach.hpp>
#include <boost/serialization/access.hpp>
#include <boost/serialization/is_bitwise_serializable.hpp>
#include <boost/serialization/array.hpp>
#include <boost/type_traits/remove_const.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/mpl/and.hpp>
#include <boost/mpl/not.hpp>
#include <boost/mpl/identity.hpp>
#if defined(BOOST_NO_CXX11_DECLTYPE)
# include <boost/typeof/typeof.hpp>
#endif
#include <boost/utility/enable_if.hpp>
#include <boost/preprocessor/cat.hpp>
#include <hpx/config/warnings_prefix.hpp>
/// \cond NOINTERNAL
namespace hpx { namespace actions { namespace detail
{
struct action_serialization_data
{
boost::uint64_t parent_id_;
boost::uint64_t parent_phase_;
boost::uint32_t parent_locality_;
boost::uint16_t priority_;
boost::uint16_t stacksize_;
};
}}}
namespace boost { namespace serialization
{
template <>
struct is_bitwise_serializable<
hpx::actions::detail::action_serialization_data>
: boost::mpl::true_
{};
}}
/// \endcond
///////////////////////////////////////////////////////////////////////////////
namespace hpx { namespace actions
{
/// \cond NOINTERNAL
struct base_action;
///////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename Action>
char const* get_action_name()
#ifdef HPX_DISABLE_AUTOMATIC_SERIALIZATION_REGISTRATION
;
#else
{
/// If you encounter this assert while compiling code, that means that
/// you have a HPX_REGISTER_ACTION macro somewhere in a source file,
/// but the header in which the action is defined misses a
/// HPX_REGISTER_ACTION_DECLARATION
BOOST_MPL_ASSERT_MSG(
traits::needs_automatic_registration<Action>::value
, HPX_REGISTER_ACTION_DECLARATION_MISSING
, (Action)
);
return util::type_id<Action>::typeid_.type_id();
}
#endif
///////////////////////////////////////////////////////////////////////
// If an action returns a future, we need to do special things
template <typename Result>
struct remote_action_result
{
typedef Result type;
};
template <>
struct remote_action_result<void>
{
typedef util::unused_type type;
};
template <typename Result>
struct remote_action_result<lcos::future<Result> >
{
typedef Result type;
};
template <>
struct remote_action_result<lcos::future<void> >
{
typedef hpx::util::unused_type type;
};
template <typename Result>
struct remote_action_result<lcos::shared_future<Result> >
{
typedef Result type;
};
template <>
struct remote_action_result<lcos::shared_future<void> >
{
typedef hpx::util::unused_type type;
};
///////////////////////////////////////////////////////////////////////
template <typename Action>
struct action_registration
{
static boost::shared_ptr<base_action> create()
{
return boost::shared_ptr<base_action>(new Action());
}
action_registration()
{
util::polymorphic_factory<base_action>::get_instance().
add_factory_function(
detail::get_action_name<typename Action::derived_type>()
, &action_registration::create
);
}
};
template <typename Action, typename Enable =
typename traits::needs_automatic_registration<Action>::type>
struct automatic_action_registration
{
automatic_action_registration()
{
action_registration<Action> auto_register;
}
automatic_action_registration & register_action()
{
return *this;
}
};
template <typename Action>
struct automatic_action_registration<Action, boost::mpl::false_>
{
automatic_action_registration()
{
}
automatic_action_registration & register_action()
{
return *this;
}
};
}
///////////////////////////////////////////////////////////////////////////
/// The \a base_action class is an abstract class used as the base class
/// for all action types. It's main purpose is to allow polymorphic
/// serialization of action instances through a shared_ptr.
struct base_action
{
/// The type of an action defines whether this action will be executed
/// directly or by a HPX-threads
enum action_type
{
plain_action = 0, ///< The action will be executed by a newly created thread
direct_action = 1 ///< The action needs to be executed directly
};
/// Destructor
virtual ~base_action() {}
/// The function \a get_component_type returns the \a component_type
/// of the component this action belongs to.
virtual int get_component_type() const = 0;
/// The function \a get_action_name returns the name of this action
/// (mainly used for debugging and logging purposes).
virtual char const* get_action_name() const = 0;
/// The function \a get_action_type returns whether this action needs
/// to be executed in a new thread or directly.
virtual action_type get_action_type() const = 0;
/// The \a get_thread_function constructs a proper thread function for
/// a \a thread, encapsulating the functionality and the arguments
/// of the action it is called for.
///
/// \param lva [in] This is the local virtual address of the
/// component the action has to be invoked on.
///
/// \returns This function returns a proper thread function usable
/// for a \a thread.
///
/// \note This \a get_thread_function will be invoked to retrieve the
/// thread function for an action which has to be invoked without
/// continuations.
virtual threads::thread_function_type
get_thread_function(naming::address::address_type lva) = 0;
/// The \a get_thread_function constructs a proper thread function for
/// a \a thread, encapsulating the functionality, the arguments, and
/// the continuations of the action it is called for.
///
/// \param cont [in] This is the list of continuations to be
/// triggered after the execution of the action
/// \param lva [in] This is the local virtual address of the
/// component the action has to be invoked on.
///
/// \returns This function returns a proper thread function usable
/// for a \a thread.
///
/// \note This \a get_thread_function will be invoked to retrieve the
/// thread function for an action which has to be invoked with
/// continuations.
virtual threads::thread_function_type
get_thread_function(continuation_type& cont,
naming::address::address_type lva) = 0;
/// return the id of the locality of the parent thread
virtual boost::uint32_t get_parent_locality_id() const = 0;
/// Return the thread id of the parent thread
virtual threads::thread_id_repr_type get_parent_thread_id() const = 0;
/// Return the thread phase of the parent thread
virtual boost::uint64_t get_parent_thread_phase() const = 0;
/// Return the thread priority this action has to be executed with
virtual threads::thread_priority get_thread_priority() const = 0;
/// Return the thread stacksize this action has to be executed with
virtual threads::thread_stacksize get_thread_stacksize() const = 0;
/// Return the size of action arguments in bytes
virtual std::size_t get_type_size() const = 0;
/// Return whether the embedded action may require id-splitting
virtual bool may_require_id_splitting() const = 0;
/// Return whether the embedded action is part of termination detection
virtual bool does_termination_detection() const = 0;
virtual void load(hpx::util::portable_binary_iarchive & ar) = 0;
virtual void save(hpx::util::portable_binary_oarchive & ar) const = 0;
/// Wait for embedded futures to become ready
virtual void wait_for_futures() = 0;
// /// Return all data needed for thread initialization
// virtual threads::thread_init_data&
// get_thread_init_data(naming::id_type const& target,
// naming::address::address_type lva, threads::thread_init_data& data) = 0;
//
// virtual threads::thread_init_data&
// get_thread_init_data(continuation_type& cont,
// naming::id_type const& target, naming::address::address_type lva,
// threads::thread_init_data& data) = 0;
/// Return all data needed for thread initialization
virtual void schedule_thread(naming::id_type const& target,
naming::address::address_type lva,
threads::thread_state_enum initial_state) = 0;
virtual void schedule_thread(continuation_type& cont,
naming::id_type const& target, naming::address::address_type lva,
threads::thread_state_enum initial_state) = 0;
/// Return a pointer to the filter to be used while serializing an
/// instance of this action type.
virtual util::binary_filter* get_serialization_filter(
parcelset::parcel const& p) const = 0;
/// Return a pointer to the message handler to be used for this action.
virtual parcelset::policies::message_handler* get_message_handler(
parcelset::parcelhandler* ph, parcelset::locality const& loc,
parcelset::parcel const& p) const = 0;
#if defined(HPX_HAVE_SECURITY)
/// Return the set of capabilities required to invoke this action
virtual components::security::capability get_required_capabilities(
naming::address::address_type lva) const = 0;
#endif
};
///////////////////////////////////////////////////////////////////////////
namespace detail
{
///////////////////////////////////////////////////////////////////////
// Figure out what priority the action has to be be associated with
// A dynamically specified default priority results in using the static
// Priority.
template <threads::thread_priority Priority>
struct thread_priority
{
static threads::thread_priority
call(threads::thread_priority priority)
{
if (priority == threads::thread_priority_default)
return Priority;
return priority;
}
};
// If the static Priority is default, a dynamically specified default
// priority results in using the normal priority.
template <>
struct thread_priority<threads::thread_priority_default>
{
static threads::thread_priority
call(threads::thread_priority priority)
{
if (priority == threads::thread_priority_default)
return threads::thread_priority_normal;
return priority;
}
};
///////////////////////////////////////////////////////////////////////
// Figure out what stacksize the action has to be be associated with
// A dynamically specified default stacksize results in using the static
// Stacksize.
template <threads::thread_stacksize Stacksize>
struct thread_stacksize
{
static threads::thread_stacksize
call(threads::thread_stacksize stacksize)
{
if (stacksize == threads::thread_stacksize_default)
return Stacksize;
return stacksize;
}
};
// If the static Stacksize is default, a dynamically specified default
// stacksize results in using the normal stacksize.
template <>
struct thread_stacksize<threads::thread_stacksize_default>
{
static threads::thread_stacksize
call(threads::thread_stacksize stacksize)
{
if (stacksize == threads::thread_stacksize_default)
return threads::thread_stacksize_minimal;
return stacksize;
}
};
}
template <typename Action>
struct init_registration;
///////////////////////////////////////////////////////////////////////////
template <typename Action>
struct transfer_action : base_action
{
HPX_MOVABLE_BUT_NOT_COPYABLE(transfer_action);
public:
typedef typename Action::component_type component_type;
typedef typename Action::derived_type derived_type;
typedef typename Action::result_type result_type;
typedef typename Action::arguments_type arguments_type;
// This is the priority value this action has been instantiated with
// (statically). This value might be different from the priority member
// holding the runtime value an action has been created with
enum { priority_value = traits::action_priority<Action>::value };
// This is the stacksize value this action has been instantiated with
// (statically). This value might be different from the stacksize member
// holding the runtime value an action has been created with
enum { stacksize_value = traits::action_stacksize<Action>::value };
typedef typename Action::direct_execution direct_execution;
// default constructor is needed for serialization
transfer_action() {}
// construct an action from its arguments
template <typename Args>
explicit transfer_action(Args && args)
: arguments_(std::forward<Args>(args)),
#if defined(HPX_THREAD_MAINTAIN_PARENT_REFERENCE)
parent_locality_(transfer_action::get_locality_id()),
parent_id_(reinterpret_cast<boost::uint64_t>(threads::get_parent_id())),
parent_phase_(threads::get_parent_phase()),
#endif
priority_(
detail::thread_priority<
static_cast<threads::thread_priority>(priority_value)
>::call(threads::thread_priority_default)),
stacksize_(
detail::thread_stacksize<
static_cast<threads::thread_stacksize>(stacksize_value)
>::call(threads::thread_stacksize_default))
{}
template <typename Args>
transfer_action(threads::thread_priority priority, Args && args)
: arguments_(std::forward<Args>(args)),
#if defined(HPX_THREAD_MAINTAIN_PARENT_REFERENCE)
parent_locality_(transfer_action::get_locality_id()),
parent_id_(reinterpret_cast<boost::uint64_t>(threads::get_parent_id())),
parent_phase_(threads::get_parent_phase()),
#endif
priority_(
detail::thread_priority<
static_cast<threads::thread_priority>(priority_value)
>::call(priority)),
stacksize_(
detail::thread_stacksize<
static_cast<threads::thread_stacksize>(stacksize_value)
>::call(threads::thread_stacksize_default))
{}
//
~transfer_action()
{
init_registration<transfer_action<Action> >::g.register_action();
}
public:
/// retrieve component type
static int get_static_component_type()
{
return derived_type::get_component_type();
}
private:
/// The function \a get_component_type returns the \a component_type
/// of the component this action belongs to.
int get_component_type() const
{
return derived_type::get_component_type();
}
/// The function \a get_action_name returns the name of this action
/// (mainly used for debugging and logging purposes).
char const* get_action_name() const
{
return detail::get_action_name<derived_type>();
}
/// The function \a get_action_type returns whether this action needs
/// to be executed in a new thread or directly.
action_type get_action_type() const
{
return derived_type::get_action_type();
}
/// The \a get_thread_function constructs a proper thread function for
/// a \a thread, encapsulating the functionality and the arguments
/// of the action it is called for.
///
/// \param lva [in] This is the local virtual address of the
/// component the action has to be invoked on.
///
/// \returns This function returns a proper thread function usable
/// for a \a thread.
///
/// \note This \a get_thread_function will be invoked to retrieve the
/// thread function for an action which has to be invoked without
/// continuations.
threads::thread_function_type
get_thread_function(naming::address::address_type lva)
{
return derived_type::construct_thread_function(lva,
std::move(arguments_));
}
/// The \a get_thread_function constructs a proper thread function for
/// a \a thread, encapsulating the functionality, the arguments, and
/// the continuations of the action it is called for.
///
/// \param cont [in] This is the list of continuations to be
/// triggered after the execution of the action
/// \param lva [in] This is the local virtual address of the
/// component the action has to be invoked on.
///
/// \returns This function returns a proper thread function usable
/// for a \a thread.
///
/// \note This \a get_thread_function will be invoked to retrieve the
/// thread function for an action which has to be invoked with
/// continuations.
threads::thread_function_type
get_thread_function(continuation_type& cont,
naming::address::address_type lva)
{
return derived_type::construct_thread_function(cont, lva,
std::move(arguments_));
}
#if !defined(HPX_THREAD_MAINTAIN_PARENT_REFERENCE)
/// Return the locality of the parent thread
boost::uint32_t get_parent_locality_id() const
{
return naming::invalid_locality_id;
}
/// Return the thread id of the parent thread
threads::thread_id_repr_type get_parent_thread_id() const
{
return threads::invalid_thread_id_repr;
}
/// Return the phase of the parent thread
boost::uint64_t get_parent_thread_phase() const
{
return 0;
}
#else
/// Return the locality of the parent thread
boost::uint32_t get_parent_locality_id() const
{
return parent_locality_;
}
/// Return the thread id of the parent thread
threads::thread_id_repr_type get_parent_thread_id() const
{
return reinterpret_cast<threads::thread_id_repr_type>(parent_id_);
}
/// Return the phase of the parent thread
boost::uint64_t get_parent_thread_phase() const
{
return parent_phase_;
}
#endif
/// Return the thread priority this action has to be executed with
threads::thread_priority get_thread_priority() const
{
return priority_;
}
/// Return the thread stacksize this action has to be executed with
threads::thread_stacksize get_thread_stacksize() const
{
return stacksize_;
}
/// Return the size of action arguments in bytes
std::size_t get_type_size() const
{
return traits::type_size<arguments_type>::call(arguments_);
}
/// Return whether the embedded action may require id-splitting
bool may_require_id_splitting() const
{
return traits::action_may_require_id_splitting<derived_type>::call(arguments_);
}
/// Wait for embedded futures to become ready
void wait_for_futures()
{
traits::serialize_as_future<arguments_type>::call(arguments_);
}
/// Return whether the embedded action is part of termination detection
bool does_termination_detection() const
{
return traits::action_does_termination_detection<derived_type>::call();
}
/// Return all data needed for thread initialization
threads::thread_init_data&
get_thread_init_data(naming::id_type const& target,
naming::address::address_type lva, threads::thread_init_data& data)
{
data.func = get_thread_function(lva);
#if defined(HPX_THREAD_MAINTAIN_TARGET_ADDRESS)
data.lva = lva;
#endif
#if defined(HPX_THREAD_MAINTAIN_DESCRIPTION)
data.description = detail::get_action_name<derived_type>();
#endif
#if defined(HPX_THREAD_MAINTAIN_PARENT_REFERENCE)
data.parent_id = reinterpret_cast<threads::thread_id_repr_type>(parent_id_);
data.parent_locality_id = parent_locality_;
#endif
data.priority = priority_;
data.stacksize = threads::get_stack_size(stacksize_);
data.target = target;
return data;
}
threads::thread_init_data&
get_thread_init_data(continuation_type& cont, naming::id_type const& target,
naming::address::address_type lva, threads::thread_init_data& data)
{
data.func = get_thread_function(cont, lva);
#if defined(HPX_THREAD_MAINTAIN_TARGET_ADDRESS)
data.lva = lva;
#endif
#if defined(HPX_THREAD_MAINTAIN_DESCRIPTION)
data.description = detail::get_action_name<derived_type>();
#endif
#if defined(HPX_THREAD_MAINTAIN_PARENT_REFERENCE)
data.parent_id = reinterpret_cast<threads::thread_id_repr_type>(parent_id_);
data.parent_locality_id = parent_locality_;
#endif
data.priority = priority_;
data.stacksize = threads::get_stack_size(stacksize_);
data.target = target;
return data;
}
// schedule a new thread
void schedule_thread(naming::id_type const& target,
naming::address::address_type lva,
threads::thread_state_enum initial_state)
{
continuation_type cont;
threads::thread_init_data data;
if (traits::action_decorate_continuation<derived_type>::call(cont))
{
traits::action_schedule_thread<derived_type>::call(lva,
get_thread_init_data(cont, target, lva, data), initial_state);
}
else
{
traits::action_schedule_thread<derived_type>::call(lva,
get_thread_init_data(target, lva, data), initial_state);
}
}
void schedule_thread(continuation_type& cont,
naming::id_type const& target, naming::address::address_type lva,
threads::thread_state_enum initial_state)
{
// first decorate the continuation
traits::action_decorate_continuation<derived_type>::call(cont);
// now, schedule the thread
threads::thread_init_data data;
traits::action_schedule_thread<derived_type>::call(lva,
get_thread_init_data(cont, target, lva, data), initial_state);
}
/// Return a pointer to the filter to be used while serializing an
/// instance of this action type.
util::binary_filter* get_serialization_filter(
parcelset::parcel const& p) const
{
return traits::action_serialization_filter<derived_type>::call(p);
}
/// Return a pointer to the message handler to be used for this action.
parcelset::policies::message_handler* get_message_handler(
parcelset::parcelhandler* ph, parcelset::locality const& loc,
parcelset::parcel const& p) const
{
return traits::action_message_handler<derived_type>::
call(ph, loc, p);
}
#if defined(HPX_HAVE_SECURITY)
/// Return the set of capabilities required to invoke this action
components::security::capability get_required_capabilities(
naming::address::address_type lva) const
{
return traits::action_capability_provider<derived_type>::call(lva);
}
#endif
public:
/// retrieve the N's argument
template <int N>
typename boost::fusion::result_of::at_c<arguments_type, N>::type
get()
{
return boost::fusion::at_c<N>(arguments_);
}
// serialization support
void load(hpx::util::portable_binary_iarchive & ar)
{
util::serialize_sequence(ar, arguments_);
// Always serialize the parent information to maintain binary
// compatibility on the wire.
if (ar.flags() & util::disable_array_optimization) {
#if !defined(HPX_THREAD_MAINTAIN_PARENT_REFERENCE)
boost::uint32_t parent_locality_ = naming::invalid_locality_id;
boost::uint64_t parent_id_ = boost::uint64_t(-1);
boost::uint64_t parent_phase_ = 0;
#endif
ar >> parent_locality_;
ar >> parent_id_;
ar >> parent_phase_;
ar >> priority_;
ar >> stacksize_;
}
else {
detail::action_serialization_data data;
ar.load(data);
#if defined(HPX_THREAD_MAINTAIN_PARENT_REFERENCE)
parent_id_ = data.parent_id_;
parent_phase_ = data.parent_phase_;
parent_locality_ = data.parent_locality_;
#endif
priority_ = static_cast<threads::thread_priority>(data.priority_);
stacksize_ = static_cast<threads::thread_stacksize>(data.stacksize_);
}
}
void save(hpx::util::portable_binary_oarchive & ar) const
{
util::serialize_sequence(ar, arguments_);
// Always serialize the parent information to maintain binary
// compatibility on the wire.
#if !defined(HPX_THREAD_MAINTAIN_PARENT_REFERENCE)
boost::uint32_t parent_locality_ = naming::invalid_locality_id;
boost::uint64_t parent_id_ = boost::uint64_t(-1);
boost::uint64_t parent_phase_ = 0;
#endif
if (ar.flags() & util::disable_array_optimization) {
ar << parent_locality_;
ar << parent_id_;
ar << parent_phase_;
ar << priority_;
ar << stacksize_;
}
else {
detail::action_serialization_data data;
data.parent_id_ = parent_id_;
data.parent_phase_ = parent_phase_;
data.parent_locality_ = parent_locality_;
data.priority_ = priority_;
data.stacksize_ = stacksize_;
ar.save(data);
}
}
private:
static boost::uint32_t get_locality_id()
{
error_code ec(lightweight); // ignore any errors
return hpx::get_locality_id(ec);
}
protected:
arguments_type arguments_;
#if defined(HPX_THREAD_MAINTAIN_PARENT_REFERENCE)
boost::uint32_t parent_locality_;
boost::uint64_t parent_id_;
boost::uint64_t parent_phase_;
#endif
threads::thread_priority priority_;
threads::thread_stacksize stacksize_;
};
///////////////////////////////////////////////////////////////////////////
template <int N, typename Action>
inline typename boost::fusion::result_of::at_c<
typename transfer_action<Action>::arguments_type, N
>::type
get(transfer_action<Action> & args)
{
return args.template get<N>();
}
// bring in all overloads for
// construct_continuation_thread_functionN()
// construct_continuation_thread_function_voidN()
#include <hpx/runtime/actions/construct_continuation_function_objects.hpp>
///////////////////////////////////////////////////////////////////////////
/// \tparam Component component type
/// \tparam Signature return type and arguments
/// \tparam Derived derived action class
template <typename Component, typename Signature, typename Derived>
struct basic_action;
template <typename Component, typename R, typename ...Args, typename Derived>
struct basic_action<Component, R(Args...), Derived>
{
typedef Component component_type;
typedef Derived derived_type;
typedef R result_type;
typedef typename traits::promise_local_result<R>::type local_result_type;
typedef typename detail::remote_action_result<R>::type remote_result_type;
static const std::size_t arity = sizeof...(Args);
typedef util::tuple<Args...> arguments_type;
typedef void action_tag;
///////////////////////////////////////////////////////////////////////
static bool is_target_valid(naming::id_type const& id)
{
return true; // by default we don't do any verification
}
///////////////////////////////////////////////////////////////////////
template <typename Func, typename Arguments_>
static threads::thread_function_type
construct_continuation_thread_function_void(
continuation_type cont, Func && func, Arguments_ && args)
{
typedef typename boost::remove_reference<Arguments_>::type arguments_type;
return detail::construct_continuation_thread_function_voidN<
derived_type, util::tuple_size<arguments_type>::value
>::call(cont, std::forward<Func>(func), std::forward<Arguments_>(args));
}
template <typename Func, typename Arguments_>
static threads::thread_function_type
construct_continuation_thread_function(
continuation_type cont, Func && func, Arguments_ && args)
{
typedef typename boost::remove_reference<Arguments_>::type arguments_type;
return detail::construct_continuation_thread_functionN<
derived_type, util::tuple_size<arguments_type>::value
>::call(cont, std::forward<Func>(func), std::forward<Arguments_>(args));
}
// bring in all overloads for
// construct_continuation_thread_function_void()
// construct_continuation_thread_object_function_void()
// construct_continuation_thread_function()
// construct_continuation_thread_object_function()
#include <hpx/runtime/actions/construct_continuation_functions.hpp>
typedef typename traits::is_future<local_result_type>::type is_future_pred;
// bring in the definition for all overloads for operator()
#include <hpx/runtime/actions/define_function_operators.hpp>
/// retrieve component type
static int get_component_type()
{
return static_cast<int>(components::get_component_type<Component>());
}
/// The function \a get_action_type returns whether this action needs
/// to be executed in a new thread or directly.
static base_action::action_type get_action_type()
{
return base_action::plain_action;
}
private:
// serialization support
friend class boost::serialization::access;
template <typename Archive>
BOOST_FORCEINLINE void serialize(Archive& ar, const unsigned int) {}
};
///////////////////////////////////////////////////////////////////////////
namespace detail
{
// simple type allowing to distinguish whether an action is the most
// derived one
struct this_type {};
template <typename Action, typename Derived>
struct action_type
: boost::mpl::if_<boost::is_same<Derived, this_type>, Action, Derived>
{};
}
///////////////////////////////////////////////////////////////////////////
template <typename TF, TF F, typename Derived>
class basic_action_impl;
///////////////////////////////////////////////////////////////////////////
template <typename TF, TF F, typename Derived = detail::this_type>
struct action;
///////////////////////////////////////////////////////////////////////////
template <typename TF, TF F, typename Derived = detail::this_type>
struct direct_action;
///////////////////////////////////////////////////////////////////////////
// Base template allowing to generate a concrete action type from a function
// pointer. It is instantiated only if the supplied pointer is not a
// supported function pointer.
template <typename TF, TF F, typename Derived = detail::this_type,
typename Direct = boost::mpl::false_>
struct make_action;
template <typename TF, TF F, typename Derived>
struct make_action<TF, F, Derived, boost::mpl::false_>
: action<TF, F, Derived>
{
typedef action<TF, F, Derived> type;
};
template <typename TF, TF F, typename Derived>
struct make_action<TF, F, Derived, boost::mpl::true_>
: direct_action<TF, F, Derived>
{
typedef direct_action<TF, F, Derived> type;
};
template <typename TF, TF F, typename Derived = detail::this_type>
struct make_direct_action
: make_action<TF, F, Derived, boost::mpl::true_>
{};
// older compilers require BOOST_TYPEOF, newer compilers have decltype()
#if defined(HPX_HAVE_CXX11_DECLTYPE)
# define HPX_TYPEOF(x) decltype(x)
# define HPX_TYPEOF_TPL(x) decltype(x)
#else
# define HPX_TYPEOF(x) BOOST_TYPEOF(x)
# define HPX_TYPEOF_TPL(x) BOOST_TYPEOF_TPL(x)
#endif
// Macros usable to refer to an action given the function to expose
#define HPX_MAKE_ACTION(f) \
hpx::actions::make_action<HPX_TYPEOF(&f), &f> /**/ \
/**/
#define HPX_MAKE_DIRECT_ACTION(f) \
hpx::actions::make_direct_action<HPX_TYPEOF(&f), &f> /**/ \
/**/
#define HPX_MAKE_ACTION_TPL(f) \
hpx::actions::make_action<HPX_TYPEOF_TPL(&f), &f> /**/ \
/**/
#define HPX_MAKE_DIRECT_ACTION_TPL(f) \