-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathservice.hpp
288 lines (231 loc) · 6.76 KB
/
service.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
// solid/frame/service.hpp
//
// Copyright (c) 2013, 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/common.hpp"
#include "solid/frame/manager.hpp"
#include "solid/frame/schedulerbase.hpp"
#include "solid/utility/any.hpp"
#include <atomic>
#include <mutex>
#include <vector>
namespace solid {
class EventBase;
namespace frame {
class ActorBase;
class Service;
template <class S = Service>
class ServiceShell;
struct UseServiceShell : NonCopyable {
Manager& rmanager;
UseServiceShell(UseServiceShell&& _uss)
: rmanager(_uss.rmanager)
{
}
UseServiceShell(const UseServiceShell& _uss)
: rmanager(_uss.rmanager)
{
}
private:
template <class S>
friend class ServiceShell;
explicit UseServiceShell(Manager& _rmanager)
: rmanager(_rmanager)
{
}
};
class Service : NonCopyable {
enum struct StatusE {
Stopped,
Running,
Stopping,
};
Manager& rm_;
std::atomic<size_t> idx_;
Any<> any_;
protected:
explicit Service(
UseServiceShell _force_shell, const bool _start = true);
template <typename A>
Service(
UseServiceShell _force_shell, A&& _a, const bool _start = true);
public:
using ActorMutexT = Manager::ActorMutexT;
using ServiceMutexT = Manager::ServiceMutexT;
virtual ~Service();
bool registered() const;
void notifyAll(EventBase const& _e);
template <class F>
bool forEach(F& _rf);
void stop(const bool _wait = true, const bool _check = true);
Manager& manager();
ActorMutexT& mutex(const ActorBase& _ract) const;
ActorIdT id(const ActorBase& _ract) const;
auto& any()
{
return any_;
}
const auto& any() const
{
return any_;
}
ServiceStatusE status() const;
protected:
ServiceMutexT& mutex() const;
ServiceStatusE status(std::unique_lock<ServiceMutexT>& _rlock) const;
void doStart();
template <typename A>
void doStart(A&& _a);
template <typename A, typename F>
void doStartWithAny(A&& _a, F&& _f);
template <typename F>
void doStartWithoutAny(F&& _f);
private:
friend class Manager;
friend class SchedulerBase;
ActorIdT registerActor(ActorBase& _ract, ReactorBase& _rr, ScheduleFunctionT& _rfct, ErrorConditionT& _rerr);
size_t index() const;
void index(const size_t _idx);
virtual void onLockedStoppingBeforeActors();
};
inline Service::Service(
UseServiceShell _force_shell, const bool _start)
: rm_(_force_shell.rmanager)
, idx_(static_cast<size_t>(InvalidIndex()))
{
rm_.registerService(*this, _start);
}
template <typename AnyType>
inline Service::Service(
UseServiceShell _force_shell, AnyType&& _any, const bool _start)
: rm_(_force_shell.rmanager)
, idx_(static_cast<size_t>(InvalidIndex()))
, any_(std::forward<AnyType>(_any))
{
rm_.registerService(*this, _start);
}
inline Service::~Service()
{
stop(true, false);
rm_.unregisterService(*this);
}
template <class F>
inline bool Service::forEach(F& _rf)
{
return rm_.forEachServiceActor(*this, _rf);
}
inline Manager& Service::manager()
{
return rm_;
}
inline bool Service::registered() const
{
return idx_.load(/*std::memory_order_seq_cst*/) != InvalidIndex();
}
inline size_t Service::index() const
{
return idx_.load();
}
inline void Service::index(const size_t _idx)
{
idx_.store(_idx);
}
inline void Service::notifyAll(EventBase const& _revt)
{
rm_.notifyAll(*this, _revt);
}
inline void Service::doStart()
{
rm_.startService(
*this, [](std::unique_lock<ServiceMutexT>&) {});
}
template <typename AnyType>
inline void Service::doStart(AnyType&& _any)
{
Any<> any{std::forward<AnyType>(_any)};
rm_.startService(
*this, [this, &any](std::unique_lock<ServiceMutexT>&) { any_ = std::move(any); });
}
template <typename AnyType, typename F>
inline void Service::doStartWithAny(AnyType&& _any, F&& _on_locked_start)
{
Any<> any{std::forward<AnyType>(_any)};
rm_.startService(
*this, [this, &any, &_on_locked_start](std::unique_lock<ServiceMutexT>& _lock) { any_ = std::move(any); _on_locked_start(_lock); });
}
template <typename F>
inline void Service::doStartWithoutAny(F&& _on_locked_start)
{
rm_.startService(
*this, [&_on_locked_start](std::unique_lock<ServiceMutexT>& _lock) { _on_locked_start(_lock); });
}
inline void Service::stop(const bool _wait, const bool _check)
{
rm_.stopService(*this, _wait, _check);
}
inline Service::ActorMutexT& Service::mutex(const ActorBase& _ract) const
{
return rm_.mutex(_ract);
}
inline ActorIdT Service::id(const ActorBase& _ract) const
{
return rm_.id(_ract);
}
inline Service::ServiceMutexT& Service::mutex() const
{
return rm_.mutex(*this);
}
inline ServiceStatusE Service::status(std::unique_lock<ServiceMutexT>& _rlock) const
{
return rm_.status(*this, _rlock);
}
inline ServiceStatusE Service::status() const
{
return rm_.status(*this);
}
inline ActorIdT Service::registerActor(ActorBase& _ract, ReactorBase& _rr, ScheduleFunctionT& _rfct, ErrorConditionT& _rerr)
{
return rm_.registerActor(*this, _ract, _rr, _rfct, _rerr);
}
//! A Shell class for every Service
/*!
* This class is provided for defensive C++ programming.
* Actors from a Service use reference to their service.
* Situation: we have ServiceA: public Service. Actors from ServiceA use reference to ServiceA.
* If we only call Service::stop() from within frame::Service::~Service, when ServiceA gets destroyed,
* existing actors (at the moment we call Service::stop) might be still accessing ServiceA actor layer
* which was destroyed.
* That is why we've introduce the ServiceShell which will stand as an upper layer for all Service
* instantiations which will call Service::stop on its destructor, so that when the lower layer Service
* gets destroyed no actor will exist.
* ServiceShell is final to prevent inheriting from it.
* More over, we introduce the UseServiceShell stub to force all Service instantiations to happen through
* a ServiceShell.
*/
template <class S>
class ServiceShell final : public S {
public:
template <typename... Args>
explicit ServiceShell(Manager& _rm, Args&&... _args)
: S(UseServiceShell(_rm), std::forward<Args>(_args)...)
{
}
~ServiceShell()
{
Service::stop(true, false);
}
template <typename... Args>
void start(Args&&... _args)
{
S::doStart(std::forward<Args>(_args)...);
}
};
using ServiceT = ServiceShell<>;
} // namespace frame
} // namespace solid