MultiThreadingExample.cpp 6.6 KB

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