b3Win32ThreadSupport.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
  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. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "Bullet3Common/b3Scalar.h"
  14. #ifndef BT_WIN32_THREAD_SUPPORT_H
  15. #define BT_WIN32_THREAD_SUPPORT_H
  16. #include "Bullet3Common/b3AlignedObjectArray.h"
  17. #include "b3ThreadSupportInterface.h"
  18. typedef void (*b3Win32ThreadFunc)(void* userPtr,void* lsMemory);
  19. typedef void* (*b3Win32lsMemorySetupFunc)();
  20. ///b3Win32ThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
  21. class b3Win32ThreadSupport : public b3ThreadSupportInterface
  22. {
  23. public:
  24. ///placeholder, until libspe2 support is there
  25. struct b3ThreadStatus
  26. {
  27. int m_taskId;
  28. int m_commandId;
  29. int m_status;
  30. b3Win32ThreadFunc m_userThreadFunc;
  31. void* m_userPtr; //for taskDesc etc
  32. void* m_lsMemory; //initialized using Win32LocalStoreMemorySetupFunc
  33. void* m_threadHandle; //this one is calling 'Win32ThreadFunc'
  34. void* m_eventStartHandle;
  35. char m_eventStartHandleName[32];
  36. void* m_eventCompletetHandle;
  37. char m_eventCompletetHandleName[32];
  38. };
  39. private:
  40. b3AlignedObjectArray<b3ThreadStatus> m_activeThreadStatus;
  41. b3AlignedObjectArray<void*> m_completeHandles;
  42. int m_maxNumTasks;
  43. public:
  44. ///Setup and initialize SPU/CELL/Libspe2
  45. struct Win32ThreadConstructionInfo
  46. {
  47. Win32ThreadConstructionInfo(const char* uniqueName,
  48. b3Win32ThreadFunc userThreadFunc,
  49. b3Win32lsMemorySetupFunc lsMemoryFunc,
  50. int numThreads=1,
  51. int threadStackSize=65535
  52. )
  53. :m_uniqueName(uniqueName),
  54. m_userThreadFunc(userThreadFunc),
  55. m_lsMemoryFunc(lsMemoryFunc),
  56. m_numThreads(numThreads),
  57. m_threadStackSize(threadStackSize),
  58. m_priority(0)
  59. {
  60. }
  61. const char* m_uniqueName;
  62. b3Win32ThreadFunc m_userThreadFunc;
  63. b3Win32lsMemorySetupFunc m_lsMemoryFunc;
  64. int m_numThreads;
  65. int m_threadStackSize;
  66. int m_priority;
  67. };
  68. b3Win32ThreadSupport(const Win32ThreadConstructionInfo& threadConstructionInfo);
  69. ///cleanup/shutdown Libspe2
  70. virtual ~b3Win32ThreadSupport();
  71. void startThreads(const Win32ThreadConstructionInfo& threadInfo);
  72. ///send messages to SPUs
  73. virtual void runTask(int uiCommand, void* uiArgument0, int uiArgument1);
  74. ///check for messages from SPUs
  75. virtual void waitForResponse(int *puiArgument0, int *puiArgument1);
  76. virtual bool isTaskCompleted(int *puiArgument0, int *puiArgument1, int timeOutInMilliseconds);
  77. ///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
  78. virtual void startThreads();
  79. ///tell the task scheduler we are done with the SPU tasks
  80. virtual void stopThreads();
  81. virtual void setNumTasks(int numTasks)
  82. {
  83. m_maxNumTasks = numTasks;
  84. }
  85. virtual int getNumTasks() const
  86. {
  87. return m_maxNumTasks;
  88. }
  89. virtual void* getThreadLocalMemory(int taskId)
  90. {
  91. return m_activeThreadStatus[taskId].m_lsMemory;
  92. }
  93. virtual b3Barrier* createBarrier();
  94. virtual b3CriticalSection* createCriticalSection();
  95. virtual void deleteBarrier(b3Barrier* barrier);
  96. virtual void deleteCriticalSection(b3CriticalSection* criticalSection);
  97. };
  98. #endif //BT_WIN32_THREAD_SUPPORT_H