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
const char * m_name
Definition: btThreads.h:126
bool btIsMainThread()
Definition: btThreads.cpp:338
btITaskScheduler * btGetOpenMPTaskScheduler()
Definition: btThreads.cpp:689
virtual void forLoop(int iBegin, int iEnd) const =0
bool tryLock()
Definition: btThreads.cpp:216
virtual void deactivate()
Definition: btThreads.cpp:372
virtual int getNumThreads() const =0
#define SIMD_FORCE_INLINE
Definition: btScalar.h:81
btSpinMutex – lightweight spin-mutex implemented with atomic ops, never puts a thread to sleep becaus...
Definition: btThreads.h:45
btITaskScheduler * btGetPPLTaskScheduler()
Definition: btThreads.cpp:713
bool btThreadsAreRunning()
Definition: btThreads.cpp:395
virtual ~btITaskScheduler()
Definition: btThreads.h:113
btITaskScheduler * btGetSequentialTaskScheduler()
Definition: btThreads.cpp:681
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:429
virtual void setNumThreads(int numThreads)=0
const char * getName() const
Definition: btThreads.h:114
btITaskScheduler * btGetTaskScheduler()
Definition: btThreads.cpp:423
void lock()
Definition: btThreads.cpp:206
virtual void parallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)=0
bool btMutexTryLock(btSpinMutex *mutex)
Definition: btThreads.h:85
unsigned int btGetCurrentThreadIndex()
Definition: btThreads.cpp:304
void btResetThreadIndexCounter()
Definition: btThreads.cpp:343
void unlock()
Definition: btThreads.cpp:211
btITaskScheduler(const char *name)
Definition: btThreads.cpp:350
void btMutexUnlock(btSpinMutex *mutex)
Definition: btThreads.h:78
virtual void activate()
Definition: btThreads.cpp:357
void btMutexLock(btSpinMutex *mutex)
Definition: btThreads.h:71
unsigned int m_savedThreadCounter
Definition: btThreads.h:127
virtual ~btIParallelForBody()
Definition: btThreads.h:101
btITaskScheduler * btGetTBBTaskScheduler()
Definition: btThreads.cpp:701
void btSetTaskScheduler(btITaskScheduler *ts)
Definition: btThreads.cpp:401
virtual int getMaxNumThreads() const =0