b3Win32ThreadSupport.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. typedef void (*b3Win32lsMemoryReleaseFunc)(void*);
  21. ///b3Win32ThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
  22. class b3Win32ThreadSupport : public b3ThreadSupportInterface
  23. {
  24. public:
  25. ///placeholder, until libspe2 support is there
  26. struct b3ThreadStatus
  27. {
  28. int m_taskId;
  29. int m_commandId;
  30. int m_status;
  31. b3Win32ThreadFunc m_userThreadFunc;
  32. void* m_userPtr; //for taskDesc etc
  33. void* m_lsMemory; //initialized using Win32LocalStoreMemorySetupFunc
  34. b3Win32lsMemoryReleaseFunc m_lsMemoryReleaseFunc;
  35. void* m_threadHandle; //this one is calling 'Win32ThreadFunc'
  36. void* m_eventStartHandle;
  37. char m_eventStartHandleName[32];
  38. void* m_eventCompletetHandle;
  39. char m_eventCompletetHandleName[32];
  40. };
  41. private:
  42. b3AlignedObjectArray<b3ThreadStatus> m_activeThreadStatus;
  43. b3AlignedObjectArray<void*> m_completeHandles;
  44. int m_maxNumTasks;
  45. public:
  46. ///Setup and initialize SPU/CELL/Libspe2
  47. struct Win32ThreadConstructionInfo
  48. {
  49. Win32ThreadConstructionInfo(const char* uniqueName,
  50. b3Win32ThreadFunc userThreadFunc,
  51. b3Win32lsMemorySetupFunc lsMemoryFunc,
  52. b3Win32lsMemoryReleaseFunc lsMemoryReleaseFunc,
  53. int numThreads = 1,
  54. int threadStackSize = 65535)
  55. : m_uniqueName(uniqueName),
  56. m_userThreadFunc(userThreadFunc),
  57. m_lsMemoryFunc(lsMemoryFunc),
  58. m_lsMemoryReleaseFunc(lsMemoryReleaseFunc),
  59. m_numThreads(numThreads),
  60. m_threadStackSize(threadStackSize),
  61. m_priority(0)
  62. {
  63. }
  64. const char* m_uniqueName;
  65. b3Win32ThreadFunc m_userThreadFunc;
  66. b3Win32lsMemorySetupFunc m_lsMemoryFunc;
  67. b3Win32lsMemoryReleaseFunc m_lsMemoryReleaseFunc;
  68. int m_numThreads;
  69. int m_threadStackSize;
  70. int m_priority;
  71. };
  72. b3Win32ThreadSupport(const Win32ThreadConstructionInfo& threadConstructionInfo);
  73. ///cleanup/shutdown Libspe2
  74. virtual ~b3Win32ThreadSupport();
  75. void startThreads(const Win32ThreadConstructionInfo& threadInfo);
  76. ///send messages to SPUs
  77. virtual void runTask(int uiCommand, void* uiArgument0, int uiArgument1);
  78. ///check for messages from SPUs
  79. virtual void waitForResponse(int* puiArgument0, int* puiArgument1);
  80. virtual bool isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds);
  81. ///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
  82. virtual void startThreads();
  83. ///tell the task scheduler we are done with the SPU tasks
  84. virtual void stopThreads();
  85. virtual void setNumTasks(int numTasks)
  86. {
  87. m_maxNumTasks = numTasks;
  88. }
  89. virtual int getNumTasks() const
  90. {
  91. return m_maxNumTasks;
  92. }
  93. virtual void* getThreadLocalMemory(int taskId)
  94. {
  95. return m_activeThreadStatus[taskId].m_lsMemory;
  96. }
  97. virtual b3Barrier* createBarrier();
  98. virtual b3CriticalSection* createCriticalSection();
  99. virtual void deleteBarrier(b3Barrier* barrier);
  100. virtual void deleteCriticalSection(b3CriticalSection* criticalSection);
  101. };
  102. #endif //BT_WIN32_THREAD_SUPPORT_H