as_thread.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2013 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. asCThreadManager::asCThreadManager()
  86. {
  87. // We're already in the critical section when this function is called
  88. #ifdef AS_NO_THREADS
  89. tld = 0;
  90. #else
  91. // Allocate the thread local storage
  92. #if defined AS_POSIX_THREADS
  93. pthread_key_t pKey;
  94. pthread_key_create(&pKey, 0);
  95. tlsKey = (asDWORD)pKey;
  96. #elif defined AS_WINDOWS_THREADS
  97. tlsKey = (asDWORD)TlsAlloc();
  98. #endif
  99. #endif
  100. refCount = 1;
  101. }
  102. int asCThreadManager::Prepare(asIThreadManager *externalThreadMgr)
  103. {
  104. // Don't allow an external thread manager if there
  105. // is already a thread manager defined
  106. if( externalThreadMgr && threadManager )
  107. return asINVALID_ARG;
  108. // The critical section cannot be declared globally, as there is no
  109. // guarantee for the order in which global variables are initialized
  110. // or uninitialized.
  111. // For this reason it's not possible to prevent two threads from calling
  112. // AddRef at the same time, so there is a chance for a race condition here.
  113. // To avoid the race condition when the thread manager is first created,
  114. // the application must make sure to call the global asPrepareForMultiThread()
  115. // in the main thread before any other thread creates a script engine.
  116. if( threadManager == 0 && externalThreadMgr == 0 )
  117. threadManager = asNEW(asCThreadManager);
  118. else
  119. {
  120. // If an application uses different dlls each dll will get it's own memory
  121. // space for global variables. If multiple dlls then uses AngelScript's
  122. // global thread support functions it is then best to share the thread
  123. // manager to make sure all dlls use the same critical section.
  124. if( externalThreadMgr )
  125. threadManager = reinterpret_cast<asCThreadManager*>(externalThreadMgr);
  126. ENTERCRITICALSECTION(threadManager->criticalSection);
  127. threadManager->refCount++;
  128. LEAVECRITICALSECTION(threadManager->criticalSection);
  129. }
  130. // Success
  131. return 0;
  132. }
  133. void asCThreadManager::Unprepare()
  134. {
  135. asASSERT(threadManager);
  136. if( threadManager == 0 )
  137. return;
  138. // It's necessary to protect this section so no
  139. // other thread attempts to call AddRef or Release
  140. // while clean up is in progress.
  141. ENTERCRITICALSECTION(threadManager->criticalSection);
  142. if( --threadManager->refCount == 0 )
  143. {
  144. // Make sure the local data is destroyed, at least for the current thread
  145. CleanupLocalData();
  146. // As the critical section will be destroyed together
  147. // with the thread manager we must first clear the global
  148. // variable in case a new thread manager needs to be created;
  149. asCThreadManager *mgr = threadManager;
  150. threadManager = 0;
  151. // Leave the critical section before it is destroyed
  152. LEAVECRITICALSECTION(mgr->criticalSection);
  153. asDELETE(mgr,asCThreadManager);
  154. }
  155. else
  156. LEAVECRITICALSECTION(threadManager->criticalSection);
  157. }
  158. asCThreadManager::~asCThreadManager()
  159. {
  160. #ifndef AS_NO_THREADS
  161. // Deallocate the thread local storage
  162. #if defined AS_POSIX_THREADS
  163. pthread_key_delete((pthread_key_t)tlsKey);
  164. #elif defined AS_WINDOWS_THREADS
  165. TlsFree((DWORD)tlsKey);
  166. #endif
  167. #else
  168. if( tld )
  169. {
  170. asDELETE(tld,asCThreadLocalData);
  171. }
  172. tld = 0;
  173. #endif
  174. }
  175. int asCThreadManager::CleanupLocalData()
  176. {
  177. if( threadManager == 0 )
  178. return 0;
  179. #ifndef AS_NO_THREADS
  180. #if defined AS_POSIX_THREADS
  181. asCThreadLocalData *tld = (asCThreadLocalData*)pthread_getspecific((pthread_key_t)threadManager->tlsKey);
  182. #elif defined AS_WINDOWS_THREADS
  183. asCThreadLocalData *tld = (asCThreadLocalData*)TlsGetValue((DWORD)threadManager->tlsKey);
  184. #endif
  185. if( tld == 0 )
  186. return 0;
  187. if( tld->activeContexts.GetLength() == 0 )
  188. {
  189. asDELETE(tld,asCThreadLocalData);
  190. #if defined AS_POSIX_THREADS
  191. pthread_setspecific((pthread_key_t)threadManager->tlsKey, 0);
  192. #elif defined AS_WINDOWS_THREADS
  193. TlsSetValue((DWORD)threadManager->tlsKey, 0);
  194. #endif
  195. return 0;
  196. }
  197. else
  198. return asCONTEXT_ACTIVE;
  199. #else
  200. if( threadManager->tld )
  201. {
  202. if( threadManager->tld->activeContexts.GetLength() == 0 )
  203. {
  204. asDELETE(threadManager->tld,asCThreadLocalData);
  205. threadManager->tld = 0;
  206. }
  207. else
  208. return asCONTEXT_ACTIVE;
  209. }
  210. return 0;
  211. #endif
  212. }
  213. asCThreadLocalData *asCThreadManager::GetLocalData()
  214. {
  215. if( threadManager == 0 )
  216. return 0;
  217. #ifndef AS_NO_THREADS
  218. #if defined AS_POSIX_THREADS
  219. asCThreadLocalData *tld = (asCThreadLocalData*)pthread_getspecific((pthread_key_t)threadManager->tlsKey);
  220. if( tld == 0 )
  221. {
  222. tld = asNEW(asCThreadLocalData)();
  223. pthread_setspecific((pthread_key_t)threadManager->tlsKey, tld);
  224. }
  225. #elif defined AS_WINDOWS_THREADS
  226. asCThreadLocalData *tld = (asCThreadLocalData*)TlsGetValue((DWORD)threadManager->tlsKey);
  227. if( tld == 0 )
  228. {
  229. tld = asNEW(asCThreadLocalData)();
  230. TlsSetValue((DWORD)threadManager->tlsKey, tld);
  231. }
  232. #endif
  233. return tld;
  234. #else
  235. if( threadManager->tld == 0 )
  236. threadManager->tld = asNEW(asCThreadLocalData)();
  237. return threadManager->tld;
  238. #endif
  239. }
  240. //=========================================================================
  241. asCThreadLocalData::asCThreadLocalData()
  242. {
  243. }
  244. asCThreadLocalData::~asCThreadLocalData()
  245. {
  246. }
  247. //=========================================================================
  248. #ifndef AS_NO_THREADS
  249. asCThreadCriticalSection::asCThreadCriticalSection()
  250. {
  251. #if defined AS_POSIX_THREADS
  252. pthread_mutex_init(&cs, 0);
  253. #elif defined AS_WINDOWS_THREADS
  254. InitializeCriticalSection(&cs);
  255. #endif
  256. }
  257. asCThreadCriticalSection::~asCThreadCriticalSection()
  258. {
  259. #if defined AS_POSIX_THREADS
  260. pthread_mutex_destroy(&cs);
  261. #elif defined AS_WINDOWS_THREADS
  262. DeleteCriticalSection(&cs);
  263. #endif
  264. }
  265. void asCThreadCriticalSection::Enter()
  266. {
  267. #if defined AS_POSIX_THREADS
  268. pthread_mutex_lock(&cs);
  269. #elif defined AS_WINDOWS_THREADS
  270. EnterCriticalSection(&cs);
  271. #endif
  272. }
  273. void asCThreadCriticalSection::Leave()
  274. {
  275. #if defined AS_POSIX_THREADS
  276. pthread_mutex_unlock(&cs);
  277. #elif defined AS_WINDOWS_THREADS
  278. LeaveCriticalSection(&cs);
  279. #endif
  280. }
  281. bool asCThreadCriticalSection::TryEnter()
  282. {
  283. #if defined AS_POSIX_THREADS
  284. return !pthread_mutex_trylock(&cs);
  285. #elif defined AS_WINDOWS_THREADS
  286. return TryEnterCriticalSection(&cs) ? true : false;
  287. #else
  288. return true;
  289. #endif
  290. }
  291. asCThreadReadWriteLock::asCThreadReadWriteLock()
  292. {
  293. #if defined AS_POSIX_THREADS
  294. int r = pthread_rwlock_init(&lock, 0);
  295. asASSERT( r == 0 );
  296. UNUSED_VAR(r);
  297. #elif defined AS_WINDOWS_THREADS
  298. // Create a semaphore to allow up to maxReaders simultaneous readers
  299. readLocks = CreateSemaphore(NULL, maxReaders, maxReaders, 0);
  300. // Create a critical section to synchronize writers
  301. InitializeCriticalSection(&writeLock);
  302. #endif
  303. }
  304. asCThreadReadWriteLock::~asCThreadReadWriteLock()
  305. {
  306. #if defined AS_POSIX_THREADS
  307. pthread_rwlock_destroy(&lock);
  308. #elif defined AS_WINDOWS_THREADS
  309. DeleteCriticalSection(&writeLock);
  310. CloseHandle(readLocks);
  311. #endif
  312. }
  313. void asCThreadReadWriteLock::AcquireExclusive()
  314. {
  315. #if defined AS_POSIX_THREADS
  316. pthread_rwlock_wrlock(&lock);
  317. #elif defined AS_WINDOWS_THREADS
  318. // Synchronize writers, so only one tries to lock out the readers
  319. EnterCriticalSection(&writeLock);
  320. // Lock all reader out from the semaphore. Do this one by one,
  321. // so the lock doesn't have to wait until there are no readers at all.
  322. // If we try to lock all at once it is quite possible the writer will
  323. // never succeed.
  324. for( asUINT n = 0; n < maxReaders; n++ )
  325. WaitForSingleObject(readLocks, INFINITE);
  326. // Allow another writer to lock. It will only be able to
  327. // lock the readers when this writer releases them anyway.
  328. LeaveCriticalSection(&writeLock);
  329. #endif
  330. }
  331. void asCThreadReadWriteLock::ReleaseExclusive()
  332. {
  333. #if defined AS_POSIX_THREADS
  334. pthread_rwlock_unlock(&lock);
  335. #elif defined AS_WINDOWS_THREADS
  336. // Release all readers at once
  337. ReleaseSemaphore(readLocks, maxReaders, 0);
  338. #endif
  339. }
  340. void asCThreadReadWriteLock::AcquireShared()
  341. {
  342. #if defined AS_POSIX_THREADS
  343. pthread_rwlock_rdlock(&lock);
  344. #elif defined AS_WINDOWS_THREADS
  345. // Lock a reader slot
  346. WaitForSingleObject(readLocks, INFINITE);
  347. #endif
  348. }
  349. void asCThreadReadWriteLock::ReleaseShared()
  350. {
  351. #if defined AS_POSIX_THREADS
  352. pthread_rwlock_unlock(&lock);
  353. #elif defined AS_WINDOWS_THREADS
  354. // Release the reader slot
  355. ReleaseSemaphore(readLocks, 1, 0);
  356. #endif
  357. }
  358. #endif
  359. //========================================================================
  360. END_AS_NAMESPACE