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-2021 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 "foundation/PxAssert.h"
35 
36 namespace physx
37 {
38 
45 {
46 public:
47  PxBaseTask() : mContextID(0), mTm(NULL) {}
48  virtual ~PxBaseTask() {}
49 
56  virtual void run() = 0;
57 
65  virtual const char* getName() const = 0;
66 
68  virtual void addReference() = 0;
70  virtual void removeReference() = 0;
72  virtual int32_t getReference() const = 0;
73 
80  virtual void release() = 0;
81 
89  {
90  return mTm;
91  }
92 
95 
96 protected:
99 
100  friend class PxTaskMgr;
101 };
102 
103 
110 class PxTask : public PxBaseTask
111 {
112 public:
113  PxTask() : mTaskID(0) {}
114  virtual ~PxTask() {}
115 
117  virtual void release()
118  {
119  PX_ASSERT(mTm);
120 
121  // clear mTm before calling taskCompleted() for safety
122  PxTaskManager* save = mTm;
123  mTm = NULL;
124  save->taskCompleted( *this );
125  }
126 
128  // task is allowed to start.
130  {
131  PX_ASSERT(mTm);
132  mTm->finishBefore( *this, taskID);
133  }
134 
136  // task has completed.
138  {
139  PX_ASSERT(mTm);
140  mTm->startAfter( *this, taskID );
141  }
142 
148  {
149  PX_ASSERT(mTm);
150  mTm->addReference( mTaskID );
151  }
152 
158  {
159  PX_ASSERT(mTm);
160  mTm->decrReference( mTaskID );
161  }
162 
166  PX_INLINE int32_t getReference() const
167  {
168  return mTm->getReference( mTaskID );
169  }
170 
175  {
176  return mTaskID;
177  }
178 
184  virtual void submitted()
185  {
186  }
187 
188 protected:
190 
191  friend class PxTaskMgr;
192 };
193 
194 
209 {
210 public:
212  : mCont( NULL )
213  , mRefCount( 0 )
214  {
215  }
216  virtual ~PxLightCpuTask()
217  {
218  mTm = NULL;
219  }
220 
231  {
232  PX_ASSERT( mRefCount == 0 );
233  mRefCount = 1;
234  mCont = c;
235  mTm = &tm;
236  if( mCont )
237  {
238  mCont->addReference();
239  }
240  }
241 
250  {
251  PX_ASSERT( c );
252  PX_ASSERT( mRefCount == 0 );
253  mRefCount = 1;
254  mCont = c;
255  if( mCont )
256  {
257  mCont->addReference();
258  mTm = mCont->getTaskManager();
259  PX_ASSERT( mTm );
260  }
261  }
262 
267  {
268  return mCont;
269  }
270 
276  {
277  mTm->decrReference(*this);
278  }
279 
281  PX_INLINE int32_t getReference() const
282  {
283  return mRefCount;
284  }
285 
291  {
292  mTm->addReference(*this);
293  }
294 
301  {
302  if( mCont )
303  {
305  }
306  }
307 
308 protected:
309 
311  volatile int32_t mRefCount;
312 
313  friend class PxTaskMgr;
314 };
315 
316 
317 }// end physx namespace
318 
319 
320 #endif // PXTASK_PXTASK_H
Definition: GuContactBuffer.h:37
Base class of all task types.
Definition: PxTask.h:44
PX_INLINE void release()
called by CpuDispatcher after run method has completed
Definition: PxTask.h:300
PxLightCpuTask()
Definition: PxTask.h:211
virtual void release()
Release method implementation.
Definition: PxTask.h:117
PX_FORCE_INLINE PxU64 getContextId() const
Definition: PxTask.h:94
virtual ~PxBaseTask()
Definition: PxTask.h:48
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:157
volatile int32_t mRefCount
PxTask is dispatched when reaches 0.
Definition: PxTask.h:311
PxU64 mContextID
Context ID for profiler interface.
Definition: PxTask.h:97
unsigned int PxTaskID
Definition: PxTaskManager.h:41
#define PX_FORCE_INLINE
Definition: PxPreprocessor.h:351
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:281
PxBaseTask * mCont
Continuation task, can be NULL.
Definition: PxTask.h:310
PX_INLINE void startAfter(PxTaskID taskID)
Inform the PxTaskManager this task cannot start until the given.
Definition: PxTask.h:137
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:230
A PxBaseTask implementation with immediate execution and simple dependencies.
Definition: PxTask.h:208
PX_INLINE void addReference()
Manually increment this task's reference count. The task will not be allowed to run until removeRefer...
Definition: PxTask.h:147
PxTask()
Definition: PxTask.h:113
uint64_t PxU64
Definition: PxSimpleTypes.h:69
friend class PxTaskMgr
Definition: PxTask.h:191
PxTaskID mTaskID
ID assigned at submission.
Definition: PxTask.h:189
virtual void submitted()
Called by PxTaskManager at submission time for initialization.
Definition: PxTask.h:184
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:249
PX_FORCE_INLINE PxTaskManager * getTaskManager() const
Return PxTaskManager to which this task was submitted.
Definition: PxTask.h:88
PxTaskManager * mTm
Owning PxTaskManager instance.
Definition: PxTask.h:98
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:93
friend class PxTaskMgr
Definition: PxTask.h:313
The PxTaskManager interface.
Definition: PxTaskManager.h:78
PX_INLINE PxBaseTask * getContinuation() const
Retrieves continuation task.
Definition: PxTask.h:266
friend class PxTaskMgr
Definition: PxTask.h:100
virtual ~PxTask()
Definition: PxTask.h:114
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:275
virtual ~PxLightCpuTask()
Definition: PxTask.h:216
PX_INLINE int32_t getReference() const
Return the ref-count for this task.
Definition: PxTask.h:166
PX_INLINE PxTaskID getTaskID() const
Return the unique ID for this task.
Definition: PxTask.h:174
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:129
PxBaseTask()
Definition: PxTask.h:47
#define PX_ASSERT(exp)
Definition: PxAssert.h:59
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:290
virtual int32_t getReference() const =0
Implemented by derived implementation classes.
#define PX_INLINE
Definition: PxPreprocessor.h:336
A PxBaseTask implementation with deferred execution and full dependencies.
Definition: PxTask.h:110