Bullet Collision Detection & Physics Library: btThreads.h Source File

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btThreads.h
Go to the documentation of this file.
1/*
2Copyright (c) 2003-2014 Erwin Coumans http://bullet.googlecode.com
3
4This software is provided 'as-is', without any express or implied warranty.
5In no event will the authors be held liable for any damages arising from the use of this software.
6Permission is granted to anyone to use this software for any purpose,
7including commercial applications, and to alter it and redistribute it freely,
8subject to the following restrictions:
9
101. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
112. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
123. This notice may not be removed or altered from any source distribution.
13*/
14
15#ifndef BT_THREADS_H
16#define BT_THREADS_H
17
18#include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
19
20#if defined(_MSC_VER) && _MSC_VER >= 1600
21// give us a compile error if any signatures of overriden methods is changed
22#define BT_OVERRIDE override
23#endif
24
25#ifndef BT_OVERRIDE
26#define BT_OVERRIDE
27#endif
28
29// Don't set this to larger than 64, without modifying btThreadSupportPosix
30// and btThreadSupportWin32. They use UINT64 bit-masks.
31 const unsigned int BT_MAX_THREAD_COUNT = 64; // only if BT_THREADSAFE is 1
32
33// for internal use only
34bool btIsMainThread();
35bool btThreadsAreRunning();
36unsigned int btGetCurrentThreadIndex();
37void btResetThreadIndexCounter(); // notify that all worker threads have been destroyed
38
45 class btSpinMutex
46{
47 int mLock;
48
49public:
50 btSpinMutex()
51 {
52 mLock = 0;
53 }
54 void lock();
55 void unlock();
56 bool tryLock();
57};
58
59//
60// NOTE: btMutex* is for internal Bullet use only
61//
62// If BT_THREADSAFE is undefined or 0, should optimize away to nothing.
63// This is good because for the single-threaded build of Bullet, any calls
64// to these functions will be optimized out.
65//
66// However, for users of the multi-threaded build of Bullet this is kind
67// of bad because if you call any of these functions from external code
68// (where BT_THREADSAFE is undefined) you will get unexpected race conditions.
69//
70 SIMD_FORCE_INLINE void btMutexLock(btSpinMutex* mutex)
71{
72#if BT_THREADSAFE
73 mutex->lock();
74#else
75 (void)mutex;
76#endif // #if BT_THREADSAFE
77}
78
79 SIMD_FORCE_INLINE void btMutexUnlock(btSpinMutex* mutex)
80{
81#if BT_THREADSAFE
82 mutex->unlock();
83#else
84 (void)mutex;
85#endif // #if BT_THREADSAFE
86}
87
88 SIMD_FORCE_INLINE bool btMutexTryLock(btSpinMutex* mutex)
89{
90#if BT_THREADSAFE
91 return mutex->tryLock();
92#else
93 (void)mutex;
94 return true;
95#endif // #if BT_THREADSAFE
96}
97
98//
99// btIParallelForBody -- subclass this to express work that can be done in parallel
100//
101 class btIParallelForBody
102{
103public:
104 virtual ~btIParallelForBody() {}
105 virtual void forLoop(int iBegin, int iEnd) const = 0;
106};
107
108//
109// btIParallelSumBody -- subclass this to express work that can be done in parallel
110// and produces a sum over all loop elements
111//
112 class btIParallelSumBody
113{
114public:
115 virtual ~btIParallelSumBody() {}
116 virtual btScalar sumLoop(int iBegin, int iEnd) const = 0;
117};
118
119//
120// btITaskScheduler -- subclass this to implement a task scheduler that can dispatch work to
121// worker threads
122//
123 class btITaskScheduler
124{
125public:
126 btITaskScheduler(const char* name);
127 virtual ~btITaskScheduler() {}
128 const char* getName() const { return m_name; }
129
130 virtual int getMaxNumThreads() const = 0;
131 virtual int getNumThreads() const = 0;
132 virtual void setNumThreads(int numThreads) = 0;
133 virtual void parallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody& body) = 0;
134 virtual btScalar parallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody& body) = 0;
135 virtual void sleepWorkerThreadsHint() {} // hint the task scheduler that we may not be using these threads for a little while
136
137 // internal use only
138 virtual void activate();
139 virtual void deactivate();
140
141protected:
142 const char* m_name;
143 unsigned int m_savedThreadCounter;
144 bool m_isActive;
145};
146
147// set the task scheduler to use for all calls to btParallelFor()
148// NOTE: you must set this prior to using any of the multi-threaded "Mt" classes
149void btSetTaskScheduler(btITaskScheduler* ts);
150
151// get the current task scheduler
152 btITaskScheduler* btGetTaskScheduler();
153
154// get non-threaded task scheduler (always available)
155 btITaskScheduler* btGetSequentialTaskScheduler();
156
157// create a default task scheduler (Win32 or pthreads based)
158 btITaskScheduler* btCreateDefaultTaskScheduler();
159
160// get OpenMP task scheduler (if available, otherwise returns null)
161 btITaskScheduler* btGetOpenMPTaskScheduler();
162
163// get Intel TBB task scheduler (if available, otherwise returns null)
164 btITaskScheduler* btGetTBBTaskScheduler();
165
166// get PPL task scheduler (if available, otherwise returns null)
167 btITaskScheduler* btGetPPLTaskScheduler();
168
169// btParallelFor -- call this to dispatch work like a for-loop
170// (iterations may be done out of order, so no dependencies are allowed)
171void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody& body);
172
173// btParallelSum -- call this to dispatch work like a for-loop, returns the sum of all iterations
174// (iterations may be done out of order, so no dependencies are allowed)
175 btScalar btParallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody& body);
176
177#endif
btScalar.h
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:98
btGetTBBTaskScheduler
btITaskScheduler * btGetTBBTaskScheduler()
Definition: btThreads.cpp:773
btMutexLock
void btMutexLock(btSpinMutex *mutex)
Definition: btThreads.h:70
btMutexUnlock
void btMutexUnlock(btSpinMutex *mutex)
Definition: btThreads.h:79
btGetPPLTaskScheduler
btITaskScheduler * btGetPPLTaskScheduler()
Definition: btThreads.cpp:784
btResetThreadIndexCounter
void btResetThreadIndexCounter()
Definition: btThreads.cpp:329
btParallelSum
btScalar btParallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody &body)
Definition: btThreads.cpp:439
btThreadsAreRunning
bool btThreadsAreRunning()
Definition: btThreads.cpp:381
btGetOpenMPTaskScheduler
btITaskScheduler * btGetOpenMPTaskScheduler()
Definition: btThreads.cpp:762
btGetCurrentThreadIndex
unsigned int btGetCurrentThreadIndex()
Definition: btThreads.cpp:290
btMutexTryLock
bool btMutexTryLock(btSpinMutex *mutex)
Definition: btThreads.h:88
btCreateDefaultTaskScheduler
btITaskScheduler * btCreateDefaultTaskScheduler()
Definition: btTaskScheduler.cpp:787
btSetTaskScheduler
void btSetTaskScheduler(btITaskScheduler *ts)
Definition: btThreads.cpp:386
btGetTaskScheduler
btITaskScheduler * btGetTaskScheduler()
Definition: btThreads.cpp:407
btGetSequentialTaskScheduler
btITaskScheduler * btGetSequentialTaskScheduler()
Definition: btThreads.cpp:755
btIsMainThread
bool btIsMainThread()
Definition: btThreads.cpp:324
BT_MAX_THREAD_COUNT
const unsigned int BT_MAX_THREAD_COUNT
Definition: btThreads.h:31
btParallelFor
void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)
Definition: btThreads.cpp:412
btIParallelForBody
Definition: btThreads.h:102
btIParallelForBody::~btIParallelForBody
virtual ~btIParallelForBody()
Definition: btThreads.h:104
btIParallelForBody::forLoop
virtual void forLoop(int iBegin, int iEnd) const =0
btIParallelSumBody
Definition: btThreads.h:113
btIParallelSumBody::sumLoop
virtual btScalar sumLoop(int iBegin, int iEnd) const =0
btIParallelSumBody::~btIParallelSumBody
virtual ~btIParallelSumBody()
Definition: btThreads.h:115
btITaskScheduler
Definition: btThreads.h:124
btITaskScheduler::getNumThreads
virtual int getNumThreads() const =0
btITaskScheduler::m_savedThreadCounter
unsigned int m_savedThreadCounter
Definition: btThreads.h:143
btITaskScheduler::getMaxNumThreads
virtual int getMaxNumThreads() const =0
btITaskScheduler::parallelFor
virtual void parallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)=0
btITaskScheduler::sleepWorkerThreadsHint
virtual void sleepWorkerThreadsHint()
Definition: btThreads.h:135
btITaskScheduler::m_isActive
bool m_isActive
Definition: btThreads.h:144
btITaskScheduler::deactivate
virtual void deactivate()
Definition: btThreads.cpp:358
btITaskScheduler::setNumThreads
virtual void setNumThreads(int numThreads)=0
btITaskScheduler::m_name
const char * m_name
Definition: btThreads.h:142
btITaskScheduler::parallelSum
virtual btScalar parallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody &body)=0
btITaskScheduler::getName
const char * getName() const
Definition: btThreads.h:128
btITaskScheduler::activate
virtual void activate()
Definition: btThreads.cpp:343
btITaskScheduler::~btITaskScheduler
virtual ~btITaskScheduler()
Definition: btThreads.h:127
btSpinMutex
btSpinMutex – lightweight spin-mutex implemented with atomic ops, never puts a thread to sleep becaus...
Definition: btThreads.h:46
btSpinMutex::lock
void lock()
Definition: btThreads.cpp:196
btSpinMutex::tryLock
bool tryLock()
Definition: btThreads.cpp:206
btSpinMutex::unlock
void unlock()
Definition: btThreads.cpp:201
btSpinMutex::mLock
int mLock
Definition: btThreads.h:47
btSpinMutex::btSpinMutex
btSpinMutex()
Definition: btThreads.h:50