MultiThreadingExample.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. #include "MultiThreadingExample.h"
  2. #include "../CommonInterfaces/CommonGraphicsAppInterface.h"
  3. #include "../CommonInterfaces/CommonRenderInterface.h"
  4. #include "../CommonInterfaces/CommonExampleInterface.h"
  5. #include "LinearMath/btTransform.h"
  6. #include "../CommonInterfaces/CommonGUIHelperInterface.h"
  7. #include "../RenderingExamples/TimeSeriesCanvas.h"
  8. #include "stb_image/stb_image.h"
  9. #include "Bullet3Common/b3Quaternion.h"
  10. #include "Bullet3Common/b3Matrix3x3.h"
  11. #include "../CommonInterfaces/CommonParameterInterface.h"
  12. #include "LinearMath/btAlignedObjectArray.h"
  13. #define stdvector btAlignedObjectArray
  14. #define MAGIC_RESET_NUMBER 64738
  15. void SampleThreadFunc(void* userPtr,void* lsMemory);
  16. void* SamplelsMemoryFunc();
  17. #include <stdio.h>
  18. //#include "BulletMultiThreaded/PlatformDefinitions.h"
  19. #ifndef _WIN32
  20. #include "b3PosixThreadSupport.h"
  21. b3ThreadSupportInterface* createThreadSupport(int numThreads)
  22. {
  23. b3PosixThreadSupport::ThreadConstructionInfo constructionInfo("testThreads",
  24. SampleThreadFunc,
  25. SamplelsMemoryFunc,
  26. numThreads);
  27. b3ThreadSupportInterface* threadSupport = new b3PosixThreadSupport(constructionInfo);
  28. return threadSupport;
  29. }
  30. #elif defined( _WIN32)
  31. #include "b3Win32ThreadSupport.h"
  32. b3ThreadSupportInterface* createThreadSupport(int numThreads)
  33. {
  34. b3Win32ThreadSupport::Win32ThreadConstructionInfo threadConstructionInfo("testThreads",SampleThreadFunc,SamplelsMemoryFunc,numThreads);
  35. b3Win32ThreadSupport* threadSupport = new b3Win32ThreadSupport(threadConstructionInfo);
  36. return threadSupport;
  37. }
  38. #endif
  39. struct SampleJobInterface
  40. {
  41. virtual void executeJob(int threadIndex)=0;
  42. };
  43. struct SampleJob1 : public SampleJobInterface
  44. {
  45. float m_fakeWork;
  46. int m_jobId;
  47. SampleJob1(int jobId)
  48. :m_fakeWork(0),
  49. m_jobId(jobId)
  50. {
  51. }
  52. virtual ~SampleJob1() {}
  53. virtual void executeJob(int threadIndex)
  54. {
  55. printf("start SampleJob1 %d using threadIndex %d\n",m_jobId,threadIndex);
  56. //do some fake work
  57. for (int i=0;i<1000000;i++)
  58. m_fakeWork = b3Scalar(1.21)*m_fakeWork;
  59. printf("finished SampleJob1 %d using threadIndex %d\n",m_jobId,threadIndex);
  60. }
  61. };
  62. struct SampleArgs
  63. {
  64. SampleArgs()
  65. {
  66. }
  67. b3CriticalSection* m_cs;
  68. btAlignedObjectArray<SampleJobInterface*> m_jobQueue;
  69. void submitJob(SampleJobInterface* job)
  70. {
  71. m_cs->lock();
  72. m_jobQueue.push_back(job);
  73. m_cs->unlock();
  74. }
  75. SampleJobInterface* consumeJob()
  76. {
  77. SampleJobInterface* job = 0;
  78. m_cs->lock();
  79. int sz = m_jobQueue.size();
  80. if (sz)
  81. {
  82. job = m_jobQueue[sz-1];
  83. m_jobQueue.pop_back();
  84. }
  85. m_cs->unlock();
  86. return job;
  87. }
  88. };
  89. SampleArgs args;
  90. struct SampleThreadLocalStorage
  91. {
  92. int threadId;
  93. };
  94. void SampleThreadFunc(void* userPtr,void* lsMemory)
  95. {
  96. printf("SampleThreadFunc thread started\n");
  97. SampleThreadLocalStorage* localStorage = (SampleThreadLocalStorage*) lsMemory;
  98. SampleArgs* args = (SampleArgs*) userPtr;
  99. bool requestExit = false;
  100. while (!requestExit)
  101. {
  102. SampleJobInterface* job = args->consumeJob();
  103. if (job)
  104. {
  105. job->executeJob(localStorage->threadId);
  106. }
  107. args->m_cs->lock();
  108. int exitMagicNumber = args->m_cs->getSharedParam(1);
  109. requestExit = (exitMagicNumber==MAGIC_RESET_NUMBER);
  110. args->m_cs->unlock();
  111. }
  112. printf("finished\n");
  113. //do nothing
  114. }
  115. void* SamplelsMemoryFunc()
  116. {
  117. //don't create local store memory, just return 0
  118. return new SampleThreadLocalStorage;
  119. }
  120. class MultiThreadingExample : public CommonExampleInterface
  121. {
  122. CommonGraphicsApp* m_app;
  123. GUIHelperInterface* m_guiHelper;
  124. int m_exampleIndex;
  125. b3ThreadSupportInterface* m_threadSupport;
  126. btAlignedObjectArray<SampleJob1*> m_jobs;
  127. int m_numThreads;
  128. public:
  129. MultiThreadingExample(GUIHelperInterface* guiHelper, int tutorialIndex)
  130. :m_app(guiHelper->getAppInterface()),
  131. m_guiHelper(guiHelper),
  132. m_exampleIndex(tutorialIndex),
  133. m_threadSupport(0),
  134. m_numThreads(8)
  135. {
  136. int numBodies = 1;
  137. m_app->setUpAxis(1);
  138. m_app->m_renderer->enableBlend(true);
  139. }
  140. virtual ~MultiThreadingExample()
  141. {
  142. m_app->m_renderer->enableBlend(false);
  143. }
  144. virtual void renderScene()
  145. {
  146. }
  147. virtual void initPhysics()
  148. {
  149. b3Printf("initPhysics");
  150. m_threadSupport = createThreadSupport(m_numThreads);
  151. for (int i=0;i<m_threadSupport->getNumTasks();i++)
  152. {
  153. SampleThreadLocalStorage* storage = (SampleThreadLocalStorage*)m_threadSupport->getThreadLocalMemory(i);
  154. b3Assert(storage);
  155. storage->threadId = i;
  156. }
  157. args.m_cs = m_threadSupport->createCriticalSection();
  158. args.m_cs->setSharedParam(0,100);
  159. for (int i=0;i<100;i++)
  160. {
  161. SampleJob1* job = new SampleJob1(i);
  162. args.submitJob(job);
  163. }
  164. int i;
  165. for (i=0;i<m_numThreads;i++)
  166. {
  167. m_threadSupport->runTask(B3_THREAD_SCHEDULE_TASK, (void*) &args, i);
  168. }
  169. b3Printf("Threads started");
  170. }
  171. virtual void exitPhysics()
  172. {
  173. b3Printf("exitPhysics, stopping threads");
  174. bool blockingWait =false;
  175. int arg0,arg1;
  176. args.m_cs->lock();
  177. //terminate all threads
  178. args.m_cs->setSharedParam(1,MAGIC_RESET_NUMBER);
  179. args.m_cs->unlock();
  180. if (blockingWait)
  181. {
  182. for (int i=0;i<m_numThreads;i++)
  183. {
  184. m_threadSupport->waitForResponse(&arg0,&arg1);
  185. printf("finished waiting for response: %d %d\n", arg0,arg1);
  186. }
  187. } else
  188. {
  189. int numActiveThreads = m_numThreads;
  190. while (numActiveThreads)
  191. {
  192. if (m_threadSupport->isTaskCompleted(&arg0,&arg1,0))
  193. {
  194. numActiveThreads--;
  195. printf("numActiveThreads = %d\n",numActiveThreads);
  196. } else
  197. {
  198. // printf("polling..");
  199. }
  200. };
  201. }
  202. delete m_threadSupport;
  203. b3Printf("Threads stopped");
  204. for (int i=0;i<m_jobs.size();i++)
  205. {
  206. delete m_jobs[i];
  207. }
  208. m_jobs.clear();
  209. }
  210. virtual void stepSimulation(float deltaTime)
  211. {
  212. }
  213. virtual void physicsDebugDraw(int debugDrawFlags)
  214. {
  215. }
  216. virtual bool mouseMoveCallback(float x,float y)
  217. {
  218. return false;
  219. }
  220. virtual bool mouseButtonCallback(int button, int state, float x, float y)
  221. {
  222. return false;
  223. }
  224. virtual bool keyboardCallback(int key, int state)
  225. {
  226. return false;
  227. }
  228. virtual void resetCamera()
  229. {
  230. float dist = 10.5;
  231. float pitch = 136;
  232. float yaw = 32;
  233. float targetPos[3]={0,0,0};
  234. if (m_app->m_renderer && m_app->m_renderer->getActiveCamera())
  235. {
  236. m_app->m_renderer->getActiveCamera()->setCameraDistance(dist);
  237. m_app->m_renderer->getActiveCamera()->setCameraPitch(pitch);
  238. m_app->m_renderer->getActiveCamera()->setCameraYaw(yaw);
  239. m_app->m_renderer->getActiveCamera()->setCameraTargetPosition(targetPos[0],targetPos[1],targetPos[2]);
  240. }
  241. }
  242. };
  243. class CommonExampleInterface* MultiThreadingExampleCreateFunc(struct CommonExampleOptions& options)
  244. {
  245. return new MultiThreadingExample(options.m_guiHelper, options.m_option);
  246. }