NvBlastExtPxTaskImpl.h
Go to the documentation of this file.
1 // This code contains NVIDIA Confidential Information and is disclosed to you
2 // under a form of NVIDIA software license agreement provided separately to you.
3 //
4 // Notice
5 // NVIDIA Corporation and its licensors retain all intellectual property and
6 // proprietary rights in and to this software and related documentation and
7 // any modifications thereto. Any use, reproduction, disclosure, or
8 // distribution of this software and related documentation without an express
9 // license agreement from NVIDIA Corporation is strictly prohibited.
10 //
11 // ALL NVIDIA DESIGN SPECIFICATIONS, CODE ARE PROVIDED "AS IS.". NVIDIA MAKES
12 // NO WARRANTIES, EXPRESSED, IMPLIED, STATUTORY, OR OTHERWISE WITH RESPECT TO
13 // THE MATERIALS, AND EXPRESSLY DISCLAIMS ALL IMPLIED WARRANTIES OF NONINFRINGEMENT,
14 // MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // Information and code furnished is believed to be accurate and reliable.
17 // However, NVIDIA Corporation assumes no responsibility for the consequences of use of such
18 // information or for any infringement of patents or other rights of third parties that may
19 // result from its use. No license is granted by implication or otherwise under any patent
20 // or patent rights of NVIDIA Corporation. Details are subject to change without notice.
21 // This code supersedes and replaces all information previously supplied.
22 // NVIDIA Corporation products are not authorized for use as critical
23 // components in life support devices or systems without express written approval of
24 // NVIDIA Corporation.
25 //
26 // Copyright (c) 2016-2020 NVIDIA Corporation. All rights reserved.
27 
28 
29 #ifndef NVBLASTEXTPXTASKIMPL_H
30 #define NVBLASTEXTPXTASKIMPL_H
31 
32 #include "NvBlastExtPxTask.h"
33 #include "PxTask.h"
34 #include "NvBlastTkGroup.h"
35 
36 #include <atomic>
37 #include <mutex>
38 #include <condition_variable>
39 
40 namespace Nv
41 {
42 namespace Blast
43 {
44 
49 {
50 public:
54  ExtTaskSync(uint32_t count) : m_count(count) {}
55 
59  void wait()
60  {
61  std::unique_lock<std::mutex> lk(m_mutex);
62  m_cv.wait(lk, [&] { return m_count == 0; });
63  }
64 
68  void notify()
69  {
70  //PERF_SCOPE_H("TaskSync::notify");
71  std::unique_lock<std::mutex> lk(m_mutex);
72  if (m_count > 0)
73  {
74  m_count--;
75  }
76  if (m_count == 0)
77  {
78  lk.unlock();
79  m_cv.notify_one();
80  }
81  }
82 
86  bool isDone()
87  {
88  std::unique_lock<std::mutex> lk(m_mutex);
89  return m_count == 0;
90  }
91 
95  void setCount(uint32_t count)
96  {
97  m_count = count;
98  }
99 
100 private:
101  std::mutex m_mutex;
102  std::condition_variable m_cv;
103  uint32_t m_count;
104 };
105 
106 
111 {
112 public:
113  ExtAtomicCounter() : m_current(0), m_maxCount(0) {}
114 
115  bool isValid(uint32_t val)
116  {
117  return val < m_maxCount;
118  }
119 
120  uint32_t next()
121  {
122  return m_current.fetch_add(1);
123  }
124 
125  void reset(uint32_t maxCount)
126  {
127  m_maxCount = maxCount;
128  m_current = 0;
129  }
130 private:
131  std::atomic<uint32_t> m_current;
132  uint32_t m_maxCount;
133 };
134 
135 
139 class ExtGroupWorkerTask : public physx::PxLightCpuTask
140 {
141 public:
142  ExtGroupWorkerTask() : PxLightCpuTask(), m_group(nullptr), m_counter(nullptr), m_sync(nullptr)
143  {
144  }
145 
146  void setup(TkGroup* group, ExtAtomicCounter* counter, ExtTaskSync* sync)
147  {
148  m_group = group;
149  m_counter = counter;
150  m_sync = sync;
151  }
152 
153  virtual void run() override
154  {
155  Nv::Blast::TkGroupWorker* worker = m_group->acquireWorker();
156  uint32_t jobID = m_counter->next();
157  while (m_counter->isValid(jobID))
158  {
159  worker->process(jobID);
160  jobID = m_counter->next();
161  }
162  m_group->returnWorker(worker);
163  }
164 
165  virtual void release() override
166  {
167  PxLightCpuTask::release();
168 
169  // release the sync last
170  m_sync->notify();
171  }
172 
173  virtual const char* getName() const override { return "BlastGroupWorkerTask"; }
174 
175 private:
176  TkGroup* m_group;
177  ExtAtomicCounter* m_counter;
178  ExtTaskSync* m_sync;
179 };
180 
181 
186 {
187 public:
188  ExtGroupTaskManagerImpl(physx::PxTaskManager& taskManager, TkGroup* group)
189  : m_taskManager(taskManager), m_sync(0), m_group(group) {}
190 
191  // ExtGroupTaskManager API
192  virtual void setGroup(TkGroup*) override;
193  virtual uint32_t process(uint32_t) override;
194  virtual void release() override;
195  virtual bool wait(bool block) override;
196 
197 private:
198  static const uint32_t TASKS_MAX_COUNT = 16;
199  physx::PxTaskManager& m_taskManager;
200  ExtAtomicCounter m_counter;
201  ExtGroupWorkerTask m_tasks[TASKS_MAX_COUNT];
202  ExtTaskSync m_sync;
203  TkGroup* m_group;
204 };
205 
206 } // namespace Blast
207 } // namespace Nv
208 
209 #endif // NVBLASTEXTPXTASKIMPL_H
bool isDone()
Definition: NvBlastExtPxTaskImpl.h:86
Definition: NvBlastExtPxTaskImpl.h:185
virtual void process(uint32_t jobId)=0
ExtAtomicCounter()
Definition: NvBlastExtPxTaskImpl.h:113
Definition: NvBlastExtPxTaskImpl.h:48
Definition: NvBlastExtPxTaskImpl.h:110
virtual const char * getName() const override
Definition: NvBlastExtPxTaskImpl.h:173
void reset(uint32_t maxCount)
Definition: NvBlastExtPxTaskImpl.h:125
void wait()
Definition: NvBlastExtPxTaskImpl.h:59
ExtGroupTaskManagerImpl(physx::PxTaskManager &taskManager, TkGroup *group)
Definition: NvBlastExtPxTaskImpl.h:188
uint32_t next()
Definition: NvBlastExtPxTaskImpl.h:120
Definition: NvBlastExtPxTask.h:55
ExtGroupWorkerTask()
Definition: NvBlastExtPxTaskImpl.h:142
ExtTaskSync(uint32_t count)
Definition: NvBlastExtPxTaskImpl.h:54
void notify()
Definition: NvBlastExtPxTaskImpl.h:68
void setCount(uint32_t count)
Definition: NvBlastExtPxTaskImpl.h:95
bool isValid(uint32_t val)
Definition: NvBlastExtPxTaskImpl.h:115
virtual void release() override
Definition: NvBlastExtPxTaskImpl.h:165
void setup(TkGroup *group, ExtAtomicCounter *counter, ExtTaskSync *sync)
Definition: NvBlastExtPxTaskImpl.h:146
virtual void run() override
Definition: NvBlastExtPxTaskImpl.h:153
Definition: NvBlastTkGroup.h:74
Definition: NvBlastTkGroup.h:101
Definition: NvBlastArray.h:37
Definition: NvBlastExtPxTaskImpl.h:139