as_thread.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. andreas@angelcode.com
  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_FAMILY_PHONE_APP)
  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_FAMILY_PHONE_APP)
  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_FAMILY_PHONE_APP)
  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_FAMILY_PHONE_APP)
  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_FAMILY_PHONE_APP)
  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_FAMILY_PHONE_APP)
  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. #if defined(_MSC_VER) && (WINAPI_FAMILY & WINAPI_FAMILY_PHONE_APP)
  277. // Only the Ex version is available on Windows Store
  278. InitializeCriticalSectionEx(&cs, 4000, 0);
  279. #else
  280. // Only the non-Ex version is available on WinXP and older
  281. // MinGW also only defines this version
  282. InitializeCriticalSection(&cs);
  283. #endif
  284. #endif
  285. }
  286. asCThreadCriticalSection::~asCThreadCriticalSection()
  287. {
  288. #if defined AS_POSIX_THREADS
  289. pthread_mutex_destroy(&cs);
  290. #elif defined AS_WINDOWS_THREADS
  291. DeleteCriticalSection(&cs);
  292. #endif
  293. }
  294. void asCThreadCriticalSection::Enter()
  295. {
  296. #if defined AS_POSIX_THREADS
  297. pthread_mutex_lock(&cs);
  298. #elif defined AS_WINDOWS_THREADS
  299. EnterCriticalSection(&cs);
  300. #endif
  301. }
  302. void asCThreadCriticalSection::Leave()
  303. {
  304. #if defined AS_POSIX_THREADS
  305. pthread_mutex_unlock(&cs);
  306. #elif defined AS_WINDOWS_THREADS
  307. LeaveCriticalSection(&cs);
  308. #endif
  309. }
  310. bool asCThreadCriticalSection::TryEnter()
  311. {
  312. #if defined AS_POSIX_THREADS
  313. return !pthread_mutex_trylock(&cs);
  314. #elif defined AS_WINDOWS_THREADS
  315. return TryEnterCriticalSection(&cs) ? true : false;
  316. #else
  317. return true;
  318. #endif
  319. }
  320. asCThreadReadWriteLock::asCThreadReadWriteLock()
  321. {
  322. #if defined AS_POSIX_THREADS
  323. int r = pthread_rwlock_init(&lock, 0);
  324. asASSERT( r == 0 );
  325. UNUSED_VAR(r);
  326. #elif defined AS_WINDOWS_THREADS
  327. #if defined(_MSC_VER) && (WINAPI_FAMILY & WINAPI_FAMILY_PHONE_APP)
  328. // Only the Ex versions are available on Windows Store
  329. // Create a semaphore to allow up to maxReaders simultaneous readers
  330. readLocks = CreateSemaphoreExW(NULL, maxReaders, maxReaders, 0, 0, 0);
  331. // Create a critical section to synchronize writers
  332. InitializeCriticalSectionEx(&writeLock, 4000, 0);
  333. #else
  334. readLocks = CreateSemaphoreW(NULL, maxReaders, maxReaders, 0);
  335. InitializeCriticalSection(&writeLock);
  336. #endif
  337. #endif
  338. }
  339. asCThreadReadWriteLock::~asCThreadReadWriteLock()
  340. {
  341. #if defined AS_POSIX_THREADS
  342. pthread_rwlock_destroy(&lock);
  343. #elif defined AS_WINDOWS_THREADS
  344. DeleteCriticalSection(&writeLock);
  345. CloseHandle(readLocks);
  346. #endif
  347. }
  348. void asCThreadReadWriteLock::AcquireExclusive()
  349. {
  350. #if defined AS_POSIX_THREADS
  351. pthread_rwlock_wrlock(&lock);
  352. #elif defined AS_WINDOWS_THREADS
  353. // Synchronize writers, so only one tries to lock out the readers
  354. EnterCriticalSection(&writeLock);
  355. // Lock all reader out from the semaphore. Do this one by one,
  356. // so the lock doesn't have to wait until there are no readers at all.
  357. // If we try to lock all at once it is quite possible the writer will
  358. // never succeed.
  359. for( asUINT n = 0; n < maxReaders; n++ )
  360. WaitForSingleObjectEx(readLocks, INFINITE, FALSE);
  361. // Allow another writer to lock. It will only be able to
  362. // lock the readers when this writer releases them anyway.
  363. LeaveCriticalSection(&writeLock);
  364. #endif
  365. }
  366. void asCThreadReadWriteLock::ReleaseExclusive()
  367. {
  368. #if defined AS_POSIX_THREADS
  369. pthread_rwlock_unlock(&lock);
  370. #elif defined AS_WINDOWS_THREADS
  371. // Release all readers at once
  372. ReleaseSemaphore(readLocks, maxReaders, 0);
  373. #endif
  374. }
  375. void asCThreadReadWriteLock::AcquireShared()
  376. {
  377. #if defined AS_POSIX_THREADS
  378. pthread_rwlock_rdlock(&lock);
  379. #elif defined AS_WINDOWS_THREADS
  380. // Lock a reader slot
  381. WaitForSingleObjectEx(readLocks, INFINITE, FALSE);
  382. #endif
  383. }
  384. void asCThreadReadWriteLock::ReleaseShared()
  385. {
  386. #if defined AS_POSIX_THREADS
  387. pthread_rwlock_unlock(&lock);
  388. #elif defined AS_WINDOWS_THREADS
  389. // Release the reader slot
  390. ReleaseSemaphore(readLocks, 1, 0);
  391. #endif
  392. }
  393. #endif
  394. //========================================================================
  395. END_AS_NAMESPACE