JITServer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. class ProcessContextManager
  6. {
  7. private:
  8. static BaseDictionary<DWORD, ProcessContext*, HeapAllocator> ProcessContexts;
  9. static CriticalSection cs;
  10. public:
  11. static HRESULT RegisterNewProcess(DWORD pid, HANDLE processHandle, intptr_t chakraBaseAddress, intptr_t crtBaseAddress);
  12. static ProcessContext* GetProcessContext(DWORD pid);
  13. };
  14. class ServerContextManager
  15. {
  16. public:
  17. static void RegisterThreadContext(ServerThreadContext* threadContext);
  18. static void UnRegisterThreadContext(ServerThreadContext* threadContext);
  19. static void RegisterScriptContext(ServerScriptContext* scriptContext);
  20. static void UnRegisterScriptContext(ServerScriptContext* scriptContext);
  21. static bool CheckLivenessAndAddref(ServerScriptContext* context);
  22. static bool CheckLivenessAndAddref(ServerThreadContext* context);
  23. private:
  24. static JsUtil::BaseHashSet<ServerThreadContext*, HeapAllocator> threadContexts;
  25. static JsUtil::BaseHashSet<ServerScriptContext*, HeapAllocator> scriptContexts;
  26. static CriticalSection cs;
  27. public:
  28. #ifdef STACK_BACK_TRACE
  29. template<class T>
  30. struct ClosedContextEntry
  31. {
  32. __declspec(noinline)
  33. ClosedContextEntry(T* context)
  34. :context(context)
  35. {
  36. stack = StackBackTrace::Capture(&NoThrowHeapAllocator::Instance, 2);
  37. }
  38. ~ClosedContextEntry()
  39. {
  40. if (stack)
  41. {
  42. stack->Delete(&NoThrowHeapAllocator::Instance);
  43. }
  44. }
  45. T* context;
  46. union {
  47. DWORD runtimeProcId;
  48. ServerThreadContext* threadCtx;
  49. };
  50. StackBackTrace* stack;
  51. };
  52. static void RecordCloseContext(ServerThreadContext* context)
  53. {
  54. auto record = HeapNewNoThrow(ClosedContextEntry<ServerThreadContext>, context);
  55. if (record)
  56. {
  57. record->runtimeProcId = context->GetRuntimePid();
  58. }
  59. ClosedThreadContextList.PrependNoThrow(&NoThrowHeapAllocator::Instance, record);
  60. }
  61. static void RecordCloseContext(ServerScriptContext* context)
  62. {
  63. auto record = HeapNewNoThrow(ClosedContextEntry<ServerScriptContext>, context);
  64. if (record)
  65. {
  66. record->threadCtx = context->GetThreadContext();
  67. }
  68. ClosedScriptContextList.PrependNoThrow(&NoThrowHeapAllocator::Instance, record);
  69. }
  70. static SList<ClosedContextEntry<ServerThreadContext>*, NoThrowHeapAllocator> ClosedThreadContextList;
  71. static SList<ClosedContextEntry<ServerScriptContext>*, NoThrowHeapAllocator> ClosedScriptContextList;
  72. #endif
  73. static void Shutdown()
  74. {
  75. #ifdef STACK_BACK_TRACE
  76. while (!ClosedThreadContextList.Empty())
  77. {
  78. auto record = ClosedThreadContextList.Pop();
  79. if (record)
  80. {
  81. HeapDelete(record);
  82. }
  83. }
  84. while (!ClosedScriptContextList.Empty())
  85. {
  86. auto record = ClosedScriptContextList.Pop();
  87. if (record)
  88. {
  89. HeapDelete(record);
  90. }
  91. }
  92. #endif
  93. }
  94. };
  95. struct ContextClosedException {};
  96. struct AutoReleaseThreadContext
  97. {
  98. AutoReleaseThreadContext(ServerThreadContext* threadContext)
  99. :threadContext(threadContext)
  100. {
  101. if (!ServerContextManager::CheckLivenessAndAddref(threadContext))
  102. {
  103. // Don't assert here because ThreadContext can be closed before scriptContext closing call
  104. // and ThreadContext closing causes all related scriptContext be closed
  105. threadContext = nullptr;
  106. throw ContextClosedException();
  107. }
  108. }
  109. ~AutoReleaseThreadContext()
  110. {
  111. if (threadContext)
  112. {
  113. threadContext->Release();
  114. }
  115. }
  116. ServerThreadContext* threadContext;
  117. };
  118. struct AutoReleaseScriptContext
  119. {
  120. AutoReleaseScriptContext(ServerScriptContext* scriptContext)
  121. :scriptContext(scriptContext)
  122. {
  123. if (!ServerContextManager::CheckLivenessAndAddref(scriptContext))
  124. {
  125. // Don't assert here because ThreadContext can be closed before scriptContext closing call
  126. // and ThreadContext closing causes all related scriptContext be closed
  127. scriptContext = nullptr;
  128. threadContext = nullptr;
  129. throw ContextClosedException();
  130. }
  131. threadContext = scriptContext->GetThreadContext();
  132. }
  133. ~AutoReleaseScriptContext()
  134. {
  135. if (scriptContext)
  136. {
  137. scriptContext->Release();
  138. }
  139. if (threadContext)
  140. {
  141. threadContext->Release();
  142. }
  143. }
  144. ServerScriptContext* scriptContext;
  145. ServerThreadContext* threadContext;
  146. };
  147. template<typename Fn>
  148. HRESULT ServerCallWrapper(ServerThreadContext* threadContextInfo, Fn fn);
  149. template<typename Fn>
  150. HRESULT ServerCallWrapper(ServerScriptContext* scriptContextInfo, Fn fn);