The Gaudi Framework  v33r0 (d5ea422b)
ThreadInitTask.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #include "ThreadInitTask.h"
15 #include "GaudiKernel/MsgStream.h"
16 #include "GaudiKernel/SmartIF.h"
17 #include "GaudiKernel/ToolHandle.h"
18 #include <thread>
19 
20 namespace Gaudi {
21  namespace Concurrency {
22  thread_local bool ThreadInitDone{false};
23  }
24 } // namespace Gaudi
25 
27 
29 
31  MsgStream log( messageSvc, "ThreadInitTask" );
32 
33  const auto debug = log.level() <= MSG::DEBUG;
34 
35  if ( debug )
36  log << MSG::DEBUG << "execute() in thread 0x" << std::hex << pthread_self() << " at " << this << std::dec
37  << " state: " << ( m_terminate ? "terminate" : "initialize" ) << endmsg;
38 
40  log << MSG::WARNING << "thread initialization has already been done on thread " << std::hex << pthread_self()
41  << endmsg;
42  }
43 
44  if ( m_tools.retrieve().isFailure() ) {
45  log << MSG::ERROR << "unable to retrieve ToolHandleArray " << m_tools << endmsg;
46  m_execFailed = true;
47  } else {
48 
49  if ( m_tools.empty() ) {
50  log << MSG::DEBUG << "no entries in Tool Array" << endmsg;
52  } else {
53  if ( debug ) log << MSG::DEBUG << "executing in thread 0x" << std::hex << pthread_self() << std::dec << endmsg;
54 
55  // only call terminate for threads that have been initialized
57  log << MSG::INFO << "Not calling terminateThread for thread 0x" << std::hex << pthread_self()
58  << " as it has not been initialized" << endmsg;
59  } else {
60 
61  for ( auto& t : m_tools ) {
62  try {
63 
64  if ( debug ) log << MSG::DEBUG << "calling IThreadInitTool " << t << endmsg;
65 
66  if ( !m_terminate ) {
67  t->initThread();
69  } else {
70  t->terminateThread();
71  }
72 
73  } catch ( const GaudiException& exc ) {
74  log << MSG::ERROR << "ThreadInitTool " << t << " in thread 0x" << std::hex << pthread_self() << std::dec
75  << " threw GaudiException: " << exc << endmsg;
76  m_execFailed = true;
77  } catch ( const std::exception& exc ) {
78  log << MSG::ERROR << "ThreadInitTool " << t << " in thread 0x" << std::hex << pthread_self() << std::dec
79  << " threw std::exception: " << exc.what() << endmsg;
80  m_execFailed = true;
81  } catch ( ... ) {
82  log << MSG::ERROR << "ThreadInitTool " << t << " in thread 0x" << std::hex << pthread_self() << std::dec
83  << " threw unknown exception" << endmsg;
84  m_execFailed = true;
85  }
86  }
87  }
88 
90  }
91  }
92 
93  if ( m_barrier ) {
94  log << MSG::DEBUG << "waiting at barrier in thread 0x" << std::hex << pthread_self() << std::dec << endmsg;
95  m_barrier->wait();
96  }
97 
98  return nullptr;
99 }
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:34
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:477
StatusCode retrieve()
Retrieve all tools.
Definition: GaudiHandle.h:504
T what(T... args)
STL class.
ToolHandleArray< IThreadInitTool > m_tools
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:164
thread_local bool ThreadInitDone
boost::barrier * m_barrier
T hex(T... args)
bool isFailure() const
Definition: StatusCode.h:141
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:202
StatusCode release()
Release all tools.
Definition: GaudiHandle.h:518