The Gaudi Framework  master (37c0b60a)
ThreadPoolSvc.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 #ifndef GAUDIHIVE_THREADPOOLSVC_H
12 #define GAUDIHIVE_THREADPOOLSVC_H
13 
16 #include <GaudiKernel/Service.h>
17 #include <GaudiKernel/ToolHandle.h>
18 
19 #include <boost/thread/barrier.hpp>
20 
21 #include <tbb/global_control.h>
22 #include <tbb/spin_mutex.h>
23 #include <tbb/task_arena.h>
24 
25 #include <memory>
26 #include <vector>
27 
38 class ThreadPoolSvc : public extends<Service, IThreadPoolSvc> {
39 public:
41  ThreadPoolSvc( const std::string& name, ISvcLocator* svc );
42 
44  StatusCode initialize() override final;
45 
47  StatusCode finalize() override final;
48 
50  StatusCode initPool( const int& poolSize, const int& maxParallelismExtra ) override final;
51 
53  StatusCode terminatePool() override final;
54 
55  int poolSize() const override final { return m_threadPoolSize; }
56 
57  virtual bool isInit() const { return m_init; }
58 
59  virtual void initThisThread() override;
60 
61  tbb::task_arena* getArena() { return &m_arena; }
62 
63 private:
65  StatusCode launchTasks( bool finalize = false );
66 
69 
71  bool m_init = false;
72 
75 
77  tbb::spin_mutex m_initMutex;
78 
81 
84 
86  tbb::task_arena m_arena;
87 
90 };
91 
92 #endif
IThreadInitTool.h
ThreadPoolSvc::m_initMutex
tbb::spin_mutex m_initMutex
Mutex used to protect the initPool and terminatePool methods.
Definition: ThreadPoolSvc.h:77
ThreadPoolSvc::ThreadPoolSvc
ThreadPoolSvc(const std::string &name, ISvcLocator *svc)
Constructor.
Definition: ThreadPoolSvc.cpp:32
ThreadPoolSvc::poolSize
int poolSize() const override final
Definition: ThreadPoolSvc.h:55
ThreadPoolSvc::m_threadInitCount
std::atomic< int > m_threadInitCount
Counter for all threads that are initialised.
Definition: ThreadPoolSvc.h:89
std::string
STL class.
ThreadPoolSvc::launchTasks
StatusCode launchTasks(bool finalize=false)
Launch tasks to execute the ThreadInitTools.
Definition: ThreadPoolSvc.cpp:158
ISvcLocator
Definition: ISvcLocator.h:46
ThreadPoolSvc::initialize
StatusCode initialize() override final
Initialise.
Definition: ThreadPoolSvc.cpp:38
ThreadPoolSvc::isInit
virtual bool isInit() const
Definition: ThreadPoolSvc.h:57
ThreadPoolSvc::m_tbbgc
std::unique_ptr< tbb::global_control > m_tbbgc
TBB global control parameter.
Definition: ThreadPoolSvc.h:83
ThreadPoolSvc::m_threadInitTools
ToolHandleArray< IThreadInitTool > m_threadInitTools
Handle array of thread init tools.
Definition: ThreadPoolSvc.h:68
ThreadPoolSvc::m_init
bool m_init
Was the thread pool initialized?
Definition: ThreadPoolSvc.h:71
ThreadPoolSvc::initPool
StatusCode initPool(const int &poolSize, const int &maxParallelismExtra) override final
Initialize the thread pool and launch the ThreadInitTasks.
Definition: ThreadPoolSvc.cpp:73
Service::name
const std::string & name() const override
Retrieve name of the service
Definition: Service.cpp:332
StatusCode
Definition: StatusCode.h:65
ThreadPoolSvc::m_threadPoolSize
int m_threadPoolSize
Size of the thread pool allocated.
Definition: ThreadPoolSvc.h:74
ThreadPoolSvc::getArena
tbb::task_arena * getArena()
Definition: ThreadPoolSvc.h:61
ThreadPoolSvc::finalize
StatusCode finalize() override final
Finalise.
Definition: ThreadPoolSvc.cpp:62
ToolHandleArray< IThreadInitTool >
ThreadPoolSvc::m_barrier
std::unique_ptr< boost::barrier > m_barrier
Barrier used to synchronization thread init tasks.
Definition: ThreadPoolSvc.h:80
std::atomic< int >
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:20
Service.h
ThreadPoolSvc::m_arena
tbb::task_arena m_arena
TBB task arena to run all algorithms.
Definition: ThreadPoolSvc.h:86
ToolHandle.h
ThreadPoolSvc::terminatePool
StatusCode terminatePool() override final
Terminate the thread pool and launch thread termination tasks.
Definition: ThreadPoolSvc.cpp:137
std::unique_ptr< boost::barrier >
ThreadPoolSvc::initThisThread
virtual void initThisThread() override
Definition: ThreadPoolSvc.cpp:234
IThreadPoolSvc.h
ThreadPoolSvc
A service which initializes a TBB thread pool.
Definition: ThreadPoolSvc.h:38