Imported Upstream version 2.81
[platform/upstream/libbullet.git] / src / BulletMultiThreaded / SequentialThreadSupport.cpp
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 #include "SequentialThreadSupport.h"
17
18
19 #include "SpuCollisionTaskProcess.h"
20 #include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
21
22 SequentialThreadSupport::SequentialThreadSupport(SequentialThreadConstructionInfo& threadConstructionInfo)
23 {
24         startThreads(threadConstructionInfo);
25 }
26
27 ///cleanup/shutdown Libspe2
28 SequentialThreadSupport::~SequentialThreadSupport()
29 {
30         stopSPU();
31 }
32
33 #include <stdio.h>
34
35 ///send messages to SPUs
36 void SequentialThreadSupport::sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t taskId)
37 {
38         switch (uiCommand)
39         {
40         case    CMD_GATHER_AND_PROCESS_PAIRLIST:
41                 {
42                         btSpuStatus&    spuStatus = m_activeSpuStatus[0];
43                         spuStatus.m_userPtr=(void*)uiArgument0;
44                         spuStatus.m_userThreadFunc(spuStatus.m_userPtr,spuStatus.m_lsMemory);
45                 }
46         break;
47         default:
48                 {
49                         ///not implemented
50                         btAssert(0 && "Not implemented");
51                 }
52
53         };
54
55
56 }
57
58 ///check for messages from SPUs
59 void SequentialThreadSupport::waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1)
60 {
61         btAssert(m_activeSpuStatus.size());
62         btSpuStatus& spuStatus = m_activeSpuStatus[0];
63         *puiArgument0 = spuStatus.m_taskId;
64         *puiArgument1 = spuStatus.m_status;
65 }
66
67 void SequentialThreadSupport::startThreads(SequentialThreadConstructionInfo& threadConstructionInfo)
68 {
69         m_activeSpuStatus.resize(1);
70         printf("STS: Not starting any threads\n");
71         btSpuStatus& spuStatus = m_activeSpuStatus[0];
72         spuStatus.m_userPtr = 0;
73         spuStatus.m_taskId = 0;
74         spuStatus.m_commandId = 0;
75         spuStatus.m_status = 0;
76         spuStatus.m_lsMemory = threadConstructionInfo.m_lsMemoryFunc();
77         spuStatus.m_userThreadFunc = threadConstructionInfo.m_userThreadFunc;
78         printf("STS: Created local store at %p for task %s\n", spuStatus.m_lsMemory, threadConstructionInfo.m_uniqueName);
79 }
80
81 void SequentialThreadSupport::startSPU()
82 {
83 }
84
85 void SequentialThreadSupport::stopSPU()
86 {
87         m_activeSpuStatus.clear();
88 }
89
90 void SequentialThreadSupport::setNumTasks(int numTasks)
91 {
92         printf("SequentialThreadSupport::setNumTasks(%d) is not implemented and has no effect\n",numTasks);
93 }
94
95
96
97
98 class btDummyBarrier : public btBarrier
99 {
100 private:
101                 
102 public:
103         btDummyBarrier()
104         {
105         }
106         
107         virtual ~btDummyBarrier()
108         {
109         }
110         
111         void sync()
112         {
113         }
114         
115         virtual void setMaxCount(int n) {}
116         virtual int  getMaxCount() {return 1;}
117 };
118
119 class btDummyCriticalSection : public btCriticalSection
120 {
121         
122 public:
123         btDummyCriticalSection()
124         {
125         }
126         
127         virtual ~btDummyCriticalSection()
128         {
129         }
130         
131         unsigned int getSharedParam(int i)
132         {
133                 btAssert(i>=0&&i<31);
134                 return mCommonBuff[i+1];
135         }
136         
137         void setSharedParam(int i,unsigned int p)
138         {
139                 btAssert(i>=0&&i<31);
140                 mCommonBuff[i+1] = p;
141         }
142         
143         void lock()
144         {
145                 mCommonBuff[0] = 1;
146         }
147         
148         void unlock()
149         {
150                 mCommonBuff[0] = 0;
151         }
152 };
153
154
155
156
157 btBarrier*      SequentialThreadSupport::createBarrier()
158 {
159         return new btDummyBarrier();
160 }
161
162 btCriticalSection* SequentialThreadSupport::createCriticalSection()
163 {
164         return new btDummyCriticalSection();
165         
166 }
167
168 void SequentialThreadSupport::deleteBarrier(btBarrier* barrier)
169 {
170     delete barrier;
171 }
172
173 void SequentialThreadSupport::deleteCriticalSection(btCriticalSection* criticalSection)
174 {
175     delete criticalSection;
176 }
177
178
179
180
181