Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 
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.size() == 0 ) {
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  for ( auto& t : m_tools ) {
46 
47  try {
48 
49  if ( debug ) log << MSG::DEBUG << "calling IThreadInitTool " << t << endmsg;
50 
51  if ( !m_terminate ) {
52  t->initThread();
54  } else {
55  t->terminateThread();
56  }
57 
58  } catch ( const GaudiException& exc ) {
59  log << MSG::ERROR << "ThreadInitTool " << t << " in thread 0x" << std::hex << pthread_self() << std::dec
60  << " threw GaudiException: " << exc << endmsg;
61  m_execFailed = true;
62  } catch ( const std::exception& exc ) {
63  log << MSG::ERROR << "ThreadInitTool " << t << " in thread 0x" << std::hex << pthread_self() << std::dec
64  << " threw std::exception: " << exc.what() << endmsg;
65  m_execFailed = true;
66  } catch ( ... ) {
67  log << MSG::ERROR << "ThreadInitTool " << t << " in thread 0x" << std::hex << pthread_self() << std::dec
68  << " threw unknown exception" << endmsg;
69  m_execFailed = true;
70  }
71  }
72 
73  m_tools.release().ignore();
74  }
75  }
76 
77  if ( m_barrier != 0 ) {
78  log << MSG::DEBUG << "waiting at barrier in thread 0x" << std::hex << pthread_self() << std::dec << endmsg;
79  m_barrier->wait();
80  }
81 
82  return nullptr;
83 }
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.
static std::atomic< bool > m_execFailed
T what(T...args)
STL class.
thread_local bool ThreadInitDone
T hex(T...args)
MSG::Level level() const
Retrieve output level.
Definition: MsgStream.h:103
Helper functions to set/get the application return code.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192