Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / PosixThreadSupport.h
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
33 #include "LinearMath/btAlignedObjectArray.h"
34
35 #include "btThreadSupportInterface.h"
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
42 class PosixThreadSupport : public btThreadSupportInterface 
43 {
44 public:
45     typedef enum sStatus {
46         STATUS_BUSY,
47         STATUS_READY,
48         STATUS_FINISHED
49     } Status;
50
51         // placeholder, until libspe2 support is there
52         struct  btSpuStatus
53         {
54                 uint32_t        m_taskId;
55                 uint32_t        m_commandId;
56                 uint32_t        m_status;
57
58                 PosixThreadFunc m_userThreadFunc;
59                 void*   m_userPtr; //for taskDesc etc
60                 void*   m_lsMemory; //initialized using PosixLocalStoreMemorySetupFunc
61
62                 pthread_t thread;
63                 sem_t* startSemaphore;
64
65         unsigned long threadUsed;
66         };
67 private:
68
69         btAlignedObjectArray<btSpuStatus>       m_activeSpuStatus;
70 public:
71         ///Setup and initialize SPU/CELL/Libspe2
72
73         
74
75         struct  ThreadConstructionInfo
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;
93                 PosixThreadFunc                 m_userThreadFunc;
94                 PosixlsMemorySetupFunc  m_lsMemoryFunc;
95                 int                                             m_numThreads;
96                 int                                             m_threadStackSize;
97
98         };
99
100         PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo);
101
102 ///cleanup/shutdown Libspe2
103         virtual ~PosixThreadSupport();
104
105         void    startThreads(ThreadConstructionInfo&    threadInfo);
106
107
108 ///send messages to SPUs
109         virtual void sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t uiArgument1);
110
111 ///check for messages from SPUs
112         virtual void waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1);
113
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();
116
117 ///tell the task scheduler we are done with the SPU tasks
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
129         virtual btCriticalSection* createCriticalSection();
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