as_thread.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2014 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_thread.cpp
  25. //
  26. // Functions for multi threading support
  27. //
  28. #include "as_config.h"
  29. #include "as_thread.h"
  30. #include "as_atomic.h"
  31. BEGIN_AS_NAMESPACE
  32. //=======================================================================
  33. // Singleton
  34. static asCThreadManager *threadManager = 0;
  35. //======================================================================
  36. // Global API functions
  37. extern "C"
  38. {
  39. AS_API int asThreadCleanup()
  40. {
  41. return asCThreadManager::CleanupLocalData();
  42. }
  43. AS_API asIThreadManager *asGetThreadManager()
  44. {
  45. return threadManager;
  46. }
  47. AS_API int asPrepareMultithread(asIThreadManager *externalThreadMgr)
  48. {
  49. return asCThreadManager::Prepare(externalThreadMgr);
  50. }
  51. AS_API void asUnprepareMultithread()
  52. {
  53. asCThreadManager::Unprepare();
  54. }
  55. AS_API void asAcquireExclusiveLock()
  56. {
  57. if( threadManager )
  58. {
  59. ACQUIREEXCLUSIVE(threadManager->appRWLock);
  60. }
  61. }
  62. AS_API void asReleaseExclusiveLock()
  63. {
  64. if( threadManager )
  65. {
  66. RELEASEEXCLUSIVE(threadManager->appRWLock);
  67. }
  68. }
  69. AS_API void asAcquireSharedLock()
  70. {
  71. if( threadManager )
  72. {
  73. ACQUIRESHARED(threadManager->appRWLock);
  74. }
  75. }
  76. AS_API void asReleaseSharedLock()
  77. {
  78. if( threadManager )
  79. {
  80. RELEASESHARED(threadManager->appRWLock);
  81. }
  82. }
  83. }
  84. //======================================================================
  85. #if !defined(AS_NO_THREADS) && defined(_MSC_VER) && defined(AS_WINDOWS_THREADS) && !(WINAPI_FAMILY & WINAPI_PARTITION_DESKTOP)
  86. __declspec(thread) asCThreadLocalData *asCThreadManager::tld = 0;
  87. #endif
  88. asCThreadManager::asCThreadManager()
  89. {
  90. // We're already in the critical section when this function is called
  91. #ifdef AS_NO_THREADS
  92. tld = 0;
  93. #else
  94. // Allocate the thread local storage
  95. #if defined AS_POSIX_THREADS
  96. pthread_key_t pKey;
  97. pthread_key_create(&pKey, 0);
  98. tlsKey = (asDWORD)pKey;
  99. #elif defined AS_WINDOWS_THREADS
  100. #if defined(_MSC_VER) && !(WINAPI_FAMILY & WINAPI_PARTITION_DESKTOP)
  101. tld = 0;
  102. #else
  103. tlsKey = (asDWORD)TlsAlloc();
  104. #endif
  105. #endif
  106. #endif
  107. refCount = 1;
  108. }
  109. int asCThreadManager::Prepare(asIThreadManager *externalThreadMgr)
  110. {
  111. // Don't allow an external thread manager if there
  112. // is already a thread manager defined
  113. if( externalThreadMgr && threadManager )
  114. return asINVALID_ARG;
  115. // The critical section cannot be declared globally, as there is no
  116. // guarantee for the order in which global variables are initialized
  117. // or uninitialized.
  118. // For this reason it's not possible to prevent two threads from calling
  119. // AddRef at the same time, so there is a chance for a race condition here.
  120. // To avoid the race condition when the thread manager is first created,
  121. // the application must make sure to call the global asPrepareForMultiThread()
  122. // in the main thread before any other thread creates a script engine.
  123. if( threadManager == 0 && externalThreadMgr == 0 )
  124. threadManager = asNEW(asCThreadManager);
  125. else
  126. {
  127. // If an application uses different dlls each dll will get it's own memory
  128. // space for global variables. If multiple dlls then uses AngelScript's
  129. // global thread support functions it is then best to share the thread
  130. // manager to make sure all dlls use the same critical section.
  131. if( externalThreadMgr )
  132. threadManager = reinterpret_cast<asCThreadManager*>(externalThreadMgr);
  133. ENTERCRITICALSECTION(threadManager->criticalSection);
  134. threadManager->refCount++;
  135. LEAVECRITICALSECTION(threadManager->criticalSection);
  136. }
  137. // Success
  138. return 0;
  139. }
  140. void asCThreadManager::Unprepare()
  141. {
  142. asASSERT(threadManager);
  143. if( threadManager == 0 )
  144. return;
  145. // It's necessary to protect this section so no
  146. // other thread attempts to call AddRef or Release
  147. // while clean up is in progress.
  148. ENTERCRITICALSECTION(threadManager->criticalSection);
  149. if( --threadManager->refCount == 0 )
  150. {
  151. // Make sure the local data is destroyed, at least for the current thread
  152. CleanupLocalData();
  153. // As the critical section will be destroyed together
  154. // with the thread manager we must first clear the global
  155. // variable in case a new thread manager needs to be created;
  156. asCThreadManager *mgr = threadManager;
  157. threadManager = 0;
  158. // Leave the critical section before it is destroyed
  159. LEAVECRITICALSECTION(mgr->criticalSection);
  160. asDELETE(mgr,asCThreadManager);
  161. }
  162. else
  163. LEAVECRITICALSECTION(threadManager->criticalSection);
  164. }
  165. asCThreadManager::~asCThreadManager()
  166. {
  167. #ifndef AS_NO_THREADS
  168. // Deallocate the thread local storage
  169. #if defined AS_POSIX_THREADS
  170. pthread_key_delete((pthread_key_t)tlsKey);
  171. #elif defined AS_WINDOWS_THREADS
  172. #if defined(_MSC_VER) && !(WINAPI_FAMILY & WINAPI_PARTITION_DESKTOP)
  173. tld = 0;
  174. #else
  175. TlsFree((DWORD)tlsKey);
  176. #endif
  177. #endif
  178. #else
  179. if( tld )
  180. {
  181. asDELETE(tld,asCThreadLocalData);
  182. }
  183. tld = 0;
  184. #endif
  185. }
  186. int asCThreadManager::CleanupLocalData()
  187. {
  188. if( threadManager == 0 )
  189. return 0;
  190. #ifndef AS_NO_THREADS
  191. #if defined AS_POSIX_THREADS
  192. asCThreadLocalData *tld = (asCThreadLocalData*)pthread_getspecific((pthread_key_t)threadManager->tlsKey);
  193. #elif defined AS_WINDOWS_THREADS
  194. #if !defined(_MSC_VER) || (WINAPI_FAMILY & WINAPI_PARTITION_DESKTOP)
  195. asCThreadLocalData *tld = (asCThreadLocalData*)TlsGetValue((DWORD)threadManager->tlsKey);
  196. #endif
  197. #endif
  198. if( tld == 0 )
  199. return 0;
  200. if( tld->activeContexts.GetLength() == 0 )
  201. {
  202. asDELETE(tld,asCThreadLocalData);
  203. #if defined AS_POSIX_THREADS
  204. pthread_setspecific((pthread_key_t)threadManager->tlsKey, 0);
  205. #elif defined AS_WINDOWS_THREADS
  206. #if defined(_MSC_VER) && !(WINAPI_FAMILY & WINAPI_PARTITION_DESKTOP)
  207. tld = 0;
  208. #else
  209. TlsSetValue((DWORD)threadManager->tlsKey, 0);
  210. #endif
  211. #endif
  212. return 0;
  213. }
  214. else
  215. return asCONTEXT_ACTIVE;
  216. #else
  217. if( threadManager->tld )
  218. {
  219. if( threadManager->tld->activeContexts.GetLength() == 0 )
  220. {
  221. asDELETE(threadManager->tld,asCThreadLocalData);
  222. threadManager->tld = 0;
  223. }
  224. else
  225. return asCONTEXT_ACTIVE;
  226. }
  227. return 0;
  228. #endif
  229. }
  230. asCThreadLocalData *asCThreadManager::GetLocalData()
  231. {
  232. if( threadManager == 0 )
  233. return 0;
  234. #ifndef AS_NO_THREADS
  235. #if defined AS_POSIX_THREADS
  236. asCThreadLocalData *tld = (asCThreadLocalData*)pthread_getspecific((pthread_key_t)threadManager->tlsKey);
  237. if( tld == 0 )
  238. {
  239. tld = asNEW(asCThreadLocalData)();
  240. pthread_setspecific((pthread_key_t)threadManager->tlsKey, tld);
  241. }
  242. #elif defined AS_WINDOWS_THREADS
  243. #if defined(_MSC_VER) && !(WINAPI_FAMILY & WINAPI_PARTITION_DESKTOP)
  244. if( tld == 0 )
  245. tld = asNEW(asCThreadLocalData)();
  246. #else
  247. asCThreadLocalData *tld = (asCThreadLocalData*)TlsGetValue((DWORD)threadManager->tlsKey);
  248. if( tld == 0 )
  249. {
  250. tld = asNEW(asCThreadLocalData)();
  251. TlsSetValue((DWORD)threadManager->tlsKey, tld);
  252. }
  253. #endif
  254. #endif
  255. return tld;
  256. #else
  257. if( threadManager->tld == 0 )
  258. threadManager->tld = asNEW(asCThreadLocalData)();
  259. return threadManager->tld;
  260. #endif
  261. }
  262. //=========================================================================
  263. asCThreadLocalData::asCThreadLocalData()
  264. {
  265. }
  266. asCThreadLocalData::~asCThreadLocalData()
  267. {
  268. }
  269. //=========================================================================
  270. #ifndef AS_NO_THREADS
  271. asCThreadCriticalSection::asCThreadCriticalSection()
  272. {
  273. #if defined AS_POSIX_THREADS
  274. pthread_mutex_init(&cs, 0);
  275. #elif defined AS_WINDOWS_THREADS
  276. #ifdef _MSC_VER
  277. InitializeCriticalSectionEx(&cs, 4000, 0);
  278. #else
  279. InitializeCriticalSection(&cs);
  280. #endif
  281. #endif
  282. }
  283. asCThreadCriticalSection::~asCThreadCriticalSection()
  284. {
  285. #if defined AS_POSIX_THREADS
  286. pthread_mutex_destroy(&cs);
  287. #elif defined AS_WINDOWS_THREADS
  288. DeleteCriticalSection(&cs);
  289. #endif
  290. }
  291. void asCThreadCriticalSection::Enter()
  292. {
  293. #if defined AS_POSIX_THREADS
  294. pthread_mutex_lock(&cs);
  295. #elif defined AS_WINDOWS_THREADS
  296. EnterCriticalSection(&cs);
  297. #endif
  298. }
  299. void asCThreadCriticalSection::Leave()
  300. {
  301. #if defined AS_POSIX_THREADS
  302. pthread_mutex_unlock(&cs);
  303. #elif defined AS_WINDOWS_THREADS
  304. LeaveCriticalSection(&cs);
  305. #endif
  306. }
  307. bool asCThreadCriticalSection::TryEnter()
  308. {
  309. #if defined AS_POSIX_THREADS
  310. return !pthread_mutex_trylock(&cs);
  311. #elif defined AS_WINDOWS_THREADS
  312. return TryEnterCriticalSection(&cs) ? true : false;
  313. #else
  314. return true;
  315. #endif
  316. }
  317. asCThreadReadWriteLock::asCThreadReadWriteLock()
  318. {
  319. #if defined AS_POSIX_THREADS
  320. int r = pthread_rwlock_init(&lock, 0);
  321. asASSERT( r == 0 );
  322. UNUSED_VAR(r);
  323. #elif defined AS_WINDOWS_THREADS
  324. #ifdef _MSC_VER
  325. // Create a semaphore to allow up to maxReaders simultaneous readers
  326. readLocks = CreateSemaphoreExW(NULL, maxReaders, maxReaders, 0, 0, 0);
  327. // Create a critical section to synchronize writers
  328. InitializeCriticalSectionEx(&writeLock, 4000, 0);
  329. #else
  330. readLocks = CreateSemaphoreW(NULL, maxReaders, maxReaders, 0);
  331. InitializeCriticalSection(&writeLock);
  332. #endif
  333. #endif
  334. }
  335. asCThreadReadWriteLock::~asCThreadReadWriteLock()
  336. {
  337. #if defined AS_POSIX_THREADS
  338. pthread_rwlock_destroy(&lock);
  339. #elif defined AS_WINDOWS_THREADS
  340. DeleteCriticalSection(&writeLock);
  341. CloseHandle(readLocks);
  342. #endif
  343. }
  344. void asCThreadReadWriteLock::AcquireExclusive()
  345. {
  346. #if defined AS_POSIX_THREADS
  347. pthread_rwlock_wrlock(&lock);
  348. #elif defined AS_WINDOWS_THREADS
  349. // Synchronize writers, so only one tries to lock out the readers
  350. EnterCriticalSection(&writeLock);
  351. // Lock all reader out from the semaphore. Do this one by one,
  352. // so the lock doesn't have to wait until there are no readers at all.
  353. // If we try to lock all at once it is quite possible the writer will
  354. // never succeed.
  355. for( asUINT n = 0; n < maxReaders; n++ )
  356. WaitForSingleObjectEx(readLocks, INFINITE, FALSE);
  357. // Allow another writer to lock. It will only be able to
  358. // lock the readers when this writer releases them anyway.
  359. LeaveCriticalSection(&writeLock);
  360. #endif
  361. }
  362. void asCThreadReadWriteLock::ReleaseExclusive()
  363. {
  364. #if defined AS_POSIX_THREADS
  365. pthread_rwlock_unlock(&lock);
  366. #elif defined AS_WINDOWS_THREADS
  367. // Release all readers at once
  368. ReleaseSemaphore(readLocks, maxReaders, 0);
  369. #endif
  370. }
  371. void asCThreadReadWriteLock::AcquireShared()
  372. {
  373. #if defined AS_POSIX_THREADS
  374. pthread_rwlock_rdlock(&lock);
  375. #elif defined AS_WINDOWS_THREADS
  376. // Lock a reader slot
  377. WaitForSingleObjectEx(readLocks, INFINITE, FALSE);
  378. #endif
  379. }
  380. void asCThreadReadWriteLock::ReleaseShared()
  381. {
  382. #if defined AS_POSIX_THREADS
  383. pthread_rwlock_unlock(&lock);
  384. #elif defined AS_WINDOWS_THREADS
  385. // Release the reader slot
  386. ReleaseSemaphore(readLocks, 1, 0);
  387. #endif
  388. }
  389. #endif
  390. //========================================================================
  391. END_AS_NAMESPACE