CMS 3D CMS Logo

WaitingTaskWithArenaHolder.h
Go to the documentation of this file.
1 #ifndef FWCore_Concurrency_WaitingTaskWithArenaHolder_h
2 #define FWCore_Concurrency_WaitingTaskWithArenaHolder_h
3 // -*- C++ -*-
4 //
5 // Package: FWCore/Concurrency
6 // Class : WaitingTaskWithArenaHolder
7 //
17 //
18 // Original Author: W. David Dagenhart
19 // Created: 9 November 2017
20 //
21 
22 #include <exception>
23 #include <memory>
24 
25 #include "tbb/task_arena.h"
26 #include "tbb/task_group.h"
27 
29 
30 namespace edm {
31  class WaitingTask;
32  class WaitingTaskHolder;
33 
35  public:
37 
38  // Note that the arena will be the one containing the thread
39  // that runs this constructor. This is the arena where you
40  // eventually intend for the task to be spawned.
41  explicit WaitingTaskWithArenaHolder(tbb::task_group&, WaitingTask* iTask);
42 
43  // Takes ownership of the underlying task and uses the current
44  // arena.
46 
48 
50 
52 
54 
56 
57  // This spawns the task. The arena is needed to get the task spawned
58  // into the correct arena of threads. Use of the arena allows doneWaiting
59  // to be called from a thread outside the arena of threads that will manage
60  // the task. doneWaiting can be called from a non-TBB thread.
61  void doneWaiting(std::exception_ptr iExcept);
62 
63  // This next function is useful if you know from the context that
64  // m_arena (which is set when the constructor was executes) is the
65  // same arena in which you want to execute the doneWaiting function.
66  // It allows an optimization which avoids the enqueue step in the
67  // doneWaiting function.
68  //
69  // Be warned though that in general this function cannot be used.
70  // Spawning a task outside the correct arena could create a new separate
71  // arena with its own extra TBB worker threads if this function is used
72  // in an inappropriate context (and silently such that you might not notice
73  // the problem quickly).
75 
76  bool taskHasFailed() const noexcept;
77 
78  bool hasTask() const noexcept;
79 
83  CMS_SA_ALLOW tbb::task_group* group() const { return m_group; }
84 
85  private:
86  // ---------- member data --------------------------------
88  tbb::task_group* m_group;
89  std::shared_ptr<tbb::task_arena> m_arena;
90  };
91 
92  template <typename F>
94  return [holder = std::move(h), func = std::forward<F>(f)]() mutable {
95  try {
96  func(holder);
97  } catch (...) {
98  holder.doneWaiting(std::current_exception());
99  }
100  };
101  }
102 
103  template <typename F>
105  return make_waiting_task(
106  [holder = h, func = make_lambda_with_holder(h, std::forward<F>(f))](std::exception_ptr const* excptr) mutable {
107  if (excptr) {
108  holder.doneWaiting(*excptr);
109  return;
110  }
111  func();
112  });
113  }
114 } // namespace edm
115 #endif
edm::WaitingTaskWithArenaHolder::operator=
WaitingTaskWithArenaHolder & operator=(const WaitingTaskWithArenaHolder &iRHS)
Definition: WaitingTaskWithArenaHolder.cc:49
edm::WaitingTaskWithArenaHolder::WaitingTaskWithArenaHolder
WaitingTaskWithArenaHolder()
Definition: WaitingTaskWithArenaHolder.cc:16
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::WaitingTaskWithArenaHolder::m_task
WaitingTask * m_task
Definition: WaitingTaskWithArenaHolder.h:87
edm::make_waiting_task_with_holder
auto make_waiting_task_with_holder(WaitingTaskWithArenaHolder h, F &&f)
Definition: WaitingTaskWithArenaHolder.h:104
CMS_SA_ALLOW
#define CMS_SA_ALLOW
Definition: thread_safety_macros.h:5
F
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
edm::WaitingTaskWithArenaHolder::group
tbb::task_group * group() const
Definition: WaitingTaskWithArenaHolder.h:83
edm::WaitingTaskWithArenaHolder::m_arena
std::shared_ptr< tbb::task_arena > m_arena
Definition: WaitingTaskWithArenaHolder.h:89
edm::make_lambda_with_holder
auto make_lambda_with_holder(WaitingTaskWithArenaHolder h, F &&f)
Definition: WaitingTaskWithArenaHolder.h:93
edm::WaitingTaskWithArenaHolder
Definition: WaitingTaskWithArenaHolder.h:34
WaitingTask
WaitingTaskHolder
h
edm::WaitingTaskWithArenaHolder::m_group
tbb::task_group * m_group
Definition: WaitingTaskWithArenaHolder.h:88
edm::WaitingTaskWithArenaHolder::~WaitingTaskWithArenaHolder
~WaitingTaskWithArenaHolder()
Definition: WaitingTaskWithArenaHolder.cc:31
edm::make_waiting_task
FunctorWaitingTask< F > * make_waiting_task(F f)
Definition: WaitingTask.h:101
edm::WaitingTaskHolder
Definition: WaitingTaskHolder.h:32
thread_safety_macros.h
TrackCollections2monitor_cff.func
func
Definition: TrackCollections2monitor_cff.py:359
edm::WaitingTask
Definition: WaitingTask.h:36
eostools.move
def move(src, dest)
Definition: eostools.py:511
edm::WaitingTaskWithArenaHolder::makeWaitingTaskHolderAndRelease
WaitingTaskHolder makeWaitingTaskHolderAndRelease()
Definition: WaitingTaskWithArenaHolder.cc:103
edm::WaitingTaskWithArenaHolder::taskHasFailed
bool taskHasFailed() const noexcept
Definition: WaitingTaskWithArenaHolder.cc:110
edm::WaitingTaskWithArenaHolder::hasTask
bool hasTask() const noexcept
Definition: WaitingTaskWithArenaHolder.cc:112
edm::WaitingTaskWithArenaHolder::doneWaiting
void doneWaiting(std::exception_ptr iExcept)
Definition: WaitingTaskWithArenaHolder.cc:69