as_thread.cpp 11 KB

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