Bullet Collision Detection & Physics Library
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.
31const unsigned int BT_MAX_THREAD_COUNT = 64; // only if BT_THREADSAFE is 1
32
33// for internal use only
34bool btIsMainThread();
36unsigned int btGetCurrentThreadIndex();
37void btResetThreadIndexCounter(); // notify that all worker threads have been destroyed
38
46{
47 int mLock;
48
49public:
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//
71{
72#if BT_THREADSAFE
73 mutex->lock();
74#else
75 (void)mutex;
76#endif // #if BT_THREADSAFE
77}
78
80{
81#if BT_THREADSAFE
82 mutex->unlock();
83#else
84 (void)mutex;
85#endif // #if BT_THREADSAFE
86}
87
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//
102{
103public:
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//
113{
114public:
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//
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;
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
150
151// get the current task scheduler
153
154// get non-threaded task scheduler (always available)
156
157// create a default task scheduler (Win32 or pthreads based)
159
160// get OpenMP task scheduler (if available, otherwise returns null)
162
163// get Intel TBB task scheduler (if available, otherwise returns null)
165
166// get PPL task scheduler (if available, otherwise returns null)
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)
175btScalar btParallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody& body);
176
177#endif
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:314
#define SIMD_FORCE_INLINE
Definition: btScalar.h:98
btITaskScheduler * btGetTBBTaskScheduler()
Definition: btThreads.cpp:773
void btMutexLock(btSpinMutex *mutex)
Definition: btThreads.h:70
void btMutexUnlock(btSpinMutex *mutex)
Definition: btThreads.h:79
btITaskScheduler * btGetPPLTaskScheduler()
Definition: btThreads.cpp:784
void btResetThreadIndexCounter()
Definition: btThreads.cpp:329
btScalar btParallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody &body)
Definition: btThreads.cpp:439
bool btThreadsAreRunning()
Definition: btThreads.cpp:381
btITaskScheduler * btGetOpenMPTaskScheduler()
Definition: btThreads.cpp:762
unsigned int btGetCurrentThreadIndex()
Definition: btThreads.cpp:290
bool btMutexTryLock(btSpinMutex *mutex)
Definition: btThreads.h:88
btITaskScheduler * btCreateDefaultTaskScheduler()
void btSetTaskScheduler(btITaskScheduler *ts)
Definition: btThreads.cpp:386
btITaskScheduler * btGetTaskScheduler()
Definition: btThreads.cpp:407
btITaskScheduler * btGetSequentialTaskScheduler()
Definition: btThreads.cpp:755
bool btIsMainThread()
Definition: btThreads.cpp:324
const unsigned int BT_MAX_THREAD_COUNT
Definition: btThreads.h:31
void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)
Definition: btThreads.cpp:412
virtual ~btIParallelForBody()
Definition: btThreads.h:104
virtual void forLoop(int iBegin, int iEnd) const =0
virtual btScalar sumLoop(int iBegin, int iEnd) const =0
virtual ~btIParallelSumBody()
Definition: btThreads.h:115
btITaskScheduler(const char *name)
Definition: btThreads.cpp:336
virtual int getNumThreads() const =0
unsigned int m_savedThreadCounter
Definition: btThreads.h:143
virtual int getMaxNumThreads() const =0
virtual void parallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)=0
virtual void sleepWorkerThreadsHint()
Definition: btThreads.h:135
virtual void deactivate()
Definition: btThreads.cpp:358
virtual void setNumThreads(int numThreads)=0
const char * m_name
Definition: btThreads.h:142
virtual btScalar parallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody &body)=0
const char * getName() const
Definition: btThreads.h:128
virtual void activate()
Definition: btThreads.cpp:343
virtual ~btITaskScheduler()
Definition: btThreads.h:127
btSpinMutex – lightweight spin-mutex implemented with atomic ops, never puts a thread to sleep becaus...
Definition: btThreads.h:46
void lock()
Definition: btThreads.cpp:196
bool tryLock()
Definition: btThreads.cpp:206
void unlock()
Definition: btThreads.cpp:201