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 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 //
61 // NOTE: btMutex* is for internal Bullet use only
62 //
63 // If BT_THREADSAFE is undefined or 0, should optimize away to nothing.
64 // This is good because for the single-threaded build of Bullet, any calls
65 // to these functions will be optimized out.
66 //
67 // However, for users of the multi-threaded build of Bullet this is kind
68 // of bad because if you call any of these functions from external code
69 // (where BT_THREADSAFE is undefined) you will get unexpected race conditions.
70 //
72 {
73 #if BT_THREADSAFE
74  mutex->lock();
75 #endif // #if BT_THREADSAFE
76 }
77 
79 {
80 #if BT_THREADSAFE
81  mutex->unlock();
82 #endif // #if BT_THREADSAFE
83 }
84 
86 {
87 #if BT_THREADSAFE
88  return mutex->tryLock();
89 #else
90  return true;
91 #endif // #if BT_THREADSAFE
92 }
93 
94 
95 //
96 // btIParallelForBody -- subclass this to express work that can be done in parallel
97 //
99 {
100 public:
101  virtual ~btIParallelForBody() {}
102  virtual void forLoop( int iBegin, int iEnd ) const = 0;
103 };
104 
105 //
106 // btITaskScheduler -- subclass this to implement a task scheduler that can dispatch work to
107 // worker threads
108 //
110 {
111 public:
112  btITaskScheduler( const char* name );
113  virtual ~btITaskScheduler() {}
114  const char* getName() const { return m_name; }
115 
116  virtual int getMaxNumThreads() const = 0;
117  virtual int getNumThreads() const = 0;
118  virtual void setNumThreads( int numThreads ) = 0;
119  virtual void parallelFor( int iBegin, int iEnd, int grainSize, const btIParallelForBody& body ) = 0;
120 
121  // internal use only
122  virtual void activate();
123  virtual void deactivate();
124 
125 protected:
126  const char* m_name;
127  unsigned int m_savedThreadCounter;
129 };
130 
131 // set the task scheduler to use for all calls to btParallelFor()
132 // NOTE: you must set this prior to using any of the multi-threaded "Mt" classes
134 
135 // get the current task scheduler
137 
138 // get non-threaded task scheduler (always available)
140 
141 // get OpenMP task scheduler (if available, otherwise returns null)
143 
144 // get Intel TBB task scheduler (if available, otherwise returns null)
146 
147 // get PPL task scheduler (if available, otherwise returns null)
149 
150 // btParallelFor -- call this to dispatch work like a for-loop
151 // (iterations may be done out of order, so no dependencies are allowed)
152 void btParallelFor( int iBegin, int iEnd, int grainSize, const btIParallelForBody& body );
153 
154 
155 #endif
btMutexLock
void btMutexLock(btSpinMutex *mutex)
Definition: btThreads.h:71
btIParallelForBody::~btIParallelForBody
virtual ~btIParallelForBody()
Definition: btThreads.h:101
btSetTaskScheduler
void btSetTaskScheduler(btITaskScheduler *ts)
Definition: btThreads.cpp:401
btSpinMutex::tryLock
bool tryLock()
Definition: btThreads.cpp:216
btMutexTryLock
bool btMutexTryLock(btSpinMutex *mutex)
Definition: btThreads.h:85
btIParallelForBody::forLoop
virtual void forLoop(int iBegin, int iEnd) const =0
btScalar.h
btITaskScheduler::getName
const char * getName() const
Definition: btThreads.h:114
btITaskScheduler::m_name
const char * m_name
Definition: btThreads.h:126
btITaskScheduler::setNumThreads
virtual void setNumThreads(int numThreads)=0
btIsMainThread
bool btIsMainThread()
Definition: btThreads.cpp:338
btGetTaskScheduler
btITaskScheduler * btGetTaskScheduler()
Definition: btThreads.cpp:423
btGetTBBTaskScheduler
btITaskScheduler * btGetTBBTaskScheduler()
Definition: btThreads.cpp:701
btGetSequentialTaskScheduler
btITaskScheduler * btGetSequentialTaskScheduler()
Definition: btThreads.cpp:681
btSpinMutex::mLock
int mLock
Definition: btThreads.h:47
btGetCurrentThreadIndex
unsigned int btGetCurrentThreadIndex()
Definition: btThreads.cpp:304
btResetThreadIndexCounter
void btResetThreadIndexCounter()
Definition: btThreads.cpp:343
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:372
btITaskScheduler::m_isActive
bool m_isActive
Definition: btThreads.h:128
btITaskScheduler
Definition: btThreads.h:109
btSpinMutex::lock
void lock()
Definition: btThreads.cpp:206
btSpinMutex::unlock
void unlock()
Definition: btThreads.cpp:211
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:81
btITaskScheduler::activate
virtual void activate()
Definition: btThreads.cpp:357
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:127
btGetOpenMPTaskScheduler
btITaskScheduler * btGetOpenMPTaskScheduler()
Definition: btThreads.cpp:689
btITaskScheduler::parallelFor
virtual void parallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)=0
btMutexUnlock
void btMutexUnlock(btSpinMutex *mutex)
Definition: btThreads.h:78
btIParallelForBody
Definition: btThreads.h:98
btParallelFor
void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)
Definition: btThreads.cpp:429
btITaskScheduler::btITaskScheduler
btITaskScheduler(const char *name)
Definition: btThreads.cpp:350
btITaskScheduler::~btITaskScheduler
virtual ~btITaskScheduler()
Definition: btThreads.h:113
btGetPPLTaskScheduler
btITaskScheduler * btGetPPLTaskScheduler()
Definition: btThreads.cpp:713
btITaskScheduler::getMaxNumThreads
virtual int getMaxNumThreads() const =0
btThreadsAreRunning
bool btThreadsAreRunning()
Definition: btThreads.cpp:395