7#ifndef PXR_BASE_WORK_DISPATCHER_H
8#define PXR_BASE_WORK_DISPATCHER_H
14#include "pxr/base/work/api.h"
21#include <tbb/blocked_range.h>
22#include <tbb/concurrent_vector.h>
23#if TBB_INTERFACE_VERSION_MAJOR >= 12
24#include <tbb/task_group.h>
33PXR_NAMESPACE_OPEN_SCOPE
89 template <
class Callable,
class A1,
class A2, ...
class AN>
90 void Run(Callable &&c, A1 &&a1, A2 &&a2, ... AN &&aN);
94 template <
class Callable>
95 inline void Run(Callable &&c) {
96#if TBB_INTERFACE_VERSION_MAJOR >= 12
97 _taskGroup.run(_InvokerTask<
typename std::remove_reference<Callable>::type>(std::forward<Callable>(c), &_errors));
99 _rootTask->spawn(_MakeInvokerTask(std::forward<Callable>(c)));
103 template <
class Callable,
class A0,
class ... Args>
104 inline void Run(Callable &&c, A0 &&a0, Args&&... args) {
105 Run(std::bind(std::forward<Callable>(c),
106 std::forward<A0>(a0),
107 std::forward<Args>(args)...));
128 typedef tbb::concurrent_vector<TfErrorTransport> _ErrorTransports;
133#if TBB_INTERFACE_VERSION_MAJOR >= 12
135 struct _InvokerTask {
136 explicit _InvokerTask(Fn &&fn, _ErrorTransports *err)
137 : _fn(
std::move(fn)), _errors(err) {}
139 explicit _InvokerTask(Fn
const &fn, _ErrorTransports *err)
140 : _fn(fn), _errors(err) {}
143 _InvokerTask(_InvokerTask &&other) =
default;
144 _InvokerTask(
const _InvokerTask &other) =
delete;
145 _InvokerTask &operator=(
const _InvokerTask &other) =
delete;
147 void operator()()
const {
151 WorkDispatcher::_TransportErrors(m, _errors);
155 _ErrorTransports *_errors;
159 struct _InvokerTask :
public tbb::task {
160 explicit _InvokerTask(Fn &&fn, _ErrorTransports *err)
161 : _fn(
std::move(fn)), _errors(err) {}
163 explicit _InvokerTask(Fn
const &fn, _ErrorTransports *err)
164 : _fn(fn), _errors(err) {}
166 virtual tbb::task* execute() {
170 const_cast<_InvokerTask
const *
>(
this)->_fn();
172 WorkDispatcher::_TransportErrors(m, _errors);
177 _ErrorTransports *_errors;
182 _InvokerTask<typename std::remove_reference<Fn>::type>&
183 _MakeInvokerTask(Fn &&fn) {
184 return *
new( _rootTask->allocate_additional_child_of(*_rootTask) )
185 _InvokerTask<
typename std::remove_reference<Fn>::type>(
186 std::forward<Fn>(fn), &_errors);
193 _TransportErrors(
const TfErrorMark &m, _ErrorTransports *errors);
196 tbb::task_group_context _context;
197#if TBB_INTERFACE_VERSION_MAJOR >= 12
199 class _TaskGroup :
public tbb::task_group {
201 _TaskGroup(tbb::task_group_context& ctx) : tbb::task_group(ctx) {}
202 inline tbb::detail::d1::wait_context& _GetInternalWaitContext();
205 _TaskGroup _taskGroup;
209 tbb::empty_task* _rootTask;
214 _ErrorTransports _errors;
217 std::atomic_flag _waitCleanupFlag;
222struct Work_DeprecatedMutableTask {
223 explicit Work_DeprecatedMutableTask(Fn &&fn)
224 : _fn(
std::move(fn)) {}
226 explicit Work_DeprecatedMutableTask(Fn
const &fn)
230 Work_DeprecatedMutableTask
231 (Work_DeprecatedMutableTask &&other) =
default;
232 Work_DeprecatedMutableTask
233 (
const Work_DeprecatedMutableTask &other) =
delete;
234 Work_DeprecatedMutableTask
235 &operator= (
const Work_DeprecatedMutableTask &other) =
delete;
237 void operator()()
const {
247template <
typename Fn>
248Work_DeprecatedMutableTask<typename std::remove_reference_t<Fn>>
249WorkMakeDeprecatedMutableTask(Fn &&fn) {
250 return Work_DeprecatedMutableTask<typename std::remove_reference_t<Fn>>
251 (std::forward<Fn>(fn));
256PXR_NAMESPACE_CLOSE_SCOPE
Class used to record the end of the error-list.
bool IsClean() const
Return true if no new errors were posted in this thread since the last call to SetMark(),...
A work dispatcher runs concurrent tasks.
WORK_API ~WorkDispatcher() noexcept
Wait() for any pending tasks to complete, then destroy the dispatcher.
WORK_API void Cancel()
Cancel remaining work and return immediately.
WORK_API void Wait()
Block until the work started by Run() completes.
WORK_API WorkDispatcher()
Construct a new dispatcher.
void Run(Callable &&c, A1 &&a1, A2 &&a2,... AN &&aN)
Add work for the dispatcher to run.