forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuntimeThreadData.cpp
More file actions
129 lines (101 loc) · 3.98 KB
/
RuntimeThreadData.cpp
File metadata and controls
129 lines (101 loc) · 3.98 KB
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft Corporation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "stdafx.h"
void RuntimeThreadLocalData::Initialize(RuntimeThreadData* threadData)
{
this->threadData = threadData;
}
void RuntimeThreadLocalData::Uninitialize()
{
}
THREAD_LOCAL RuntimeThreadLocalData threadLocalData;
RuntimeThreadLocalData& GetRuntimeThreadLocalData()
{
return threadLocalData;
}
RuntimeThreadData::RuntimeThreadData() :
hSemaphore(nullptr),
hThread(nullptr),
sharedContent(nullptr),
receiveBroadcastCallbackFunc(nullptr),
runtime(nullptr),
context(nullptr),
parent(nullptr),
leaving(false)
{
this->hevntInitialScriptCompleted = CreateEvent(NULL, TRUE, FALSE, NULL);
this->hevntReceivedBroadcast = CreateEvent(NULL, FALSE, FALSE, NULL);
this->hevntShutdown = CreateEvent(NULL, TRUE, FALSE, NULL);
InitializeCriticalSection(&csReportQ);
}
RuntimeThreadData::~RuntimeThreadData()
{
CloseHandle(this->hevntInitialScriptCompleted);
CloseHandle(this->hevntReceivedBroadcast);
CloseHandle(this->hevntShutdown);
CloseHandle(this->hThread);
DeleteCriticalSection(&csReportQ);
}
DWORD RuntimeThreadData::ThreadProc()
{
JsValueRef scriptSource;
JsValueRef fname;
const char* fullPath = "agent source";
HRESULT hr = S_OK;
threadLocalData.Initialize(this);
IfJsErrorFailLog(ChakraRTInterface::JsCreateRuntime(JsRuntimeAttributeNone, nullptr, &runtime));
IfJsErrorFailLog(ChakraRTInterface::JsCreateContext(runtime, &context));
IfJsErrorFailLog(ChakraRTInterface::JsSetCurrentContext(context));
if (!WScriptJsrt::Initialize())
{
IfFailGo(E_FAIL);
}
IfJsErrorFailLog(ChakraRTInterface::JsCreateExternalArrayBuffer((void*)this->initialSource.c_str(),
(unsigned int)this->initialSource.size(), nullptr, nullptr, &scriptSource));
ChakraRTInterface::JsCreateString(fullPath, strlen(fullPath), &fname);
ChakraRTInterface::JsRun(scriptSource, WScriptJsrt::GetNextSourceContext(), fname, JsParseScriptAttributeNone, nullptr);
SetEvent(this->parent->hevntInitialScriptCompleted);
// loop waiting for work;
while (true)
{
HANDLE handles[] = { this->hevntReceivedBroadcast, this->hevntShutdown };
DWORD waitRet = WaitForMultipleObjects(_countof(handles), handles, false, INFINITE);
if (waitRet == WAIT_OBJECT_0)
{
JsValueRef args[3];
ChakraRTInterface::JsGetGlobalObject(&args[0]);
ChakraRTInterface::JsCreateSharedArrayBufferWithSharedContent(this->parent->sharedContent, &args[1]);
ChakraRTInterface::JsDoubleToNumber(1, &args[2]);
// notify the parent we received the data
ReleaseSemaphore(this->parent->hSemaphore, 1, NULL);
if (this->receiveBroadcastCallbackFunc)
{
ChakraRTInterface::JsCallFunction(this->receiveBroadcastCallbackFunc, args, 3, nullptr);
}
}
if (waitRet == WAIT_OBJECT_0 + 1 || this->leaving)
{
WScriptJsrt::Uninitialize();
if (this->receiveBroadcastCallbackFunc)
{
ChakraRTInterface::JsRelease(this->receiveBroadcastCallbackFunc, nullptr);
}
ChakraRTInterface::JsSetCurrentContext(nullptr);
ChakraRTInterface::JsDisposeRuntime(runtime);
threadLocalData.Uninitialize();
return 0;
}
else if (waitRet != WAIT_OBJECT_0)
{
Assert(false);
break;
}
}
Error:
ChakraRTInterface::JsSetCurrentContext(nullptr);
ChakraRTInterface::JsDisposeRuntime(runtime);
threadLocalData.Uninitialize();
return 0;
}