All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
arenaDispatcher.h
Go to the documentation of this file.
1 //
2 // Copyright 2016 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 #ifndef WORK_ARENA_DISPATCHER_H
25 #define WORK_ARENA_DISPATCHER_H
26 
28 
29 #include "pxr/pxr.h"
30 #include "pxr/base/work/dispatcher.h"
31 #include "pxr/base/work/threadLimits.h"
32 #include "pxr/base/work/api.h"
33 
34 #include <tbb/task_arena.h>
35 
36 #include <functional>
37 #include <type_traits>
38 #include <utility>
39 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
56 {
57 public:
60  WorkArenaDispatcher() : _arena(_GetArena()) {}
61 
63  WORK_API ~WorkArenaDispatcher();
64 
65  WorkArenaDispatcher(WorkArenaDispatcher const &) = delete;
66  WorkArenaDispatcher &operator=(WorkArenaDispatcher const &) = delete;
67 
68 #ifdef doxygen
69 
79  template <class Callable, class A1, class A2, ... class AN>
80  void Run(Callable &&c, A1 &&a1, A2 &&a2, ... AN &&aN);
81 
82 #else // doxygen
83 
84  template <class Callable, class ... Args>
85  inline void Run(Callable &&c, Args&&... args) {
86  _arena->execute(
87  _MakeRunner(&_dispatcher,
88  std::bind(std::forward<Callable>(c),
89  std::forward<Args>(args)...)));
90  }
91 
92 #endif // doxygen
93 
95  WORK_API void Wait();
96 
101  WORK_API void Cancel();
102 
103 private:
104  WORK_API tbb::task_arena *_GetArena() const;
105 
106  template <class Fn>
107  struct _Runner {
108  _Runner(WorkDispatcher *wd, Fn &&fn) : _wd(wd), _fn(std::move(fn)) {}
109  _Runner(WorkDispatcher *wd, Fn const &fn) : _wd(wd), _fn(fn) {}
110  void operator()() { _wd->Run(std::move(_fn)); }
111  void operator()() const { _wd->Run(std::move(_fn)); }
112  private:
113  WorkDispatcher *_wd;
114  mutable Fn _fn;
115  };
116 
117  template <class Fn>
118  _Runner<typename std::remove_reference<Fn>::type>
119  _MakeRunner(WorkDispatcher *wd, Fn &&fn) {
120  return _Runner<typename std::remove_reference<Fn>::type>(
121  wd, std::forward<Fn>(fn));
122  }
123 
124  // The task arena.
125  tbb::task_arena *_arena;
126 
127  // The dispatcher.
128  WorkDispatcher _dispatcher;
129 };
130 
132 
133 PXR_NAMESPACE_CLOSE_SCOPE
134 
135 #endif // WORK_ARENA_DISPATCHER_H
A work dispatcher runs concurrent tasks.
Definition: dispatcher.h:71
void Run(Callable &&c, A1 &&a1, A2 &&a2,...AN &&aN)
Add work for the dispatcher to run.
WorkArenaDispatcher()
Constructs a new dispatcher.
WORK_API ~WorkArenaDispatcher()
Wait() for any pending tasks to complete, then destroy the dispatcher.
This is a specialization of the WorkDispatcher that uses an isolated arena to Run() all its tasks in...
WORK_API void Wait()
Block until the work started by Run() completes.
WORK_API void Cancel()
Cancel remaining work and return immediately.