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 ///
23 /// btSpinMutex -- lightweight spin-mutex implemented with atomic ops, never puts
24 ///               a thread to sleep because it is designed to be used with a task scheduler
25 ///               which has one thread per core and the threads don't sleep until they
26 ///               run out of tasks. Not good for general purpose use.
27 ///
28 class btSpinMutex
29 {
30     int mLock;
31 
32 public:
btSpinMutex()33     btSpinMutex()
34     {
35         mLock = 0;
36     }
37     void lock();
38     void unlock();
39     bool tryLock();
40 };
41 
42 #if BT_THREADSAFE
43 
44 // for internal Bullet use only
btMutexLock(btSpinMutex * mutex)45 SIMD_FORCE_INLINE void btMutexLock( btSpinMutex* mutex )
46 {
47     mutex->lock();
48 }
49 
btMutexUnlock(btSpinMutex * mutex)50 SIMD_FORCE_INLINE void btMutexUnlock( btSpinMutex* mutex )
51 {
52     mutex->unlock();
53 }
54 
btMutexTryLock(btSpinMutex * mutex)55 SIMD_FORCE_INLINE bool btMutexTryLock( btSpinMutex* mutex )
56 {
57     return mutex->tryLock();
58 }
59 
60 // for internal use only
61 bool btIsMainThread();
62 unsigned int btGetCurrentThreadIndex();
63 const unsigned int BT_MAX_THREAD_COUNT = 64;
64 
65 #else
66 
67 // for internal Bullet use only
68 // if BT_THREADSAFE is undefined or 0, should optimize away to nothing
btMutexLock(btSpinMutex *)69 SIMD_FORCE_INLINE void btMutexLock( btSpinMutex* ) {}
btMutexUnlock(btSpinMutex *)70 SIMD_FORCE_INLINE void btMutexUnlock( btSpinMutex* ) {}
btMutexTryLock(btSpinMutex *)71 SIMD_FORCE_INLINE bool btMutexTryLock( btSpinMutex* ) {return true;}
72 #endif
73 
74 
75 #endif //BT_THREADS_H
76