8 #include "tbb/task_scheduler_init.h" 9 #include "tbb/task_scheduler_observer.h" 10 #include "tbb/tbb_thread.h" 11 #include "tbb/tick_count.h" 21 , m_threadInitTools( this )
23 , m_threadPoolSize( 0 )
24 , m_tbbSchedInit(
nullptr )
25 , m_barrier(
nullptr )
28 declareProperty(
"ThreadInitTools", m_threadInitTools,
"ToolHandleArray of IThreadInitTools" );
39 warning() <<
"Base class could not be initialized" <<
endmsg;
43 if ( m_threadInitTools.retrieve().isFailure() ) {
44 error() <<
"Unable to retrieve ThreadInitTools Array" <<
endmsg;
48 if ( m_threadInitTools.size() != 0 ) {
49 info() <<
"retrieved " << m_threadInitTools.size() <<
" thread init tools" <<
endmsg;
51 info() <<
"no thread init tools attached" <<
endmsg;
64 warning() <<
"Looks like the ThreadPoolSvc was created, but thread pool " 65 <<
"was never initialized" <<
endmsg;
76 tbb::spin_mutex::scoped_lock lock( m_initMutex );
78 m_threadPoolSize = poolSize;
80 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"ThreadPoolSvc::initPool() poolSize = " << poolSize <<
endmsg;
92 if ( -100 != m_threadPoolSize ) {
93 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"Initialising a thread pool of size " << m_threadPoolSize <<
endmsg;
97 int thePoolSize = m_threadPoolSize;
98 if ( thePoolSize != -1 ) thePoolSize += 1;
103 if ( m_threadPoolSize <= -1 ) thePoolSize = m_tbbSchedInit->default_num_threads();
105 debug() <<
"creating barrier of size " << thePoolSize <<
endmsg;
116 const bool terminate =
false;
119 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"Thread Pool initialization complete!" <<
endmsg;
130 tbb::spin_mutex::scoped_lock lock( m_initMutex );
131 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"ThreadPoolSvc::terminatePool()" <<
endmsg;
134 error() <<
"Trying to terminate uninitialized thread pool!" <<
endmsg;
139 const bool terminate =
true;
142 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"Thread pool termination complete!" <<
endmsg;
154 const std::string taskType = terminate ?
"termination" :
"initialization";
158 if ( m_tbbSchedInit ) {
161 for (
int i = 0; i < m_threadPoolSize; ++i ) {
162 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"creating ThreadInitTask " << i <<
endmsg;
163 tbb::task* t =
new ( tbb::task::allocate_root() )
164 ThreadInitTask( m_threadInitTools, m_barrier.get(), serviceLocator(), terminate );
167 tbb::task::enqueue( *t );
168 this_tbb_thread::sleep( tbb::tick_count::interval_t( .1 ) );
172 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"waiting at barrier for all ThreadInitTool to finish executing" <<
endmsg;
177 for (
auto& t : m_threadInitTools ) {
179 int numInit = t->nInit();
181 int expectedNumInit = terminate ? 0 : m_threadPoolSize;
182 if ( numInit != expectedNumInit ) {
183 error() <<
"not all threads " << ( terminate ?
"terminated" :
"initialized" ) <<
" for tool " << t <<
" : " 184 << t->nInit() <<
" out of " << m_threadPoolSize <<
" are currently active" <<
endmsg;
194 if ( msgLevel(
MSG::DEBUG ) ) debug() <<
"launching ThreadInitTask " << taskType <<
"in this thread." <<
endmsg;
195 boost::barrier* noBarrier =
nullptr;
196 ThreadInitTask theTask( m_threadInitTools, noBarrier, serviceLocator(), terminate );
202 error() <<
"a ThreadInitTask failed to execute successfully" <<
endmsg;
virtual StatusCode finalize() override final
Finalise.
static GAUDI_API void setNumThreads(const std::size_t &nT)
StatusCode initialize() override
tbb::task * execute() override
Execute the task.
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
virtual StatusCode terminatePool() override final
Terminate the thread pool and launch thread termination tasks.
bool isSuccess() const
Test for a status code of SUCCESS.
Special TBB task used by ThreadPoolSvc to wrap execution of IThreadInitTools.
This class is used for returning status codes from appropriate routines.
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
virtual StatusCode initialize() override final
Initialise.
#define DECLARE_SERVICE_FACTORY(x)
A service which initializes a TBB thread pool.
virtual StatusCode initPool(const int &poolSize) override final
Initialize the thread pool and launch the ThreadInitTasks.
StatusCode launchTasks(bool finalize=false)
Launch tasks to execute the ThreadInitTools.
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.