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 
16 
17 #ifndef BT_THREADS_H
18 #define BT_THREADS_H
19 
20 #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
21 
22 #if defined (_MSC_VER) && _MSC_VER >= 1600
23 // give us a compile error if any signatures of overriden methods is changed
24 #define BT_OVERRIDE override
25 #endif
26 
27 #ifndef BT_OVERRIDE
28 #define BT_OVERRIDE
29 #endif
30 
31 // Don't set this to larger than 64, without modifying btThreadSupportPosix
32 // and btThreadSupportWin32. They use UINT64 bit-masks.
33 const unsigned int BT_MAX_THREAD_COUNT = 64; // only if BT_THREADSAFE is 1
34 
35 // for internal use only
36 bool btIsMainThread();
37 bool btThreadsAreRunning();
38 unsigned int btGetCurrentThreadIndex();
39 void btResetThreadIndexCounter(); // notify that all worker threads have been destroyed
40 
48 {
49  int mLock;
50 
51 public:
53  {
54  mLock = 0;
55  }
56  void lock();
57  void unlock();
58  bool tryLock();
59 };
60 
61 
62 //
63 // NOTE: btMutex* is for internal Bullet use only
64 //
65 // If BT_THREADSAFE is undefined or 0, should optimize away to nothing.
66 // This is good because for the single-threaded build of Bullet, any calls
67 // to these functions will be optimized out.
68 //
69 // However, for users of the multi-threaded build of Bullet this is kind
70 // of bad because if you call any of these functions from external code
71 // (where BT_THREADSAFE is undefined) you will get unexpected race conditions.
72 //
74 {
75 #if BT_THREADSAFE
76  mutex->lock();
77 #else
78  (void)mutex;
79 #endif // #if BT_THREADSAFE
80 }
81 
83 {
84 #if BT_THREADSAFE
85  mutex->unlock();
86 #else
87  (void)mutex;
88 #endif // #if BT_THREADSAFE
89 }
90 
92 {
93 #if BT_THREADSAFE
94  return mutex->tryLock();
95 #else
96  (void)mutex;
97  return true;
98 #endif // #if BT_THREADSAFE
99 }
100 
101 
102 //
103 // btIParallelForBody -- subclass this to express work that can be done in parallel
104 //
106 {
107 public:
108  virtual ~btIParallelForBody() {}
109  virtual void forLoop( int iBegin, int iEnd ) const = 0;
110 };
111 
112 //
113 // btIParallelSumBody -- subclass this to express work that can be done in parallel
114 // and produces a sum over all loop elements
115 //
117 {
118 public:
119  virtual ~btIParallelSumBody() {}
120  virtual btScalar sumLoop( int iBegin, int iEnd ) const = 0;
121 };
122 
123 //
124 // btITaskScheduler -- subclass this to implement a task scheduler that can dispatch work to
125 // worker threads
126 //
128 {
129 public:
130  btITaskScheduler( const char* name );
131  virtual ~btITaskScheduler() {}
132  const char* getName() const { return m_name; }
133 
134  virtual int getMaxNumThreads() const = 0;
135  virtual int getNumThreads() const = 0;
136  virtual void setNumThreads( int numThreads ) = 0;
137  virtual void parallelFor( int iBegin, int iEnd, int grainSize, const btIParallelForBody& body ) = 0;
138  virtual btScalar parallelSum( int iBegin, int iEnd, int grainSize, const btIParallelSumBody& body ) = 0;
139  virtual void sleepWorkerThreadsHint() {} // hint the task scheduler that we may not be using these threads for a little while
140 
141  // internal use only
142  virtual void activate();
143  virtual void deactivate();
144 
145 protected:
146  const char* m_name;
147  unsigned int m_savedThreadCounter;
149 };
150 
151 // set the task scheduler to use for all calls to btParallelFor()
152 // NOTE: you must set this prior to using any of the multi-threaded "Mt" classes
154 
155 // get the current task scheduler
157 
158 // get non-threaded task scheduler (always available)
160 
161 // create a default task scheduler (Win32 or pthreads based)
163 
164 // get OpenMP task scheduler (if available, otherwise returns null)
166 
167 // get Intel TBB task scheduler (if available, otherwise returns null)
169 
170 // get PPL task scheduler (if available, otherwise returns null)
172 
173 // btParallelFor -- call this to dispatch work like a for-loop
174 // (iterations may be done out of order, so no dependencies are allowed)
175 void btParallelFor( int iBegin, int iEnd, int grainSize, const btIParallelForBody& body );
176 
177 // btParallelSum -- call this to dispatch work like a for-loop, returns the sum of all iterations
178 // (iterations may be done out of order, so no dependencies are allowed)
179 btScalar btParallelSum( int iBegin, int iEnd, int grainSize, const btIParallelSumBody& body );
180 
181 
182 #endif
const char * m_name
Definition: btThreads.h:146
bool btIsMainThread()
Definition: btThreads.cpp:338
btITaskScheduler * btGetOpenMPTaskScheduler()
Definition: btThreads.cpp:786
virtual void sleepWorkerThreadsHint()
Definition: btThreads.h:139
bool tryLock()
Definition: btThreads.cpp:216
#define SIMD_FORCE_INLINE
Definition: btScalar.h:81
btSpinMutex – lightweight spin-mutex implemented with atomic ops, never puts a thread to sleep becau...
Definition: btThreads.h:47
const char * getName() const
Definition: btThreads.h:132
btITaskScheduler * btGetPPLTaskScheduler()
Definition: btThreads.cpp:810
bool btThreadsAreRunning()
Definition: btThreads.cpp:395
virtual ~btITaskScheduler()
Definition: btThreads.h:131
btScalar btParallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody &body)
Definition: btThreads.cpp:456
btITaskScheduler * btGetSequentialTaskScheduler()
Definition: btThreads.cpp:778
const unsigned int BT_MAX_THREAD_COUNT
Definition: btThreads.h:33
virtual ~btIParallelSumBody()
Definition: btThreads.h:119
void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)
Definition: btThreads.cpp:429
btITaskScheduler * btGetTaskScheduler()
Definition: btThreads.cpp:423
void lock()
Definition: btThreads.cpp:206
bool btMutexTryLock(btSpinMutex *mutex)
Definition: btThreads.h:91
unsigned int btGetCurrentThreadIndex()
Definition: btThreads.cpp:304
void btResetThreadIndexCounter()
Definition: btThreads.cpp:343
void unlock()
Definition: btThreads.cpp:211
btITaskScheduler * btCreateDefaultTaskScheduler()
void btMutexUnlock(btSpinMutex *mutex)
Definition: btThreads.h:82
void btMutexLock(btSpinMutex *mutex)
Definition: btThreads.h:73
unsigned int m_savedThreadCounter
Definition: btThreads.h:147
virtual ~btIParallelForBody()
Definition: btThreads.h:108
btITaskScheduler * btGetTBBTaskScheduler()
Definition: btThreads.cpp:798
void btSetTaskScheduler(btITaskScheduler *ts)
Definition: btThreads.cpp:401
float btScalar
The btScalar type abstracts floating point numbers, to easily switch between double and single floati...
Definition: btScalar.h:292