CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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
#define CMS_SA_ALLOW
WaitingTaskWithArenaHolder & operator=(const WaitingTaskWithArenaHolder &iRHS)
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t Func __host__ __device__ V int Func func
void doneWaiting(std::exception_ptr iExcept)
def move
Definition: eostools.py:511
FunctorWaitingTask< F > * make_waiting_task(F f)
Definition: WaitingTask.h:101
auto make_lambda_with_holder(WaitingTaskWithArenaHolder h, F &&f)
auto make_waiting_task_with_holder(WaitingTaskWithArenaHolder h, F &&f)
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
std::shared_ptr< tbb::task_arena > m_arena