Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TPoolManager.cxx
Go to the documentation of this file.
1 #include "ROOT/TPoolManager.hxx"
2 #include "TError.h"
3 #include "TROOT.h"
4 #include <algorithm>
5 #include "tbb/task_scheduler_init.h"
6 
7 namespace ROOT {
8 
9  namespace Internal {
10  //Returns the weak_ptr reflecting a shared_ptr to the only instance of the Pool Manager.
11  //This will allow to check if the shared_ptr is still alive, solving the dangling pointer problem.
12  std::weak_ptr<TPoolManager> &GetWP()
13  {
14  static std::weak_ptr<TPoolManager> weak_sched;
15  return weak_sched;
16  }
17 
18  UInt_t TPoolManager::fgPoolSize = 0;
19 
20  TPoolManager::TPoolManager(UInt_t nThreads): fSched(new tbb::task_scheduler_init(tbb::task_scheduler_init::deferred))
21  {
22  //Is it there another instance of the tbb scheduler running?
23  if (fSched->is_active()) {
24  mustDelete = false;
25  }
26 
27  nThreads = nThreads != 0 ? nThreads : tbb::task_scheduler_init::default_num_threads();
28  fSched ->initialize(nThreads);
29  fgPoolSize = nThreads;
30  };
31 
32  TPoolManager::~TPoolManager()
33  {
34  //Only terminate the tbb scheduler if there was not another instance already
35  // running when the constructor was called.
36  if (mustDelete) {
37  fSched->terminate();
38  fgPoolSize = 0;
39  }
40  }
41 
42  //Number of threads the PoolManager has been initialized with.
43  UInt_t TPoolManager::GetPoolSize()
44  {
45  return fgPoolSize;
46  }
47 
48  //Factory function returning a shared pointer to the only instance of the PoolManager.
49  std::shared_ptr<TPoolManager> GetPoolManager(UInt_t nThreads)
50  {
51  if (GetWP().expired()) {
52  std::shared_ptr<TPoolManager> shared(new TPoolManager(nThreads));
53  GetWP() = shared;
54  return GetWP().lock();
55  }
56  return GetWP().lock();
57  }
58  }
59 }