Bullet Collision Detection & Physics Library
btThreads.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2003-2014 Erwin Coumans http://bullet.googlecode.com
3 
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
9 
10 1. 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. 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
34 bool btIsMainThread();
35 bool btThreadsAreRunning();
36 unsigned int btGetCurrentThreadIndex();
37 void btResetThreadIndexCounter(); // notify that all worker threads have been destroyed
38 
46 {
47  int mLock;
48 
49 public:
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 {
103 public:
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 //
113 {
114 public:
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 //
124 {
125 public:
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 
141 protected:
142  const char* m_name;
143  unsigned int m_savedThreadCounter;
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)
171 void 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
btMutexLock
void btMutexLock(btSpinMutex *mutex)
Definition: btThreads.h:70
btIParallelForBody::~btIParallelForBody
virtual ~btIParallelForBody()
Definition: btThreads.h:104
btScalar
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:294
btSetTaskScheduler
void btSetTaskScheduler(btITaskScheduler *ts)
Definition: btThreads.cpp:386
btParallelSum
btScalar btParallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody &body)
Definition: btThreads.cpp:439
btIParallelSumBody::~btIParallelSumBody
virtual ~btIParallelSumBody()
Definition: btThreads.h:115
btSpinMutex::tryLock
bool tryLock()
Definition: btThreads.cpp:206
btMutexTryLock
bool btMutexTryLock(btSpinMutex *mutex)
Definition: btThreads.h:88
btIParallelForBody::forLoop
virtual void forLoop(int iBegin, int iEnd) const =0
btScalar.h
btITaskScheduler::getName
const char * getName() const
Definition: btThreads.h:128
btITaskScheduler::m_name
const char * m_name
Definition: btThreads.h:142
btITaskScheduler::setNumThreads
virtual void setNumThreads(int numThreads)=0
btIsMainThread
bool btIsMainThread()
Definition: btThreads.cpp:324
btGetTaskScheduler
btITaskScheduler * btGetTaskScheduler()
Definition: btThreads.cpp:407
btITaskScheduler::parallelSum
virtual btScalar parallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody &body)=0
btGetTBBTaskScheduler
btITaskScheduler * btGetTBBTaskScheduler()
Definition: btThreads.cpp:773
btGetSequentialTaskScheduler
btITaskScheduler * btGetSequentialTaskScheduler()
Definition: btThreads.cpp:755
btSpinMutex::mLock
int mLock
Definition: btThreads.h:47
btGetCurrentThreadIndex
unsigned int btGetCurrentThreadIndex()
Definition: btThreads.cpp:290
btResetThreadIndexCounter
void btResetThreadIndexCounter()
Definition: btThreads.cpp:329
BT_MAX_THREAD_COUNT
const unsigned int BT_MAX_THREAD_COUNT
Definition: btThreads.h:31
btSpinMutex::btSpinMutex
btSpinMutex()
Definition: btThreads.h:50
btITaskScheduler::getNumThreads
virtual int getNumThreads() const =0
btITaskScheduler::deactivate
virtual void deactivate()
Definition: btThreads.cpp:358
btITaskScheduler::m_isActive
bool m_isActive
Definition: btThreads.h:144
btITaskScheduler
Definition: btThreads.h:123
btSpinMutex::lock
void lock()
Definition: btThreads.cpp:196
btSpinMutex::unlock
void unlock()
Definition: btThreads.cpp:201
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:83
btCreateDefaultTaskScheduler
btITaskScheduler * btCreateDefaultTaskScheduler()
Definition: btTaskScheduler.cpp:787
btITaskScheduler::activate
virtual void activate()
Definition: btThreads.cpp:343
btSpinMutex
btSpinMutex – lightweight spin-mutex implemented with atomic ops, never puts a thread to sleep becaus...
Definition: btThreads.h:45
btITaskScheduler::m_savedThreadCounter
unsigned int m_savedThreadCounter
Definition: btThreads.h:143
btGetOpenMPTaskScheduler
btITaskScheduler * btGetOpenMPTaskScheduler()
Definition: btThreads.cpp:762
btIParallelSumBody::sumLoop
virtual btScalar sumLoop(int iBegin, int iEnd) const =0
btITaskScheduler::parallelFor
virtual void parallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)=0
btMutexUnlock
void btMutexUnlock(btSpinMutex *mutex)
Definition: btThreads.h:79
btITaskScheduler::sleepWorkerThreadsHint
virtual void sleepWorkerThreadsHint()
Definition: btThreads.h:135
btIParallelForBody
Definition: btThreads.h:101
btIParallelSumBody
Definition: btThreads.h:112
btParallelFor
void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)
Definition: btThreads.cpp:412
btITaskScheduler::btITaskScheduler
btITaskScheduler(const char *name)
Definition: btThreads.cpp:336
btITaskScheduler::~btITaskScheduler
virtual ~btITaskScheduler()
Definition: btThreads.h:127
btGetPPLTaskScheduler
btITaskScheduler * btGetPPLTaskScheduler()
Definition: btThreads.cpp:784
btITaskScheduler::getMaxNumThreads
virtual int getMaxNumThreads() const =0
btThreadsAreRunning
bool btThreadsAreRunning()
Definition: btThreads.cpp:381