NvThreadConfig.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. NvThreadConfig.cpp : A simple wrapper class to define threading and mutex locks.
  3. */
  4. /*!
  5. **
  6. ** Copyright (c) 2009 by John W. Ratcliff mailto:[email protected]
  7. **
  8. ** Portions of this source has been released with the PhysXViewer application, as well as
  9. ** Rocket, CreateDynamics, ODF, and as a number of sample code snippets.
  10. **
  11. ** If you find this code useful or you are feeling particularily generous I would
  12. ** ask that you please go to http://www.amillionpixels.us and make a donation
  13. ** to Troy DeMolay.
  14. **
  15. ** DeMolay is a youth group for young men between the ages of 12 and 21.
  16. ** It teaches strong moral principles, as well as leadership skills and
  17. ** public speaking. The donations page uses the 'pay for pixels' paradigm
  18. ** where, in this case, a pixel is only a single penny. Donations can be
  19. ** made for as small as $4 or as high as a $100 block. Each person who donates
  20. ** will get a link to their own site as well as acknowledgement on the
  21. ** donations blog located here http://www.amillionpixels.blogspot.com/
  22. **
  23. ** If you wish to contact me you can use the following methods:
  24. **
  25. ** Skype ID: jratcliff63367
  26. ** Yahoo: jratcliff63367
  27. ** AOL: jratcliff1961
  28. ** email: [email protected]
  29. **
  30. **
  31. ** The MIT license:
  32. **
  33. ** Permission is hereby granted, free of charge, to any person obtaining a copy
  34. ** of this software and associated documentation files (the "Software"), to deal
  35. ** in the Software without restriction, including without limitation the rights
  36. ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  37. ** copies of the Software, and to permit persons to whom the Software is furnished
  38. ** to do so, subject to the following conditions:
  39. **
  40. ** The above copyright notice and this permission notice shall be included in all
  41. ** copies or substantial portions of the Software.
  42. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  43. ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  44. ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  45. ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  46. ** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  47. ** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  48. */
  49. #include <cassert>
  50. #include "NvThreadConfig.h"
  51. #if defined(WIN32)
  52. #define _WIN32_WINNT 0x400
  53. #include <windows.h>
  54. #pragma comment(lib,"winmm.lib")
  55. // #ifndef _WIN32_WINNT
  56. // #endif
  57. // #include <windows.h>
  58. //#include <winbase.h>
  59. #endif
  60. #if defined(_XBOX)
  61. #include <xtl.h>
  62. #endif
  63. #if defined(__linux__) || defined( __APPLE__ )
  64. //#include <sys/time.h>
  65. #include <time.h>
  66. #include <unistd.h>
  67. #include <errno.h>
  68. #define __stdcall
  69. #endif
  70. #if defined( __APPLE__ )
  71. #include <sys/time.h>
  72. #endif
  73. #if defined(__APPLE__) || defined(__linux__)
  74. #include <pthread.h>
  75. #endif
  76. #if defined( __APPLE__ )
  77. #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
  78. #endif
  79. #ifdef NDEBUG
  80. #define VERIFY( x ) (x)
  81. #else
  82. #define VERIFY( x ) assert((x))
  83. #endif
  84. namespace CONVEX_DECOMPOSITION
  85. {
  86. NxU32 tc_timeGetTime(void)
  87. {
  88. #if defined(__linux__)
  89. struct timespec ts;
  90. clock_gettime(CLOCK_REALTIME, &ts);
  91. return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
  92. #elif defined( __APPLE__ )
  93. struct timeval tp;
  94. gettimeofday(&tp, (struct timezone *)0);
  95. return tp.tv_sec * 1000 + tp.tv_usec / 1000;
  96. #elif defined( _XBOX )
  97. return GetTickCount();
  98. #else
  99. return timeGetTime();
  100. #endif
  101. }
  102. void tc_sleep(NxU32 ms)
  103. {
  104. #if defined(__linux__) || defined( __APPLE__ )
  105. usleep(ms * 1000);
  106. #else
  107. Sleep(ms);
  108. #endif
  109. }
  110. void tc_spinloop()
  111. {
  112. #ifdef __linux__
  113. asm ( "pause" );
  114. #elif defined( _XBOX )
  115. // Pause would do nothing on the Xbox. Threads are not scheduled.
  116. #elif defined( _WIN64 )
  117. YieldProcessor( );
  118. #elif (defined( __arm64__ ) && defined( __APPLE__ )) || defined( __arch64__ )
  119. pthread_yield_np();
  120. #else
  121. __asm { pause };
  122. #endif
  123. }
  124. void tc_interlockedExchange(void *dest, const int64_t exchange)
  125. {
  126. #if defined( __linux__ ) || defined( __APPLE__ )
  127. // not working
  128. assert(false);
  129. //__sync_lock_test_and_set((int64_t*)dest, exchange);
  130. #elif defined( _XBOX ) || defined( _WIN64 )
  131. InterlockedExchange((volatile LONG *)dest, exchange);
  132. #else
  133. __asm
  134. {
  135. mov ebx, dword ptr [exchange]
  136. mov ecx, dword ptr [exchange + 4]
  137. mov edi, dest
  138. mov eax, dword ptr [edi]
  139. mov edx, dword ptr [edi + 4]
  140. jmp start
  141. retry:
  142. pause
  143. start:
  144. lock cmpxchg8b [edi]
  145. jnz retry
  146. };
  147. #endif
  148. }
  149. NxI32 tc_interlockedCompareExchange(void *dest, NxI32 exchange, NxI32 compare)
  150. {
  151. #if defined( __linux__ ) || defined( __APPLE__ )
  152. // not working
  153. assert(false);
  154. return 0;
  155. //return __sync_val_compare_and_swap((uintptr_t*)dest, exchange, compare);
  156. //return __sync_bool_compare_and_swap((uintptr_t*)dest, exchange, compare);
  157. #elif defined( _XBOX ) || defined( _WIN64 )
  158. return InterlockedCompareExchange((volatile LONG *)dest, exchange, compare);
  159. #else
  160. char _ret;
  161. //
  162. __asm
  163. {
  164. mov edx, [dest]
  165. mov eax, [compare]
  166. mov ecx, [exchange]
  167. lock cmpxchg [edx], ecx
  168. setz al
  169. mov byte ptr [_ret], al
  170. }
  171. //
  172. return _ret;
  173. #endif
  174. }
  175. NxI32 tc_interlockedCompareExchange(void *dest, const NxI32 exchange1, const NxI32 exchange2, const NxI32 compare1, const NxI32 compare2)
  176. {
  177. #if defined( __linux__ ) || defined( __APPLE__ )
  178. // not working
  179. assert(false);
  180. return 0;
  181. //uint64_t exchange = ((uint64_t)exchange1 << 32) | (uint64_t)exchange2;
  182. //uint64_t compare = ((uint64_t)compare1 << 32) | (uint64_t)compare2;
  183. //return __sync_bool_compare_and_swap((int64_t*)dest, exchange, compare);
  184. #elif defined( _XBOX ) || defined( _WIN64 )
  185. assert(false);
  186. return 0;
  187. #else
  188. char _ret;
  189. //
  190. __asm
  191. {
  192. mov ebx, [exchange1]
  193. mov ecx, [exchange2]
  194. mov edi, [dest]
  195. mov eax, [compare1]
  196. mov edx, [compare2]
  197. lock cmpxchg8b [edi]
  198. setz al
  199. mov byte ptr [_ret], al
  200. }
  201. //
  202. return _ret;
  203. #endif
  204. }
  205. class MyThreadMutex : public ThreadMutex
  206. {
  207. public:
  208. MyThreadMutex(void)
  209. {
  210. #if defined(WIN32) || defined(_XBOX)
  211. InitializeCriticalSection(&m_Mutex);
  212. #elif defined(__APPLE__) || defined(__linux__)
  213. pthread_mutexattr_t mutexAttr; // Mutex Attribute
  214. VERIFY( pthread_mutexattr_init(&mutexAttr) == 0 );
  215. VERIFY( pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) == 0 );
  216. VERIFY( pthread_mutex_init(&m_Mutex, &mutexAttr) == 0 );
  217. VERIFY( pthread_mutexattr_destroy(&mutexAttr) == 0 );
  218. #endif
  219. }
  220. ~MyThreadMutex(void)
  221. {
  222. #if defined(WIN32) || defined(_XBOX)
  223. DeleteCriticalSection(&m_Mutex);
  224. #elif defined(__APPLE__) || defined(__linux__)
  225. VERIFY( pthread_mutex_destroy(&m_Mutex) == 0 );
  226. #endif
  227. }
  228. void lock(void)
  229. {
  230. #if defined(WIN32) || defined(_XBOX)
  231. EnterCriticalSection(&m_Mutex);
  232. #elif defined(__APPLE__) || defined(__linux__)
  233. VERIFY( pthread_mutex_lock(&m_Mutex) == 0 );
  234. #endif
  235. }
  236. bool tryLock(void)
  237. {
  238. #if defined(WIN32) || defined(_XBOX)
  239. bool bRet = false;
  240. //assert(("TryEnterCriticalSection seems to not work on XP???", 0));
  241. bRet = TryEnterCriticalSection(&m_Mutex) ? true : false;
  242. return bRet;
  243. #elif defined(__APPLE__) || defined(__linux__)
  244. NxI32 result = pthread_mutex_trylock(&m_Mutex);
  245. return (result == 0);
  246. #endif
  247. }
  248. void unlock(void)
  249. {
  250. #if defined(WIN32) || defined(_XBOX)
  251. LeaveCriticalSection(&m_Mutex);
  252. #elif defined(__APPLE__) || defined(__linux__)
  253. VERIFY( pthread_mutex_unlock(&m_Mutex) == 0 );
  254. #endif
  255. }
  256. private:
  257. #if defined(WIN32) || defined(_XBOX)
  258. CRITICAL_SECTION m_Mutex;
  259. #elif defined(__APPLE__) || defined(__linux__)
  260. pthread_mutex_t m_Mutex;
  261. #endif
  262. };
  263. ThreadMutex * tc_createThreadMutex(void)
  264. {
  265. MyThreadMutex *m = new MyThreadMutex;
  266. return static_cast< ThreadMutex *>(m);
  267. }
  268. void tc_releaseThreadMutex(ThreadMutex *tm)
  269. {
  270. MyThreadMutex *m = static_cast< MyThreadMutex *>(tm);
  271. delete m;
  272. }
  273. #if defined(WIN32) || defined(_XBOX)
  274. static unsigned long __stdcall _ThreadWorkerFunc(LPVOID arg);
  275. #elif defined(__APPLE__) || defined(__linux__)
  276. static void* _ThreadWorkerFunc(void* arg);
  277. #endif
  278. class MyThread : public Thread
  279. {
  280. public:
  281. MyThread(ThreadInterface *iface)
  282. {
  283. mInterface = iface;
  284. #if defined(WIN32) || defined(_XBOX)
  285. mThread = CreateThread(0, 0, _ThreadWorkerFunc, this, 0, 0);
  286. #elif defined(__APPLE__) || defined(__linux__)
  287. VERIFY( pthread_create(&mThread, NULL, _ThreadWorkerFunc, this) == 0 );
  288. #endif
  289. }
  290. ~MyThread(void)
  291. {
  292. #if defined(WIN32) || defined(_XBOX)
  293. if ( mThread )
  294. {
  295. CloseHandle(mThread);
  296. mThread = 0;
  297. }
  298. #endif
  299. }
  300. void onJobExecute(void)
  301. {
  302. mInterface->threadMain();
  303. }
  304. private:
  305. ThreadInterface *mInterface;
  306. #if defined(WIN32) || defined(_XBOX)
  307. HANDLE mThread;
  308. #elif defined(__APPLE__) || defined(__linux__)
  309. pthread_t mThread;
  310. #endif
  311. };
  312. Thread * tc_createThread(ThreadInterface *tinterface)
  313. {
  314. MyThread *m = new MyThread(tinterface);
  315. return static_cast< Thread *>(m);
  316. }
  317. void tc_releaseThread(Thread *t)
  318. {
  319. MyThread *m = static_cast<MyThread *>(t);
  320. delete m;
  321. }
  322. #if defined(WIN32) || defined(_XBOX)
  323. static unsigned long __stdcall _ThreadWorkerFunc(LPVOID arg)
  324. #elif defined(__APPLE__) || defined(__linux__)
  325. static void* _ThreadWorkerFunc(void* arg)
  326. #endif
  327. {
  328. MyThread *worker = (MyThread *) arg;
  329. worker->onJobExecute();
  330. return 0;
  331. }
  332. class MyThreadEvent : public ThreadEvent
  333. {
  334. public:
  335. MyThreadEvent(void)
  336. {
  337. #if defined(WIN32) || defined(_XBOX)
  338. mEvent = ::CreateEventA(NULL,TRUE,TRUE,"ThreadEvent");
  339. #elif defined(__APPLE__) || defined(__linux__)
  340. pthread_mutexattr_t mutexAttr; // Mutex Attribute
  341. VERIFY( pthread_mutexattr_init(&mutexAttr) == 0 );
  342. VERIFY( pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) == 0 );
  343. VERIFY( pthread_mutex_init(&mEventMutex, &mutexAttr) == 0 );
  344. VERIFY( pthread_mutexattr_destroy(&mutexAttr) == 0 );
  345. VERIFY( pthread_cond_init(&mEvent, NULL) == 0 );
  346. #endif
  347. }
  348. ~MyThreadEvent(void)
  349. {
  350. #if defined(WIN32) || defined(_XBOX)
  351. if ( mEvent )
  352. {
  353. ::CloseHandle(mEvent);
  354. }
  355. #elif defined(__APPLE__) || defined(__linux__)
  356. VERIFY( pthread_cond_destroy(&mEvent) == 0 );
  357. VERIFY( pthread_mutex_destroy(&mEventMutex) == 0 );
  358. #endif
  359. }
  360. virtual void setEvent(void) // signal the event
  361. {
  362. #if defined(WIN32) || defined(_XBOX)
  363. if ( mEvent )
  364. {
  365. ::SetEvent(mEvent);
  366. }
  367. #elif defined(__APPLE__) || defined(__linux__)
  368. VERIFY( pthread_mutex_lock(&mEventMutex) == 0 );
  369. VERIFY( pthread_cond_signal(&mEvent) == 0 );
  370. VERIFY( pthread_mutex_unlock(&mEventMutex) == 0 );
  371. #endif
  372. }
  373. void resetEvent(void)
  374. {
  375. #if defined(WIN32) || defined(_XBOX)
  376. if ( mEvent )
  377. {
  378. ::ResetEvent(mEvent);
  379. }
  380. #endif
  381. }
  382. virtual void waitForSingleObject(NxU32 ms)
  383. {
  384. #if defined(WIN32) || defined(_XBOX)
  385. if ( mEvent )
  386. {
  387. ::WaitForSingleObject(mEvent,ms);
  388. }
  389. #elif defined(__APPLE__) || defined(__linux__)
  390. VERIFY( pthread_mutex_lock(&mEventMutex) == 0 );
  391. if (ms == 0xffffffff)
  392. {
  393. VERIFY( pthread_cond_wait(&mEvent, &mEventMutex) == 0 );
  394. }
  395. else
  396. {
  397. struct timespec ts;
  398. #ifdef __APPLE__
  399. struct timeval tp;
  400. gettimeofday(&tp, (struct timezone *)0);
  401. ts.tv_nsec = tp.tv_usec * 1000;
  402. ts.tv_sec = tp.tv_sec;
  403. #else
  404. clock_gettime(CLOCK_REALTIME, &ts);
  405. #endif
  406. ts.tv_nsec += ms * 1000000;
  407. ts.tv_sec += ts.tv_nsec / 1000000000;
  408. ts.tv_nsec %= 1000000000;
  409. NxI32 result = pthread_cond_timedwait(&mEvent, &mEventMutex, &ts);
  410. assert(result == 0 || result == ETIMEDOUT);
  411. }
  412. VERIFY( pthread_mutex_unlock(&mEventMutex) == 0 );
  413. #endif
  414. }
  415. private:
  416. #if defined(WIN32) || defined(_XBOX)
  417. HANDLE mEvent;
  418. #elif defined(__APPLE__) || defined(__linux__)
  419. pthread_mutex_t mEventMutex;
  420. pthread_cond_t mEvent;
  421. #endif
  422. };
  423. ThreadEvent * tc_createThreadEvent(void)
  424. {
  425. MyThreadEvent *m = new MyThreadEvent;
  426. return static_cast<ThreadEvent *>(m);
  427. }
  428. void tc_releaseThreadEvent(ThreadEvent *t)
  429. {
  430. MyThreadEvent *m = static_cast< MyThreadEvent *>(t);
  431. delete m;
  432. }
  433. }; // end of namespace