The Gaudi Framework  v32r2 (46d42edc)
ThreadInitTask.cpp
Go to the documentation of this file.
1 #include "ThreadInitTask.h"
6 #include "GaudiKernel/SmartIF.h"
8 #include <thread>
9 
10 namespace Gaudi {
11  namespace Concurrency {
12  thread_local bool ThreadInitDone{false};
13  }
14 } // namespace Gaudi
15 
17 
19 
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 
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;
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
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();
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 
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 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
tbb::task * execute() override
Execute the task.
Define general base for Gaudi exception.
SmartIF< ISvcLocator > m_serviceLocator
static std::atomic< bool > m_execFailed
bool empty() const override
Return whether the list of tools is empty.
Definition: GaudiHandle.h:467
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:494
T what(T... args)
STL class.
ToolHandleArray< IThreadInitTool > m_tools
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
thread_local bool ThreadInitDone
boost::barrier * m_barrier
T hex(T... args)
bool isFailure() const
Definition: StatusCode.h:130
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:508