7 #ifndef PXR_BASE_WORK_DISPATCHER_H
8 #define PXR_BASE_WORK_DISPATCHER_H
19 #include "pxr/base/tf/mallocTag.h"
22 #include <type_traits>
43 Work_Dispatcher &
operator=(Work_Dispatcher
const &) =
delete;
57 template <
class Callable,
class A1,
class A2, ...
class AN>
58 void Run(Callable &&
c, A1 &&a1, A2 &&a2, ... AN &&aN);
62 template <
class Callable>
63 inline void Run(Callable &&
c) {
64 if (TfMallocTag::IsInitialized()) {
66 _MallocTagsInvokerTask<
68 std::forward<Callable>(
c), &_errors));
73 std::forward<Callable>(
c), &_errors));
77 template <
class Callable,
class A0,
class ... Args>
78 inline void Run(Callable &&
c, A0 &&a0, Args&&...
args) {
79 Run(std::bind(std::forward<Callable>(
c),
81 std::forward<Args>(
args)...));
106 typedef tbb::concurrent_vector<TfErrorTransport> _ErrorTransports;
112 struct _InvokerTask {
113 explicit _InvokerTask(Fn &&fn, _ErrorTransports *err)
117 explicit _InvokerTask(Fn
const &fn, _ErrorTransports *err)
122 _InvokerTask(_InvokerTask &&other) =
default;
123 _InvokerTask(
const _InvokerTask &other) =
delete;
124 _InvokerTask &
operator=(
const _InvokerTask &other) =
delete;
126 void operator()()
const {
130 Work_Dispatcher::_TransportErrors(m, _errors);
134 _ErrorTransports *_errors;
142 struct _MallocTagsInvokerTask {
143 explicit _MallocTagsInvokerTask(Fn &&fn, _ErrorTransports *err)
146 , _mallocTagStack(TfMallocTag::GetCurrentStackState())
149 explicit _MallocTagsInvokerTask(Fn
const &fn, _ErrorTransports *err)
152 , _mallocTagStack(TfMallocTag::GetCurrentStackState()) {}
155 _MallocTagsInvokerTask(_MallocTagsInvokerTask &&other) =
default;
156 _MallocTagsInvokerTask(
const _MallocTagsInvokerTask &other) =
delete;
157 _MallocTagsInvokerTask &
158 operator=(
const _MallocTagsInvokerTask &other) =
delete;
160 void operator()()
const {
162 TfMallocTag::StackOverride ovr(_mallocTagStack);
165 Work_Dispatcher::_TransportErrors(m, _errors);
169 _ErrorTransports *_errors;
170 TfMallocTag::StackState _mallocTagStack;
176 _TransportErrors(
const TfErrorMark &m, _ErrorTransports *errors);
180 std::atomic<bool> _isCancelled;
184 _ErrorTransports _errors;
187 std::atomic_flag _waitCleanupFlag;
222 :
public Work_Dispatcher<PXR_WORK_IMPL_NS::WorkImpl_Dispatcher>
229 : _fn(std::move(fn)) {}
252 template <
typename Fn>
256 (std::forward<Fn>(fn));
263 #endif // PXR_BASE_WORK_DISPATCHER_H
Work_Dispatcher & operator=(Work_Dispatcher const &)=delete
Work_DeprecatedMutableTask(Fn const &fn)
#define PXR_NAMESPACE_OPEN_SCOPE
WORK_API Work_Dispatcher()
WORK_API bool IsCancelled() const
void Run(Callable &&c, A0 &&a0, Args &&...args)
Work_DeprecatedMutableTask< typename std::remove_reference_t< Fn > > WorkMakeDeprecatedMutableTask(Fn &&fn)
Work_DeprecatedMutableTask(Fn &&fn)
#define PXR_NAMESPACE_CLOSE_SCOPE
**If you just want to fire and args
WORK_API void Wait()
Block until the work started by Run() completes.
Work_DeprecatedMutableTask & operator=(const Work_DeprecatedMutableTask &other)=delete
WORK_API ~Work_Dispatcher() noexcept
Wait() for any pending tasks to complete, then destroy the dispatcher.