Bullet Collision Detection & Physics Library
PosixThreadSupport.h
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
4 
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:
10 
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.
14 */
15 
16 #ifndef BT_POSIX_THREAD_SUPPORT_H
17 #define BT_POSIX_THREAD_SUPPORT_H
18 
19 
20 #include "LinearMath/btScalar.h"
21 #include "PlatformDefinitions.h"
22 
23 #ifdef USE_PTHREADS //platform specifc defines are defined in PlatformDefinitions.h
24 
25 #ifndef _XOPEN_SOURCE
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
28 #include <pthread.h>
29 #include <semaphore.h>
30 
31 
32 
34 
36 
37 
38 typedef void (*PosixThreadFunc)(void* userPtr,void* lsMemory);
39 typedef void* (*PosixlsMemorySetupFunc)();
40 
41 // PosixThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
43 {
44 public:
45  typedef enum sStatus {
49  } Status;
50 
51  // placeholder, until libspe2 support is there
52  struct btSpuStatus
53  {
57 
59  void* m_userPtr; //for taskDesc etc
60  void* m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
61 
62  pthread_t thread;
64 
65  unsigned long threadUsed;
66  };
67 private:
68 
70 public:
72 
73 
74 
76  {
77  ThreadConstructionInfo(const char* uniqueName,
78  PosixThreadFunc userThreadFunc,
79  PosixlsMemorySetupFunc lsMemoryFunc,
80  int numThreads=1,
81  int threadStackSize=65535
82  )
83  :m_uniqueName(uniqueName),
84  m_userThreadFunc(userThreadFunc),
85  m_lsMemoryFunc(lsMemoryFunc),
86  m_numThreads(numThreads),
87  m_threadStackSize(threadStackSize)
88  {
89 
90  }
91 
92  const char* m_uniqueName;
97 
98  };
99 
100  PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo);
101 
103  virtual ~PosixThreadSupport();
104 
105  void startThreads(ThreadConstructionInfo& threadInfo);
106 
107 
109  virtual void sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t uiArgument1);
110 
112  virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
113 
115  virtual void startSPU();
116 
118  virtual void stopSPU();
119 
120  virtual void setNumTasks(int numTasks) {}
121 
122  virtual int getNumTasks() const
123  {
124  return m_activeSpuStatus.size();
125  }
126 
127  virtual btBarrier* createBarrier();
128 
130 
131  virtual void deleteBarrier(btBarrier* barrier);
132 
133  virtual void deleteCriticalSection(btCriticalSection* criticalSection);
134 
135 
136  virtual void* getThreadLocalMemory(int taskId)
137  {
138  return m_activeSpuStatus[taskId].m_lsMemory;
139  }
140 
141 };
142 
143 #endif // USE_PTHREADS
144 
145 #endif // BT_POSIX_THREAD_SUPPORT_H
146 
147 
virtual btCriticalSection * createCriticalSection()
void startThreads(ThreadConstructionInfo &threadInfo)
The btAlignedObjectArray template class uses a subset of the stl::vector interface for its methods It...
uint32_t ppu_address_t
virtual void deleteBarrier(btBarrier *barrier)
virtual btBarrier * createBarrier()
virtual ~PosixThreadSupport()
cleanup/shutdown Libspe2
void(* PosixThreadFunc)(void *userPtr, void *lsMemory)
virtual void * getThreadLocalMemory(int taskId)
void *(* PosixlsMemorySetupFunc)()
unsigned int uint32_t
virtual int getNumTasks() const
enum PosixThreadSupport::sStatus Status
virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1)
check for messages from SPUs
PosixThreadSupport(ThreadConstructionInfo &threadConstructionInfo)
btAlignedObjectArray< btSpuStatus > m_activeSpuStatus
ThreadConstructionInfo(const char *uniqueName, PosixThreadFunc userThreadFunc, PosixlsMemorySetupFunc lsMemoryFunc, int numThreads=1, int threadStackSize=65535)
virtual void stopSPU()
tell the task scheduler we are done with the SPU tasks
Setup and initialize SPU/CELL/Libspe2.
virtual void deleteCriticalSection(btCriticalSection *criticalSection)
static void barrier(unsigned int a)
virtual void sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t uiArgument1)
send messages to SPUs
virtual void setNumTasks(int numTasks)
tell the task scheduler to use no more than numTasks tasks
virtual void startSPU()
start the spus (can be called at the beginning of each frame, to make sure that the right SPU program...