b3Win32ThreadSupport.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. #ifdef _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 "b3Win32ThreadSupport.h"
  15. #include <windows.h>
  16. ///The number of threads should be equal to the number of available cores
  17. ///@todo: each worker should be linked to a single core, using SetThreadIdealProcessor.
  18. ///b3Win32ThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
  19. ///Setup and initialize SPU/CELL/Libspe2
  20. b3Win32ThreadSupport::b3Win32ThreadSupport(const Win32ThreadConstructionInfo & threadConstructionInfo)
  21. {
  22. m_maxNumTasks = threadConstructionInfo.m_numThreads;
  23. startThreads(threadConstructionInfo);
  24. }
  25. ///cleanup/shutdown Libspe2
  26. b3Win32ThreadSupport::~b3Win32ThreadSupport()
  27. {
  28. stopThreads();
  29. }
  30. #include <stdio.h>
  31. DWORD WINAPI Thread_no_1( LPVOID lpParam )
  32. {
  33. b3Win32ThreadSupport::b3ThreadStatus* status = (b3Win32ThreadSupport::b3ThreadStatus*)lpParam;
  34. while (1)
  35. {
  36. WaitForSingleObject(status->m_eventStartHandle,INFINITE);
  37. void* userPtr = status->m_userPtr;
  38. if (userPtr)
  39. {
  40. b3Assert(status->m_status);
  41. status->m_userThreadFunc(userPtr,status->m_lsMemory);
  42. status->m_status = 2;
  43. SetEvent(status->m_eventCompletetHandle);
  44. } else
  45. {
  46. //exit Thread
  47. status->m_status = 3;
  48. printf("Thread with taskId %i with handle %p exiting\n",status->m_taskId, status->m_threadHandle);
  49. SetEvent(status->m_eventCompletetHandle);
  50. break;
  51. }
  52. }
  53. printf("Thread TERMINATED\n");
  54. return 0;
  55. }
  56. ///send messages to SPUs
  57. void b3Win32ThreadSupport::runTask(int uiCommand, void* uiArgument0, int taskId)
  58. {
  59. /// gMidphaseSPU.sendRequest(CMD_GATHER_AND_PROCESS_PAIRLIST, (void*) &taskDesc);
  60. ///we should spawn an SPU task here, and in 'waitForResponse' it should wait for response of the (one of) the first tasks that finished
  61. switch (uiCommand)
  62. {
  63. case B3_THREAD_SCHEDULE_TASK:
  64. {
  65. //#define SINGLE_THREADED 1
  66. #ifdef SINGLE_THREADED
  67. b3ThreadStatus& threadStatus = m_activeThreadStatus[0];
  68. threadStatus.m_userPtr=(void*)uiArgument0;
  69. threadStatus.m_userThreadFunc(threadStatus.m_userPtr,threadStatus.m_lsMemory);
  70. HANDLE handle =0;
  71. #else
  72. b3ThreadStatus& threadStatus = m_activeThreadStatus[taskId];
  73. b3Assert(taskId>=0);
  74. b3Assert(int(taskId)<m_activeThreadStatus.size());
  75. threadStatus.m_commandId = uiCommand;
  76. threadStatus.m_status = 1;
  77. threadStatus.m_userPtr = (void*)uiArgument0;
  78. ///fire event to start new task
  79. SetEvent(threadStatus.m_eventStartHandle);
  80. #endif //CollisionTask_LocalStoreMemory
  81. break;
  82. }
  83. default:
  84. {
  85. ///not implemented
  86. b3Assert(0);
  87. }
  88. };
  89. }
  90. ///check for messages from SPUs
  91. void b3Win32ThreadSupport::waitForResponse(int *puiArgument0, int *puiArgument1)
  92. {
  93. ///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
  94. ///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
  95. b3Assert(m_activeThreadStatus.size());
  96. int last = -1;
  97. #ifndef SINGLE_THREADED
  98. DWORD res = WaitForMultipleObjects(m_completeHandles.size(), &m_completeHandles[0], FALSE, INFINITE);
  99. b3Assert(res != WAIT_FAILED);
  100. last = res - WAIT_OBJECT_0;
  101. b3ThreadStatus& threadStatus = m_activeThreadStatus[last];
  102. b3Assert(threadStatus.m_threadHandle);
  103. b3Assert(threadStatus.m_eventCompletetHandle);
  104. //WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE);
  105. b3Assert(threadStatus.m_status > 1);
  106. threadStatus.m_status = 0;
  107. ///need to find an active spu
  108. b3Assert(last>=0);
  109. #else
  110. last=0;
  111. b3ThreadStatus& threadStatus = m_activeThreadStatus[last];
  112. #endif //SINGLE_THREADED
  113. *puiArgument0 = threadStatus.m_taskId;
  114. *puiArgument1 = threadStatus.m_status;
  115. }
  116. ///check for messages from SPUs
  117. bool b3Win32ThreadSupport::isTaskCompleted(int *puiArgument0, int *puiArgument1, int timeOutInMilliseconds)
  118. {
  119. ///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
  120. ///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
  121. b3Assert(m_activeThreadStatus.size());
  122. int last = -1;
  123. #ifndef SINGLE_THREADED
  124. DWORD res = WaitForMultipleObjects(m_completeHandles.size(), &m_completeHandles[0], FALSE, timeOutInMilliseconds);
  125. if ((res != STATUS_TIMEOUT) && (res != WAIT_FAILED))
  126. {
  127. b3Assert(res != WAIT_FAILED);
  128. last = res - WAIT_OBJECT_0;
  129. b3ThreadStatus& threadStatus = m_activeThreadStatus[last];
  130. b3Assert(threadStatus.m_threadHandle);
  131. b3Assert(threadStatus.m_eventCompletetHandle);
  132. //WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE);
  133. b3Assert(threadStatus.m_status > 1);
  134. threadStatus.m_status = 0;
  135. ///need to find an active spu
  136. b3Assert(last>=0);
  137. #else
  138. last=0;
  139. b3ThreadStatus& threadStatus = m_activeThreadStatus[last];
  140. #endif //SINGLE_THREADED
  141. *puiArgument0 = threadStatus.m_taskId;
  142. *puiArgument1 = threadStatus.m_status;
  143. return true;
  144. }
  145. return false;
  146. }
  147. void b3Win32ThreadSupport::startThreads(const Win32ThreadConstructionInfo& threadConstructionInfo)
  148. {
  149. static int uniqueId = 0;
  150. uniqueId++;
  151. m_activeThreadStatus.resize(threadConstructionInfo.m_numThreads);
  152. m_completeHandles.resize(threadConstructionInfo.m_numThreads);
  153. m_maxNumTasks = threadConstructionInfo.m_numThreads;
  154. for (int i=0;i<threadConstructionInfo.m_numThreads;i++)
  155. {
  156. printf("starting thread %d\n",i);
  157. b3ThreadStatus& threadStatus = m_activeThreadStatus[i];
  158. LPSECURITY_ATTRIBUTES lpThreadAttributes=NULL;
  159. SIZE_T dwStackSize=threadConstructionInfo.m_threadStackSize;
  160. LPTHREAD_START_ROUTINE lpStartAddress=&Thread_no_1;
  161. LPVOID lpParameter=&threadStatus;
  162. DWORD dwCreationFlags=0;
  163. LPDWORD lpThreadId=0;
  164. threadStatus.m_userPtr=0;
  165. sprintf(threadStatus.m_eventStartHandleName,"es%.8s%d%d",threadConstructionInfo.m_uniqueName,uniqueId,i);
  166. threadStatus.m_eventStartHandle = CreateEventA (0,false,false,threadStatus.m_eventStartHandleName);
  167. sprintf(threadStatus.m_eventCompletetHandleName,"ec%.8s%d%d",threadConstructionInfo.m_uniqueName,uniqueId,i);
  168. threadStatus.m_eventCompletetHandle = CreateEventA (0,false,false,threadStatus.m_eventCompletetHandleName);
  169. m_completeHandles[i] = threadStatus.m_eventCompletetHandle;
  170. HANDLE handle = CreateThread(lpThreadAttributes,dwStackSize,lpStartAddress,lpParameter, dwCreationFlags,lpThreadId);
  171. switch(threadConstructionInfo.m_priority)
  172. {
  173. case 0:
  174. {
  175. SetThreadPriority(handle,THREAD_PRIORITY_HIGHEST);
  176. break;
  177. }
  178. case 1:
  179. {
  180. SetThreadPriority(handle,THREAD_PRIORITY_TIME_CRITICAL);
  181. break;
  182. }
  183. case 2:
  184. {
  185. SetThreadPriority(handle,THREAD_PRIORITY_BELOW_NORMAL);
  186. break;
  187. }
  188. default:
  189. {
  190. }
  191. }
  192. SetThreadAffinityMask(handle, 1<<i);
  193. threadStatus.m_taskId = i;
  194. threadStatus.m_commandId = 0;
  195. threadStatus.m_status = 0;
  196. threadStatus.m_threadHandle = handle;
  197. threadStatus.m_lsMemory = threadConstructionInfo.m_lsMemoryFunc();
  198. threadStatus.m_userThreadFunc = threadConstructionInfo.m_userThreadFunc;
  199. printf("started %s thread %d with threadHandle %p\n",threadConstructionInfo.m_uniqueName,i,handle);
  200. }
  201. }
  202. void b3Win32ThreadSupport::startThreads()
  203. {
  204. }
  205. ///tell the task scheduler we are done with the SPU tasks
  206. void b3Win32ThreadSupport::stopThreads()
  207. {
  208. int i;
  209. for (i=0;i<m_activeThreadStatus.size();i++)
  210. {
  211. b3ThreadStatus& threadStatus = m_activeThreadStatus[i];
  212. if (threadStatus.m_status>0)
  213. {
  214. WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE);
  215. }
  216. threadStatus.m_userPtr = 0;
  217. SetEvent(threadStatus.m_eventStartHandle);
  218. WaitForSingleObject(threadStatus.m_eventCompletetHandle, INFINITE);
  219. CloseHandle(threadStatus.m_eventCompletetHandle);
  220. CloseHandle(threadStatus.m_eventStartHandle);
  221. CloseHandle(threadStatus.m_threadHandle);
  222. }
  223. m_activeThreadStatus.clear();
  224. m_completeHandles.clear();
  225. }
  226. class b3Win32Barrier : public b3Barrier
  227. {
  228. private:
  229. CRITICAL_SECTION mExternalCriticalSection;
  230. CRITICAL_SECTION mLocalCriticalSection;
  231. HANDLE mRunEvent,mNotifyEvent;
  232. int mCounter,mEnableCounter;
  233. int mMaxCount;
  234. public:
  235. b3Win32Barrier()
  236. {
  237. mCounter = 0;
  238. mMaxCount = 1;
  239. mEnableCounter = 0;
  240. InitializeCriticalSection(&mExternalCriticalSection);
  241. InitializeCriticalSection(&mLocalCriticalSection);
  242. mRunEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  243. mNotifyEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  244. }
  245. virtual ~b3Win32Barrier()
  246. {
  247. DeleteCriticalSection(&mExternalCriticalSection);
  248. DeleteCriticalSection(&mLocalCriticalSection);
  249. CloseHandle(mRunEvent);
  250. CloseHandle(mNotifyEvent);
  251. }
  252. void sync()
  253. {
  254. int eventId;
  255. EnterCriticalSection(&mExternalCriticalSection);
  256. //PFX_PRINTF("enter taskId %d count %d stage %d phase %d mEnableCounter %d\n",taskId,mCounter,debug&0xff,debug>>16,mEnableCounter);
  257. if(mEnableCounter > 0) {
  258. ResetEvent(mNotifyEvent);
  259. LeaveCriticalSection(&mExternalCriticalSection);
  260. WaitForSingleObject(mNotifyEvent,INFINITE);
  261. EnterCriticalSection(&mExternalCriticalSection);
  262. }
  263. eventId = mCounter;
  264. mCounter++;
  265. if(eventId == mMaxCount-1) {
  266. SetEvent(mRunEvent);
  267. mEnableCounter = mCounter-1;
  268. mCounter = 0;
  269. }
  270. else {
  271. ResetEvent(mRunEvent);
  272. LeaveCriticalSection(&mExternalCriticalSection);
  273. WaitForSingleObject(mRunEvent,INFINITE);
  274. EnterCriticalSection(&mExternalCriticalSection);
  275. mEnableCounter--;
  276. }
  277. if(mEnableCounter == 0) {
  278. SetEvent(mNotifyEvent);
  279. }
  280. //PFX_PRINTF("leave taskId %d count %d stage %d phase %d mEnableCounter %d\n",taskId,mCounter,debug&0xff,debug>>16,mEnableCounter);
  281. LeaveCriticalSection(&mExternalCriticalSection);
  282. }
  283. virtual void setMaxCount(int n) {mMaxCount = n;}
  284. virtual int getMaxCount() {return mMaxCount;}
  285. };
  286. class b3Win32CriticalSection : public b3CriticalSection
  287. {
  288. private:
  289. CRITICAL_SECTION mCriticalSection;
  290. public:
  291. b3Win32CriticalSection()
  292. {
  293. InitializeCriticalSection(&mCriticalSection);
  294. }
  295. ~b3Win32CriticalSection()
  296. {
  297. DeleteCriticalSection(&mCriticalSection);
  298. }
  299. unsigned int getSharedParam(int i)
  300. {
  301. b3Assert(i>=0&&i<31);
  302. return mCommonBuff[i+1];
  303. }
  304. void setSharedParam(int i,unsigned int p)
  305. {
  306. b3Assert(i>=0&&i<31);
  307. mCommonBuff[i+1] = p;
  308. }
  309. void lock()
  310. {
  311. EnterCriticalSection(&mCriticalSection);
  312. mCommonBuff[0] = 1;
  313. }
  314. void unlock()
  315. {
  316. mCommonBuff[0] = 0;
  317. LeaveCriticalSection(&mCriticalSection);
  318. }
  319. };
  320. b3Barrier* b3Win32ThreadSupport::createBarrier()
  321. {
  322. unsigned char* mem = (unsigned char*)b3AlignedAlloc(sizeof(b3Win32Barrier),16);
  323. b3Win32Barrier* barrier = new(mem) b3Win32Barrier();
  324. barrier->setMaxCount(getNumTasks());
  325. return barrier;
  326. }
  327. b3CriticalSection* b3Win32ThreadSupport::createCriticalSection()
  328. {
  329. unsigned char* mem = (unsigned char*) b3AlignedAlloc(sizeof(b3Win32CriticalSection),16);
  330. b3Win32CriticalSection* cs = new(mem) b3Win32CriticalSection();
  331. return cs;
  332. }
  333. void b3Win32ThreadSupport::deleteBarrier(b3Barrier* barrier)
  334. {
  335. barrier->~b3Barrier();
  336. b3AlignedFree(barrier);
  337. }
  338. void b3Win32ThreadSupport::deleteCriticalSection(b3CriticalSection* criticalSection)
  339. {
  340. criticalSection->~b3CriticalSection();
  341. b3AlignedFree(criticalSection);
  342. }
  343. #endif //_WIN32