btThreads.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. const unsigned int BT_MAX_THREAD_COUNT = 64; // only if BT_THREADSAFE is 1
  23. // for internal use only
  24. bool btIsMainThread();
  25. bool btThreadsAreRunning();
  26. unsigned int btGetCurrentThreadIndex();
  27. void btResetThreadIndexCounter(); // notify that all worker threads have been destroyed
  28. ///
  29. /// btSpinMutex -- lightweight spin-mutex implemented with atomic ops, never puts
  30. /// a thread to sleep because it is designed to be used with a task scheduler
  31. /// which has one thread per core and the threads don't sleep until they
  32. /// run out of tasks. Not good for general purpose use.
  33. ///
  34. class btSpinMutex
  35. {
  36. int mLock;
  37. public:
  38. btSpinMutex()
  39. {
  40. mLock = 0;
  41. }
  42. void lock();
  43. void unlock();
  44. bool tryLock();
  45. };
  46. //
  47. // NOTE: btMutex* is for internal Bullet use only
  48. //
  49. // If BT_THREADSAFE is undefined or 0, should optimize away to nothing.
  50. // This is good because for the single-threaded build of Bullet, any calls
  51. // to these functions will be optimized out.
  52. //
  53. // However, for users of the multi-threaded build of Bullet this is kind
  54. // of bad because if you call any of these functions from external code
  55. // (where BT_THREADSAFE is undefined) you will get unexpected race conditions.
  56. //
  57. SIMD_FORCE_INLINE void btMutexLock( btSpinMutex* mutex )
  58. {
  59. #if BT_THREADSAFE
  60. mutex->lock();
  61. #endif // #if BT_THREADSAFE
  62. }
  63. SIMD_FORCE_INLINE void btMutexUnlock( btSpinMutex* mutex )
  64. {
  65. #if BT_THREADSAFE
  66. mutex->unlock();
  67. #endif // #if BT_THREADSAFE
  68. }
  69. SIMD_FORCE_INLINE bool btMutexTryLock( btSpinMutex* mutex )
  70. {
  71. #if BT_THREADSAFE
  72. return mutex->tryLock();
  73. #else
  74. return true;
  75. #endif // #if BT_THREADSAFE
  76. }
  77. //
  78. // btIParallelForBody -- subclass this to express work that can be done in parallel
  79. //
  80. class btIParallelForBody
  81. {
  82. public:
  83. virtual ~btIParallelForBody() {}
  84. virtual void forLoop( int iBegin, int iEnd ) const = 0;
  85. };
  86. //
  87. // btITaskScheduler -- subclass this to implement a task scheduler that can dispatch work to
  88. // worker threads
  89. //
  90. class btITaskScheduler
  91. {
  92. public:
  93. btITaskScheduler( const char* name );
  94. virtual ~btITaskScheduler() {}
  95. const char* getName() const { return m_name; }
  96. virtual int getMaxNumThreads() const = 0;
  97. virtual int getNumThreads() const = 0;
  98. virtual void setNumThreads( int numThreads ) = 0;
  99. virtual void parallelFor( int iBegin, int iEnd, int grainSize, const btIParallelForBody& body ) = 0;
  100. // internal use only
  101. virtual void activate();
  102. virtual void deactivate();
  103. protected:
  104. const char* m_name;
  105. unsigned int m_savedThreadCounter;
  106. bool m_isActive;
  107. };
  108. // set the task scheduler to use for all calls to btParallelFor()
  109. // NOTE: you must set this prior to using any of the multi-threaded "Mt" classes
  110. void btSetTaskScheduler( btITaskScheduler* ts );
  111. // get the current task scheduler
  112. btITaskScheduler* btGetTaskScheduler();
  113. // get non-threaded task scheduler (always available)
  114. btITaskScheduler* btGetSequentialTaskScheduler();
  115. // get OpenMP task scheduler (if available, otherwise returns null)
  116. btITaskScheduler* btGetOpenMPTaskScheduler();
  117. // get Intel TBB task scheduler (if available, otherwise returns null)
  118. btITaskScheduler* btGetTBBTaskScheduler();
  119. // get PPL task scheduler (if available, otherwise returns null)
  120. btITaskScheduler* btGetPPLTaskScheduler();
  121. // btParallelFor -- call this to dispatch work like a for-loop
  122. // (iterations may be done out of order, so no dependencies are allowed)
  123. void btParallelFor( int iBegin, int iEnd, int grainSize, const btIParallelForBody& body );
  124. #endif