| The Gaudi Framework v32r1 (f65d50dc)

ThreadInitTask.cpp
Go to the documentation of this file.
1 #include "ThreadInitTask.h"
2 #include "GaudiKernel/IMessageSvc.h"
3 #include "GaudiKernel/ISvcLocator.h"
4 #include "GaudiKernel/IThreadInitTool.h"
5 #include "GaudiKernel/MsgStream.h"
6 #include "GaudiKernel/SmartIF.h"
7 #include "GaudiKernel/ToolHandle.h"
8 #include <thread>
9
10 namespace Gaudi {
11 namespace Concurrency {
12 thread_local bool ThreadInitDone{false};
13 }
14 } // namespace Gaudi
15
16 std::atomic<bool> ThreadInitTask::m_execFailed( false );
17
18 tbb::task* ThreadInitTask::execute() {
19
20 SmartIF<IMessageSvc> messageSvc( m_serviceLocator );
21 MsgStream log( messageSvc, "ThreadInitTask" );
22
23 const auto debug = log.level() <= MSG::DEBUG;
24
25 if ( debug )
26 log << MSG::DEBUG << "execute() in thread 0x" << std::hex << pthread_self() << " at " << this << std::dec
27 << " state: " << ( m_terminate ? "terminate" : "initialize" ) << endmsg;
28
29 if ( !m_terminate && Gaudi::Concurrency::ThreadInitDone ) {
30 log << MSG::WARNING << "thread initialization has already been done on thread " << std::hex << pthread_self()
31 << endmsg;
32 }
33
34 if ( m_tools.retrieve().isFailure() ) {
35 log << MSG::ERROR << "unable to retrieve ToolHandleArray " << m_tools << endmsg;
36 m_execFailed = true;
37 } else {
38
39 if ( m_tools.empty() ) {
40 log << MSG::DEBUG << "no entries in Tool Array" << endmsg;
41 if ( !m_terminate ) { Gaudi::Concurrency::ThreadInitDone = true; }
42 } else {
43 if ( debug ) log << MSG::DEBUG << "executing in thread 0x" << std::hex << pthread_self() << std::dec << endmsg;
44
45 // only call terminate for threads that have been initialized
46 if ( m_terminate && !Gaudi::Concurrency::ThreadInitDone ) {
47 log << MSG::INFO << "Not calling terminateThread for thread 0x" << std::hex << pthread_self()
48 << " as it has not been initialized" << endmsg;
49 } else {
50
51 for ( auto& t : m_tools ) {
52 try {
53
54 if ( debug ) log << MSG::DEBUG << "calling IThreadInitTool " << t << endmsg;
55
56 if ( !m_terminate ) {
57 t->initThread();
58 Gaudi::Concurrency::ThreadInitDone = true;
59 } else {
60 t->terminateThread();
61 }
62
63 } catch ( const GaudiException& exc ) {
64 log << MSG::ERROR << "ThreadInitTool " << t << " in thread 0x" << std::hex << pthread_self() << std::dec
65 << " threw GaudiException: " << exc << endmsg;
66 m_execFailed = true;
67 } catch ( const std::exception& exc ) {
68 log << MSG::ERROR << "ThreadInitTool " << t << " in thread 0x" << std::hex << pthread_self() << std::dec
69 << " threw std::exception: " << exc.what() << endmsg;
70 m_execFailed = true;
71 } catch ( ... ) {
72 log << MSG::ERROR << "ThreadInitTool " << t << " in thread 0x" << std::hex << pthread_self() << std::dec
73 << " threw unknown exception" << endmsg;
74 m_execFailed = true;
75 }
76 }
77 }
78
79 m_tools.release().ignore();
80 }
81 }
82
83 if ( m_barrier ) {
84 log << MSG::DEBUG << "waiting at barrier in thread 0x" << std::hex << pthread_self() << std::dec << endmsg;
85 m_barrier->wait();
86 }
87
88 return nullptr;
89 }
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
SmartIF< IMessageSvc >
ThreadInitTask::execute
tbb::task * execute() override
Execute the task.
Definition: ThreadInitTask.cpp:18
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
std::atomic< bool >
ThreadInitTask::m_serviceLocator
SmartIF< ISvcLocator > m_serviceLocator
Definition: ThreadInitTask.h:42
MsgStream.h
ThreadInitTask::m_execFailed
static std::atomic< bool > m_execFailed
Definition: ThreadInitTask.h:45
MSG::DEBUG
Definition: IMessageSvc.h:15
GaudiHandleArray::empty
bool empty() const override
Return whether the list of tools is empty.
Definition: GaudiHandle.h:471
ThreadInitTask.h
GaudiHandleArray::retrieve
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:498
std::exception::what
T what(T... args)
SmartIF.h
IMessageSvc.h
std::exception
STL class.
ThreadInitTask::m_tools
ToolHandleArray< IThreadInitTool > m_tools
Definition: ThreadInitTask.h:39
StatusCode::ignore
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
Gaudi::Concurrency::ThreadInitDone
thread_local bool ThreadInitDone
Definition: ThreadInitTask.cpp:12
ThreadInitTask::m_terminate
bool m_terminate
Definition: ThreadInitTask.h:43
ThreadInitTask::m_barrier
boost::barrier * m_barrier
Definition: ThreadInitTask.h:41
ToolHandle.h
std::hex
T hex(T... args)
MSG::ERROR
Definition: IMessageSvc.h:15
MSG::WARNING
Definition: IMessageSvc.h:15
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
IThreadInitTool.h
MSG::INFO
Definition: IMessageSvc.h:15
ISvcLocator.h
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
Gaudi.Configuration.log
log
Definition: Configuration.py:13
GaudiHandleArray::release
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:512