Loading...
Searching...
No Matches
taskGraph_impl.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_TBB_TASK_GRAPH_IMPL_H
8#define PXR_BASE_WORK_TBB_TASK_GRAPH_IMPL_H
9
10#include "pxr/pxr.h"
11#include "pxr/base/work/api.h"
12#include "pxr/base/work/workTBB/tbb_version.h"
13
14#if TBB_INTERFACE_VERSION_MAJOR < 12
15
16// Under legacy TBB we implement the task graph using tbb::task.
17//
18// tbb::task does not exist in oneTBB, so we fall back to WorkTaskGraph's
19// default implementation by not defining the WorkImpl_TaskGraph customization.
20#define WORK_IMPL_HAS_TASK_GRAPH
21
22#include <tbb/task.h>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
26class WorkImpl_TaskGraph
27{
28public:
29 WORK_API WorkImpl_TaskGraph();
30 WORK_API ~WorkImpl_TaskGraph() noexcept;
31
32 WorkImpl_TaskGraph(WorkImpl_TaskGraph const &) = delete;
33 WorkImpl_TaskGraph &operator=(WorkImpl_TaskGraph const &) = delete;
34
35 class BaseTask;
36
37 template <typename F, typename ... Args>
38 F * AllocateTask(Args&&... args);
39
40 template <typename F>
41 inline void RunTask(F * task) {
42 tbb::task::spawn(*task);
43 }
44
45 WORK_API void Wait();
46
47private:
48 // Task group context for initializing the root task.
49 tbb::task_group_context _context;
50
51 // Root task that serves as an anchor to allocate additional children
52 // during execution.
53 tbb::empty_task *_rootTask;
54};
55
56class WorkImpl_TaskGraph::BaseTask : public tbb::task {
57public:
58 BaseTask() = default;
59 WORK_API virtual ~BaseTask();
60
61 void AddChildReference() {
62 increment_ref_count();
63 }
64
65 int RemoveChildReference() {
66 return decrement_ref_count();
67 }
68
69 template <typename F, typename... Args>
70 F * AllocateChild(Args &&...args) {
71 return new (tbb::task::allocate_additional_child_of(*this))
72 F{std::forward<Args>(args)...};
73 }
74
75protected:
76 void _RecycleAsContinuation() {
77 recycle_as_safe_continuation();
78 }
79
80};
81
82template <typename F, typename ... Args>
83F * WorkImpl_TaskGraph::AllocateTask(Args&&... args) {
84 return new (tbb::task::allocate_additional_child_of(*_rootTask))
85 F{std::forward<Args>(args)...};
86}
87
88PXR_NAMESPACE_CLOSE_SCOPE
89
90#endif // TBB_INTERFACE_VERSION_MAJOR < 12
91
92#endif // PXR_BASE_WORK_TBB_TASK_GRAPH_IMPL_H