Imported Upstream version 2.81
[platform/upstream/libbullet.git] / Demos / ThreadingDemo / main.cpp
1 /*\r
2 Bullet Continuous Collision Detection and Physics Library\r
3 Copyright (c) 2010 Erwin Coumans  http://bulletphysics.org\r
4 \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
10 \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
14 */\r
15 \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
19 \r
20 /// June 2010 \r
21 /// New: critical section/barriers and non-blocking pollingn for completion, currently Windows only\r
22 \r
23 void    SampleThreadFunc(void* userPtr,void* lsMemory);\r
24 void*   SamplelsMemoryFunc();\r
25 \r
26 #include <stdio.h>\r
27 #include "BulletMultiThreaded/PlatformDefinitions.h"\r
28 \r
29 #ifdef USE_PTHREADS\r
30 //#ifdef __APPLE__\r
31 #include "BulletMultiThreaded/PosixThreadSupport.h"\r
32 \r
33 btThreadSupportInterface* createThreadSupport(int numThreads)\r
34 {\r
35         PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads",\r
36                                                                 SampleThreadFunc,\r
37                                                                 SamplelsMemoryFunc,\r
38                                                                 numThreads);\r
39     btThreadSupportInterface* threadSupport = new PosixThreadSupport(constructionInfo);\r
40         \r
41         return threadSupport;\r
42         \r
43 }\r
44 \r
45 \r
46 #elif defined( _WIN32)\r
47 #include "BulletMultiThreaded/Win32ThreadSupport.h"\r
48 \r
49 btThreadSupportInterface* createThreadSupport(int numThreads)\r
50 {\r
51         Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,numThreads);\r
52         Win32ThreadSupport* threadSupport = new Win32ThreadSupport(threadConstructionInfo);\r
53         return threadSupport;\r
54         \r
55 }\r
56 \r
57 #endif\r
58 \r
59 \r
60 struct  SampleArgs\r
61 {\r
62         SampleArgs()\r
63                 :m_fakeWork(1)\r
64         {\r
65         }\r
66         btCriticalSection* m_cs;\r
67         float m_fakeWork;\r
68 };\r
69 \r
70 struct SampleThreadLocalStorage\r
71 {\r
72         int threadId;\r
73 };\r
74 \r
75 \r
76 void    SampleThreadFunc(void* userPtr,void* lsMemory)\r
77 {\r
78         printf("thread started\n");\r
79 \r
80         SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*) lsMemory;\r
81 \r
82         SampleArgs* args = (SampleArgs*) userPtr;\r
83         int workLeft = true;\r
84         while (workLeft)\r
85         {\r
86                 args->m_cs->lock();\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
90                 if (count>0)\r
91                 {\r
92                         printf("thread %d processed number %d\n",localStorage->threadId, count);\r
93                 }\r
94                 //do some fake work\r
95                 for (int i=0;i<1000000;i++)\r
96                         args->m_fakeWork = btScalar(1.21)*args->m_fakeWork;\r
97                 workLeft = count>0;\r
98         }\r
99         printf("finished\n");\r
100         //do nothing\r
101 }\r
102 \r
103 \r
104 void*   SamplelsMemoryFunc()\r
105 {\r
106         //don't create local store memory, just return 0\r
107         return new SampleThreadLocalStorage;\r
108 }\r
109 \r
110 \r
111 \r
112 \r
113 \r
114 \r
115 \r
116 \r
117 \r
118 \r
119 int main(int argc,char** argv)\r
120 {\r
121         int numThreads = 8;\r
122 \r
123         btThreadSupportInterface* threadSupport = createThreadSupport(numThreads);\r
124 \r
125         \r
126         threadSupport->startSPU();\r
127 \r
128         for (int i=0;i<threadSupport->getNumTasks();i++)\r
129         {\r
130                 SampleThreadLocalStorage* storage = (SampleThreadLocalStorage*)threadSupport->getThreadLocalMemory(i);\r
131                 btAssert(storage);\r
132                 storage->threadId = i;\r
133         }\r
134         \r
135 \r
136         SampleArgs      args;\r
137         args.m_cs = threadSupport->createCriticalSection();\r
138         args.m_cs->setSharedParam(0,100);\r
139 \r
140         \r
141                 unsigned int arg0,arg1;\r
142         int i;\r
143         for (i=0;i<numThreads;i++)\r
144         {\r
145                 threadSupport->sendRequest(1, (ppu_address_t) &args, i);\r
146         }\r
147 \r
148         bool blockingWait =true;\r
149         if (blockingWait)\r
150         {\r
151                 for (i=0;i<numThreads;i++)\r
152                 {\r
153                         threadSupport->waitForResponse(&arg0,&arg1);\r
154                         printf("finished waiting for response: %d %d\n", arg0,arg1);\r
155                 }\r
156         } else\r
157         {\r
158 #if _WIN32\r
159                 int numActiveThreads = numThreads;\r
160                 while (numActiveThreads)\r
161                 {\r
162                         if (((Win32ThreadSupport*)threadSupport)->isTaskCompleted(&arg0,&arg1,0))\r
163                         {\r
164                                 numActiveThreads--;\r
165                                 printf("numActiveThreads = %d\n",numActiveThreads);\r
166 \r
167                         } else\r
168                         {\r
169                                 printf("polling\n");\r
170                         }\r
171                 };\r
172 #else\r
173         btAssert(0);\r
174         printf("non-blocking wait is not supported on this platform\n");\r
175         exit(0);\r
176 #endif\r
177         }\r
178 \r
179 printf("stopping threads\n");\r
180 \r
181         delete threadSupport;\r
182         printf("Press ENTER to quit\n");\r
183         getchar();\r
184         return 0;\r
185 }\r