-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathJITServer.h
171 lines (150 loc) · 5.16 KB
/
JITServer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
class ProcessContextManager
{
private:
static BaseDictionary<DWORD, ProcessContext*, HeapAllocator> ProcessContexts;
static CriticalSection cs;
public:
static HRESULT RegisterNewProcess(DWORD pid, HANDLE processHandle, intptr_t chakraBaseAddress, intptr_t crtBaseAddress);
static ProcessContext* GetProcessContext(DWORD pid);
};
class ServerContextManager
{
public:
static void RegisterThreadContext(ServerThreadContext* threadContext);
static void UnRegisterThreadContext(ServerThreadContext* threadContext);
static void RegisterScriptContext(ServerScriptContext* scriptContext);
static void UnRegisterScriptContext(ServerScriptContext* scriptContext);
static bool CheckLivenessAndAddref(ServerScriptContext* context);
static bool CheckLivenessAndAddref(ServerThreadContext* context);
private:
static JsUtil::BaseHashSet<ServerThreadContext*, HeapAllocator> threadContexts;
static JsUtil::BaseHashSet<ServerScriptContext*, HeapAllocator> scriptContexts;
static CriticalSection cs;
public:
#ifdef STACK_BACK_TRACE
template<class T>
struct ClosedContextEntry
{
__declspec(noinline)
ClosedContextEntry(T* context)
:context(context)
{
stack = StackBackTrace::Capture(&NoThrowHeapAllocator::Instance, 2);
}
~ClosedContextEntry()
{
if (stack)
{
stack->Delete(&NoThrowHeapAllocator::Instance);
}
}
T* context;
union {
DWORD runtimeProcId;
ServerThreadContext* threadCtx;
};
StackBackTrace* stack;
};
static void RecordCloseContext(ServerThreadContext* context)
{
auto record = HeapNewNoThrow(ClosedContextEntry<ServerThreadContext>, context);
if (record)
{
record->runtimeProcId = context->GetRuntimePid();
}
ClosedThreadContextList.PrependNoThrow(&NoThrowHeapAllocator::Instance, record);
}
static void RecordCloseContext(ServerScriptContext* context)
{
auto record = HeapNewNoThrow(ClosedContextEntry<ServerScriptContext>, context);
if (record)
{
record->threadCtx = context->GetThreadContext();
}
ClosedScriptContextList.PrependNoThrow(&NoThrowHeapAllocator::Instance, record);
}
static SList<ClosedContextEntry<ServerThreadContext>*, NoThrowHeapAllocator> ClosedThreadContextList;
static SList<ClosedContextEntry<ServerScriptContext>*, NoThrowHeapAllocator> ClosedScriptContextList;
#endif
static void Shutdown()
{
#ifdef STACK_BACK_TRACE
while (!ClosedThreadContextList.Empty())
{
auto record = ClosedThreadContextList.Pop();
if (record)
{
HeapDelete(record);
}
}
while (!ClosedScriptContextList.Empty())
{
auto record = ClosedScriptContextList.Pop();
if (record)
{
HeapDelete(record);
}
}
#endif
}
};
struct ContextClosedException {};
struct AutoReleaseThreadContext
{
AutoReleaseThreadContext(ServerThreadContext* threadContext)
:threadContext(threadContext)
{
if (!ServerContextManager::CheckLivenessAndAddref(threadContext))
{
// Don't assert here because ThreadContext can be closed before scriptContext closing call
// and ThreadContext closing causes all related scriptContext be closed
threadContext = nullptr;
throw ContextClosedException();
}
}
~AutoReleaseThreadContext()
{
if (threadContext)
{
threadContext->Release();
}
}
ServerThreadContext* threadContext;
};
struct AutoReleaseScriptContext
{
AutoReleaseScriptContext(ServerScriptContext* scriptContext)
:scriptContext(scriptContext)
{
if (!ServerContextManager::CheckLivenessAndAddref(scriptContext))
{
// Don't assert here because ThreadContext can be closed before scriptContext closing call
// and ThreadContext closing causes all related scriptContext be closed
scriptContext = nullptr;
threadContext = nullptr;
throw ContextClosedException();
}
threadContext = scriptContext->GetThreadContext();
}
~AutoReleaseScriptContext()
{
if (scriptContext)
{
scriptContext->Release();
}
if (threadContext)
{
threadContext->Release();
}
}
ServerScriptContext* scriptContext;
ServerThreadContext* threadContext;
};
template<typename Fn>
HRESULT ServerCallWrapper(ServerThreadContext* threadContextInfo, Fn fn);
template<typename Fn>
HRESULT ServerCallWrapper(ServerScriptContext* scriptContextInfo, Fn fn);