b3PosixThreadSupport.cpp 11 KB

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