Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics/src/bullet/BulletMultiThreaded/PosixThreadSupport.cpp @ 1963

Last change on this file since 1963 was 1963, checked in by rgrieder, 15 years ago

Added Bullet physics engine.

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2007 Erwin Coumans  http://bulletphysics.com
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. 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.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#include <stdio.h>
17#include "PosixThreadSupport.h"
18
19
20#ifdef USE_PTHREADS
21
22#include "SpuCollisionTaskProcess.h"
23#include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
24
25#define checkPThreadFunction(returnValue) \
26    if(0 != returnValue) { \
27        printf("PThread problem at line %i in file %s: %i\n", __LINE__, __FILE__, returnValue); \
28    }
29
30// The number of threads should be equal to the number of available cores
31// Todo: each worker should be linked to a single core, using SetThreadIdealProcessor.
32
33// PosixThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
34// Setup and initialize SPU/CELL/Libspe2
35PosixThreadSupport::PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo)
36{
37        startThreads(threadConstructionInfo);
38}
39
40// cleanup/shutdown Libspe2
41PosixThreadSupport::~PosixThreadSupport()
42{
43        stopSPU();
44}
45
46// this semaphore will signal, if and how many threads are finished with their work
47static sem_t mainSemaphore;
48
49static void *threadFunction(void *argument) 
50{
51
52        PosixThreadSupport::btSpuStatus* status = (PosixThreadSupport::btSpuStatus*)argument;
53
54       
55        while (1)
56        {
57            checkPThreadFunction(sem_wait(&status->startSemaphore));
58               
59                void* userPtr = status->m_userPtr;
60
61                if (userPtr)
62                {
63                        btAssert(status->m_status);
64                        status->m_userThreadFunc(userPtr,status->m_lsMemory);
65                        status->m_status = 2;
66                        checkPThreadFunction(sem_post(&mainSemaphore));
67
68            status->threadUsed++;
69                } else {
70                        //exit Thread
71                        status->m_status = 3;
72                        checkPThreadFunction(sem_post(&mainSemaphore));
73                        printf("Thread with taskId %i exiting\n",status->m_taskId);
74                        break;
75                }
76               
77        }
78
79        printf("Thread TERMINATED\n");
80        return 0;
81
82}
83
84///send messages to SPUs
85void PosixThreadSupport::sendRequest(uint32_t uiCommand, uint32_t uiArgument0, uint32_t taskId)
86{
87        ///     gMidphaseSPU.sendRequest(CMD_GATHER_AND_PROCESS_PAIRLIST, (uint32_t) &taskDesc);
88       
89        ///we should spawn an SPU task here, and in 'waitForResponse' it should wait for response of the (one of) the first tasks that finished
90       
91
92
93        switch (uiCommand)
94        {
95        case    CMD_GATHER_AND_PROCESS_PAIRLIST:
96                {
97                        btSpuStatus&    spuStatus = m_activeSpuStatus[taskId];
98                        btAssert(taskId >= 0);
99                        btAssert(taskId < m_activeSpuStatus.size());
100
101                        spuStatus.m_commandId = uiCommand;
102                        spuStatus.m_status = 1;
103                        spuStatus.m_userPtr = (void*)uiArgument0;
104
105                        // fire event to start new task
106            checkPThreadFunction(sem_post(&spuStatus.startSemaphore));
107                        break;
108                }
109        default:
110                {
111                        ///not implemented
112                        btAssert(0);
113                }
114
115        };
116
117
118}
119
120
121///check for messages from SPUs
122void PosixThreadSupport::waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1)
123{
124        ///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
125       
126        ///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
127
128
129        btAssert(m_activeSpuStatus.size());
130
131        // wait for any of the threads to finish
132        checkPThreadFunction(sem_wait(&mainSemaphore));
133       
134        // get at least one thread which has finished
135        size_t last = -1;
136       
137        for(size_t t=0; t < m_activeSpuStatus.size(); ++t) {
138            if(2 == m_activeSpuStatus[t].m_status) {
139                last = t;
140                break;
141            }
142        }
143
144        btSpuStatus& spuStatus = m_activeSpuStatus[last];
145
146        btAssert(spuStatus.m_status > 1);
147        spuStatus.m_status = 0;
148
149        // need to find an active spu
150        btAssert(last >= 0);
151
152        *puiArgument0 = spuStatus.m_taskId;
153        *puiArgument1 = spuStatus.m_status;
154}
155
156
157
158void PosixThreadSupport::startThreads(ThreadConstructionInfo& threadConstructionInfo)
159{
160        printf("%s creating %i threads.\n", __FUNCTION__, threadConstructionInfo.m_numThreads);
161        m_activeSpuStatus.resize(threadConstructionInfo.m_numThreads);
162       
163        checkPThreadFunction(sem_init(&mainSemaphore, 0, 0));
164
165        for (int i=0;i < threadConstructionInfo.m_numThreads;i++)
166        {
167                printf("starting thread %d\n",i);
168
169                btSpuStatus&    spuStatus = m_activeSpuStatus[i];
170               
171                checkPThreadFunction(sem_init(&spuStatus.startSemaphore, 0, 0));
172                checkPThreadFunction(pthread_create(&spuStatus.thread, NULL, &threadFunction, (void*)&spuStatus));
173
174                spuStatus.m_userPtr=0;
175
176                spuStatus.m_taskId = i;
177                spuStatus.m_commandId = 0;
178                spuStatus.m_status = 0;
179                spuStatus.m_lsMemory = threadConstructionInfo.m_lsMemoryFunc();
180                spuStatus.m_userThreadFunc = threadConstructionInfo.m_userThreadFunc;
181        spuStatus.threadUsed = 0;
182
183                printf("started thread %d \n",i);
184               
185        }
186
187}
188
189void PosixThreadSupport::startSPU()
190{
191}
192
193
194///tell the task scheduler we are done with the SPU tasks
195void PosixThreadSupport::stopSPU()
196{
197        for(size_t t=0; t < m_activeSpuStatus.size(); ++t) {
198            btSpuStatus&        spuStatus = m_activeSpuStatus[t];
199        printf("%s: Thread %i used: %ld\n", __FUNCTION__, t, spuStatus.threadUsed);
200       
201       
202            checkPThreadFunction(sem_destroy(&spuStatus.startSemaphore));
203            checkPThreadFunction(pthread_cancel(spuStatus.thread));
204        }
205        checkPThreadFunction(sem_destroy(&mainSemaphore));
206
207        m_activeSpuStatus.clear();
208}
209
210#endif // USE_PTHREADS
211
Note: See TracBrowser for help on using the repository browser.