PxTask.h
Go to the documentation of this file.
1 //
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions
4 // are met:
5 // * Redistributions of source code must retain the above copyright
6 // notice, this list of conditions and the following disclaimer.
7 // * Redistributions in binary form must reproduce the above copyright
8 // notice, this list of conditions and the following disclaimer in the
9 // documentation and/or other materials provided with the distribution.
10 // * Neither the name of NVIDIA CORPORATION nor the names of its
11 // contributors may be used to endorse or promote products derived
12 // from this software without specific prior written permission.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
15 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 // Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
27 
28 #ifndef PXTASK_PXTASK_H
29 #define PXTASK_PXTASK_H
30 
31 #include "task/PxTaskDefine.h"
32 #include "task/PxTaskManager.h"
33 #include "task/PxCpuDispatcher.h"
34 #include "task/PxGpuDispatcher.h"
35 #include "foundation/PxAssert.h"
36 
37 namespace physx
38 {
39 
46 {
47 public:
48  PxBaseTask() : mContextID(0), mTm(NULL) {}
49  virtual ~PxBaseTask() {}
50 
57  virtual void run() = 0;
58 
66  virtual const char* getName() const = 0;
67 
69  virtual void addReference() = 0;
71  virtual void removeReference() = 0;
73  virtual int32_t getReference() const = 0;
74 
81  virtual void release() = 0;
82 
90  {
91  return mTm;
92  }
93 
96 
97 protected:
100 
101  friend class PxTaskMgr;
102 };
103 
104 
111 class PxTask : public PxBaseTask
112 {
113 public:
114  PxTask() : mTaskID(0) {}
115  virtual ~PxTask() {}
116 
118  virtual void release()
119  {
120  PX_ASSERT(mTm);
121 
122  // clear mTm before calling taskCompleted() for safety
123  PxTaskManager* save = mTm;
124  mTm = NULL;
125  save->taskCompleted( *this );
126  }
127 
129  // task is allowed to start.
131  {
132  PX_ASSERT(mTm);
133  mTm->finishBefore( *this, taskID);
134  }
135 
137  // task has completed.
139  {
140  PX_ASSERT(mTm);
141  mTm->startAfter( *this, taskID );
142  }
143 
149  {
150  PX_ASSERT(mTm);
151  mTm->addReference( mTaskID );
152  }
153 
159  {
160  PX_ASSERT(mTm);
161  mTm->decrReference( mTaskID );
162  }
163 
167  PX_INLINE int32_t getReference() const
168  {
169  return mTm->getReference( mTaskID );
170  }
171 
176  {
177  return mTaskID;
178  }
179 
185  virtual void submitted()
186  {
187  mStreamIndex = 0;
188  mPreSyncRequired = false;
189  }
190 
195  {
196  mPreSyncRequired = true;
197  }
198 
199 protected:
201  uint32_t mStreamIndex;
203 
204  friend class PxTaskMgr;
205  friend class PxGpuWorkerThread;
206 };
207 
208 
223 {
224 public:
226  : mCont( NULL )
227  , mRefCount( 0 )
228  {
229  }
230  virtual ~PxLightCpuTask()
231  {
232  mTm = NULL;
233  }
234 
245  {
246  PX_ASSERT( mRefCount == 0 );
247  mRefCount = 1;
248  mCont = c;
249  mTm = &tm;
250  if( mCont )
251  {
252  mCont->addReference();
253  }
254  }
255 
264  {
265  PX_ASSERT( c );
266  PX_ASSERT( mRefCount == 0 );
267  mRefCount = 1;
268  mCont = c;
269  if( mCont )
270  {
271  mCont->addReference();
272  mTm = mCont->getTaskManager();
273  PX_ASSERT( mTm );
274  }
275  }
276 
281  {
282  return mCont;
283  }
284 
290  {
291  mTm->decrReference(*this);
292  }
293 
295  PX_INLINE int32_t getReference() const
296  {
297  return mRefCount;
298  }
299 
305  {
306  mTm->addReference(*this);
307  }
308 
315  {
316  if( mCont )
317  {
319  }
320  }
321 
322 protected:
323 
325  volatile int32_t mRefCount;
326 
327  friend class PxTaskMgr;
328 };
329 
330 
331 }// end physx namespace
332 
333 
334 #endif // PXTASK_PXTASK_H
Definition: GuContactBuffer.h:37
Base class of all task types.
Definition: PxTask.h:45
PX_INLINE void release()
called by CpuDispatcher after run method has completed
Definition: PxTask.h:314
PxLightCpuTask()
Definition: PxTask.h:225
virtual void release()
Release method implementation.
Definition: PxTask.h:118
friend class PxGpuWorkerThread
Definition: PxTask.h:205
PX_FORCE_INLINE PxU64 getContextId() const
Definition: PxTask.h:95
virtual ~PxBaseTask()
Definition: PxTask.h:49
PX_INLINE void removeReference()
Manually decrement this task's reference count. If the reference count reaches zero, the task will be dispatched.
Definition: PxTask.h:158
volatile int32_t mRefCount
PxTask is dispatched when reaches 0.
Definition: PxTask.h:325
PxU64 mContextID
Context ID for profiler interface.
Definition: PxTask.h:98
unsigned int PxTaskID
Definition: PxTaskManager.h:41
#define PX_FORCE_INLINE
Definition: PxPreprocessor.h:364
virtual void release()=0
Implemented by derived implementation classes.
PX_INLINE int32_t getReference() const
Return the ref-count for this task.
Definition: PxTask.h:295
PxBaseTask * mCont
Continuation task, can be NULL.
Definition: PxTask.h:324
uint32_t mStreamIndex
GpuTask CUDA stream index.
Definition: PxTask.h:201
PX_INLINE void startAfter(PxTaskID taskID)
Inform the PxTaskManager this task cannot start until the given.
Definition: PxTask.h:138
PX_INLINE void setContinuation(PxTaskManager &tm, PxBaseTask *c)
Initialize this task and specify the task that will have its ref count decremented on completion...
Definition: PxTask.h:244
A PxBaseTask implementation with immediate execution and simple dependencies.
Definition: PxTask.h:222
PX_INLINE void addReference()
Manually increment this task's reference count. The task will not be allowed to run until removeRefer...
Definition: PxTask.h:148
PxTask()
Definition: PxTask.h:114
uint64_t PxU64
Definition: PxSimpleTypes.h:69
friend class PxTaskMgr
Definition: PxTask.h:204
PxTaskID mTaskID
ID assigned at submission.
Definition: PxTask.h:200
virtual void submitted()
Called by PxTaskManager at submission time for initialization.
Definition: PxTask.h:185
PX_INLINE void setContinuation(PxBaseTask *c)
Initialize this task and specify the task that will have its ref count decremented on completion...
Definition: PxTask.h:263
PX_FORCE_INLINE PxTaskManager * getTaskManager() const
Return PxTaskManager to which this task was submitted.
Definition: PxTask.h:89
PxTaskManager * mTm
Owning PxTaskManager instance.
Definition: PxTask.h:99
virtual void run()=0
The user-implemented run method where the task's work should be performed.
PX_FORCE_INLINE void setContextId(PxU64 id)
Definition: PxTask.h:94
friend class PxTaskMgr
Definition: PxTask.h:327
bool mPreSyncRequired
GpuTask sync flag.
Definition: PxTask.h:202
The PxTaskManager interface.
Definition: PxTaskManager.h:81
PX_INLINE PxBaseTask * getContinuation() const
Retrieves continuation task.
Definition: PxTask.h:280
friend class PxTaskMgr
Definition: PxTask.h:101
virtual ~PxTask()
Definition: PxTask.h:115
PX_INLINE void removeReference()
Manually decrement this task's reference count. If the reference count reaches zero, the task will be dispatched.
Definition: PxTask.h:289
virtual ~PxLightCpuTask()
Definition: PxTask.h:230
PX_INLINE int32_t getReference() const
Return the ref-count for this task.
Definition: PxTask.h:167
PX_INLINE PxTaskID getTaskID() const
Return the unique ID for this task.
Definition: PxTask.h:175
virtual void addReference()=0
Implemented by derived implementation classes.
virtual void taskCompleted(PxTask &task)=0
Called by the worker threads to inform the PxTaskManager that a task has completed processing...
PX_INLINE void finishBefore(PxTaskID taskID)
Inform the PxTaskManager this task must finish before the given.
Definition: PxTask.h:130
PxBaseTask()
Definition: PxTask.h:48
#define PX_ASSERT(exp)
Definition: PxAssert.h:61
virtual const char * getName() const =0
Return a user-provided task name for profiling purposes.
virtual void removeReference()=0
Implemented by derived implementation classes.
PX_INLINE void addReference()
Manually increment this task's reference count. The task will not be allowed to run until removeRefer...
Definition: PxTask.h:304
virtual int32_t getReference() const =0
Implemented by derived implementation classes.
#define PX_INLINE
Definition: PxPreprocessor.h:349
A PxBaseTask implementation with deferred execution and full dependencies.
Definition: PxTask.h:111
PX_INLINE void requestSyncPoint()
Specify that the GpuTask sync flag be set.
Definition: PxTask.h:194