HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parallelTaskWaitlist.h
Go to the documentation of this file.
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_EXEC_VDF_PARALLEL_TASK_WAITLIST_H
8 #define PXR_EXEC_VDF_PARALLEL_TASK_WAITLIST_H
9 
10 ///\file
11 
12 #include "pxr/pxr.h"
13 
14 #include "pxr/exec/vdf/api.h"
15 
17 
18 #include <tbb/concurrent_vector.h>
19 
20 #include <atomic>
21 
23 
24 /// VdfParallelTaskWaitlist
25 ///
26 /// This class manages lists of tasks waiting on uncompleted work. One instance
27 /// of this class can manage multiple independent queues denoted by separate
28 /// VdfParallelTaskWaitlist::HeadPtr instances.
29 ///
30 /// The client is expected to instantiate one or more heads, and then use the
31 /// methods WaitOn(), to wait on completion of the work denoted by those heads
32 /// respectively. Once the work has been completed, CloseAndNotify() can be
33 /// called to close the waiting list denoted by the respective head, and
34 /// simultanously notify all the currently waiting tasks to continue their
35 /// execution - assuming their task reference count reaches 0. Tasks with
36 /// reference counts greater than 0 are still waiting on other, unfulfilled
37 /// dependencies.
38 ///
39 /// The client is expected to call Rewind() once all heads have been closed and
40 /// notified. This ensures that the internal state of this class has been reset,
41 /// and its allocated memory does not grow past invocations of Rewind().
42 ///
44 {
45 public:
46  /// Represents a node in one of the waiting queues.
47  ///
48  struct Node;
49  /// This type denotes the head of an independent waitlist. Clients are
50  /// expected to instantiate this one of these for each independent list.
51  ///
52  using HeadPtr = std::atomic<Node*>;
53 
54  /// Noncopyable.
55  ///
58  delete;
59 
60  /// Constructor.
61  ///
62  /// Reserves \p numReserved waiting nodes as an optimization that can
63  /// eliminate many smaller allocations for when the approximate size of the
64  /// waiting lists is known ahead of time.
65  ///
66  VDF_API
67  explicit VdfParallelTaskWaitlist(size_t numReserved = 0);
68 
69  /// Destructor.
70  ///
71  VDF_API
73 
74  /// Rewind the internal state and ensure that internally allocated memory
75  /// does not grow beyond this point.
76  ///
77  VDF_API
78  void Rewind();
79 
80  /// Registers \p successor as waiting on the list denoted by \p headPtr.
81  /// The method will return \c false if the list is already closed and
82  /// successor does not need to wait. Returns \c true if the successor is
83  /// now successfully waiting for the list to be closed.
84  ///
85  VDF_API
86  bool WaitOn(HeadPtr *headPtr, WorkTaskGraph::BaseTask *successor);
87 
88  /// Closes the list denoted by \p headPtr, and notifies any tasks that are
89  /// waiting on this list. Returns \c false if the list had already been
90  /// closed prior to calling CloseAndNotify().
91  ///
92  VDF_API
93  bool CloseAndNotify(HeadPtr *headPtr, WorkTaskGraph *taskGraph);
94 
95 private:
96  // Allocate a new node for a waiting queue.
97  //
98  VDF_API
99  Node *_AllocateNode(WorkTaskGraph::BaseTask *task, Node *next);
100 
101  // A simple vector that serves as a way of scratch-allocating new
102  // waiting nodes.
103  //
104  tbb::concurrent_vector<Node> _allocator;
105 };
106 
108 
109 #endif
Definition: Node.h:52
#define PXR_NAMESPACE_OPEN_SCOPE
Definition: pxr.h:73
VdfParallelTaskWaitlist(const VdfParallelTaskWaitlist &)=delete
#define VDF_API
Definition: api.h:25
VDF_API ~VdfParallelTaskWaitlist()
std::atomic< Node * > HeadPtr
VdfParallelTaskWaitlist & operator=(const VdfParallelTaskWaitlist &)=delete
VDF_API void Rewind()
VDF_API bool WaitOn(HeadPtr *headPtr, WorkTaskGraph::BaseTask *successor)
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
VDF_API bool CloseAndNotify(HeadPtr *headPtr, WorkTaskGraph *taskGraph)