RavEngine
Loading...
Searching...
No Matches
ExtDefaultCpuDispatcher.h
1// Redistribution and use in source and binary forms, with or without
2// modification, are permitted provided that the following conditions
3// are met:
4// * Redistributions of source code must retain the above copyright
5// notice, this list of conditions and the following disclaimer.
6// * Redistributions in binary form must reproduce the above copyright
7// notice, this list of conditions and the following disclaimer in the
8// documentation and/or other materials provided with the distribution.
9// * Neither the name of NVIDIA CORPORATION nor the names of its
10// contributors may be used to endorse or promote products derived
11// from this software without specific prior written permission.
12//
13// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
14// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24//
25// Copyright (c) 2008-2022 NVIDIA Corporation. All rights reserved.
26// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
27// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
28
29#ifndef EXT_DEFAULT_CPU_DISPATCHER_H
30#define EXT_DEFAULT_CPU_DISPATCHER_H
31
32#include "common/PxProfileZone.h"
33#include "task/PxTask.h"
34#include "extensions/PxDefaultCpuDispatcher.h"
35
36#include "foundation/PxUserAllocated.h"
37#include "foundation/PxSync.h"
38#include "foundation/PxSList.h"
39#include "ExtSharedQueueEntryPool.h"
40
41namespace physx
42{
43namespace Ext
44{
45 class CpuWorkerThread;
46
47#if PX_VC
48#pragma warning(push)
49#pragma warning(disable:4324) // Padding was added at the end of a structure because of a __declspec(align) value.
50#endif // Because of the SList member I assume
51
53 {
54 friend class TaskQueueHelper;
55 PX_NOCOPY(DefaultCpuDispatcher)
56 private:
58 public:
59 DefaultCpuDispatcher(PxU32 numThreads, PxU32* affinityMasks, PxDefaultCpuDispatcherWaitForWorkMode::Enum mode = PxDefaultCpuDispatcherWaitForWorkMode::eWAIT_FOR_WORK, PxU32 yieldProcessorCount = 0);
60
61 // PxCpuDispatcher
62 virtual void submitTask(PxBaseTask& task) PX_OVERRIDE;
63 virtual PxU32 getWorkerCount() const PX_OVERRIDE { return mNumThreads; }
64 //~PxCpuDispatcher
65
66 // PxDefaultCpuDispatcher
67 virtual void release() PX_OVERRIDE;
68 virtual void setRunProfiled(bool runProfiled) PX_OVERRIDE { mRunProfiled = runProfiled; }
69 virtual bool getRunProfiled() const PX_OVERRIDE { return mRunProfiled; }
70 //~PxDefaultCpuDispatcher
71
72 PxBaseTask* getJob();
73 PxBaseTask* stealJob();
74 PxBaseTask* fetchNextTask();
75
76 PX_FORCE_INLINE void runTask(PxBaseTask& task)
77 {
78 if(mRunProfiled)
79 {
80 PX_PROFILE_ZONE(task.getName(), task.getContextId());
81 task.run();
82 }
83 else
84 task.run();
85 }
86
87 void waitForWork() { PX_ASSERT(PxDefaultCpuDispatcherWaitForWorkMode::eWAIT_FOR_WORK == mWaitForWorkMode); mWorkReady.wait(); }
88 void resetWakeSignal();
89
90 static void getAffinityMasks(PxU32* affinityMasks, PxU32 threadCount);
91
92 PX_FORCE_INLINE PxDefaultCpuDispatcherWaitForWorkMode::Enum getWaitForWorkMode() const { return mWaitForWorkMode; }
93 PX_FORCE_INLINE PxU32 getYieldProcessorCount() const { return mYieldProcessorCount; }
94
95 protected:
96 CpuWorkerThread* mWorkerThreads;
97 SharedQueueEntryPool<> mQueueEntryPool;
98 PxSList mJobList;
99 PxSync mWorkReady;
100 PxU8* mThreadNames;
101 PxU32 mNumThreads;
102 bool mShuttingDown;
103 bool mRunProfiled;
104 const PxDefaultCpuDispatcherWaitForWorkMode::Enum mWaitForWorkMode;
105 const PxU32 mYieldProcessorCount;
106 };
107
108#if PX_VC
109#pragma warning(pop)
110#endif
111
112} // namespace Ext
113}
114
115#endif
Definition ExtDefaultCpuDispatcher.h:53
virtual void release() PX_OVERRIDE
Deletes the dispatcher.
Definition ExtDefaultCpuDispatcher.cpp:126
virtual PxU32 getWorkerCount() const PX_OVERRIDE
Returns the number of available worker threads for this dispatcher.
Definition ExtDefaultCpuDispatcher.h:63
virtual bool getRunProfiled() const PX_OVERRIDE
Checks if profiling is enabled at task level.
Definition ExtDefaultCpuDispatcher.h:69
virtual void setRunProfiled(bool runProfiled) PX_OVERRIDE
Enables profiling at task level.
Definition ExtDefaultCpuDispatcher.h:68
virtual void submitTask(PxBaseTask &task) PX_OVERRIDE
Called by the TaskManager when a task is to be queued for execution.
Definition ExtDefaultCpuDispatcher.cpp:131
Definition ExtTaskQueueHelper.h:43
Base class of all task types.
Definition PxTask.h:45
virtual void run()=0
The user-implemented run method where the task's work should be performed.
virtual const char * getName() const =0
Return a user-provided task name for profiling purposes.
A default implementation for a CPU task dispatcher.
Definition PxDefaultCpuDispatcher.h:49
bool wait(uint32_t milliseconds=PxSyncImpl::waitForever)
Definition PxSync.h:109
Definition PxUserAllocated.h:43
#define PX_FORCE_INLINE
Definition PxPreprocessor.h:335
#define PX_OVERRIDE
Definition PxPreprocessor.h:375
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39