2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
16 #ifndef BT_POSIX_THREAD_SUPPORT_H
17 #define BT_POSIX_THREAD_SUPPORT_H
20 #include "LinearMath/btScalar.h"
21 #include "PlatformDefinitions.h"
23 #ifdef USE_PTHREADS //platform specifc defines are defined in PlatformDefinitions.h
26 #define _XOPEN_SOURCE 600 //for definition of pthread_barrier_t, see http://pages.cs.wisc.edu/~travitch/pthreads_primer.html
27 #endif //_XOPEN_SOURCE
29 #include <semaphore.h>
33 #include "LinearMath/btAlignedObjectArray.h"
35 #include "btThreadSupportInterface.h"
38 typedef void (*PosixThreadFunc)(void* userPtr,void* lsMemory);
39 typedef void* (*PosixlsMemorySetupFunc)();
41 // PosixThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
42 class PosixThreadSupport : public btThreadSupportInterface
45 typedef enum sStatus {
51 // placeholder, until libspe2 support is there
58 PosixThreadFunc m_userThreadFunc;
59 void* m_userPtr; //for taskDesc etc
60 void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
63 sem_t* startSemaphore;
65 unsigned long threadUsed;
69 btAlignedObjectArray<btSpuStatus> m_activeSpuStatus;
71 ///Setup and initialize SPU/CELL/Libspe2
75 struct ThreadConstructionInfo
77 ThreadConstructionInfo(const char* uniqueName,
78 PosixThreadFunc userThreadFunc,
79 PosixlsMemorySetupFunc lsMemoryFunc,
81 int threadStackSize=65535
83 :m_uniqueName(uniqueName),
84 m_userThreadFunc(userThreadFunc),
85 m_lsMemoryFunc(lsMemoryFunc),
86 m_numThreads(numThreads),
87 m_threadStackSize(threadStackSize)
92 const char* m_uniqueName;
93 PosixThreadFunc m_userThreadFunc;
94 PosixlsMemorySetupFunc m_lsMemoryFunc;
96 int m_threadStackSize;
100 PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo);
102 ///cleanup/shutdown Libspe2
103 virtual ~PosixThreadSupport();
105 void startThreads(ThreadConstructionInfo& threadInfo);
108 ///send messages to SPUs
109 virtual void sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t uiArgument1);
111 ///check for messages from SPUs
112 virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
114 ///start the spus (can be called at the beginning of each frame, to make sure that the right SPU program is loaded)
115 virtual void startSPU();
117 ///tell the task scheduler we are done with the SPU tasks
118 virtual void stopSPU();
120 virtual void setNumTasks(int numTasks) {}
122 virtual int getNumTasks() const
124 return m_activeSpuStatus.size();
127 virtual btBarrier* createBarrier();
129 virtual btCriticalSection* createCriticalSection();
131 virtual void deleteBarrier(btBarrier* barrier);
133 virtual void deleteCriticalSection(btCriticalSection* criticalSection);
136 virtual void* getThreadLocalMemory(int taskId)
138 return m_activeSpuStatus[taskId].m_lsMemory;
143 #endif // USE_PTHREADS
145 #endif // BT_POSIX_THREAD_SUPPORT_H