btThreads.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. Copyright (c) 2003-2014 Erwin Coumans http://bullet.googlecode.com
  3. This software is provided 'as-is', without any express or implied warranty.
  4. In no event will the authors be held liable for any damages arising from the use of this software.
  5. Permission is granted to anyone to use this software for any purpose,
  6. including commercial applications, and to alter it and redistribute it freely,
  7. subject to the following restrictions:
  8. 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.
  9. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. 3. This notice may not be removed or altered from any source distribution.
  11. */
  12. #ifndef BT_THREADS_H
  13. #define BT_THREADS_H
  14. #include "btScalar.h" // has definitions like SIMD_FORCE_INLINE
  15. #if defined(_MSC_VER) && _MSC_VER >= 1600
  16. // give us a compile error if any signatures of overriden methods is changed
  17. #define BT_OVERRIDE override
  18. #endif
  19. #ifndef BT_OVERRIDE
  20. #define BT_OVERRIDE
  21. #endif
  22. // Don't set this to larger than 64, without modifying btThreadSupportPosix
  23. // and btThreadSupportWin32. They use UINT64 bit-masks.
  24. const unsigned int BT_MAX_THREAD_COUNT = 64; // only if BT_THREADSAFE is 1
  25. // for internal use only
  26. bool btIsMainThread();
  27. bool btThreadsAreRunning();
  28. unsigned int btGetCurrentThreadIndex();
  29. void btResetThreadIndexCounter(); // notify that all worker threads have been destroyed
  30. ///
  31. /// btSpinMutex -- lightweight spin-mutex implemented with atomic ops, never puts
  32. /// a thread to sleep because it is designed to be used with a task scheduler
  33. /// which has one thread per core and the threads don't sleep until they
  34. /// run out of tasks. Not good for general purpose use.
  35. ///
  36. class btSpinMutex
  37. {
  38. int mLock;
  39. public:
  40. btSpinMutex()
  41. {
  42. mLock = 0;
  43. }
  44. void lock();
  45. void unlock();
  46. bool tryLock();
  47. };
  48. //
  49. // NOTE: btMutex* is for internal Bullet use only
  50. //
  51. // If BT_THREADSAFE is undefined or 0, should optimize away to nothing.
  52. // This is good because for the single-threaded build of Bullet, any calls
  53. // to these functions will be optimized out.
  54. //
  55. // However, for users of the multi-threaded build of Bullet this is kind
  56. // of bad because if you call any of these functions from external code
  57. // (where BT_THREADSAFE is undefined) you will get unexpected race conditions.
  58. //
  59. SIMD_FORCE_INLINE void btMutexLock(btSpinMutex* mutex)
  60. {
  61. #if BT_THREADSAFE
  62. mutex->lock();
  63. #else
  64. (void)mutex;
  65. #endif // #if BT_THREADSAFE
  66. }
  67. SIMD_FORCE_INLINE void btMutexUnlock(btSpinMutex* mutex)
  68. {
  69. #if BT_THREADSAFE
  70. mutex->unlock();
  71. #else
  72. (void)mutex;
  73. #endif // #if BT_THREADSAFE
  74. }
  75. SIMD_FORCE_INLINE bool btMutexTryLock(btSpinMutex* mutex)
  76. {
  77. #if BT_THREADSAFE
  78. return mutex->tryLock();
  79. #else
  80. (void)mutex;
  81. return true;
  82. #endif // #if BT_THREADSAFE
  83. }
  84. //
  85. // btIParallelForBody -- subclass this to express work that can be done in parallel
  86. //
  87. class btIParallelForBody
  88. {
  89. public:
  90. virtual ~btIParallelForBody() {}
  91. virtual void forLoop(int iBegin, int iEnd) const = 0;
  92. };
  93. //
  94. // btIParallelSumBody -- subclass this to express work that can be done in parallel
  95. // and produces a sum over all loop elements
  96. //
  97. class btIParallelSumBody
  98. {
  99. public:
  100. virtual ~btIParallelSumBody() {}
  101. virtual btScalar sumLoop(int iBegin, int iEnd) const = 0;
  102. };
  103. //
  104. // btITaskScheduler -- subclass this to implement a task scheduler that can dispatch work to
  105. // worker threads
  106. //
  107. class btITaskScheduler
  108. {
  109. public:
  110. btITaskScheduler(const char* name);
  111. virtual ~btITaskScheduler() {}
  112. const char* getName() const { return m_name; }
  113. virtual int getMaxNumThreads() const = 0;
  114. virtual int getNumThreads() const = 0;
  115. virtual void setNumThreads(int numThreads) = 0;
  116. virtual void parallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody& body) = 0;
  117. virtual btScalar parallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody& body) = 0;
  118. virtual void sleepWorkerThreadsHint() {} // hint the task scheduler that we may not be using these threads for a little while
  119. // internal use only
  120. virtual void activate();
  121. virtual void deactivate();
  122. protected:
  123. const char* m_name;
  124. unsigned int m_savedThreadCounter;
  125. bool m_isActive;
  126. };
  127. // set the task scheduler to use for all calls to btParallelFor()
  128. // NOTE: you must set this prior to using any of the multi-threaded "Mt" classes
  129. void btSetTaskScheduler(btITaskScheduler* ts);
  130. // get the current task scheduler
  131. btITaskScheduler* btGetTaskScheduler();
  132. // get non-threaded task scheduler (always available)
  133. btITaskScheduler* btGetSequentialTaskScheduler();
  134. // create a default task scheduler (Win32 or pthreads based)
  135. btITaskScheduler* btCreateDefaultTaskScheduler();
  136. // get OpenMP task scheduler (if available, otherwise returns null)
  137. btITaskScheduler* btGetOpenMPTaskScheduler();
  138. // get Intel TBB task scheduler (if available, otherwise returns null)
  139. btITaskScheduler* btGetTBBTaskScheduler();
  140. // get PPL task scheduler (if available, otherwise returns null)
  141. btITaskScheduler* btGetPPLTaskScheduler();
  142. // btParallelFor -- call this to dispatch work like a for-loop
  143. // (iterations may be done out of order, so no dependencies are allowed)
  144. void btParallelFor(int iBegin, int iEnd, int grainSize, const btIParallelForBody& body);
  145. // btParallelSum -- call this to dispatch work like a for-loop, returns the sum of all iterations
  146. // (iterations may be done out of order, so no dependencies are allowed)
  147. btScalar btParallelSum(int iBegin, int iEnd, int grainSize, const btIParallelSumBody& body);
  148. #endif