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 "oneapi/tbb/task_arena.h"
26 #include "oneapi/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(oneapi::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  // Use in the case where you need to inform the parent task of a
64  // failure before some other child task which may be run later
65  // reports a different, but related failure. You must later call
66  // doneWaiting in the same thread passing the same exception.
67  void presetTaskAsFailed(std::exception_ptr iExcept) noexcept;
68 
69  // This next function is useful if you know from the context that
70  // m_arena (which is set when the constructor was executes) is the
71  // same arena in which you want to execute the doneWaiting function.
72  // It allows an optimization which avoids the enqueue step in the
73  // doneWaiting function.
74  //
75  // Be warned though that in general this function cannot be used.
76  // Spawning a task outside the correct arena could create a new separate
77  // arena with its own extra TBB worker threads if this function is used
78  // in an inappropriate context (and silently such that you might not notice
79  // the problem quickly).
81 
82  bool taskHasFailed() const noexcept;
83 
84  bool hasTask() const noexcept;
85 
89  CMS_SA_ALLOW oneapi::tbb::task_group* group() const { return m_group; }
90 
91  private:
92  // ---------- member data --------------------------------
94  oneapi::tbb::task_group* m_group;
95  std::shared_ptr<oneapi::tbb::task_arena> m_arena;
96  };
97 
98  template <typename F>
100  return [holder = std::move(h), func = std::forward<F>(f)]() mutable {
101  try {
102  func(holder);
103  } catch (...) {
104  holder.doneWaiting(std::current_exception());
105  }
106  };
107  }
108 
109  template <typename F>
111  return make_waiting_task(
112  [holder = h, func = make_lambda_with_holder(h, std::forward<F>(f))](std::exception_ptr const* excptr) mutable {
113  if (excptr) {
114  holder.doneWaiting(*excptr);
115  return;
116  }
117  func();
118  });
119  }
120 } // namespace edm
121 #endif
#define CMS_SA_ALLOW
WaitingTaskWithArenaHolder & operator=(const WaitingTaskWithArenaHolder &iRHS)
std::shared_ptr< oneapi::tbb::task_arena > m_arena
void doneWaiting(std::exception_ptr iExcept)
oneapi::tbb::task_group * group() const
FunctorWaitingTask< F > * make_waiting_task(F f)
Definition: WaitingTask.h:92
double f[11][100]
auto make_lambda_with_holder(WaitingTaskWithArenaHolder h, F &&f)
void presetTaskAsFailed(std::exception_ptr iExcept) noexcept
auto make_waiting_task_with_holder(WaitingTaskWithArenaHolder h, F &&f)
HLT enums.
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
Definition: blowfish.cc:163
The Signals That Services Can Subscribe To This is based on ActivityRegistry h
Helper function to determine trigger accepts.
Definition: Activities.doc:4
def move(src, dest)
Definition: eostools.py:511