2 Copyright (c) 2003-2014 Erwin Coumans http://bullet.googlecode.com
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:
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.
18 #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
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
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
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
40 /// btSpinMutex -- lightweight spin-mutex implemented with atomic ops, never puts
41 /// a thread to sleep because it is designed to be used with a task scheduler
42 /// which has one thread per core and the threads don't sleep until they
43 /// run out of tasks. Not good for general purpose use.
60 // NOTE: btMutex* is for internal Bullet use only
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.
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.
70 SIMD_FORCE_INLINE void btMutexLock(btSpinMutex* mutex)
76 #endif // #if BT_THREADSAFE
79 SIMD_FORCE_INLINE void btMutexUnlock(btSpinMutex* mutex)
85 #endif // #if BT_THREADSAFE
88 SIMD_FORCE_INLINE bool btMutexTryLock(btSpinMutex* mutex)
91 return mutex->tryLock();
95 #endif // #if BT_THREADSAFE
99 // btIParallelForBody -- subclass this to express work that can be done in parallel
101 class btIParallelForBody
104 virtual ~btIParallelForBody() {}
105 virtual void forLoop(int iBegin, int iEnd) const = 0;
109 // btIParallelSumBody -- subclass this to express work that can be done in parallel
110 // and produces a sum over all loop elements
112 class btIParallelSumBody
115 virtual ~btIParallelSumBody() {}
116 virtual btScalar sumLoop(int iBegin, int iEnd) const = 0;
120 // btITaskScheduler -- subclass this to implement a task scheduler that can dispatch work to
123 class btITaskScheduler
126 btITaskScheduler(const char* name);
127 virtual ~btITaskScheduler() {}
128 const char* getName() const { return m_name; }
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
138 virtual void activate();
139 virtual void deactivate();
143 unsigned int m_savedThreadCounter;
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
149 void btSetTaskScheduler(btITaskScheduler* ts);
151 // get the current task scheduler
152 btITaskScheduler* btGetTaskScheduler();
154 // get non-threaded task scheduler (always available)
155 btITaskScheduler* btGetSequentialTaskScheduler();
157 // create a default task scheduler (Win32 or pthreads based)
158 btITaskScheduler* btCreateDefaultTaskScheduler();
160 // get OpenMP task scheduler (if available, otherwise returns null)
161 btITaskScheduler* btGetOpenMPTaskScheduler();
163 // get Intel TBB task scheduler (if available, otherwise returns null)
164 btITaskScheduler* btGetTBBTaskScheduler();
166 // get PPL task scheduler (if available, otherwise returns null)
167 btITaskScheduler* btGetPPLTaskScheduler();
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);
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);