7#ifndef PXR_BASE_WORK_TASK_GRAPH_DEFAULT_IMPL_H
8#define PXR_BASE_WORK_TASK_GRAPH_DEFAULT_IMPL_H
12#include "pxr/base/work/api.h"
18PXR_NAMESPACE_OPEN_SCOPE
23class WorkTaskGraph_DefaultImpl
26 WorkTaskGraph_DefaultImpl() =
default;
27 ~WorkTaskGraph_DefaultImpl() noexcept = default;
29 WorkTaskGraph_DefaultImpl(WorkTaskGraph_DefaultImpl const &) = delete;
30 WorkTaskGraph_DefaultImpl &
31 operator=(WorkTaskGraph_DefaultImpl const &) = delete;
35 template <typename F, typename ... Args>
36 F * AllocateTask(Args&&... args);
39 inline
void RunTask(F * task) {
40 task->_SetTaskGraph(
this);
41 _dispatcher.Run(std::ref(*task));
51class WorkTaskGraph_DefaultImpl::BaseTask {
54 WORK_API
virtual ~BaseTask();
58 WORK_API
void operator()(
const int depth = 0,
59 WorkTaskGraph_DefaultImpl *
const taskGraph =
nullptr)
const;
61 virtual BaseTask * execute() = 0;
63 void AddChildReference() {
64 _childCount.fetch_add(1, std::memory_order_acquire);
67 int RemoveChildReference() {
68 return _childCount.fetch_sub(1, std::memory_order_release) - 1;
71 template <
typename F,
typename ... Args>
72 F * AllocateChild(Args&&... args) {
74 F* obj =
new F{std::forward<Args>(args)...};
80 void _RecycleAsContinuation() {
86 friend class WorkTaskGraph_DefaultImpl;
89 void _SetTaskGraph(WorkTaskGraph_DefaultImpl *
const taskGraph) {
90 _taskGraph = taskGraph;
95 WorkTaskGraph_DefaultImpl * _taskGraph =
nullptr;
98 BaseTask * _parent =
nullptr;
101 bool _recycle =
false;
104 std::atomic<int> _childCount = 0;
107template <
typename F,
typename ... Args>
108F * WorkTaskGraph_DefaultImpl::AllocateTask(Args&&... args) {
109 return new F{std::forward<Args>(args)...};
112PXR_NAMESPACE_CLOSE_SCOPE
A work dispatcher runs concurrent tasks.