-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscheduler.hpp
198 lines (166 loc) · 5.81 KB
/
scheduler.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
// solid/frame/scheduler.hpp
//
// Copyright (c) 2014 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// 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.
//
#pragma once
#include "solid/frame/manager.hpp"
#include "solid/frame/schedulerbase.hpp"
#include <deque>
namespace solid {
namespace frame {
class Service;
constexpr size_t default_reactor_wake_capacity = 1024;
template <class ReactorStats>
struct SchedulerStatistic : solid::Statistic {
std::deque<ReactorStats> reactor_stats_dq_;
void resize(const size_t _size)
{
reactor_stats_dq_.resize(_size);
}
ReactorStats& reactorStatistic(const size_t _index)
{
return reactor_stats_dq_[_index];
}
std::ostream& print(std::ostream& _ros) const override
{
for (size_t i = 0; i < reactor_stats_dq_.size(); ++i) {
_ros << '[' << i << "]{";
reactor_stats_dq_[i].print(_ros) << '}';
}
return _ros;
}
void clear()
{
for (auto& rs : reactor_stats_dq_) {
rs.clear();
}
}
};
template <class R>
class Scheduler : private SchedulerBase {
using ReactorT = R;
using ThisT = Scheduler<ReactorT>;
public:
using ActorT = typename ReactorT::ActorT;
using EventT = typename ReactorT::EventT;
using ReactorStatisticT = typename ReactorT::StatisticT;
using ActorPointerT = std::shared_ptr<ActorT>;
using StatisticT = SchedulerStatistic<ReactorStatisticT>;
private:
StatisticT statistic_;
struct Worker {
static void run(SchedulerBase* _psched, const size_t _idx, const size_t _wake_capacity)
{
auto& rthis = *static_cast<ThisT*>(_psched);
auto reactor_ptr = std::make_shared<ReactorT>(*_psched, rthis.statistic_.reactorStatistic(_idx), _idx, _wake_capacity);
if (!reactor_ptr->prepareThread(reactor_ptr->start())) {
return;
}
reactor_ptr->run();
reactor_ptr->unprepareThread();
}
static bool create(SchedulerBase& _rsched, const size_t _idx, std::thread& _rthr, const size_t _wake_capacity)
{
bool rv = false;
try {
_rthr = std::thread(Worker::run, &_rsched, _idx, _wake_capacity);
rv = true;
} catch (...) {
}
return rv;
}
};
struct ScheduleCommand {
ActorPointerT&& ractptr;
Service& rsvc;
EventBase&& revt;
ScheduleCommand(ActorPointerT&& _ractptr, Service& _rsvc, EventBase&& _revt)
: ractptr(std::move(_ractptr))
, rsvc(_rsvc)
, revt(std::move(_revt))
{
}
bool operator()(ReactorBase& _rreactor)
{
return static_cast<ReactorT&>(_rreactor).push(std::move(ractptr), rsvc, std::move(revt));
}
};
public:
Scheduler() = default;
~Scheduler()
{
doStop(true);
}
void start(const size_t _reactorcnt = 1, const size_t _wake_capacity = default_reactor_wake_capacity)
{
ThreadEnterFunctionT enf;
ThreadExitFunctionT exf;
statistic_.resize(_reactorcnt);
statistic_.clear();
SchedulerBase::doStart(Worker::create, enf, exf, _reactorcnt, _wake_capacity);
}
template <class EnterFct, class ExitFct>
std::enable_if_t<
std::conjunction_v<
std::is_invocable_r<bool, EnterFct>,
std::is_invocable<ExitFct>>>
start(EnterFct _enf, ExitFct _exf, const size_t _reactorcnt = 1, const size_t _wake_capacity = default_reactor_wake_capacity)
{
ThreadEnterFunctionT enf(std::move(_enf));
ThreadExitFunctionT exf(std::move(_exf));
statistic_.resize(_reactorcnt);
statistic_.clear();
SchedulerBase::doStart(Worker::create, enf, exf, _reactorcnt, _wake_capacity);
}
StatisticT& statistic()
{
return statistic_;
}
const StatisticT& statistic() const
{
return statistic_;
}
size_t workerCount() const
{
return SchedulerBase::workerCount();
}
void stop(const bool _wait = true)
{
SchedulerBase::doStop(_wait);
}
ActorIdT startActor(
ActorPointerT&& _ractptr, Service& _rsvc,
EventBase&& _revt, ErrorConditionT& _rerr)
{
auto& ractor = *_ractptr;
ScheduleCommand cmd(std::move(_ractptr), _rsvc, std::move(_revt));
ScheduleFunctionT fct([&cmd](ReactorBase& _rreactor) { return cmd(_rreactor); });
return doStartActor(ractor, _rsvc, fct, _rerr);
}
ActorIdT startActor(
ActorPointerT&& _ractptr, Service& _rsvc, const size_t _worker_index,
EventBase&& _revt, ErrorConditionT& _rerr)
{
auto& ractor = *_ractptr;
ScheduleCommand cmd(std::move(_ractptr), _rsvc, std::move(_revt));
ScheduleFunctionT fct([&cmd](ReactorBase& _rreactor) { return cmd(_rreactor); });
return doStartActor(ractor, _rsvc, _worker_index, fct, _rerr);
}
};
template <class Actr, class Schd, class Srvc, class... P>
inline ActorIdT make_actor(Schd& _rschd, Srvc& _rsrvc, EventBase&& _revt, ErrorConditionT& _rerr, P&&... _p)
{
return _rschd.startActor(std::make_shared<Actr>(std::forward<P>(_p)...), _rsrvc, std::move(_revt), _rerr);
}
template <class Actr, class Schd, class Srvc, class... P>
inline ActorIdT make_actor(Schd& _rschd, Srvc& _rsrvc, const size_t _worker_index, EventBase&& _revt, ErrorConditionT& _rerr, P&&... _p)
{
return _rschd.startActor(std::make_shared<Actr>(std::forward<P>(_p)...), _rsrvc, _worker_index, std::move(_revt), _rerr);
}
} // namespace frame
} // namespace solid