b3PosixThreadSupport.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #ifndef B3_POSIX_THREAD_SUPPORT_H
  14. #define B3_POSIX_THREAD_SUPPORT_H
  15. #include "Bullet3Common/b3Scalar.h"
  16. #ifndef _XOPEN_SOURCE
  17. #define _XOPEN_SOURCE 600 //for definition of pthread_barrier_t, see http://pages.cs.wisc.edu/~travitch/pthreads_primer.html
  18. #endif //_XOPEN_SOURCE
  19. #include <pthread.h>
  20. #include <semaphore.h>
  21. #include "Bullet3Common/b3AlignedObjectArray.h"
  22. #include "b3ThreadSupportInterface.h"
  23. typedef void (*b3PosixThreadFunc)(void* userPtr, void* lsMemory);
  24. typedef void* (*b3PosixlsMemorySetupFunc)();
  25. typedef void (*b3PosixlsMemoryReleaseFunc)(void* ptr);
  26. // b3PosixThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
  27. class b3PosixThreadSupport : public b3ThreadSupportInterface
  28. {
  29. public:
  30. typedef enum sStatus
  31. {
  32. STATUS_BUSY,
  33. STATUS_READY,
  34. STATUS_FINISHED
  35. } Status;
  36. // placeholder, until libspe2 support is there
  37. struct b3ThreadStatus
  38. {
  39. int m_taskId;
  40. int m_commandId;
  41. int m_status;
  42. b3PosixThreadFunc m_userThreadFunc;
  43. void* m_userPtr; //for taskDesc etc
  44. b3PosixlsMemoryReleaseFunc m_lsMemoryReleaseFunc;
  45. void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
  46. pthread_t thread;
  47. //each tread will wait until this signal to start its work
  48. sem_t* startSemaphore;
  49. // this is a copy of m_mainSemaphore,
  50. //each tread will signal once it is finished with its work
  51. sem_t* m_mainSemaphore;
  52. unsigned long threadUsed;
  53. };
  54. private:
  55. b3AlignedObjectArray<b3ThreadStatus> m_activeThreadStatus;
  56. // m_mainSemaphoresemaphore will signal, if and how many threads are finished with their work
  57. sem_t* m_mainSemaphore;
  58. public:
  59. ///Setup and initialize SPU/CELL/Libspe2
  60. struct ThreadConstructionInfo
  61. {
  62. ThreadConstructionInfo(const char* uniqueName,
  63. b3PosixThreadFunc userThreadFunc,
  64. b3PosixlsMemorySetupFunc lsMemoryFunc,
  65. b3PosixlsMemoryReleaseFunc lsMemoryReleaseFunc,
  66. int numThreads = 1,
  67. int threadStackSize = 65535)
  68. : m_uniqueName(uniqueName),
  69. m_userThreadFunc(userThreadFunc),
  70. m_lsMemoryFunc(lsMemoryFunc),
  71. m_lsMemoryReleaseFunc(lsMemoryReleaseFunc),
  72. m_numThreads(numThreads),
  73. m_threadStackSize(threadStackSize)
  74. {
  75. }
  76. const char* m_uniqueName;
  77. b3PosixThreadFunc m_userThreadFunc;
  78. b3PosixlsMemorySetupFunc m_lsMemoryFunc;
  79. b3PosixlsMemoryReleaseFunc m_lsMemoryReleaseFunc;
  80. int m_numThreads;
  81. int m_threadStackSize;
  82. };
  83. b3PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo);
  84. ///cleanup/shutdown Libspe2
  85. virtual ~b3PosixThreadSupport();
  86. void startThreads(ThreadConstructionInfo& threadInfo);
  87. virtual void runTask(int uiCommand, void* uiArgument0, int uiArgument1);
  88. virtual void waitForResponse(int* puiArgument0, int* puiArgument1);
  89. ///tell the task scheduler we are done with the SPU tasks
  90. virtual void stopThreads();
  91. virtual void setNumTasks(int numTasks) {}
  92. virtual int getNumTasks() const
  93. {
  94. return m_activeThreadStatus.size();
  95. }
  96. ///non-blocking test if a task is completed. First implement all versions, and then enable this API
  97. virtual bool isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds);
  98. virtual b3Barrier* createBarrier();
  99. virtual b3CriticalSection* createCriticalSection();
  100. virtual void deleteBarrier(b3Barrier* barrier);
  101. virtual void deleteCriticalSection(b3CriticalSection* criticalSection);
  102. virtual void* getThreadLocalMemory(int taskId)
  103. {
  104. return m_activeThreadStatus[taskId].m_lsMemory;
  105. }
  106. };
  107. #endif // B3_POSIX_THREAD_SUPPORT_H