/
pub.hpp
1591 lines (1410 loc) · 37.8 KB
/
pub.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
/*!
* \file
* \brief Implementation of Asio's Thread Pool dispatcher.
*
* \since
* v.1.0.2
*/
#pragma once
#include <so_5_extra/error_ranges.hpp>
#include <so_5/disp_binder.hpp>
#include <so_5/send_functions.hpp>
#include <so_5/disp/reuse/work_thread_activity_tracking.hpp>
#include <so_5/disp/reuse/data_source_prefix_helpers.hpp>
#include <so_5/stats/repository.hpp>
#include <so_5/stats/messages.hpp>
#include <so_5/stats/std_names.hpp>
#include <so_5/stats/impl/activity_tracking.hpp>
#include <so_5/details/invoke_noexcept_code.hpp>
#include <so_5/details/rollback_on_exception.hpp>
#include <so_5/details/abort_on_fatal_error.hpp>
#include <so_5/outliving.hpp>
#include <asio/io_context.hpp>
#include <asio/io_context_strand.hpp>
#include <asio/post.hpp>
namespace so_5 {
namespace extra {
namespace disp {
namespace asio_thread_pool {
namespace errors {
//! Asio IoService is not set for asio_thread_pool dispatcher.
const int rc_io_context_is_not_set =
so_5::extra::errors::asio_thread_pool_errors;
} /* namespace errors */
//
// disp_params_t
//
/*!
* \brief Parameters for %asio_thread_pool dispatcher.
*
* \since
* v.1.0.2
*/
class disp_params_t
: public ::so_5::disp::reuse::work_thread_activity_tracking_flag_mixin_t< disp_params_t >
{
using activity_tracking_mixin_t = ::so_5::disp::reuse::
work_thread_activity_tracking_flag_mixin_t< disp_params_t >;
public :
//! Default constructor.
disp_params_t() = default;
friend inline void
swap(
disp_params_t & a, disp_params_t & b ) noexcept
{
using std::swap;
swap(
static_cast< activity_tracking_mixin_t & >(a),
static_cast< activity_tracking_mixin_t & >(b) );
swap( a.m_thread_count, b.m_thread_count );
swap( a.m_io_context, b.m_io_context );
}
//! Setter for thread count.
disp_params_t &
thread_count( std::size_t count )
{
m_thread_count = count;
return *this;
}
//! Getter for thread count.
std::size_t
thread_count() const
{
return m_thread_count;
}
//! Use external Asio io_context object with dispatcher.
/*!
* Usage example:
* \code
* int main() {
* asio::io_context svc;
* so_5::launch( [&](so_5::environment_t & env) {
* namespace asio_tp = so_5::extra::disp::asio_thread_pool;
* auto disp = asio_tp::create_private_disp(
* env, "asio_tp",
* asio_tp::disp_params_t{}.use_external_io_context(
* so_5::outliving_mutable(svc) ) );
* ...
* } );
* }
* \endcode
*/
disp_params_t &
use_external_io_context(
::asio::io_context & service )
{
m_io_context = std::shared_ptr< ::asio::io_context >(
std::addressof( service ),
// Empty deleter.
[](::asio::io_context *) {} );
return *this;
}
//! Use external Asio io_context object with dispatcher.
/*!
* \note
* Ownership of this io_context object must be shared with
* others.
*/
disp_params_t &
use_external_io_context(
std::shared_ptr< ::asio::io_context > service )
{
m_io_context = std::move(service);
return *this;
}
//! Use own Asio io_context object.
/*!
* Note this object will be dynamically created at the start
* of the dispatcher. And will be destroyed with the dispatcher object.
*
* A created io_context can be accessed later via io_context() method.
*/
disp_params_t &
use_own_io_context()
{
m_io_context = std::make_shared< ::asio::io_context >();
return *this;
}
//! Get the io_context.
std::shared_ptr< ::asio::io_context >
io_context() const noexcept
{
return m_io_context;
}
private :
//! Count of working threads.
/*!
* Value 0 means that actual thread will be detected automatically.
*/
std::size_t m_thread_count = { 0 };
//! Asio's io_context which must be used with this dispatcher.
std::shared_ptr< ::asio::io_context > m_io_context;
};
namespace impl {
class actual_dispatcher_iface_t;
//
// basic_dispatcher_iface_t
//
/*!
* \brief The very basic interface of %asio_thread_pool dispatcher.
*
* This class contains a minimum that is necessary for implementation
* of dispatcher_handle class.
*
* \since
* v.1.3.0
*/
class basic_dispatcher_iface_t
: public std::enable_shared_from_this<actual_dispatcher_iface_t>
{
public :
virtual ~basic_dispatcher_iface_t() noexcept = default;
//! Create a binder for that dispatcher.
/*!
* The binder will use an external strand object.
*/
[[nodiscard]]
virtual disp_binder_shptr_t
binder_with_external_strand( ::asio::io_context::strand & ) = 0;
//! Create a binder for that dispatcher.
/*!
* The binder will use an internal (automatically created)
* strand object.
*/
[[nodiscard]]
virtual disp_binder_shptr_t
binder_with_own_strand() = 0;
//! Get reference to io_context from that dispatcher.
virtual ::asio::io_context &
io_context() const noexcept = 0;
};
using basic_dispatcher_iface_shptr_t =
std::shared_ptr< basic_dispatcher_iface_t >;
class dispatcher_handle_maker_t;
} /* namespace impl */
//
// dispatcher_handle_t
//
/*!
* \brief A handle for %asio_thread_pool dispatcher.
*
* \since
* v.1.3.0
*/
class [[nodiscard]] dispatcher_handle_t
{
friend class impl::dispatcher_handle_maker_t;
//! A reference to actual implementation of a dispatcher.
impl::basic_dispatcher_iface_shptr_t m_dispatcher;
dispatcher_handle_t(
impl::basic_dispatcher_iface_shptr_t dispatcher ) noexcept
: m_dispatcher{ std::move(dispatcher) }
{}
//! Is this handle empty?
[[nodiscard]]
bool
empty() const noexcept { return !m_dispatcher; }
public :
dispatcher_handle_t() noexcept = default;
//! Get a binder for that dispatcher.
/*!
* This method requires a reference to manually created strand
* object for protection of agents bound via binder returned.
* A user should create this strand object and ensure the right
* lifetime of it.
*
* Usage example:
* \code
* using namespace so_5::extra::disp::asio_thread_pool;
*
* asio::io_context io_ctx;
* asio::io_context::strand agents_strand{ io_ctx };
*
* so_5::environment_t & env = ...;
* auto disp = make_dispatcher( env, "my_disp", io_ctx );
*
* env.introduce_coop( [&]( so_5::coop_t & coop ) {
* coop.make_agent_with_binder< some_agent_type >(
* disp.binder( agents_strand ),
* ... );
*
* coop.make_agent_with_binder< another_agent_type >(
* disp.binder( agents_strand ),
* ... );
*
* ...
* } );
* \endcode
*
* \attention
* An attempt to call this method on empty handle is UB.
*/
[[nodiscard]]
disp_binder_shptr_t
binder(
::asio::io_context::strand & strand ) const
{
return m_dispatcher->binder_with_external_strand( strand );
}
//! Get a binder for that dispatcher.
/*!
* This method requires creates an internal strand object by itself.
*
* Usage example:
* \code
* using namespace so_5::extra::disp::asio_thread_pool;
*
* asio::io_context io_ctx;
*
* so_5::environment_t & env = ...;
* auto disp = make_dispatcher( env, "my_disp", io_ctx );
*
* env.introduce_coop( [&]( so_5::coop_t & coop ) {
* // This agent will use its own strand object.
* coop.make_agent_with_binder< some_agent_type >(
* disp.binder(),
* ... );
*
* // This agent will use its own strand object.
* coop.make_agent_with_binder< another_agent_type >(
* disp.binder(),
* ... );
* ...
* } );
* \endcode
*
* \attention
* An attempt to call this method on empty handle is UB.
*
* \since
* v.1.3.0
*/
[[nodiscard]]
disp_binder_shptr_t
binder() const
{
return m_dispatcher->binder_with_own_strand();
}
//! Get reference to io_context from that dispatcher.
/*!
* \attention
* An attempt to call this method on empty handle is UB.
*/
[[nodiscard]]
::asio::io_context &
io_context() noexcept
{
return m_dispatcher->io_context();
}
//! Is this handle empty?
[[nodiscard]]
operator bool() const noexcept { return empty(); }
//! Does this handle contain a reference to dispatcher?
[[nodiscard]]
bool
operator!() const noexcept { return !empty(); }
//! Drop the content of handle.
void
reset() noexcept { m_dispatcher.reset(); }
};
namespace impl {
//
// demands_counter_t
//
/*!
* \brief Type of atomic counter for counting waiting demands.
*
* \since
* v.1.0.2
*/
using demands_counter_t = std::atomic< std::size_t >;
//
// actual_dispatcher_iface_t
//
/*!
* \brief An actual interface of thread pool dispatcher.
*
* \since
* v.1.3.0
*/
class actual_dispatcher_iface_t : public basic_dispatcher_iface_t
{
public :
//! Notification about binding of yet another agent.
virtual void
agent_bound() noexcept = 0;
//! Notification about unbinding of an agent.
virtual void
agent_unbound() noexcept = 0;
//! Get a reference for counter of pending demands.
virtual demands_counter_t &
demands_counter() noexcept = 0;
};
//
// actual_dispatcher_shptr_t
//
using actual_dispatcher_shptr_t =
std::shared_ptr< actual_dispatcher_iface_t >;
//
// thread_local_ptr_holder_t
//
/*!
* \brief A helper for declaration of static and thread_local pointer
* in a header file.
*
* If non-template class will define a static member in a header file
* then there is a possibility to get a link-time error about multiple
* definition of that member. But if a static member is defined for
* template class then there won't be such problem.
*
* A typical usage intended to be:
* \code
* class some_useful_class_t : public thread_local_ptr_holder_t<some_useful_class_t> {
* ...
* };
* \endcode
*
* \since
* v.1.0.2
*/
template< class T >
class thread_local_ptr_holder_t
{
private :
//! Value of the pointer which need to be stored.
static thread_local T * m_ptr;
protected :
//! Access to the current value of the pointer.
static T *
ptr() noexcept { return m_ptr; }
//! Setter for the pointer.
static void
set_ptr( T * p ) noexcept { m_ptr = p; }
};
template< class T >
thread_local T * thread_local_ptr_holder_t<T>::m_ptr = nullptr;
//
// work_thread_t
//
/*!
* \brief Base type for implementations of work thread wrappers.
*
* Work thread wrapper creates an instance of some type on the stack
* of the new thread. Then the pointer of this instance is stored in
* thread_local variable (as a pointer to work_thread_t). This pointer
* then can be retrieved later by demand handlers to get access to
* some dispatcher-specific data.
*
* It is assumed that there will be two derived classes:
* 1. One for the case when thread activity should not be tracked.
* 2. Another for the case when thread activity must be tracked.
*
* These derived classes will reuse some functionality from
* work_thread_t. And should implement on_demand() method for
* actual demands processing.
*
* \since
* v.1.0.2
*/
class work_thread_t : private thread_local_ptr_holder_t< work_thread_t >
{
private :
//! ID of the work thread.
/*!
* Gets its value in the constructor and doesn't changed later.
*/
current_thread_id_t m_thread_id;
protected :
// Constructor and destructor are accessible for derived classes only.
work_thread_t()
: m_thread_id( query_current_thread_id() )
{}
// Just to make compilers happy.
virtual ~work_thread_t() = default;
//! Actual processing of the demand.
/*!
* Must be implemented in derived classes.
*/
virtual void
on_demand( execution_demand_t demand ) noexcept = 0;
//! ID of the work thread.
current_thread_id_t
thread_id() const noexcept
{
return m_thread_id;
}
public :
//! Lauch processing of demand on the context of current thread.
/*!
* Creates an instance of Derived class, stores pointer to it into
* a thread_local static variable, then calls io_svc.run() method.
*
* \attention
* Terminates the whole application if an exception will be thrown.
*
* \tparam Derived Type of an object to be created on the stack.
* \tparam Args Types of arguments for Derived's constructor.
*/
template< typename Derived, typename... Args >
static void
run(
//! SObjectizer Environment for which work thread was created.
environment_t & env,
//! Asio IoService to be run on the context of that thread.
::asio::io_context & io_svc,
//! Arguments to Derived's constructor.
Args &&... args )
{
// We don't expect any errors here.
// But if something happens then there is no way to
// recover and the whole application should be aborted.
try
{
Derived actual_handler{ std::forward<Args>(args)... };
// actual_handler must be accessible via thread_local variable.
set_ptr( &actual_handler );
// Prevent return from io_context::run() if there is no
// more Asio's events.
auto work = ::asio::make_work_guard( io_svc );
io_svc.run();
}
catch( const std::exception & x )
{
::so_5::details::abort_on_fatal_error( [&] {
SO_5_LOG_ERROR( env, log_stream ) {
log_stream << "An exception caught in work thread "
"of so_5::extra::disp::asio_thread_pool dispatcher."
" Exception: "
<< x.what() << std::endl;
}
} );
}
catch( ... )
{
::so_5::details::abort_on_fatal_error( [&] {
SO_5_LOG_ERROR( env, log_stream ) {
log_stream << "An unknown exception caught in work thread "
"of so_5::extra::disp::asio_thread_pool dispatcher."
<< std::endl;
}
} );
}
}
//! An interface method for passing a demand to processing.
static void
handle_demand( execution_demand_t demand )
{
ptr()->on_demand( std::move(demand) );
}
};
//
// work_thread_without_activity_tracking_t
//
/*!
* \brief An implementation of work thread stuff for the case when
* thread activity tracking is not needed.
*
* \since
* v.1.0.2
*/
class work_thread_without_activity_tracking_t final : public work_thread_t
{
public :
work_thread_without_activity_tracking_t() = default;
~work_thread_without_activity_tracking_t() override = default;
protected :
virtual void
on_demand( execution_demand_t demand ) noexcept override
{
demand.call_handler( thread_id() );
}
};
//
// work_thread_activity_collector_t
//
/*!
* \brief Type of collector of work thread activity data.
*
* Objects of this class store also an ID of work thread. This ID is
* necessary for so_5::stats::messages::work_thread_activity message.
* Because of that a work thread must call setup_thread_id() method
* before use of activity collector.
*
* \since
* v.1.0.2
*/
class work_thread_activity_collector_t
{
private :
//! ID of thread for which activity stats is collected.
current_thread_id_t m_thread_id;
//! Collected activity stats.
::so_5::stats::activity_tracking_stuff::stats_collector_t<
::so_5::stats::activity_tracking_stuff::internal_lock >
m_work_activity;
public :
/*!
* \brief Setup ID of the current work thread.
*
* \attention
* Must be called as soon as possible after the start of the work thread.
*/
void
setup_thread_id( current_thread_id_t tid )
{
m_thread_id = std::move(tid);
}
/*!
* \brief Get the ID of the thread.
*
* \attention
* Returns actual value only after call to setup_thread_id.
*/
current_thread_id_t
thread_id() const noexcept { return m_thread_id; }
/*!
* \brief Mark start point of new activity.
*/
void
activity_started() noexcept
{
m_work_activity.start();
}
/*!
* \brief Mark completion of the current activity.
*/
void
activity_finished() noexcept
{
m_work_activity.stop();
}
/*!
* \brief Get the current stats.
*/
::so_5::stats::work_thread_activity_stats_t
take_activity_stats() noexcept
{
::so_5::stats::work_thread_activity_stats_t result;
result.m_working_stats = m_work_activity.take_stats();
return result;
}
};
//
// work_thread_with_activity_tracking_t
//
/*!
* \brief An implementation of work thread stuff for the case when
* thread activity tracking must be used.
*
* \since
* v.1.0.2
*/
class work_thread_with_activity_tracking_t final : public work_thread_t
{
private :
//! Activity statistics.
outliving_reference_t< work_thread_activity_collector_t > m_activity_stats;
public :
work_thread_with_activity_tracking_t(
outliving_reference_t< work_thread_activity_collector_t > activity_stats )
: m_activity_stats(activity_stats)
{
// Collector must receive ID of this thread.
m_activity_stats.get().setup_thread_id( thread_id() );
}
~work_thread_with_activity_tracking_t() override = default;
protected :
virtual void
on_demand( execution_demand_t demand ) noexcept override
{
m_activity_stats.get().activity_started();
demand.call_handler( thread_id() );
m_activity_stats.get().activity_finished();
}
};
//
// class basic_binder_impl_t
//
/*!
* \brief Basic part of implementation of a binder for %asio_thread_pool
* dispatcher.
*
* \since
* v.1.3.0
*/
class basic_binder_impl_t
: public disp_binder_t
, public event_queue_t
{
public :
//! Initializing constructor.
basic_binder_impl_t(
//! The actual dispatcher to be used with that binder.
actual_dispatcher_shptr_t dispatcher )
: m_dispatcher{ std::move(dispatcher) }
{}
void
preallocate_resources(
agent_t & /*agent*/ ) override
{
// There is no need to do something.
}
void
undo_preallocation(
agent_t & /*agent*/ ) noexcept override
{
// There is no need to do something.
}
void
bind(
agent_t & agent ) noexcept override
{
// Dispatcher should know about yet another agent bound.
m_dispatcher->agent_bound();
// Agent should receive its event_queue.
agent.so_bind_to_dispatcher( *this );
}
void
unbind(
agent_t & /*agent*/ ) noexcept override
{
// Dispatcher should know about yet another agent unbound.
m_dispatcher->agent_bound();
}
protected :
//! The actual dispatcher.
actual_dispatcher_shptr_t m_dispatcher;
};
//
// binder_template_t
//
/*!
* \brief An implementation of a binder for %asio_thread_pool dispatcher.
*
* This binder is also an event_queue for the agents bound via that binder.
*
* There is no such thing as event_queue for %asio_thread_pool dispacher.
* All execution demands will be stored inside Asio IoServce and dispatched
* for execution via asio::post mechanism. But SObjectizer requires
* an implementation of event_queue which must be used for agents bound
* to %asio_thread_pool dispatcher. This class implements this event_queue
* concepts.
*
* This templates implements CRTP and should be parametrized by
* derived type. The derived type should provide method:
* \code
* ::asio::io_context::strand & strand() noexcept;
* \endcode
*
* \since
* v.1.3.0
*/
template< typename Derived >
class binder_template_t
: public basic_binder_impl_t
{
auto &
self_reference() noexcept
{
return static_cast< Derived & >( *this );
}
public :
using basic_binder_impl_t::basic_binder_impl_t;
void
push( execution_demand_t demand ) override
{
demands_counter_t & counter = m_dispatcher->demands_counter();
// Another demand will wait for processing.
++counter;
asio::post( self_reference().strand(),
[d = std::move(demand), &counter]() mutable {
// Another demand will be processed.
--counter;
// Delegate processing of the demand to actual
// work thread.
work_thread_t::handle_demand( std::move(d) );
} );
}
};
//
// binder_with_external_strand_t
//
/*!
* \brief An implementation of binder that uses an external strand object.
*
* \since
* v.1.3.0
*/
class binder_with_external_strand_t final
: public binder_template_t< binder_with_external_strand_t >
{
using base_type = binder_template_t< binder_with_external_strand_t >;
public :
binder_with_external_strand_t(
actual_dispatcher_shptr_t dispatcher,
outliving_reference_t< ::asio::io_context::strand > strand )
: base_type{ std::move(dispatcher) }
, m_strand{ strand }
{}
::asio::io_context::strand &
strand() noexcept { return m_strand.get(); }
private :
//! Strand to be used with this event_queue.
outliving_reference_t< ::asio::io_context::strand > m_strand;
};
//
// binder_with_own_strand_t
//
/*!
* \brief An implementation of binder that uses an own strand object.
*
* This own strand object will be a part of the binder object.
*
* \since
* v.1.3.0
*/
class binder_with_own_strand_t final
: public binder_template_t< binder_with_own_strand_t >
{
using base_type = binder_template_t< binder_with_own_strand_t >;
public :
binder_with_own_strand_t(
actual_dispatcher_shptr_t dispatcher )
: base_type{ std::move(dispatcher) }
, m_strand{ m_dispatcher->io_context() }
{}
::asio::io_context::strand &
strand() noexcept { return m_strand; }
private :
//! Strand to be used with this event_queue.
::asio::io_context::strand m_strand;
};
//
// basic_dispatcher_skeleton_t
//
/*!
* \brief Basic stuff for all implementations of dispatcher.
*
* Derived classes should implement the following virtual methods:
* - data_source();
* - launch_work_threads();
* - wait_work_threads().
*
* \since
* v.1.0.2
*/
class basic_dispatcher_skeleton_t : public actual_dispatcher_iface_t
{
protected :
class disp_data_source_t;
friend class disp_data_source_t;
public:
basic_dispatcher_skeleton_t(
disp_params_t params )
: m_thread_count( params.thread_count() )
, m_io_context( params.io_context() )
{
}
[[nodiscard]]
disp_binder_shptr_t
binder_with_external_strand(
::asio::io_context::strand & strand ) override
{
return { std::make_shared< binder_with_external_strand_t >(
shared_from_this(),
outliving_mutable(strand) )
};
}
[[nodiscard]]
disp_binder_shptr_t
binder_with_own_strand() override
{
return { std::make_shared< binder_with_own_strand_t >(
shared_from_this() )
};
}
::asio::io_context &
io_context() const noexcept override { return *m_io_context; }
void
agent_bound() noexcept override
{
++m_agents_bound;
}
void
agent_unbound() noexcept override
{
--m_agents_bound;
}
demands_counter_t &
demands_counter() noexcept override
{
return m_demands_counter;
}
protected :
void
start(
environment_t & env,
std::string_view data_sources_name_base )
{
data_source().set_data_sources_name_base( data_sources_name_base );
data_source().start( outliving_mutable(env.stats_repository()) );
::so_5::details::do_with_rollback_on_exception(
[&] { launch_work_threads(env); },
[this] { data_source().stop(); } );
}
void
shutdown()
{
::so_5::details::invoke_noexcept_code( [this] {
// Stopping Asio IO service.
m_io_context->stop();
} );
}
void
wait()
{
::so_5::details::invoke_noexcept_code( [this] {
// Waiting for complete stop of all work threads.
wait_work_threads();
// Stopping data source.
data_source().stop();
} );
}
protected :
/*!
* \brief Get the count of work threads to be created.
*/
std::size_t
thread_count() const noexcept { return m_thread_count; }
/*!
* \brief Get access to actual data source instance for that
* dispatcher.
*/
virtual disp_data_source_t &