b3PosixThreadSupport.h 4.2 KB

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