Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
ThreadPoolSvc.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2025 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 #pragma once
12 
15 #include <GaudiKernel/Service.h>
16 #include <GaudiKernel/ToolHandle.h>
17 
18 #include <boost/thread/barrier.hpp>
19 
20 #include <tbb/global_control.h>
21 #include <tbb/spin_mutex.h>
22 #include <tbb/task_arena.h>
23 
24 #include <memory>
25 #include <vector>
26 
37 class ThreadPoolSvc : public extends<Service, IThreadPoolSvc> {
38 public:
40  ThreadPoolSvc( const std::string& name, ISvcLocator* svc );
41 
43  StatusCode initialize() override final;
44 
46  StatusCode finalize() override final;
47 
49  StatusCode initPool( const int& poolSize, const int& maxParallelismExtra ) override final;
50 
52  StatusCode terminatePool() override final;
53 
54  int poolSize() const override final { return m_threadPoolSize; }
55 
56  virtual bool isInit() const { return m_init; }
57 
58  virtual void initThisThread() override;
59 
60  tbb::task_arena* getArena() { return &m_arena; }
61 
62 private:
64  StatusCode launchTasks( bool finalize = false );
65 
68 
70  bool m_init = false;
71 
74 
76  tbb::spin_mutex m_initMutex;
77 
79  std::unique_ptr<boost::barrier> m_barrier;
80 
82  std::unique_ptr<tbb::global_control> m_tbbgc;
83 
85  tbb::task_arena m_arena;
86 
88  std::atomic<int> m_threadInitCount = 0;
89 };
IThreadInitTool.h
ThreadPoolSvc::m_initMutex
tbb::spin_mutex m_initMutex
Mutex used to protect the initPool and terminatePool methods.
Definition: ThreadPoolSvc.h:76
ThreadPoolSvc::ThreadPoolSvc
ThreadPoolSvc(const std::string &name, ISvcLocator *svc)
Constructor.
Definition: ThreadPoolSvc.cpp:32
ThreadPoolSvc::poolSize
int poolSize() const override final
Definition: ThreadPoolSvc.h:54
ThreadPoolSvc::m_threadInitCount
std::atomic< int > m_threadInitCount
Counter for all threads that are initialised.
Definition: ThreadPoolSvc.h:88
ThreadPoolSvc::launchTasks
StatusCode launchTasks(bool finalize=false)
Launch tasks to execute the ThreadInitTools.
Definition: ThreadPoolSvc.cpp:158
ISvcLocator
Definition: ISvcLocator.h:42
ThreadPoolSvc::initialize
StatusCode initialize() override final
Initialise.
Definition: ThreadPoolSvc.cpp:38
ThreadPoolSvc::isInit
virtual bool isInit() const
Definition: ThreadPoolSvc.h:56
ThreadPoolSvc::m_tbbgc
std::unique_ptr< tbb::global_control > m_tbbgc
TBB global control parameter.
Definition: ThreadPoolSvc.h:82
ThreadPoolSvc::m_threadInitTools
ToolHandleArray< IThreadInitTool > m_threadInitTools
Handle array of thread init tools.
Definition: ThreadPoolSvc.h:67
ThreadPoolSvc::m_init
bool m_init
Was the thread pool initialized?
Definition: ThreadPoolSvc.h:70
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:333
StatusCode
Definition: StatusCode.h:64
ThreadPoolSvc::m_threadPoolSize
int m_threadPoolSize
Size of the thread pool allocated.
Definition: ThreadPoolSvc.h:73
ThreadPoolSvc::getArena
tbb::task_arena * getArena()
Definition: ThreadPoolSvc.h:60
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:79
extends
Base class used to extend a class implementing other interfaces.
Definition: extends.h:19
Service.h
ThreadPoolSvc::m_arena
tbb::task_arena m_arena
TBB task arena to run all algorithms.
Definition: ThreadPoolSvc.h:85
ToolHandle.h
ThreadPoolSvc::terminatePool
StatusCode terminatePool() override final
Terminate the thread pool and launch thread termination tasks.
Definition: ThreadPoolSvc.cpp:137
ThreadPoolSvc::initThisThread
virtual void initThisThread() override
Definition: ThreadPoolSvc.cpp:234
IThreadPoolSvc.h
ThreadPoolSvc
A service which initializes a TBB thread pool.
Definition: ThreadPoolSvc.h:37