NvThreadConfig.cpp 12 KB

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