Bullet Collision Detection & Physics Library
btThreads.h
Go to the documentation of this file.
1/*
2Copyright (c) 2003-2014 Erwin Coumans http://bullet.googlecode.com
3
4This software is provided 'as-is', without any express or implied warranty.
5In no event will the authors be held liable for any damages arising from the use of this software.
6Permission is granted to anyone to use this software for any purpose,
7including commercial applications, and to alter it and redistribute it freely,
8subject to the following restrictions:
9
101. 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.
112. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
123. 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
31const unsigned int BT_MAX_THREAD_COUNT = 64; // only if BT_THREADSAFE is 1
32
33// for internal use only
34bool btIsMainThread();
36unsigned int btGetCurrentThreadIndex();
37void btResetThreadIndexCounter(); // notify that all worker threads have been destroyed
38
46{
47 int mLock;
48
49public:
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{
100public:
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{
111public:
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
125protected:
126 const char* m_name;
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)
152void btParallelFor( int iBegin, int iEnd, int grainSize, const btIParallelForBody& body );
153
154
155#endif
#define SIMD_FORCE_INLINE
Definition: btScalar.h:81
btITaskScheduler * btGetTBBTaskScheduler()
Definition: btThreads.cpp:701
void btMutexLock(btSpinMutex *mutex)
Definition: btThreads.h:71
void btMutexUnlock(btSpinMutex *mutex)
Definition: btThreads.h:78
btITaskScheduler * btGetPPLTaskScheduler()
Definition: btThreads.cpp:713
void btResetThreadIndexCounter()
Definition: btThreads.cpp:343
bool btThreadsAreRunning()
Definition: btThreads.cpp:395
btITaskScheduler * btGetOpenMPTaskScheduler()
Definition: btThreads.cpp:689
unsigned int btGetCurrentThreadIndex()
Definition: btThreads.cpp:304
bool btMutexTryLock(btSpinMutex *mutex)
Definition: btThreads.h:85
void btSetTaskScheduler(btITaskScheduler *ts)
Definition: btThreads.cpp:401
btITaskScheduler * btGetTaskScheduler()
Definition: btThreads.cpp:423
btITaskScheduler * btGetSequentialTaskScheduler()
Definition: btThreads.cpp:681
bool btIsMainThread()
Definition: btThreads.cpp:338
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 ~btIParallelForBody()
Definition: btThreads.h:101
virtual void forLoop(int iBegin, int iEnd) const =0
btITaskScheduler(const char *name)
Definition: btThreads.cpp:350
virtual int getNumThreads() const =0
unsigned int m_savedThreadCounter
Definition: btThreads.h:127
virtual int getMaxNumThreads() const =0
virtual void parallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody &body)=0
virtual void deactivate()
Definition: btThreads.cpp:372
virtual void setNumThreads(int numThreads)=0
const char * m_name
Definition: btThreads.h:126
const char * getName() const
Definition: btThreads.h:114
virtual void activate()
Definition: btThreads.cpp:357
virtual ~btITaskScheduler()
Definition: btThreads.h:113
btSpinMutex – lightweight spin-mutex implemented with atomic ops, never puts a thread to sleep becaus...
Definition: btThreads.h:46
void lock()
Definition: btThreads.cpp:206
bool tryLock()
Definition: btThreads.cpp:216
void unlock()
Definition: btThreads.cpp:211