Skip to content

Commit a385609

Browse files
committed
DPL: distinguish between exit and stop callbacks
1 parent 5aa4d1e commit a385609

5 files changed

Lines changed: 36 additions & 5 deletions

File tree

Framework/Core/include/Framework/ServiceRegistry.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ struct ServiceRegistry {
9797
std::vector<ServiceDispatchingHandle> mPostDispatchingHandles;
9898
/// Callbacks for services to be executed before Start
9999
std::vector<ServiceStartHandle> mPreStartHandles;
100+
/// Callbacks for services to be executed on the Stop transition
101+
std::vector<ServiceStopHandle> mPostStopHandles;
100102
/// Callbacks for services to be executed on exit
101103
std::vector<ServiceExitHandle> mPreExitHandles;
102104

@@ -149,6 +151,9 @@ struct ServiceRegistry {
149151
/// Invoke callbacks to monitor inputs after dispatching, regardless of them
150152
/// being discarded, consumed or processed.
151153
void postDispatchingCallbacks(ProcessingContext&);
154+
155+
/// Invoke callbacks on stop.
156+
void postStopCallbacks();
152157
/// Invoke callbacks on exit.
153158
void preExitCallbacks();
154159

Framework/Core/include/Framework/ServiceSpec.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ class DanglingContext;
4141

4242
/// A callback to create a given Service.
4343
using ServiceInit = std::function<ServiceHandle(ServiceRegistry&, DeviceState&, fair::mq::ProgOptions&)>;
44-
/// A callback invoked whenever we start running, before the user callback.
44+
/// A callback invoked whenever we start running, before the user processing callback.
4545
using ServiceStartCallback = std::function<void(ServiceRegistry&, void*)>;
46-
/// A callback invoked whenever we stop running, before we exit.
46+
/// A callback invoked whenever we stop running, after the user processing callback.
47+
using ServiceStopCallback = std::function<void(ServiceRegistry&, void*)>;
48+
/// A callback invoked whenever we stop running completely, before we exit.
4749
using ServiceExitCallback = std::function<void(ServiceRegistry&, void*)>;
4850

4951
/// A callback to configure a given Service. Notice that the
@@ -150,6 +152,8 @@ struct ServiceSpec {
150152

151153
/// Callback invoked on Start
152154
ServiceStartCallback start = nullptr;
155+
/// Callback invoked on Start
156+
ServiceStopCallback stop = nullptr;
153157
/// Callback invoked on exit
154158
ServiceExitCallback exit = nullptr;
155159
/// Callback invoked on driver entering the INIT state
@@ -197,6 +201,11 @@ struct ServiceStartHandle {
197201
void* service;
198202
};
199203

204+
struct ServiceStopHandle {
205+
ServiceStopCallback callback;
206+
void* service;
207+
};
208+
200209
struct ServiceExitHandle {
201210
ServiceExitCallback callback;
202211
void* service;

Framework/Core/src/DataProcessingDevice.cxx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,13 @@ void DataProcessingDevice::PreRun()
586586
void DataProcessingDevice::PostRun()
587587
{
588588
mServiceRegistry.get<CallbackService>()(CallbackService::Id::Stop);
589-
mServiceRegistry.preExitCallbacks();
589+
mServiceRegistry.postStopCallbacks();
590590
}
591591

592-
void DataProcessingDevice::Reset() { mServiceRegistry.get<CallbackService>()(CallbackService::Id::Reset); }
592+
void DataProcessingDevice::Reset()
593+
{
594+
mServiceRegistry.get<CallbackService>()(CallbackService::Id::Reset);
595+
}
593596

594597
void DataProcessingDevice::Run()
595598
{

Framework/Core/src/ServiceRegistry.cxx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ void ServiceRegistry::bindService(ServiceSpec const& spec, void* service)
103103
if (spec.start) {
104104
mPreStartHandles.push_back(ServiceStartHandle{spec.start, service});
105105
}
106+
if (spec.stop) {
107+
mPostStopHandles.push_back(ServiceStopHandle{spec.stop, service});
108+
}
106109
if (spec.exit) {
107110
mPreExitHandles.push_back(ServiceExitHandle{spec.exit, service});
108111
}
@@ -172,6 +175,15 @@ void ServiceRegistry::preStartCallbacks()
172175
}
173176
}
174177

178+
void ServiceRegistry::postStopCallbacks()
179+
{
180+
// FIXME: we need to call the callback only once for the global services
181+
/// I guess...
182+
for (auto& stopHandle : mPostStopHandles) {
183+
stopHandle.callback(*this, stopHandle.service);
184+
}
185+
}
186+
175187
/// Invoke callback to be executed on exit, in reverse order.
176188
void ServiceRegistry::preExitCallbacks()
177189
{

Framework/Core/src/runDataProcessing.cxx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,9 @@ int doChild(int argc, char** argv, ServiceRegistry& serviceRegistry,
10741074
};
10751075

10761076
runner.AddHook<fair::mq::hooks::InstantiateDevice>(afterConfigParsingCallback);
1077-
return runner.Run();
1077+
auto result = runner.Run();
1078+
serviceRegistry.preExitCallbacks();
1079+
return result;
10781080
}
10791081

10801082
struct WorkflowInfo {

0 commit comments

Comments
 (0)