24#ifndef PXR_BASE_WORK_DISPATCHER_H
25#define PXR_BASE_WORK_DISPATCHER_H
31#include "pxr/base/work/api.h"
36#include <tbb/concurrent_vector.h>
43PXR_NAMESPACE_OPEN_SCOPE
99 template <
class Callable,
class A1,
class A2, ...
class AN>
100 void Run(Callable &&c, A1 &&a1, A2 &&a2, ... AN &&aN);
104 template <
class Callable>
105 inline void Run(Callable &&c) {
106 _rootTask->spawn(_MakeInvokerTask(std::forward<Callable>(c)));
109 template <
class Callable,
class A0,
class ... Args>
110 inline void Run(Callable &&c, A0 &&a0, Args&&... args) {
111 Run(std::bind(std::forward<Callable>(c),
112 std::forward<A0>(a0),
113 std::forward<Args>(args)...));
134 typedef tbb::concurrent_vector<TfErrorTransport> _ErrorTransports;
140 struct _InvokerTask :
public tbb::task {
141 explicit _InvokerTask(Fn &&fn, _ErrorTransports *err)
142 : _fn(
std::move(fn)), _errors(err) {}
144 explicit _InvokerTask(Fn
const &fn, _ErrorTransports *err)
145 : _fn(fn), _errors(err) {}
147 virtual tbb::task* execute() {
151 WorkDispatcher::_TransportErrors(m, _errors);
156 _ErrorTransports *_errors;
161 _InvokerTask<typename std::remove_reference<Fn>::type>&
162 _MakeInvokerTask(Fn &&fn) {
163 return *
new( _rootTask->allocate_additional_child_of(*_rootTask) )
164 _InvokerTask<
typename std::remove_reference<Fn>::type>(
165 std::forward<Fn>(fn), &_errors);
171 _TransportErrors(
const TfErrorMark &m, _ErrorTransports *errors);
175 tbb::task_group_context _context;
176 tbb::empty_task* _rootTask;
180 _ErrorTransports _errors;
183 std::atomic_flag _waitCleanupFlag;
188PXR_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 void Cancel()
Cancel remaining work and return immediately.
WORK_API ~WorkDispatcher()
Wait() for any pending tasks to complete, then destroy the dispatcher.
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.