RavEngine
Loading...
Searching...
No Matches
PxThread.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 PX_THREAD_H
30#define PX_THREAD_H
31
32#include "foundation/PxUserAllocated.h"
33
34// todo: these need to go somewhere else
35// PT: looks like this is still used on some platforms
36
37#if PX_WINDOWS_FAMILY
38#define PxSpinLockPause() __asm pause
39#elif PX_LINUX || PX_APPLE_FAMILY || PX_SWITCH
40#define PxSpinLockPause() asm("nop")
41#else
42#error "Platform not supported!"
43#endif
44
45#if !PX_DOXYGEN
46namespace physx
47{
48#endif
49
51{
52 enum Enum
53 {
54 eHIGH = 0,
56 eNORMAL = 2,
58 eLOW = 4,
59 eFORCE_DWORD = 0xffFFffFF
60 };
61};
62
64{
65 public:
66 PxRunnable() {}
67 virtual ~PxRunnable() {}
68 virtual void execute() {}
69};
70
71class PX_FOUNDATION_API PxThreadImpl
72{
73 public:
74 typedef size_t Id; // space for a pointer or an integer
75 typedef void* (*ExecuteFn)(void*);
76
77 static PxU32 getDefaultStackSize();
78 static Id getId();
79
87
92 PxThreadImpl(ExecuteFn fn, void* arg, const char* name);
93
100
107 void start(PxU32 stackSize, PxRunnable* r);
108
115 void kill();
116
122 void signalQuit();
123
129 bool waitForQuit();
130
136 bool quitIsSignalled();
137
141 void quit();
142
160 PxU32 setAffinityMask(PxU32 mask);
161
162 static PxThreadPriority::Enum getPriority(Id threadId);
163
165 void setPriority(PxThreadPriority::Enum prio);
166
168 void setName(const char* name);
169
171 static void sleep(PxU32 ms);
172
174 static void yield();
175
178 static void yieldProcessor();
179
181 static PxU32 getNbPhysicalCores();
182
186 static PxU32 getSize();
187};
188
192template <typename Alloc = PxReflectionAllocator<PxThreadImpl> >
193class PxThreadT : protected Alloc, public PxUserAllocated, public PxRunnable
194{
195 public:
196 typedef PxThreadImpl::Id Id; // space for a pointer or an integer
197
202 PxThreadT(const Alloc& alloc = Alloc()) : Alloc(alloc)
203 {
204 mImpl = reinterpret_cast<PxThreadImpl*>(Alloc::allocate(PxThreadImpl::getSize(), __FILE__, __LINE__));
205 PX_PLACEMENT_NEW(mImpl, PxThreadImpl)();
206 }
207
211 PxThreadT(PxThreadImpl::ExecuteFn fn, void* arg, const char* name, const Alloc& alloc = Alloc()) : Alloc(alloc)
212 {
213 mImpl = reinterpret_cast<PxThreadImpl*>(Alloc::allocate(PxThreadImpl::getSize(), __FILE__, __LINE__));
214 PX_PLACEMENT_NEW(mImpl, PxThreadImpl)(fn, arg, name);
215 }
216
221 virtual ~PxThreadT()
222 {
223 mImpl->~PxThreadImpl();
224 Alloc::deallocate(mImpl);
225 }
226
231 void start(PxU32 stackSize = PxThreadImpl::getDefaultStackSize())
232 {
233 mImpl->start(stackSize, this);
234 }
235
242 void kill()
243 {
244 mImpl->kill();
245 }
246
252 virtual void execute(void)
253 {
254 }
255
262 {
263 mImpl->signalQuit();
264 }
265
272 {
273 return mImpl->waitForQuit();
274 }
275
282 {
283 return mImpl->quitIsSignalled();
284 }
285
289 void quit()
290 {
291 mImpl->quit();
292 }
293
294 PxU32 setAffinityMask(PxU32 mask)
295 {
296 return mImpl->setAffinityMask(mask);
297 }
298
299 static PxThreadPriority::Enum getPriority(PxThreadImpl::Id threadId)
300 {
301 return PxThreadImpl::getPriority(threadId);
302 }
303
306 {
307 mImpl->setPriority(prio);
308 }
309
311 void setName(const char* name)
312 {
313 mImpl->setName(name);
314 }
315
317 static void sleep(PxU32 ms)
318 {
320 }
321
323 static void yield()
324 {
326 }
327
330 static void yieldProcesor()
331 {
333 }
334
335 static PxU32 getDefaultStackSize()
336 {
337 return PxThreadImpl::getDefaultStackSize();
338 }
339
340 static PxThreadImpl::Id getId()
341 {
342 return PxThreadImpl::getId();
343 }
344
345 static PxU32 getNbPhysicalCores()
346 {
348 }
349
350 private:
351 class PxThreadImpl* mImpl;
352};
353
354typedef PxThreadT<> PxThread;
355
356PX_FOUNDATION_API PxU32 PxTlsAlloc();
357PX_FOUNDATION_API void PxTlsFree(PxU32 index);
358PX_FOUNDATION_API void* PxTlsGet(PxU32 index);
359PX_FOUNDATION_API size_t PxTlsGetValue(PxU32 index);
360PX_FOUNDATION_API PxU32 PxTlsSet(PxU32 index, void* value);
361PX_FOUNDATION_API PxU32 PxTlsSetValue(PxU32 index, size_t value);
362
363
364#if !PX_DOXYGEN
365} // namespace physx
366#endif
367
368#endif
369
Definition PxThread.h:64
Definition PxThread.h:72
void setName(const char *name)
Definition FdUnixThread.cpp:310
PxU32 setAffinityMask(PxU32 mask)
Definition FdUnixThread.cpp:282
void start(PxU32 stackSize, PxRunnable *r)
Definition FdUnixThread.cpp:170
void signalQuit()
Definition FdUnixThread.cpp:218
void quit()
Definition FdUnixThread.cpp:242
static void yield()
Definition FdUnixThread.cpp:268
~PxThreadImpl()
Definition FdUnixThread.cpp:164
bool quitIsSignalled()
Definition FdUnixThread.cpp:234
static PxU32 getNbPhysicalCores()
Definition FdUnixThread.cpp:391
bool waitForQuit()
Definition FdUnixThread.cpp:223
void setPriority(PxThreadPriority::Enum prio)
Definition FdUnixThread.cpp:363
void kill()
Definition FdUnixThread.cpp:248
static PxU32 getSize()
Definition FdUnixThread.cpp:126
static void yieldProcessor()
Definition FdUnixThread.cpp:273
static void sleep(PxU32 ms)
Definition FdUnixThread.cpp:257
Definition PxThread.h:194
void start(PxU32 stackSize=PxThreadImpl::getDefaultStackSize())
Definition PxThread.h:231
PxThreadT(const Alloc &alloc=Alloc())
Definition PxThread.h:202
static void yield()
Definition PxThread.h:323
void setName(const char *name)
Definition PxThread.h:311
void signalQuit()
Definition PxThread.h:261
bool quitIsSignalled()
Definition PxThread.h:281
virtual ~PxThreadT()
Definition PxThread.h:221
PxThreadT(PxThreadImpl::ExecuteFn fn, void *arg, const char *name, const Alloc &alloc=Alloc())
Definition PxThread.h:211
void setPriority(PxThreadPriority::Enum prio)
Definition PxThread.h:305
bool waitForQuit()
Definition PxThread.h:271
virtual void execute(void)
Definition PxThread.h:252
void kill()
Definition PxThread.h:242
static void yieldProcesor()
Definition PxThread.h:330
static void sleep(PxU32 ms)
Definition PxThread.h:317
void quit()
Definition PxThread.h:289
Definition PxUserAllocated.h:43
Sorts an array of objects in ascending order, assuming that the predicate implements the < operator:
Definition PxBoxController.h:39
Definition PxThread.h:51
Enum
Definition PxThread.h:53
@ eNORMAL
Normal/default priority.
Definition PxThread.h:56
@ eLOW
Low priority.
Definition PxThread.h:58
@ eABOVE_NORMAL
Above Normal priority.
Definition PxThread.h:55
@ eBELOW_NORMAL
Below Normal priority.
Definition PxThread.h:57
@ eHIGH
High priority.
Definition PxThread.h:54