src
FWCore
Integration
plugins
TestServicesOnNonFrameworkThreadsAnalyzer.cc
Go to the documentation of this file.
1
#include "
FWCore/Framework/interface/stream/EDAnalyzer.h
"
2
#include "
FWCore/Framework/interface/MakerMacros.h
"
3
#include "
FWCore/Framework/interface/Event.h
"
4
#include "
FWCore/Framework/interface/ModuleContextSentry.h
"
5
#include "
FWCore/ServiceRegistry/interface/ServiceRegistry.h
"
6
#include "
FWCore/ServiceRegistry/interface/Service.h
"
7
8
#include "
FWCore/Utilities/interface/RandomNumberGenerator.h
"
9
#include "
FWCore/MessageLogger/interface/MessageLogger.h
"
10
#include "
FWCore/MessageLogger/interface/edm_MessageLogger.h
"
11
#include <thread>
12
#include <mutex>
13
#include <condition_variable>
14
#include <memory>
15
#include <iostream>
16
#include <exception>
17
18
#include "CLHEP/Random/RandFlat.h"
19
20
namespace
edmtest
{
21
class
TestServicesOnNonFrameworkThreadsAnalyzer
:
public
edm::stream::EDAnalyzer
<> {
22
public
:
23
TestServicesOnNonFrameworkThreadsAnalyzer
(
edm::ParameterSet
const
&);
24
~TestServicesOnNonFrameworkThreadsAnalyzer
()
override
;
25
26
void
analyze
(
edm::Event
const
&,
edm::EventSetup
const
&)
final
;
27
28
private
:
29
void
runOnOtherThread
();
30
void
shutdownThread
();
31
std::unique_ptr<std::thread>
m_thread
;
32
std::mutex
m_mutex
;
33
std::condition_variable
m_condVar
;
34
35
bool
m_managerThreadReady
=
false
;
36
bool
m_continueProcessing
=
false
;
37
bool
m_eventWorkDone
=
false
;
38
39
//context info
40
edm::ModuleCallingContext
const
*
m_moduleCallingContext
=
nullptr
;
41
edm::ServiceToken
*
m_serviceToken
=
nullptr
;
42
edm::StreamID
m_streamID
;
43
std::exception_ptr
m_except
;
44
};
45
46
TestServicesOnNonFrameworkThreadsAnalyzer::TestServicesOnNonFrameworkThreadsAnalyzer
(
edm::ParameterSet
const
&)
47
: m_streamID(
edm
::StreamID::invalidStreamID()) {
48
m_thread
= std::make_unique<std::thread>([
this
]() { this->
runOnOtherThread
(); });
49
50
m_mutex
.lock();
51
m_managerThreadReady
=
true
;
52
m_continueProcessing
=
true
;
53
}
54
55
TestServicesOnNonFrameworkThreadsAnalyzer::~TestServicesOnNonFrameworkThreadsAnalyzer
() {
56
if
(
m_thread
) {
57
shutdownThread
();
58
}
59
}
60
61
void
TestServicesOnNonFrameworkThreadsAnalyzer::analyze
(
edm::Event
const
&
iEvent
,
edm::EventSetup
const
&) {
62
m_eventWorkDone
=
false
;
63
m_moduleCallingContext
=
iEvent
.moduleCallingContext();
64
edm::ServiceToken
token
=
edm::ServiceRegistry::instance
().
presentToken
();
65
m_serviceToken
= &
token
;
66
m_streamID
=
iEvent
.streamID();
67
{
edm::LogSystem
(
"FrameworkThread"
) <<
"new Event"
; }
68
m_mutex
.unlock();
69
{
70
std::unique_lock<std::mutex> lk(
m_mutex
);
71
m_condVar
.notify_one();
72
m_condVar
.wait(lk, [
this
] {
return
this->
m_eventWorkDone
; });
73
lk.release();
74
}
75
edm::LogSystem
(
"FrameworkThread"
) <<
" done"
;
76
m_managerThreadReady
=
true
;
77
if
(
m_except
) {
78
std::rethrow_exception(
m_except
);
79
}
80
}
81
82
void
TestServicesOnNonFrameworkThreadsAnalyzer::runOnOtherThread
() {
83
std::unique_lock<std::mutex> lk(
m_mutex
);
84
85
do
{
86
m_condVar
.wait(lk, [
this
] {
return
m_managerThreadReady
; });
87
if
(
m_continueProcessing
) {
88
edm::ModuleCallingContext
newContext(*
m_moduleCallingContext
);
89
edm::ModuleContextSentry
sentry(&newContext,
m_moduleCallingContext
->
parent
());
90
91
edm::ServiceRegistry::Operate
srSentry(*
m_serviceToken
);
92
try
{
93
edm::Service<edm::RandomNumberGenerator>
rng;
94
edm::Service<edm::MessageLogger>
ml
;
95
ml
->setThreadContext(*
m_moduleCallingContext
);
96
edm::LogSystem
(
"ModuleThread"
) <<
" ++running with rng "
97
<< CLHEP::RandFlat::shootInt(&rng->
getEngine
(
m_streamID
), 10);
98
}
catch
(...) {
99
m_except
= std::current_exception();
100
}
101
}
102
m_eventWorkDone
=
true
;
103
m_managerThreadReady
=
false
;
104
lk.unlock();
105
m_condVar
.notify_one();
106
lk.lock();
107
}
while
(
m_continueProcessing
);
108
}
109
110
void
TestServicesOnNonFrameworkThreadsAnalyzer::shutdownThread
() {
111
m_continueProcessing
=
false
;
112
m_mutex
.unlock();
113
m_condVar
.notify_one();
114
m_thread
->join();
115
}
116
117
}
// namespace edmtest
118
119
DEFINE_FWK_MODULE
(
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer
);
edm_modernize_messagelogger.ml
ml
Definition:
edm_modernize_messagelogger.py:25
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::TestServicesOnNonFrameworkThreadsAnalyzer
TestServicesOnNonFrameworkThreadsAnalyzer(edm::ParameterSet const &)
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:46
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::m_mutex
std::mutex m_mutex
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:32
edm::LogSystem
Log< level::System, false > LogSystem
Definition:
MessageLogger.h:130
edm::Service< edm::RandomNumberGenerator >
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::~TestServicesOnNonFrameworkThreadsAnalyzer
~TestServicesOnNonFrameworkThreadsAnalyzer() override
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:55
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::shutdownThread
void shutdownThread()
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:110
MessageLogger.h
RandomNumberGenerator.h
ServiceRegistry.h
edm::ModuleCallingContext
Definition:
ModuleCallingContext.h:32
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::analyze
void analyze(edm::Event const &, edm::EventSetup const &) final
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:61
edm::StreamID
Definition:
StreamID.h:30
Event.h
mutex
static std::mutex mutex
Definition:
Proxy.cc:8
ModuleContextSentry.h
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::m_except
std::exception_ptr m_except
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:43
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::m_condVar
std::condition_variable m_condVar
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:33
edmtest
Definition:
AlignPCLThresholdsReader.cc:15
EDAnalyzer.h
edm::RandomNumberGenerator::getEngine
virtual CLHEP::HepRandomEngine & getEngine(StreamID const &)=0
Use this engine in event methods.
iEvent
int iEvent
Definition:
GenABIO.cc:224
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:21
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::m_thread
std::unique_ptr< std::thread > m_thread
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:31
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::m_serviceToken
edm::ServiceToken * m_serviceToken
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:41
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::m_continueProcessing
bool m_continueProcessing
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:36
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::m_streamID
edm::StreamID m_streamID
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:42
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::runOnOtherThread
void runOnOtherThread()
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:82
edm::ServiceRegistry::instance
static ServiceRegistry & instance()
Definition:
ServiceRegistry.cc:90
Service.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition:
MakerMacros.h:16
edm::EventSetup
Definition:
EventSetup.h:56
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::m_managerThreadReady
bool m_managerThreadReady
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:35
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::m_moduleCallingContext
edm::ModuleCallingContext const * m_moduleCallingContext
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:40
edm::ServiceToken
Definition:
ServiceToken.h:42
edm::ModuleContextSentry
Definition:
ModuleContextSentry.h:11
edm::stream::EDAnalyzer
Definition:
EDAnalyzer.h:31
edmtest::TestServicesOnNonFrameworkThreadsAnalyzer::m_eventWorkDone
bool m_eventWorkDone
Definition:
TestServicesOnNonFrameworkThreadsAnalyzer.cc:37
edm_MessageLogger.h
edm::ServiceRegistry::Operate
Definition:
ServiceRegistry.h:41
edm
HLT enums.
Definition:
AlignableModifier.h:19
edm::ParameterSet
Definition:
ParameterSet.h:48
edm::Event
Definition:
Event.h:73
MakerMacros.h
edm::ServiceRegistry::presentToken
ServiceToken presentToken() const
Definition:
ServiceRegistry.cc:63
unpackBuffers-CaloStage2.token
token
Definition:
unpackBuffers-CaloStage2.py:316
edm::ModuleCallingContext::parent
ParentContext const & parent() const
Definition:
ModuleCallingContext.h:56
Generated for CMSSW Reference Manual by
1.8.14