forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBGParseManager.h
More file actions
154 lines (132 loc) · 5.74 KB
/
BGParseManager.h
File metadata and controls
154 lines (132 loc) · 5.74 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
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
// This files contains the declarations of BGParseManager and BGParseWorkItem and build upon the Job and
// JobManager classes that do work on background threads. This enables the host to offload parser work
// from the UI and execution that doesn't have strict thread dependencies. Thus, both classes are
// multi-threaded; please see the definition of each function for expectations of which thread executes
// the function.
//
// There are up to 3 threads involved per background parse. Here are the relevant BGParseManager functions
// called from these three threads:
//
// Background/Network JobProcessor UI/Executing
// Thread Thread Thread
// | | |
// QueueBackgroundParse | |
// | | |
// | Process |
// | | |
// | | GetParseResults
// . . .
// Note that the thread queueing the work can also be the UIT thread. Also, note that GetParseResults may
// block the calling thread until the JobProcessor thread finishes processing the BGParseWorkItem that
// contains the results.
// Forward Declarations
class BGParseWorkItem;
namespace Js
{
struct Utf8SourceInfo;
typedef void * Var;
class JavascriptFunction;
}
// BGParseManager is the primary interface for background parsing. It uses a cookie to publicly track the data
// involved per parse request.
class BGParseManager sealed : public JsUtil::WaitableJobManager
{
public:
BGParseManager();
~BGParseManager();
static BGParseManager* GetBGParseManager();
static void DeleteBGParseManager();
static DWORD GetNextCookie();
static DWORD IncCompleted();
static DWORD IncFailed();
HRESULT QueueBackgroundParse(LPCUTF8 pszSrc, size_t cbLength, char16 *fullPath, DWORD* dwBgParseCookie);
HRESULT GetInputFromCookie(DWORD cookie, LPCUTF8* ppszSrc, size_t* pcbLength, WCHAR** sourceUrl);
HRESULT GetParseResults(
Js::ScriptContext* scriptContextUI,
DWORD cookie,
LPCUTF8 pszSrc,
SRCINFO const * pSrcInfo,
Js::FunctionBody** ppFunc,
CompileScriptException* pse,
size_t& srcLength,
Js::Utf8SourceInfo* utf8SourceInfo,
uint& sourceIndex
);
bool DiscardParseResults(DWORD cookie, void* buffer);
virtual bool Process(JsUtil::Job *const job, JsUtil::ParallelThreadData *threadData) override;
virtual void JobProcessed(JsUtil::Job *const job, const bool succeeded) override;
virtual void JobProcessing(JsUtil::Job* job) override;
// Defines needed for jobs.inl
BGParseWorkItem* GetJob(BGParseWorkItem* workitem);
bool WasAddedToJobProcessor(JsUtil::Job *const job) const;
private:
BGParseWorkItem * FindJob(DWORD dwCookie, bool waitForResults, bool removeJob);
// BGParseWorkItem job can be in one of 3 states, based on which linked list it is in:
// - queued - JobProcessor::jobs
// - processing - BGParseManager::workitemsProcessing
// - processed - BGParseManager::workitemsProcessed
JsUtil::DoublyLinkedList<BGParseWorkItem> workitemsProcessing;
JsUtil::DoublyLinkedList<BGParseWorkItem> workitemsProcessed;
static DWORD s_lastCookie;
static DWORD s_completed;
static DWORD s_failed;
static BGParseManager* s_BGParseManager;
static CriticalSection s_staticMemberLock;
};
// BGParseWorkItem is a helper class to BGParseManager that caches the input data from the calling thread
// to parse on the background thread and caches serialized bytecode so that bytecode can be deserialized
// on the appropriate thread.
class BGParseWorkItem sealed : public JsUtil::Job
{
public:
BGParseWorkItem(
BGParseManager* manager,
const byte* script,
size_t cb,
char16 *fullPath
);
~BGParseWorkItem();
void ParseUTF8Core(Js::ScriptContext* scriptContext);
HRESULT DeserializeParseResults(
Js::ScriptContext* scriptContextUI,
LPCUTF8 pszSrc,
SRCINFO const * pSrcInfo,
Js::Utf8SourceInfo* utf8SourceInfo,
Js::FunctionBody** functionBodyReturn,
size_t& srcLength,
uint& sourceIndex
);
void TransferCSE(CompileScriptException* pse);
void CreateCompletionEvent();
void WaitForCompletion();
void JobProcessed(const bool succeeded);
void Discard() { discarded = true; }
bool IsDiscarded() const { return discarded; }
DWORD GetCookie() const { return cookie; }
const byte* GetScriptSrc() const { return script; }
size_t GetScriptLength() const { return cb; }
WCHAR* GetScriptPath() const { return path; }
private:
// This cookie is the public identifier for this parser work
DWORD cookie;
// Input data
const byte* script;
size_t cb;
BSTR path;
// Parse state
CompileScriptException cse;
HRESULT parseHR;
size_t parseSourceLength;
Event* complete;
// True when this workitem was discarded while processing. This instance
// will free itself after it has been processed.
bool discarded;
// Output data
byte * bufferReturn;
DWORD bufferReturnBytes;
};