Win32ThreadSupport.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. Bullet Continuous Collision Detection and Physics Library
  3. Copyright (c) 2003-2007 Erwin Coumans http://bulletphysics.com
  4. This software is provided 'as-is', without any express or implied warranty.
  5. In no event will the authors be held liable for any damages arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it freely,
  8. subject to the following restrictions:
  9. 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.
  10. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  11. 3. This notice may not be removed or altered from any source distribution.
  12. */
  13. #include "Win32ThreadSupport.h"
  14. #ifdef USE_WIN32_THREADING
  15. #include <windows.h>
  16. #include "SpuCollisionTaskProcess.h"
  17. #include "SpuNarrowPhaseCollisionTask/SpuGatheringCollisionTask.h"
  18. #ifdef WINRT
  19. #define InitializeCriticalSection(arg0) InitializeCriticalSectionEx(arg0, 0, 0)
  20. #define WaitForSingleObject(arg0, arg1) WaitForSingleObjectEx(arg0, arg1, false);
  21. #define WaitForMultipleObjects(arg0, arg1, arg2, arg3) WaitForMultipleObjectsEx(arg0, arg1, arg2, arg3, false);
  22. #endif
  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. ///Win32ThreadSupport helps to initialize/shutdown libspe2, start/stop SPU tasks and communication
  26. ///Setup and initialize SPU/CELL/Libspe2
  27. Win32ThreadSupport::Win32ThreadSupport(const Win32ThreadConstructionInfo & threadConstructionInfo)
  28. {
  29. m_maxNumTasks = threadConstructionInfo.m_numThreads;
  30. startThreads(threadConstructionInfo);
  31. }
  32. ///cleanup/shutdown Libspe2
  33. Win32ThreadSupport::~Win32ThreadSupport()
  34. {
  35. stopSPU();
  36. }
  37. #include <stdio.h>
  38. DWORD WINAPI Thread_no_1( LPVOID lpParam )
  39. {
  40. Win32ThreadSupport::btSpuStatus* status = (Win32ThreadSupport::btSpuStatus*)lpParam;
  41. while (1)
  42. {
  43. WaitForSingleObject(status->m_eventStartHandle,INFINITE);
  44. void* userPtr = status->m_userPtr;
  45. if (userPtr)
  46. {
  47. btAssert(status->m_status);
  48. status->m_userThreadFunc(userPtr,status->m_lsMemory);
  49. status->m_status = 2;
  50. SetEvent(status->m_eventCompletetHandle);
  51. } else
  52. {
  53. //exit Thread
  54. status->m_status = 3;
  55. printf("Thread with taskId %i with handle %p exiting\n",status->m_taskId, status->m_threadHandle);
  56. SetEvent(status->m_eventCompletetHandle);
  57. break;
  58. }
  59. }
  60. printf("Thread TERMINATED\n");
  61. return 0;
  62. }
  63. ///send messages to SPUs
  64. void Win32ThreadSupport::sendRequest(uint32_t uiCommand, ppu_address_t uiArgument0, uint32_t taskId)
  65. {
  66. /// gMidphaseSPU.sendRequest(CMD_GATHER_AND_PROCESS_PAIRLIST, (ppu_address_t) &taskDesc);
  67. ///we should spawn an SPU task here, and in 'waitForResponse' it should wait for response of the (one of) the first tasks that finished
  68. switch (uiCommand)
  69. {
  70. case CMD_GATHER_AND_PROCESS_PAIRLIST:
  71. {
  72. //#define SINGLE_THREADED 1
  73. #ifdef SINGLE_THREADED
  74. btSpuStatus& spuStatus = m_activeSpuStatus[0];
  75. spuStatus.m_userPtr=(void*)uiArgument0;
  76. spuStatus.m_userThreadFunc(spuStatus.m_userPtr,spuStatus.m_lsMemory);
  77. HANDLE handle =0;
  78. #else
  79. btSpuStatus& spuStatus = m_activeSpuStatus[taskId];
  80. btAssert(taskId>=0);
  81. btAssert(int(taskId)<m_activeSpuStatus.size());
  82. spuStatus.m_commandId = uiCommand;
  83. spuStatus.m_status = 1;
  84. spuStatus.m_userPtr = (void*)uiArgument0;
  85. ///fire event to start new task
  86. SetEvent(spuStatus.m_eventStartHandle);
  87. #endif //CollisionTask_LocalStoreMemory
  88. break;
  89. }
  90. default:
  91. {
  92. ///not implemented
  93. btAssert(0);
  94. }
  95. };
  96. }
  97. ///check for messages from SPUs
  98. void Win32ThreadSupport::waitForResponse(unsigned int *puiArgument0, unsigned int *puiArgument1)
  99. {
  100. ///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
  101. ///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
  102. btAssert(m_activeSpuStatus.size());
  103. int last = -1;
  104. #ifndef SINGLE_THREADED
  105. DWORD res = WaitForMultipleObjects(m_completeHandles.size(), &m_completeHandles[0], FALSE, INFINITE);
  106. btAssert(res != WAIT_FAILED);
  107. last = res - WAIT_OBJECT_0;
  108. btSpuStatus& spuStatus = m_activeSpuStatus[last];
  109. btAssert(spuStatus.m_threadHandle);
  110. btAssert(spuStatus.m_eventCompletetHandle);
  111. //WaitForSingleObject(spuStatus.m_eventCompletetHandle, INFINITE);
  112. btAssert(spuStatus.m_status > 1);
  113. spuStatus.m_status = 0;
  114. ///need to find an active spu
  115. btAssert(last>=0);
  116. #else
  117. last=0;
  118. btSpuStatus& spuStatus = m_activeSpuStatus[last];
  119. #endif //SINGLE_THREADED
  120. *puiArgument0 = spuStatus.m_taskId;
  121. *puiArgument1 = spuStatus.m_status;
  122. }
  123. ///check for messages from SPUs
  124. bool Win32ThreadSupport::isTaskCompleted(unsigned int *puiArgument0, unsigned int *puiArgument1, int timeOutInMilliseconds)
  125. {
  126. ///We should wait for (one of) the first tasks to finish (or other SPU messages), and report its response
  127. ///A possible response can be 'yes, SPU handled it', or 'no, please do a PPU fallback'
  128. btAssert(m_activeSpuStatus.size());
  129. int last = -1;
  130. #ifndef SINGLE_THREADED
  131. DWORD res = WaitForMultipleObjects(m_completeHandles.size(), &m_completeHandles[0], FALSE, timeOutInMilliseconds);
  132. if ((res != STATUS_TIMEOUT) && (res != WAIT_FAILED))
  133. {
  134. btAssert(res != WAIT_FAILED);
  135. last = res - WAIT_OBJECT_0;
  136. btSpuStatus& spuStatus = m_activeSpuStatus[last];
  137. btAssert(spuStatus.m_threadHandle);
  138. btAssert(spuStatus.m_eventCompletetHandle);
  139. //WaitForSingleObject(spuStatus.m_eventCompletetHandle, INFINITE);
  140. btAssert(spuStatus.m_status > 1);
  141. spuStatus.m_status = 0;
  142. ///need to find an active spu
  143. btAssert(last>=0);
  144. #else
  145. last=0;
  146. btSpuStatus& spuStatus = m_activeSpuStatus[last];
  147. #endif //SINGLE_THREADED
  148. *puiArgument0 = spuStatus.m_taskId;
  149. *puiArgument1 = spuStatus.m_status;
  150. return true;
  151. }
  152. return false;
  153. }
  154. void Win32ThreadSupport::startThreads(const Win32ThreadConstructionInfo& threadConstructionInfo)
  155. {
  156. m_activeSpuStatus.resize(threadConstructionInfo.m_numThreads);
  157. m_completeHandles.resize(threadConstructionInfo.m_numThreads);
  158. m_maxNumTasks = threadConstructionInfo.m_numThreads;
  159. for (int i=0;i<threadConstructionInfo.m_numThreads;i++)
  160. {
  161. printf("starting thread %d\n",i);
  162. btSpuStatus& spuStatus = m_activeSpuStatus[i];
  163. LPSECURITY_ATTRIBUTES lpThreadAttributes=NULL;
  164. SIZE_T dwStackSize=threadConstructionInfo.m_threadStackSize;
  165. LPTHREAD_START_ROUTINE lpStartAddress=&Thread_no_1;
  166. LPVOID lpParameter=&spuStatus;
  167. DWORD dwCreationFlags=0;
  168. LPDWORD lpThreadId=0;
  169. spuStatus.m_userPtr=0;
  170. sprintf(spuStatus.m_eventStartHandleName,"eventStart%s%d",threadConstructionInfo.m_uniqueName,i);
  171. #ifdef WINRT
  172. WCHAR wszBuf[MAX_PATH] = { 0 };
  173. MultiByteToWideChar(CP_UTF8, 0, spuStatus.m_eventStartHandleName, -1, wszBuf, sizeof(wszBuf));
  174. spuStatus.m_eventStartHandle = CreateEventEx(NULL, wszBuf, 0, EVENT_ALL_ACCESS);
  175. #else
  176. spuStatus.m_eventStartHandle = CreateEventA (0,false,false,spuStatus.m_eventStartHandleName);
  177. #endif
  178. sprintf(spuStatus.m_eventCompletetHandleName,"eventComplete%s%d",threadConstructionInfo.m_uniqueName,i);
  179. #ifdef WINRT
  180. MultiByteToWideChar(CP_UTF8, 0, spuStatus.m_eventCompletetHandleName, -1, wszBuf, sizeof(wszBuf));
  181. spuStatus.m_eventCompletetHandle = CreateEventEx(NULL, wszBuf, 0, EVENT_ALL_ACCESS);
  182. #else
  183. spuStatus.m_eventCompletetHandle = CreateEventA (0,false,false,spuStatus.m_eventCompletetHandleName);
  184. #endif
  185. m_completeHandles[i] = spuStatus.m_eventCompletetHandle;
  186. HANDLE handle = CreateThread(lpThreadAttributes,dwStackSize,lpStartAddress,lpParameter, dwCreationFlags,lpThreadId);
  187. SetThreadPriority(handle,THREAD_PRIORITY_HIGHEST);
  188. //SetThreadPriority(handle,THREAD_PRIORITY_TIME_CRITICAL);
  189. #ifndef WINRT
  190. SetThreadAffinityMask(handle, 1<<i);
  191. #endif
  192. spuStatus.m_taskId = i;
  193. spuStatus.m_commandId = 0;
  194. spuStatus.m_status = 0;
  195. spuStatus.m_threadHandle = handle;
  196. spuStatus.m_lsMemory = threadConstructionInfo.m_lsMemoryFunc();
  197. spuStatus.m_userThreadFunc = threadConstructionInfo.m_userThreadFunc;
  198. printf("started thread %d with threadHandle %p\n",i,handle);
  199. }
  200. }
  201. void Win32ThreadSupport::startSPU()
  202. {
  203. }
  204. ///tell the task scheduler we are done with the SPU tasks
  205. void Win32ThreadSupport::stopSPU()
  206. {
  207. int i;
  208. for (i=0;i<m_activeSpuStatus.size();i++)
  209. {
  210. btSpuStatus& spuStatus = m_activeSpuStatus[i];
  211. if (spuStatus.m_status>0)
  212. {
  213. WaitForSingleObject(spuStatus.m_eventCompletetHandle, INFINITE);
  214. }
  215. spuStatus.m_userPtr = 0;
  216. SetEvent(spuStatus.m_eventStartHandle);
  217. WaitForSingleObject(spuStatus.m_eventCompletetHandle, INFINITE);
  218. CloseHandle(spuStatus.m_eventCompletetHandle);
  219. CloseHandle(spuStatus.m_eventStartHandle);
  220. CloseHandle(spuStatus.m_threadHandle);
  221. }
  222. m_activeSpuStatus.clear();
  223. m_completeHandles.clear();
  224. }
  225. class btWin32Barrier : public btBarrier
  226. {
  227. private:
  228. CRITICAL_SECTION mExternalCriticalSection;
  229. CRITICAL_SECTION mLocalCriticalSection;
  230. HANDLE mRunEvent,mNotifyEvent;
  231. int mCounter,mEnableCounter;
  232. int mMaxCount;
  233. public:
  234. btWin32Barrier()
  235. {
  236. mCounter = 0;
  237. mMaxCount = 1;
  238. mEnableCounter = 0;
  239. InitializeCriticalSection(&mExternalCriticalSection);
  240. InitializeCriticalSection(&mLocalCriticalSection);
  241. #ifdef WINRT
  242. mRunEvent = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
  243. mNotifyEvent = CreateEventEx(NULL, NULL, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS);
  244. #else
  245. mRunEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
  246. mNotifyEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  247. #endif
  248. }
  249. virtual ~btWin32Barrier()
  250. {
  251. DeleteCriticalSection(&mExternalCriticalSection);
  252. DeleteCriticalSection(&mLocalCriticalSection);
  253. CloseHandle(mRunEvent);
  254. CloseHandle(mNotifyEvent);
  255. }
  256. void sync()
  257. {
  258. int eventId;
  259. EnterCriticalSection(&mExternalCriticalSection);
  260. //PFX_PRINTF("enter taskId %d count %d stage %d phase %d mEnableCounter %d\n",taskId,mCounter,debug&0xff,debug>>16,mEnableCounter);
  261. if(mEnableCounter > 0) {
  262. ResetEvent(mNotifyEvent);
  263. LeaveCriticalSection(&mExternalCriticalSection);
  264. WaitForSingleObject(mNotifyEvent,INFINITE);
  265. EnterCriticalSection(&mExternalCriticalSection);
  266. }
  267. eventId = mCounter;
  268. mCounter++;
  269. if(eventId == mMaxCount-1) {
  270. SetEvent(mRunEvent);
  271. mEnableCounter = mCounter-1;
  272. mCounter = 0;
  273. }
  274. else {
  275. ResetEvent(mRunEvent);
  276. LeaveCriticalSection(&mExternalCriticalSection);
  277. WaitForSingleObject(mRunEvent,INFINITE);
  278. EnterCriticalSection(&mExternalCriticalSection);
  279. mEnableCounter--;
  280. }
  281. if(mEnableCounter == 0) {
  282. SetEvent(mNotifyEvent);
  283. }
  284. //PFX_PRINTF("leave taskId %d count %d stage %d phase %d mEnableCounter %d\n",taskId,mCounter,debug&0xff,debug>>16,mEnableCounter);
  285. LeaveCriticalSection(&mExternalCriticalSection);
  286. }
  287. virtual void setMaxCount(int n) {mMaxCount = n;}
  288. virtual int getMaxCount() {return mMaxCount;}
  289. };
  290. class btWin32CriticalSection : public btCriticalSection
  291. {
  292. private:
  293. CRITICAL_SECTION mCriticalSection;
  294. public:
  295. btWin32CriticalSection()
  296. {
  297. InitializeCriticalSection(&mCriticalSection);
  298. }
  299. ~btWin32CriticalSection()
  300. {
  301. DeleteCriticalSection(&mCriticalSection);
  302. }
  303. unsigned int getSharedParam(int i)
  304. {
  305. btAssert(i>=0&&i<31);
  306. return mCommonBuff[i+1];
  307. }
  308. void setSharedParam(int i,unsigned int p)
  309. {
  310. btAssert(i>=0&&i<31);
  311. mCommonBuff[i+1] = p;
  312. }
  313. void lock()
  314. {
  315. EnterCriticalSection(&mCriticalSection);
  316. mCommonBuff[0] = 1;
  317. }
  318. void unlock()
  319. {
  320. mCommonBuff[0] = 0;
  321. LeaveCriticalSection(&mCriticalSection);
  322. }
  323. };
  324. btBarrier* Win32ThreadSupport::createBarrier()
  325. {
  326. unsigned char* mem = (unsigned char*)btAlignedAlloc(sizeof(btWin32Barrier),16);
  327. btWin32Barrier* barrier = new(mem) btWin32Barrier();
  328. barrier->setMaxCount(getNumTasks());
  329. return barrier;
  330. }
  331. btCriticalSection* Win32ThreadSupport::createCriticalSection()
  332. {
  333. unsigned char* mem = (unsigned char*) btAlignedAlloc(sizeof(btWin32CriticalSection),16);
  334. btWin32CriticalSection* cs = new(mem) btWin32CriticalSection();
  335. return cs;
  336. }
  337. void Win32ThreadSupport::deleteBarrier(btBarrier* barrier)
  338. {
  339. barrier->~btBarrier();
  340. btAlignedFree(barrier);
  341. }
  342. void Win32ThreadSupport::deleteCriticalSection(btCriticalSection* criticalSection)
  343. {
  344. criticalSection->~btCriticalSection();
  345. btAlignedFree(criticalSection);
  346. }
  347. #endif //USE_WIN32_THREADING