7 #include "tbb/task_scheduler_init.h" 8 #include "tbb/task_scheduler_observer.h" 10 #include "tbb/tick_count.h" 11 #include "tbb/tbb_thread.h" 22 m_threadInitTools(this),
25 m_tbbSchedInit(
nullptr),
30 "ToolHandleArray of IThreadInitTools");
42 warning () <<
"Base class could not be initialized" <<
endmsg;
46 if (m_threadInitTools.retrieve().isFailure()) {
47 error() <<
"Unable to retrieve ThreadInitTools Array" <<
endmsg;
51 if (m_threadInitTools.size() != 0) {
52 info() <<
"retrieved " << m_threadInitTools.size() <<
" thread init tools" 55 info() <<
"no thread init tools attached" <<
endmsg;
69 warning() <<
"Looks like the ThreadPoolSvc was created, but thread pool " 70 <<
"was never initialized" <<
endmsg;
82 tbb::spin_mutex::scoped_lock lock( m_initMutex );
84 m_threadPoolSize = poolSize;
87 debug() <<
"ThreadPoolSvc::initPool() poolSize = " << poolSize <<
endmsg;
99 if (-100 != m_threadPoolSize) {
101 debug() <<
"Initialising a thread pool of size " 102 << m_threadPoolSize <<
endmsg;
106 int thePoolSize = m_threadPoolSize;
107 if (thePoolSize != -1) thePoolSize += 1;
113 if(m_threadPoolSize<=-1)thePoolSize=m_tbbSchedInit->default_num_threads();
115 debug() <<
"creating barrier of size " << thePoolSize <<
endmsg;
126 const bool terminate =
false;
127 if (launchTasks(terminate).isFailure())
131 debug() <<
"Thread Pool initialization complete!" <<
endmsg;
143 tbb::spin_mutex::scoped_lock lock( m_initMutex );
145 debug() <<
"ThreadPoolSvc::terminatePool()" <<
endmsg;
148 error() <<
"Trying to terminate uninitialized thread pool!" <<
endmsg;
153 const bool terminate =
true;
154 if (launchTasks(terminate).isFailure())
158 debug() <<
"Thread pool termination complete!" <<
endmsg;
170 const std::string taskType = terminate ?
"termination" :
"initialization";
177 for (
int i = 0; i < m_threadPoolSize; ++i) {
179 debug() <<
"creating ThreadInitTask " << i <<
endmsg;
180 tbb::task* t =
new(tbb::task::allocate_root())
181 ThreadInitTask( m_threadInitTools, m_barrier.get(), serviceLocator(), terminate );
184 tbb::task::enqueue( *t );
185 this_tbb_thread::sleep(tbb::tick_count::interval_t(.1));
190 debug() <<
"waiting at barrier for all ThreadInitTool to finish executing" <<
endmsg;
195 for (
auto& t : m_threadInitTools) {
197 int numInit = t->nInit();
199 int expectedNumInit = terminate? 0 : m_threadPoolSize;
200 if (numInit != expectedNumInit) {
201 error() <<
"not all threads " << (terminate?
"terminated" :
"initialized")
202 <<
" for tool " << t <<
" : " 203 << t->nInit() <<
" out of " << m_threadPoolSize
204 <<
" are currently active" <<
endmsg;
215 debug() <<
"launching ThreadInitTask " << taskType <<
"in this thread." <<
endmsg;
216 boost::barrier* noBarrier =
nullptr;
217 ThreadInitTask theTask(m_threadInitTools, noBarrier, serviceLocator(), terminate);
223 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.