b3PosixThreadSupport.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. #ifndef _WIN32
  2. /*
  3. Bullet Continuous Collision Detection and Physics Library
  4. Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
  5. This software is provided 'as-is', without any express or implied warranty.
  6. In no event will the authors be held liable for any damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it freely,
  9. subject to the following restrictions:
  10. 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.
  11. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  12. 3. This notice may not be removed or altered from any source distribution.
  13. */
  14. #include <stdio.h>
  15. #include "b3PosixThreadSupport.h"
  16. #include <errno.h>
  17. #include <unistd.h>
  18. #define checkPThreadFunction(returnValue) \
  19. if (0 != returnValue) \
  20. { \
  21. printf("PThread problem at line %i in file %s: %i %d\n", __LINE__, __FILE__, returnValue, errno); \
  22. }
  23. // The number of threads should be equal to the number of available cores
  24. // Todo: each worker should be linked to a single core, using SetThreadIdealProcessor.
  25. b3PosixThreadSupport::b3PosixThreadSupport(ThreadConstructionInfo& threadConstructionInfo)
  26. {
  27. startThreads(threadConstructionInfo);
  28. }
  29. // cleanup/shutdown Libspe2
  30. b3PosixThreadSupport::~b3PosixThreadSupport()
  31. {
  32. stopThreads();
  33. }
  34. #if (defined(__APPLE__))
  35. #define NAMED_SEMAPHORES
  36. #endif
  37. static sem_t* createSem(const char* baseName)
  38. {
  39. static int semCount = 0;
  40. #ifdef NAMED_SEMAPHORES
  41. /// Named semaphore begin
  42. char name[32];
  43. snprintf(name, 32, "/%8.s-%4.d-%4.4d", baseName, getpid(), semCount++);
  44. sem_t* tempSem = sem_open(name, O_CREAT, 0600, 0);
  45. if (tempSem != reinterpret_cast<sem_t*>(SEM_FAILED))
  46. {
  47. // printf("Created \"%s\" Semaphore %p\n", name, tempSem);
  48. }
  49. else
  50. {
  51. //printf("Error creating Semaphore %d\n", errno);
  52. exit(-1);
  53. }
  54. /// Named semaphore end
  55. #else
  56. sem_t* tempSem = new sem_t;
  57. checkPThreadFunction(sem_init(tempSem, 0, 0));
  58. #endif
  59. return tempSem;
  60. }
  61. static void destroySem(sem_t* semaphore)
  62. {
  63. #ifdef NAMED_SEMAPHORES
  64. checkPThreadFunction(sem_close(semaphore));
  65. #else
  66. checkPThreadFunction(sem_destroy(semaphore));
  67. delete semaphore;
  68. #endif
  69. }
  70. static void* threadFunction(void* argument)
  71. {
  72. b3PosixThreadSupport::b3ThreadStatus* status = (b3PosixThreadSupport::b3ThreadStatus*)argument;
  73. while (1)
  74. {
  75. checkPThreadFunction(sem_wait(status->startSemaphore));
  76. void* userPtr = status->m_userPtr;
  77. if (userPtr)
  78. {
  79. b3Assert(status->m_status);
  80. status->m_userThreadFunc(userPtr, status->m_lsMemory);
  81. status->m_status = 2;
  82. checkPThreadFunction(sem_post(status->m_mainSemaphore));
  83. status->threadUsed++;
  84. }
  85. else
  86. {
  87. //exit Thread
  88. status->m_status = 3;
  89. checkPThreadFunction(sem_post(status->m_mainSemaphore));
  90. printf("Thread with taskId %i exiting\n", status->m_taskId);
  91. break;
  92. }
  93. }
  94. printf("Thread TERMINATED\n");
  95. return 0;
  96. }
  97. ///send messages to SPUs
  98. void b3PosixThreadSupport::runTask(int uiCommand, void* uiArgument0, int taskId)
  99. {
  100. /// gMidphaseSPU.sendRequest(CMD_GATHER_AND_PROCESS_PAIRLIST, (int) &taskDesc);
  101. ///we should spawn an SPU task here, and in 'waitForResponse' it should wait for response of the (one of) the first tasks that finished
  102. switch (uiCommand)
  103. {
  104. case B3_THREAD_SCHEDULE_TASK:
  105. {
  106. b3ThreadStatus& spuStatus = m_activeThreadStatus[taskId];
  107. b3Assert(taskId >= 0);
  108. b3Assert(taskId < m_activeThreadStatus.size());
  109. spuStatus.m_commandId = uiCommand;
  110. spuStatus.m_status = 1;
  111. spuStatus.m_userPtr = (void*)uiArgument0;
  112. // fire event to start new task
  113. checkPThreadFunction(sem_post(spuStatus.startSemaphore));
  114. break;
  115. }
  116. default:
  117. {
  118. ///not implemented
  119. b3Assert(0);
  120. }
  121. };
  122. }
  123. ///non-blocking test if a task is completed. First implement all versions, and then enable this API
  124. bool b3PosixThreadSupport::isTaskCompleted(int* puiArgument0, int* puiArgument1, int timeOutInMilliseconds)
  125. {
  126. b3Assert(m_activeThreadStatus.size());
  127. // wait for any of the threads to finish
  128. int result = sem_trywait(m_mainSemaphore);
  129. if (result == 0)
  130. {
  131. // get at least one thread which has finished
  132. int last = -1;
  133. int status = -1;
  134. for (int t = 0; t < int(m_activeThreadStatus.size()); ++t)
  135. {
  136. status = m_activeThreadStatus[t].m_status;
  137. if (2 == m_activeThreadStatus[t].m_status)
  138. {
  139. last = t;
  140. break;
  141. }
  142. }
  143. b3ThreadStatus& spuStatus = m_activeThreadStatus[last];
  144. b3Assert(spuStatus.m_status > 1);
  145. spuStatus.m_status = 0;
  146. // need to find an active spu
  147. b3Assert(last >= 0);
  148. *puiArgument0 = spuStatus.m_taskId;
  149. *puiArgument1 = spuStatus.m_status;
  150. return true;
  151. }
  152. return false;
  153. }
  154. ///check for messages from SPUs
  155. void b3PosixThreadSupport::waitForResponse(int* puiArgument0, int* puiArgument1)
  156. {
  157. ///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
  158. ///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
  159. b3Assert(m_activeThreadStatus.size());
  160. // wait for any of the threads to finish
  161. checkPThreadFunction(sem_wait(m_mainSemaphore));
  162. // get at least one thread which has finished
  163. size_t last = -1;
  164. for (size_t t = 0; t < size_t(m_activeThreadStatus.size()); ++t)
  165. {
  166. if (2 == m_activeThreadStatus[t].m_status)
  167. {
  168. last = t;
  169. break;
  170. }
  171. }
  172. b3ThreadStatus& spuStatus = m_activeThreadStatus[last];
  173. b3Assert(spuStatus.m_status > 1);
  174. spuStatus.m_status = 0;
  175. // need to find an active spu
  176. b3Assert(last >= 0);
  177. *puiArgument0 = spuStatus.m_taskId;
  178. *puiArgument1 = spuStatus.m_status;
  179. }
  180. void b3PosixThreadSupport::startThreads(ThreadConstructionInfo& threadConstructionInfo)
  181. {
  182. printf("%s creating %i threads.\n", __FUNCTION__, threadConstructionInfo.m_numThreads);
  183. m_activeThreadStatus.resize(threadConstructionInfo.m_numThreads);
  184. m_mainSemaphore = createSem("main");
  185. //checkPThreadFunction(sem_wait(mainSemaphore));
  186. for (int i = 0; i < threadConstructionInfo.m_numThreads; i++)
  187. {
  188. printf("starting thread %d\n", i);
  189. b3ThreadStatus& spuStatus = m_activeThreadStatus[i];
  190. spuStatus.startSemaphore = createSem("threadLocal");
  191. checkPThreadFunction(pthread_create(&spuStatus.thread, NULL, &threadFunction, (void*)&spuStatus));
  192. spuStatus.m_userPtr = 0;
  193. spuStatus.m_taskId = i;
  194. spuStatus.m_commandId = 0;
  195. spuStatus.m_status = 0;
  196. spuStatus.m_mainSemaphore = m_mainSemaphore;
  197. spuStatus.m_lsMemory = threadConstructionInfo.m_lsMemoryFunc();
  198. spuStatus.m_userThreadFunc = threadConstructionInfo.m_userThreadFunc;
  199. spuStatus.m_lsMemoryReleaseFunc = threadConstructionInfo.m_lsMemoryReleaseFunc;
  200. spuStatus.threadUsed = 0;
  201. printf("started thread %d \n", i);
  202. }
  203. }
  204. ///tell the task scheduler we are done with the SPU tasks
  205. void b3PosixThreadSupport::stopThreads()
  206. {
  207. for (size_t t = 0; t < size_t(m_activeThreadStatus.size()); ++t)
  208. {
  209. b3ThreadStatus& spuStatus = m_activeThreadStatus[t];
  210. // printf("%s: Thread %i used: %ld\n", __FUNCTION__, int(t), spuStatus.threadUsed);
  211. spuStatus.m_userPtr = 0;
  212. checkPThreadFunction(sem_post(spuStatus.startSemaphore));
  213. checkPThreadFunction(sem_wait(m_mainSemaphore));
  214. printf("destroy semaphore\n");
  215. destroySem(spuStatus.startSemaphore);
  216. printf("semaphore destroyed\n");
  217. checkPThreadFunction(pthread_join(spuStatus.thread, 0));
  218. if (spuStatus.m_lsMemoryReleaseFunc)
  219. {
  220. spuStatus.m_lsMemoryReleaseFunc(spuStatus.m_lsMemory);
  221. }
  222. }
  223. printf("destroy main semaphore\n");
  224. destroySem(m_mainSemaphore);
  225. printf("main semaphore destroyed\n");
  226. m_activeThreadStatus.clear();
  227. }
  228. class b3PosixCriticalSection : public b3CriticalSection
  229. {
  230. pthread_mutex_t m_mutex;
  231. public:
  232. b3PosixCriticalSection()
  233. {
  234. pthread_mutex_init(&m_mutex, NULL);
  235. }
  236. virtual ~b3PosixCriticalSection()
  237. {
  238. pthread_mutex_destroy(&m_mutex);
  239. }
  240. B3_ATTRIBUTE_ALIGNED16(unsigned int mCommonBuff[32]);
  241. virtual unsigned int getSharedParam(int i)
  242. {
  243. if (i < 32)
  244. {
  245. return mCommonBuff[i];
  246. }
  247. else
  248. {
  249. b3Assert(0);
  250. }
  251. return 0;
  252. }
  253. virtual void setSharedParam(int i, unsigned int p)
  254. {
  255. if (i < 32)
  256. {
  257. mCommonBuff[i] = p;
  258. }
  259. else
  260. {
  261. b3Assert(0);
  262. }
  263. }
  264. virtual void lock()
  265. {
  266. pthread_mutex_lock(&m_mutex);
  267. }
  268. virtual void unlock()
  269. {
  270. pthread_mutex_unlock(&m_mutex);
  271. }
  272. };
  273. #if defined(_POSIX_BARRIERS) && (_POSIX_BARRIERS - 20012L) >= 0
  274. /* OK to use barriers on this platform */
  275. class b3PosixBarrier : public b3Barrier
  276. {
  277. pthread_barrier_t m_barr;
  278. int m_numThreads;
  279. public:
  280. b3PosixBarrier()
  281. : m_numThreads(0) {}
  282. virtual ~b3PosixBarrier()
  283. {
  284. pthread_barrier_destroy(&m_barr);
  285. }
  286. virtual void sync()
  287. {
  288. int rc = pthread_barrier_wait(&m_barr);
  289. if (rc != 0 && rc != PTHREAD_BARRIER_SERIAL_THREAD)
  290. {
  291. printf("Could not wait on barrier\n");
  292. exit(-1);
  293. }
  294. }
  295. virtual void setMaxCount(int numThreads)
  296. {
  297. int result = pthread_barrier_init(&m_barr, NULL, numThreads);
  298. m_numThreads = numThreads;
  299. b3Assert(result == 0);
  300. }
  301. virtual int getMaxCount()
  302. {
  303. return m_numThreads;
  304. }
  305. };
  306. #else
  307. /* Not OK to use barriers on this platform - insert alternate code here */
  308. class b3PosixBarrier : public b3Barrier
  309. {
  310. pthread_mutex_t m_mutex;
  311. pthread_cond_t m_cond;
  312. int m_numThreads;
  313. int m_called;
  314. public:
  315. b3PosixBarrier()
  316. : m_numThreads(0)
  317. {
  318. }
  319. virtual ~b3PosixBarrier()
  320. {
  321. if (m_numThreads > 0)
  322. {
  323. pthread_mutex_destroy(&m_mutex);
  324. pthread_cond_destroy(&m_cond);
  325. }
  326. }
  327. virtual void sync()
  328. {
  329. pthread_mutex_lock(&m_mutex);
  330. m_called++;
  331. if (m_called == m_numThreads)
  332. {
  333. m_called = 0;
  334. pthread_cond_broadcast(&m_cond);
  335. }
  336. else
  337. {
  338. pthread_cond_wait(&m_cond, &m_mutex);
  339. }
  340. pthread_mutex_unlock(&m_mutex);
  341. }
  342. virtual void setMaxCount(int numThreads)
  343. {
  344. if (m_numThreads > 0)
  345. {
  346. pthread_mutex_destroy(&m_mutex);
  347. pthread_cond_destroy(&m_cond);
  348. }
  349. m_called = 0;
  350. pthread_mutex_init(&m_mutex, NULL);
  351. pthread_cond_init(&m_cond, NULL);
  352. m_numThreads = numThreads;
  353. }
  354. virtual int getMaxCount()
  355. {
  356. return m_numThreads;
  357. }
  358. };
  359. #endif //_POSIX_BARRIERS
  360. b3Barrier* b3PosixThreadSupport::createBarrier()
  361. {
  362. b3PosixBarrier* barrier = new b3PosixBarrier();
  363. barrier->setMaxCount(getNumTasks());
  364. return barrier;
  365. }
  366. b3CriticalSection* b3PosixThreadSupport::createCriticalSection()
  367. {
  368. return new b3PosixCriticalSection();
  369. }
  370. void b3PosixThreadSupport::deleteBarrier(b3Barrier* barrier)
  371. {
  372. delete barrier;
  373. }
  374. void b3PosixThreadSupport::deleteCriticalSection(b3CriticalSection* cs)
  375. {
  376. delete cs;
  377. }
  378. #endif //_WIN32