PosixThreadSupport.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 BT_POSIX_THREAD_SUPPORT_H
  14. #define BT_POSIX_THREAD_SUPPORT_H
  15. #include "bullet/LinearMath/btScalar.h"
  16. #include "PlatformDefinitions.h"
  17. #ifdef USE_PTHREADS //platform specifc defines are defined in PlatformDefinitions.h
  18. #ifndef _XOPEN_SOURCE
  19. #define _XOPEN_SOURCE 600 //for definition of pthread_barrier_t, see http://pages.cs.wisc.edu/~travitch/pthreads_primer.html
  20. #endif //_XOPEN_SOURCE
  21. #include <pthread.h>
  22. #include <semaphore.h>
  23. #include "bullet/LinearMath/btAlignedObjectArray.h"
  24. #include "btThreadSupportInterface.h"
  25. typedef void (*PosixThreadFunc)(void* userPtr,void* lsMemory);
  26. typedef void* (*PosixlsMemorySetupFunc)();
  27. // PosixThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
  28. class PosixThreadSupport : public btThreadSupportInterface
  29. {
  30. public:
  31. typedef enum sStatus {
  32. STATUS_BUSY,
  33. STATUS_READY,
  34. STATUS_FINISHED
  35. } Status;
  36. // placeholder, until libspe2 support is there
  37. struct btSpuStatus
  38. {
  39. uint32_t m_taskId;
  40. uint32_t m_commandId;
  41. uint32_t m_status;
  42. PosixThreadFunc m_userThreadFunc;
  43. void* m_userPtr; //for taskDesc etc
  44. void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
  45. pthread_t thread;
  46. sem_t* startSemaphore;
  47. unsigned long threadUsed;
  48. };
  49. private:
  50. btAlignedObjectArray<btSpuStatus> m_activeSpuStatus;
  51. public:
  52. ///Setup and initialize SPU/CELL/Libspe2
  53. struct ThreadConstructionInfo
  54. {
  55. ThreadConstructionInfo(const char* uniqueName,
  56. PosixThreadFunc userThreadFunc,
  57. PosixlsMemorySetupFunc lsMemoryFunc,
  58. int numThreads=1,
  59. int threadStackSize=65535
  60. )
  61. :m_uniqueName(uniqueName),
  62. m_userThreadFunc(userThreadFunc),
  63. m_lsMemoryFunc(lsMemoryFunc),
  64. m_numThreads(numThreads),
  65. m_threadStackSize(threadStackSize)
  66. {
  67. }
  68. const char* m_uniqueName;
  69. PosixThreadFunc m_userThreadFunc;
  70. PosixlsMemorySetupFunc m_lsMemoryFunc;
  71. int m_numThreads;
  72. int m_threadStackSize;
  73. };
  74. PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo);
  75. ///cleanup/shutdown Libspe2
  76. virtual ~PosixThreadSupport();
  77. void startThreads(ThreadConstructionInfo& threadInfo);
  78. ///send messages to SPUs
  79. virtual void sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t uiArgument1);
  80. ///check for messages from SPUs
  81. virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
  82. ///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
  83. virtual void startSPU();
  84. ///tell the task scheduler we are done with the SPU tasks
  85. virtual void stopSPU();
  86. virtual void setNumTasks(int numTasks) {}
  87. virtual int getNumTasks() const
  88. {
  89. return m_activeSpuStatus.size();
  90. }
  91. virtual btBarrier* createBarrier();
  92. virtual btCriticalSection* createCriticalSection();
  93. virtual void deleteBarrier(btBarrier* barrier);
  94. virtual void deleteCriticalSection(btCriticalSection* criticalSection);
  95. virtual void* getThreadLocalMemory(int taskId)
  96. {
  97. return m_activeSpuStatus[taskId].m_lsMemory;
  98. }
  99. };
  100. #endif // USE_PTHREADS
  101. #endif // BT_POSIX_THREAD_SUPPORT_H