Loading...
Searching...
No Matches
taskGraph.h
1//
2// Copyright 2025 Pixar
3//
4// Licensed under the terms set forth in the LICENSE.txt file available at
5// https://openusd.org/license.
6//
7#ifndef PXR_BASE_WORK_TASK_GRAPH_H
8#define PXR_BASE_WORK_TASK_GRAPH_H
9
10#include "pxr/pxr.h"
11
12#include "pxr/base/work/api.h"
14#include "pxr/base/work/impl.h"
15#include "pxr/base/work/taskGraph_defaultImpl.h"
16
17#include <tbb/enumerable_thread_specific.h>
18
19#include <utility>
20#include <vector>
21
22PXR_NAMESPACE_OPEN_SCOPE
23
37{
38private:
39 // Select underlying implementation.
40#if defined WORK_IMPL_HAS_TASK_GRAPH
41 using _Impl = PXR_WORK_IMPL_NS::WorkImpl_TaskGraph;
42#else
43 using _Impl = WorkTaskGraph_DefaultImpl;
44#endif
45
46public:
47 WorkTaskGraph() = default;
48 ~WorkTaskGraph() noexcept = default;
49
50 WorkTaskGraph(WorkTaskGraph const &) = delete;
51 WorkTaskGraph &operator=(WorkTaskGraph const &) = delete;
52
54 class BaseTask;
55
57 using TaskList = std::vector<BaseTask *>;
58
60 using TaskLists = tbb::enumerable_thread_specific<TaskList>;
61
67 template <typename F, typename ... Args>
68 F * AllocateTask(Args&&... args) {
69 return _impl.AllocateTask<F>(std::forward<Args>(args)...);
70 }
71
75 template <typename F>
76 inline void RunTask(F * task) {
77 _impl.RunTask(task);
78 }
79
85 WORK_API void RunLists(const TaskLists &taskLists);
86
88 inline void Wait() {
89 _impl.Wait();
90 }
91
92private:
93 _Impl _impl;
94};
95
106class WorkTaskGraph::BaseTask : public WorkTaskGraph::_Impl::BaseTask {
107private:
108 using _Base = WorkTaskGraph::_Impl::BaseTask;
109
110public:
111 BaseTask() = default;
112 WORK_API virtual ~BaseTask();
113
116 virtual BaseTask * execute() = 0;
117
121 _Base::AddChildReference();
122 }
123
127 return _Base::RemoveChildReference();
128 }
129
132 template <typename F, typename ... Args>
133 F * AllocateChild(Args&&... args) {
134 return _Base::AllocateChild<F>(std::forward<Args>(args)...);
135 }
136
137protected:
148 _Base::_RecycleAsContinuation();
149 }
150
151};
152
153
154PXR_NAMESPACE_CLOSE_SCOPE
155
156#endif
Base class for a parallel task that emulates tbb::task (deprecated in the oneTBB version upgrade....
Definition: taskGraph.h:106
void _RecycleAsContinuation()
Recycles this as a continuation task to mitigate the allocation overhead of the continuation task.
Definition: taskGraph.h:147
F * AllocateChild(Args &&... args)
Construct a new subtask and increment the reference count of the calling task.
Definition: taskGraph.h:133
virtual BaseTask * execute()=0
Derived classes override this method to implement a parallel unit of work.
void AddChildReference()
Increment the reference count of child tasks that must complete before this task can proceed.
Definition: taskGraph.h:120
int RemoveChildReference()
Decrement the reference count of child tasks that must complete before this task can proceed.
Definition: taskGraph.h:126
Instances of this class are used to spawn and wait on a directed graph of tasks, where tasks preserve...
Definition: taskGraph.h:37
F * AllocateTask(Args &&... args)
Allocate and construct a new top-level task to run with RunTask() or RunLists().
Definition: taskGraph.h:68
WORK_API void RunLists(const TaskLists &taskLists)
Submit concurrent tasks accumulated in thread-local lists for execution.
tbb::enumerable_thread_specific< TaskList > TaskLists
Thread-local storage for allocated tasks to be spawned.
Definition: taskGraph.h:60
void RunTask(F *task)
Submit a concurrent task for execution.
Definition: taskGraph.h:76
void Wait()
Wait on all the running tasks to complete.
Definition: taskGraph.h:88
std::vector< BaseTask * > TaskList
Container for allocated tasks to be spawned.
Definition: taskGraph.h:57