6 #include "tbb/task_scheduler_init.h" 7 #include "tbb/task_scheduler_observer.h" 9 #include "tbb/tick_count.h" 10 #include "tbb/tbb_thread.h" 21 m_threadInitTools(this),
24 m_tbbSchedInit(
nullptr),
29 "ToolHandleArray of IThreadInitTools");
41 warning () <<
"Base class could not be initialized" <<
endmsg;
45 if (m_threadInitTools.retrieve().isFailure()) {
46 error() <<
"Unable to retrieve ThreadInitTools Array" <<
endmsg;
50 if (m_threadInitTools.size() != 0) {
51 info() <<
"retrieved " << m_threadInitTools.size() <<
" thread init tools" 54 info() <<
"no thread init tools attached" <<
endmsg;
68 warning() <<
"Looks like the ThreadPoolSvc was created, but thread pool " 69 <<
"was never initialized" <<
endmsg;
81 tbb::spin_mutex::scoped_lock lock( m_initMutex );
83 m_threadPoolSize = poolSize;
86 debug() <<
"ThreadPoolSvc::initPool() poolSize = " << poolSize <<
endmsg;
98 if (-100 != m_threadPoolSize) {
100 debug() <<
"Initialising a thread pool of size " 101 << m_threadPoolSize <<
endmsg;
105 int thePoolSize = m_threadPoolSize;
106 if (thePoolSize != -1) thePoolSize += 1;
112 if(m_threadPoolSize<=-1)thePoolSize=m_tbbSchedInit->default_num_threads();
114 debug() <<
"creating barrier of size " << thePoolSize <<
endmsg;
121 const bool terminate =
false;
122 if (launchTasks(terminate).isFailure())
126 debug() <<
"Thread Pool initialization complete!" <<
endmsg;
138 tbb::spin_mutex::scoped_lock lock( m_initMutex );
140 debug() <<
"ThreadPoolSvc::terminatePool()" <<
endmsg;
143 error() <<
"Trying to terminate uninitialized thread pool!" <<
endmsg;
148 const bool terminate =
true;
149 if (launchTasks(terminate).isFailure())
153 debug() <<
"Thread pool termination complete!" <<
endmsg;
165 const std::string taskType = terminate ?
"termination" :
"initialization";
172 for (
int i = 0; i < m_threadPoolSize; ++i) {
174 debug() <<
"creating ThreadInitTask " << i <<
endmsg;
175 tbb::task* t =
new(tbb::task::allocate_root())
176 ThreadInitTask( m_threadInitTools, m_barrier.get(), serviceLocator(), terminate );
179 tbb::task::enqueue( *t );
180 this_tbb_thread::sleep(tbb::tick_count::interval_t(.1));
185 debug() <<
"waiting at barrier for all ThreadInitTool to finish executing" <<
endmsg;
190 for (
auto& t : m_threadInitTools) {
192 int numInit = t->nInit();
194 int expectedNumInit = terminate? 0 : m_threadPoolSize;
195 if (numInit != expectedNumInit) {
196 error() <<
"not all threads " << (terminate?
"terminated" :
"initialized")
197 <<
" for tool " << t <<
" : " 198 << t->nInit() <<
" out of " << m_threadPoolSize
199 <<
" are currently active" <<
endmsg;
210 debug() <<
"launching ThreadInitTask " << taskType <<
"in this thread." <<
endmsg;
211 boost::barrier* noBarrier =
nullptr;
212 ThreadInitTask theTask(m_threadInitTools, noBarrier, serviceLocator(), terminate);
218 error() <<
"a ThreadInitTask failed to execute successfully" <<
endmsg;
virtual StatusCode finalize() override final
Finalise.
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.
Gaudi::Details::PropertyBase * declareProperty(const std::string &name, TYPE &value, const std::string &doc="none")
Declare a property (templated)
Special TBB task used by ThreadPoolSvc to wrap execution of IThreadInitTools.
This class is used for returning status codes from appropriate routines.
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.