NvThreadConfig.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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. #else
  119. __asm { pause };
  120. #endif
  121. }
  122. void tc_interlockedExchange(void *dest, const int64_t exchange)
  123. {
  124. #if defined( __linux__ ) || defined( __APPLE__ )
  125. // not working
  126. assert(false);
  127. //__sync_lock_test_and_set((int64_t*)dest, exchange);
  128. #elif defined( _XBOX ) || defined( _WIN64 )
  129. InterlockedExchange((volatile LONG *)dest, exchange);
  130. #else
  131. __asm
  132. {
  133. mov ebx, dword ptr [exchange]
  134. mov ecx, dword ptr [exchange + 4]
  135. mov edi, dest
  136. mov eax, dword ptr [edi]
  137. mov edx, dword ptr [edi + 4]
  138. jmp start
  139. retry:
  140. pause
  141. start:
  142. lock cmpxchg8b [edi]
  143. jnz retry
  144. };
  145. #endif
  146. }
  147. NxI32 tc_interlockedCompareExchange(void *dest, NxI32 exchange, NxI32 compare)
  148. {
  149. #if defined( __linux__ ) || defined( __APPLE__ )
  150. // not working
  151. assert(false);
  152. return 0;
  153. //return __sync_val_compare_and_swap((uintptr_t*)dest, exchange, compare);
  154. //return __sync_bool_compare_and_swap((uintptr_t*)dest, exchange, compare);
  155. #elif defined( _XBOX ) || defined( _WIN64 )
  156. return InterlockedCompareExchange((volatile LONG *)dest, exchange, compare);
  157. #else
  158. char _ret;
  159. //
  160. __asm
  161. {
  162. mov edx, [dest]
  163. mov eax, [compare]
  164. mov ecx, [exchange]
  165. lock cmpxchg [edx], ecx
  166. setz al
  167. mov byte ptr [_ret], al
  168. }
  169. //
  170. return _ret;
  171. #endif
  172. }
  173. NxI32 tc_interlockedCompareExchange(void *dest, const NxI32 exchange1, const NxI32 exchange2, const NxI32 compare1, const NxI32 compare2)
  174. {
  175. #if defined( __linux__ ) || defined( __APPLE__ )
  176. // not working
  177. assert(false);
  178. return 0;
  179. //uint64_t exchange = ((uint64_t)exchange1 << 32) | (uint64_t)exchange2;
  180. //uint64_t compare = ((uint64_t)compare1 << 32) | (uint64_t)compare2;
  181. //return __sync_bool_compare_and_swap((int64_t*)dest, exchange, compare);
  182. #elif defined( _XBOX ) || defined( _WIN64 )
  183. assert(false);
  184. return 0;
  185. #else
  186. char _ret;
  187. //
  188. __asm
  189. {
  190. mov ebx, [exchange1]
  191. mov ecx, [exchange2]
  192. mov edi, [dest]
  193. mov eax, [compare1]
  194. mov edx, [compare2]
  195. lock cmpxchg8b [edi]
  196. setz al
  197. mov byte ptr [_ret], al
  198. }
  199. //
  200. return _ret;
  201. #endif
  202. }
  203. class MyThreadMutex : public ThreadMutex
  204. {
  205. public:
  206. MyThreadMutex(void)
  207. {
  208. #if defined(WIN32) || defined(_XBOX)
  209. InitializeCriticalSection(&m_Mutex);
  210. #elif defined(__APPLE__) || defined(__linux__)
  211. pthread_mutexattr_t mutexAttr; // Mutex Attribute
  212. VERIFY( pthread_mutexattr_init(&mutexAttr) == 0 );
  213. VERIFY( pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) == 0 );
  214. VERIFY( pthread_mutex_init(&m_Mutex, &mutexAttr) == 0 );
  215. VERIFY( pthread_mutexattr_destroy(&mutexAttr) == 0 );
  216. #endif
  217. }
  218. ~MyThreadMutex(void)
  219. {
  220. #if defined(WIN32) || defined(_XBOX)
  221. DeleteCriticalSection(&m_Mutex);
  222. #elif defined(__APPLE__) || defined(__linux__)
  223. VERIFY( pthread_mutex_destroy(&m_Mutex) == 0 );
  224. #endif
  225. }
  226. void lock(void)
  227. {
  228. #if defined(WIN32) || defined(_XBOX)
  229. EnterCriticalSection(&m_Mutex);
  230. #elif defined(__APPLE__) || defined(__linux__)
  231. VERIFY( pthread_mutex_lock(&m_Mutex) == 0 );
  232. #endif
  233. }
  234. bool tryLock(void)
  235. {
  236. #if defined(WIN32) || defined(_XBOX)
  237. bool bRet = false;
  238. //assert(("TryEnterCriticalSection seems to not work on XP???", 0));
  239. bRet = TryEnterCriticalSection(&m_Mutex) ? true : false;
  240. return bRet;
  241. #elif defined(__APPLE__) || defined(__linux__)
  242. NxI32 result = pthread_mutex_trylock(&m_Mutex);
  243. return (result == 0);
  244. #endif
  245. }
  246. void unlock(void)
  247. {
  248. #if defined(WIN32) || defined(_XBOX)
  249. LeaveCriticalSection(&m_Mutex);
  250. #elif defined(__APPLE__) || defined(__linux__)
  251. VERIFY( pthread_mutex_unlock(&m_Mutex) == 0 );
  252. #endif
  253. }
  254. private:
  255. #if defined(WIN32) || defined(_XBOX)
  256. CRITICAL_SECTION m_Mutex;
  257. #elif defined(__APPLE__) || defined(__linux__)
  258. pthread_mutex_t m_Mutex;
  259. #endif
  260. };
  261. ThreadMutex * tc_createThreadMutex(void)
  262. {
  263. MyThreadMutex *m = new MyThreadMutex;
  264. return static_cast< ThreadMutex *>(m);
  265. }
  266. void tc_releaseThreadMutex(ThreadMutex *tm)
  267. {
  268. MyThreadMutex *m = static_cast< MyThreadMutex *>(tm);
  269. delete m;
  270. }
  271. #if defined(WIN32) || defined(_XBOX)
  272. static unsigned long __stdcall _ThreadWorkerFunc(LPVOID arg);
  273. #elif defined(__APPLE__) || defined(__linux__)
  274. static void* _ThreadWorkerFunc(void* arg);
  275. #endif
  276. class MyThread : public Thread
  277. {
  278. public:
  279. MyThread(ThreadInterface *iface)
  280. {
  281. mInterface = iface;
  282. #if defined(WIN32) || defined(_XBOX)
  283. mThread = CreateThread(0, 0, _ThreadWorkerFunc, this, 0, 0);
  284. #elif defined(__APPLE__) || defined(__linux__)
  285. VERIFY( pthread_create(&mThread, NULL, _ThreadWorkerFunc, this) == 0 );
  286. #endif
  287. }
  288. ~MyThread(void)
  289. {
  290. #if defined(WIN32) || defined(_XBOX)
  291. if ( mThread )
  292. {
  293. CloseHandle(mThread);
  294. mThread = 0;
  295. }
  296. #endif
  297. }
  298. void onJobExecute(void)
  299. {
  300. mInterface->threadMain();
  301. }
  302. private:
  303. ThreadInterface *mInterface;
  304. #if defined(WIN32) || defined(_XBOX)
  305. HANDLE mThread;
  306. #elif defined(__APPLE__) || defined(__linux__)
  307. pthread_t mThread;
  308. #endif
  309. };
  310. Thread * tc_createThread(ThreadInterface *tinterface)
  311. {
  312. MyThread *m = new MyThread(tinterface);
  313. return static_cast< Thread *>(m);
  314. }
  315. void tc_releaseThread(Thread *t)
  316. {
  317. MyThread *m = static_cast<MyThread *>(t);
  318. delete m;
  319. }
  320. #if defined(WIN32) || defined(_XBOX)
  321. static unsigned long __stdcall _ThreadWorkerFunc(LPVOID arg)
  322. #elif defined(__APPLE__) || defined(__linux__)
  323. static void* _ThreadWorkerFunc(void* arg)
  324. #endif
  325. {
  326. MyThread *worker = (MyThread *) arg;
  327. worker->onJobExecute();
  328. return 0;
  329. }
  330. class MyThreadEvent : public ThreadEvent
  331. {
  332. public:
  333. MyThreadEvent(void)
  334. {
  335. #if defined(WIN32) || defined(_XBOX)
  336. mEvent = ::CreateEventA(NULL,TRUE,TRUE,"ThreadEvent");
  337. #elif defined(__APPLE__) || defined(__linux__)
  338. pthread_mutexattr_t mutexAttr; // Mutex Attribute
  339. VERIFY( pthread_mutexattr_init(&mutexAttr) == 0 );
  340. VERIFY( pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP) == 0 );
  341. VERIFY( pthread_mutex_init(&mEventMutex, &mutexAttr) == 0 );
  342. VERIFY( pthread_mutexattr_destroy(&mutexAttr) == 0 );
  343. VERIFY( pthread_cond_init(&mEvent, NULL) == 0 );
  344. #endif
  345. }
  346. ~MyThreadEvent(void)
  347. {
  348. #if defined(WIN32) || defined(_XBOX)
  349. if ( mEvent )
  350. {
  351. ::CloseHandle(mEvent);
  352. }
  353. #elif defined(__APPLE__) || defined(__linux__)
  354. VERIFY( pthread_cond_destroy(&mEvent) == 0 );
  355. VERIFY( pthread_mutex_destroy(&mEventMutex) == 0 );
  356. #endif
  357. }
  358. virtual void setEvent(void) // signal the event
  359. {
  360. #if defined(WIN32) || defined(_XBOX)
  361. if ( mEvent )
  362. {
  363. ::SetEvent(mEvent);
  364. }
  365. #elif defined(__APPLE__) || defined(__linux__)
  366. VERIFY( pthread_mutex_lock(&mEventMutex) == 0 );
  367. VERIFY( pthread_cond_signal(&mEvent) == 0 );
  368. VERIFY( pthread_mutex_unlock(&mEventMutex) == 0 );
  369. #endif
  370. }
  371. void resetEvent(void)
  372. {
  373. #if defined(WIN32) || defined(_XBOX)
  374. if ( mEvent )
  375. {
  376. ::ResetEvent(mEvent);
  377. }
  378. #endif
  379. }
  380. virtual void waitForSingleObject(NxU32 ms)
  381. {
  382. #if defined(WIN32) || defined(_XBOX)
  383. if ( mEvent )
  384. {
  385. ::WaitForSingleObject(mEvent,ms);
  386. }
  387. #elif defined(__APPLE__) || defined(__linux__)
  388. VERIFY( pthread_mutex_lock(&mEventMutex) == 0 );
  389. if (ms == 0xffffffff)
  390. {
  391. VERIFY( pthread_cond_wait(&mEvent, &mEventMutex) == 0 );
  392. }
  393. else
  394. {
  395. struct timespec ts;
  396. #ifdef __APPLE__
  397. struct timeval tp;
  398. gettimeofday(&tp, (struct timezone *)0);
  399. ts.tv_nsec = tp.tv_usec * 1000;
  400. ts.tv_sec = tp.tv_sec;
  401. #else
  402. clock_gettime(CLOCK_REALTIME, &ts);
  403. #endif
  404. ts.tv_nsec += ms * 1000000;
  405. ts.tv_sec += ts.tv_nsec / 1000000000;
  406. ts.tv_nsec %= 1000000000;
  407. NxI32 result = pthread_cond_timedwait(&mEvent, &mEventMutex, &ts);
  408. assert(result == 0 || result == ETIMEDOUT);
  409. }
  410. VERIFY( pthread_mutex_unlock(&mEventMutex) == 0 );
  411. #endif
  412. }
  413. private:
  414. #if defined(WIN32) || defined(_XBOX)
  415. HANDLE mEvent;
  416. #elif defined(__APPLE__) || defined(__linux__)
  417. pthread_mutex_t mEventMutex;
  418. pthread_cond_t mEvent;
  419. #endif
  420. };
  421. ThreadEvent * tc_createThreadEvent(void)
  422. {
  423. MyThreadEvent *m = new MyThreadEvent;
  424. return static_cast<ThreadEvent *>(m);
  425. }
  426. void tc_releaseThreadEvent(ThreadEvent *t)
  427. {
  428. MyThreadEvent *m = static_cast< MyThreadEvent *>(t);
  429. delete m;
  430. }
  431. }; // end of namespace