[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / LinearMath / btThreads.h
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 #ifndef BT_THREADS_H
16 #define BT_THREADS_H
17
18 #include "btScalar.h"  // has definitions like SIMD_FORCE_INLINE
19
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
23 #endif
24
25 #ifndef BT_OVERRIDE
26 #define BT_OVERRIDE
27 #endif
28
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
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
39 ///
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.
44 ///
45 class btSpinMutex
46 {
47         int mLock;
48
49 public:
50         btSpinMutex()
51         {
52                 mLock = 0;
53         }
54         void lock();
55         void unlock();
56         bool tryLock();
57 };
58
59 //
60 // NOTE: btMutex* is for internal Bullet use only
61 //
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.
65 //
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.
69 //
70 SIMD_FORCE_INLINE void btMutexLock(btSpinMutex* mutex)
71 {
72 #if BT_THREADSAFE
73         mutex->lock();
74 #else
75         (void)mutex;
76 #endif  // #if BT_THREADSAFE
77 }
78
79 SIMD_FORCE_INLINE void btMutexUnlock(btSpinMutex* mutex)
80 {
81 #if BT_THREADSAFE
82         mutex->unlock();
83 #else
84         (void)mutex;
85 #endif  // #if BT_THREADSAFE
86 }
87
88 SIMD_FORCE_INLINE bool btMutexTryLock(btSpinMutex* mutex)
89 {
90 #if BT_THREADSAFE
91         return mutex->tryLock();
92 #else
93         (void)mutex;
94         return true;
95 #endif  // #if BT_THREADSAFE
96 }
97
98 //
99 // btIParallelForBody -- subclass this to express work that can be done in parallel
100 //
101 class btIParallelForBody
102 {
103 public:
104         virtual ~btIParallelForBody() {}
105         virtual void forLoop(int iBegin, int iEnd) const = 0;
106 };
107
108 //
109 // btIParallelSumBody -- subclass this to express work that can be done in parallel
110 //                       and produces a sum over all loop elements
111 //
112 class btIParallelSumBody
113 {
114 public:
115         virtual ~btIParallelSumBody() {}
116         virtual btScalar sumLoop(int iBegin, int iEnd) const = 0;
117 };
118
119 //
120 // btITaskScheduler -- subclass this to implement a task scheduler that can dispatch work to
121 //                     worker threads
122 //
123 class btITaskScheduler
124 {
125 public:
126         btITaskScheduler(const char* name);
127         virtual ~btITaskScheduler() {}
128         const char* getName() const { return m_name; }
129
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
136
137         // internal use only
138         virtual void activate();
139         virtual void deactivate();
140
141 protected:
142         const char* m_name;
143         unsigned int m_savedThreadCounter;
144         bool m_isActive;
145 };
146
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);
150
151 // get the current task scheduler
152 btITaskScheduler* btGetTaskScheduler();
153
154 // get non-threaded task scheduler (always available)
155 btITaskScheduler* btGetSequentialTaskScheduler();
156
157 // create a default task scheduler (Win32 or pthreads based)
158 btITaskScheduler* btCreateDefaultTaskScheduler();
159
160 // get OpenMP task scheduler (if available, otherwise returns null)
161 btITaskScheduler* btGetOpenMPTaskScheduler();
162
163 // get Intel TBB task scheduler (if available, otherwise returns null)
164 btITaskScheduler* btGetTBBTaskScheduler();
165
166 // get PPL task scheduler (if available, otherwise returns null)
167 btITaskScheduler* btGetPPLTaskScheduler();
168
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);
172
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);
176
177 #endif