2 Bullet Continuous Collision Detection and Physics Library
\r
3 Copyright (c) 2010 Erwin Coumans http://bulletphysics.org
\r
5 This software is provided 'as-is', without any express or implied warranty.
\r
6 In no event will the authors be held liable for any damages arising from the use of this software.
\r
7 Permission is granted to anyone to use this software for any purpose,
\r
8 including commercial applications, and to alter it and redistribute it freely,
\r
9 subject to the following restrictions:
\r
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.
\r
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
\r
13 3. This notice may not be removed or altered from any source distribution.
\r
16 /// ThreadingDemo shows how to use the cross platform thread support interface.
\r
17 /// You can start threads and perform a blocking wait for completion
\r
18 /// Under Windows it uses Win32 Threads. On Mac and Linux it uses pthreads. On PlayStation 3 Cell SPU it uses SPURS.
\r
21 /// New: critical section/barriers and non-blocking pollingn for completion, currently Windows only
\r
23 void SampleThreadFunc(void* userPtr,void* lsMemory);
\r
24 void* SamplelsMemoryFunc();
\r
27 #include "BulletMultiThreaded/PlatformDefinitions.h"
\r
31 #include "BulletMultiThreaded/PosixThreadSupport.h"
\r
33 btThreadSupportInterface* createThreadSupport(int numThreads)
\r
35 PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads",
\r
39 btThreadSupportInterface* threadSupport = new PosixThreadSupport(constructionInfo);
\r
41 return threadSupport;
\r
46 #elif defined( _WIN32)
\r
47 #include "BulletMultiThreaded/Win32ThreadSupport.h"
\r
49 btThreadSupportInterface* createThreadSupport(int numThreads)
\r
51 Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,numThreads);
\r
52 Win32ThreadSupport* threadSupport = new Win32ThreadSupport(threadConstructionInfo);
\r
53 return threadSupport;
\r
66 btCriticalSection* m_cs;
\r
70 struct SampleThreadLocalStorage
\r
76 void SampleThreadFunc(void* userPtr,void* lsMemory)
\r
78 printf("thread started\n");
\r
80 SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*) lsMemory;
\r
82 SampleArgs* args = (SampleArgs*) userPtr;
\r
83 int workLeft = true;
\r
87 int count = args->m_cs->getSharedParam(0);
\r
88 args->m_cs->setSharedParam(0,count-1);
\r
89 args->m_cs->unlock();
\r
92 printf("thread %d processed number %d\n",localStorage->threadId, count);
\r
95 for (int i=0;i<1000000;i++)
\r
96 args->m_fakeWork = btScalar(1.21)*args->m_fakeWork;
\r
99 printf("finished\n");
\r
104 void* SamplelsMemoryFunc()
\r
106 //don't create local store memory, just return 0
\r
107 return new SampleThreadLocalStorage;
\r
119 int main(int argc,char** argv)
\r
121 int numThreads = 8;
\r
123 btThreadSupportInterface* threadSupport = createThreadSupport(numThreads);
\r
126 threadSupport->startSPU();
\r
128 for (int i=0;i<threadSupport->getNumTasks();i++)
\r
130 SampleThreadLocalStorage* storage = (SampleThreadLocalStorage*)threadSupport->getThreadLocalMemory(i);
\r
132 storage->threadId = i;
\r
137 args.m_cs = threadSupport->createCriticalSection();
\r
138 args.m_cs->setSharedParam(0,100);
\r
141 unsigned int arg0,arg1;
\r
143 for (i=0;i<numThreads;i++)
\r
145 threadSupport->sendRequest(1, (ppu_address_t) &args, i);
\r
148 bool blockingWait =true;
\r
151 for (i=0;i<numThreads;i++)
\r
153 threadSupport->waitForResponse(&arg0,&arg1);
\r
154 printf("finished waiting for response: %d %d\n", arg0,arg1);
\r
159 int numActiveThreads = numThreads;
\r
160 while (numActiveThreads)
\r
162 if (((Win32ThreadSupport*)threadSupport)->isTaskCompleted(&arg0,&arg1,0))
\r
164 numActiveThreads--;
\r
165 printf("numActiveThreads = %d\n",numActiveThreads);
\r
169 printf("polling\n");
\r
174 printf("non-blocking wait is not supported on this platform\n");
\r
179 printf("stopping threads\n");
\r
181 delete threadSupport;
\r
182 printf("Press ENTER to quit\n");
\r