ThreadBoundThreadContextManager.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "RuntimeBasePch.h"
  6. #include "Base/ThreadContextTlsEntry.h"
  7. #include "Base/ThreadBoundThreadContextManager.h"
  8. ThreadBoundThreadContextManager::EntryList ThreadBoundThreadContextManager::entries(&HeapAllocator::Instance);
  9. #if ENABLE_BACKGROUND_JOB_PROCESSOR
  10. JsUtil::BackgroundJobProcessor * ThreadBoundThreadContextManager::s_sharedJobProcessor = NULL;
  11. #endif
  12. CriticalSection ThreadBoundThreadContextManager::s_sharedJobProcessorCreationLock;
  13. ThreadContext * ThreadBoundThreadContextManager::EnsureContextForCurrentThread()
  14. {
  15. AutoCriticalSection lock(ThreadContext::GetCriticalSection());
  16. ThreadContextTLSEntry * entry = ThreadContextTLSEntry::GetEntryForCurrentThread();
  17. if (entry == NULL)
  18. {
  19. ThreadContextTLSEntry::CreateEntryForCurrentThread();
  20. entry = ThreadContextTLSEntry::GetEntryForCurrentThread();
  21. entries.Prepend(entry);
  22. }
  23. ThreadContext * threadContext = entry->GetThreadContext();
  24. // An existing TLS entry may have a null ThreadContext
  25. // DllCanUnload may have cleaned out all the TLS entry when the module lock count is 0,
  26. // but the library didn't get unloaded because someone is holding onto ref count via LoadLibrary.
  27. // Just reinitialize the thread context.
  28. if (threadContext == nullptr)
  29. {
  30. bool requireConcurrencySupport = true;
  31. AllocationPolicyManager * policyManager = HeapNew(AllocationPolicyManager, requireConcurrencySupport);
  32. threadContext = HeapNew(ThreadContext, policyManager);
  33. threadContext->SetIsThreadBound();
  34. if (!ThreadContextTLSEntry::TrySetThreadContext(threadContext))
  35. {
  36. HeapDelete(threadContext);
  37. HeapDelete(policyManager);
  38. return NULL;
  39. }
  40. }
  41. Assert(threadContext != NULL);
  42. return threadContext;
  43. }
  44. void ThreadBoundThreadContextManager::DestroyContextAndEntryForCurrentThread()
  45. {
  46. AutoCriticalSection lock(ThreadContext::GetCriticalSection());
  47. ThreadContextTLSEntry * entry = ThreadContextTLSEntry::GetEntryForCurrentThread();
  48. if (entry == NULL)
  49. {
  50. return;
  51. }
  52. ThreadContext * threadContext = static_cast<ThreadContext *>(entry->GetThreadContext());
  53. entries.Remove(entry);
  54. if (threadContext != NULL && threadContext->IsThreadBound())
  55. {
  56. ShutdownThreadContext(threadContext);
  57. }
  58. ThreadContextTLSEntry::CleanupThread();
  59. }
  60. void ThreadBoundThreadContextManager::DestroyAllContexts()
  61. {
  62. #if ENABLE_BACKGROUND_JOB_PROCESSOR
  63. JsUtil::BackgroundJobProcessor * jobProcessor = NULL;
  64. #endif
  65. // Since BGParseManager has a dependency on threadcontexts, make sure it shuts down first
  66. BGParseManager::DeleteBGParseManager();
  67. {
  68. AutoCriticalSection lock(ThreadContext::GetCriticalSection());
  69. ThreadContextTLSEntry * currentEntry = ThreadContextTLSEntry::GetEntryForCurrentThread();
  70. if (currentEntry == NULL)
  71. {
  72. // We need a current thread entry so that we can use it to release any thread contexts
  73. // we find below.
  74. try
  75. {
  76. AUTO_NESTED_HANDLED_EXCEPTION_TYPE(ExceptionType_OutOfMemory);
  77. currentEntry = ThreadContextTLSEntry::CreateEntryForCurrentThread();
  78. entries.Prepend(currentEntry);
  79. }
  80. catch (Js::OutOfMemoryException)
  81. {
  82. return;
  83. }
  84. }
  85. else
  86. {
  87. // We need to clear out the current thread entry so that we can use it to release any
  88. // thread contexts we find below.
  89. ThreadContext * threadContext = static_cast<ThreadContext *>(currentEntry->GetThreadContext());
  90. if (threadContext != NULL)
  91. {
  92. if (threadContext->IsThreadBound())
  93. {
  94. ShutdownThreadContext(threadContext);
  95. ThreadContextTLSEntry::ClearThreadContext(currentEntry, false);
  96. }
  97. else
  98. {
  99. ThreadContextTLSEntry::ClearThreadContext(currentEntry, true);
  100. }
  101. }
  102. }
  103. EntryList::Iterator iter(&entries);
  104. while (iter.Next())
  105. {
  106. ThreadContextTLSEntry * entry = iter.Data();
  107. ThreadContext * threadContext = static_cast<ThreadContext *>(entry->GetThreadContext());
  108. if (threadContext != nullptr)
  109. {
  110. // Found a thread context. Remove it from the containing entry.
  111. ThreadContextTLSEntry::ClearThreadContext(entry, true);
  112. // Now set it to our thread's entry.
  113. ThreadContextTLSEntry::SetThreadContext(currentEntry, threadContext);
  114. // Clear it out.
  115. ShutdownThreadContext(threadContext);
  116. // Now clear it out of our entry.
  117. ThreadContextTLSEntry::ClearThreadContext(currentEntry, false);
  118. }
  119. }
  120. // We can only clean up our own TLS entry, so we're going to go ahead and do that here.
  121. entries.Remove(currentEntry);
  122. ThreadContextTLSEntry::CleanupThread();
  123. #if ENABLE_BACKGROUND_JOB_PROCESSOR
  124. if (s_sharedJobProcessor != NULL)
  125. {
  126. jobProcessor = s_sharedJobProcessor;
  127. s_sharedJobProcessor = NULL;
  128. jobProcessor->Close();
  129. }
  130. #endif
  131. }
  132. #if ENABLE_BACKGROUND_JOB_PROCESSOR
  133. if (jobProcessor != NULL)
  134. {
  135. HeapDelete(jobProcessor);
  136. }
  137. #endif
  138. }
  139. void ThreadBoundThreadContextManager::DestroyAllContextsAndEntries(bool shouldDeleteCurrentTlsEntry)
  140. {
  141. // Since BGParseManager has a dependency on threadcontexts, make sure it shuts down first
  142. BGParseManager::DeleteBGParseManager();
  143. AutoCriticalSection lock(ThreadContext::GetCriticalSection());
  144. // When shouldDeleteCurrentTlsEntry is true, the comparison in the while loop will always be true, so
  145. // every entry in the list will be deleted.
  146. ThreadContextTLSEntry* currentThreadEntry = shouldDeleteCurrentTlsEntry ? nullptr : ThreadContextTLSEntry::GetEntryForCurrentThread();
  147. while (!entries.Empty())
  148. {
  149. ThreadContextTLSEntry * entry = entries.Head();
  150. ThreadContext * threadContext = static_cast<ThreadContext *>(entry->GetThreadContext());
  151. entries.RemoveHead();
  152. if (threadContext != nullptr)
  153. {
  154. ShutdownThreadContext(threadContext);
  155. }
  156. if (currentThreadEntry != entry)
  157. {
  158. // Note: This deletes the ThreadContextTLSEntry but does not remove its pointer
  159. // from the thread's TLS
  160. ThreadContextTLSEntry::Delete(entry);
  161. }
  162. }
  163. #if ENABLE_BACKGROUND_JOB_PROCESSOR
  164. if (s_sharedJobProcessor != NULL)
  165. {
  166. s_sharedJobProcessor->Close();
  167. HeapDelete(s_sharedJobProcessor);
  168. s_sharedJobProcessor = NULL;
  169. }
  170. #endif
  171. }
  172. JsUtil::JobProcessor * ThreadBoundThreadContextManager::GetSharedJobProcessor()
  173. {
  174. #if ENABLE_BACKGROUND_JOB_PROCESSOR
  175. if (s_sharedJobProcessor == NULL)
  176. {
  177. // Don't use ThreadContext::GetCriticalSection() because it's also locked during thread detach while the loader lock is
  178. // held, and that may prevent the background job processor's thread from being started due to contention on the loader
  179. // lock, leading to a deadlock
  180. AutoCriticalSection lock(&s_sharedJobProcessorCreationLock);
  181. if (s_sharedJobProcessor == NULL)
  182. {
  183. // We don't need to have allocation policy manager for web worker.
  184. s_sharedJobProcessor = HeapNew(JsUtil::BackgroundJobProcessor, NULL, NULL, false /*disableParallelThreads*/);
  185. }
  186. }
  187. return s_sharedJobProcessor;
  188. #else
  189. return nullptr;
  190. #endif
  191. }
  192. void RentalThreadContextManager::DestroyThreadContext(ThreadContext* threadContext)
  193. {
  194. bool deleteThreadContext = true;
  195. #ifdef CHAKRA_STATIC_LIBRARY
  196. // xplat-todo: Cleanup staticlib shutdown. Deleting contexts / finalizers having
  197. // trouble with current runtime/context.
  198. deleteThreadContext = false;
  199. #endif
  200. ShutdownThreadContext(threadContext, deleteThreadContext);
  201. }
  202. void ThreadContextManagerBase::ShutdownThreadContext(
  203. ThreadContext* threadContext, bool deleteThreadContext /*= true*/)
  204. {
  205. #if DBG
  206. PageAllocator* pageAllocator = threadContext->GetPageAllocator();
  207. if (pageAllocator)
  208. {
  209. pageAllocator->SetConcurrentThreadId(::GetCurrentThreadId());
  210. }
  211. #endif
  212. threadContext->ShutdownThreads();
  213. if (deleteThreadContext)
  214. {
  215. AllocationPolicyManager * policyManager = threadContext->IsThreadBound() ? threadContext->GetAllocationPolicyManager() : nullptr;
  216. HeapDelete(threadContext);
  217. if (policyManager)
  218. {
  219. HeapDelete(policyManager);
  220. }
  221. }
  222. }