Bug 1490055 - Use SystemAllocPolicy for ParseTask which can be used by both main thread and helper thread contexts r=nbp

This commit is contained in:
Jon Coppeard 2018-09-11 08:18:37 +01:00
Родитель d3a29b8761
Коммит 0358bed5a7
2 изменённых файлов: 26 добавлений и 5 удалений

Просмотреть файл

@ -431,9 +431,12 @@ ParseTask::ParseTask(ParseTaskKind kind, JSContext* cx,
alloc(JSContext::TEMP_LIFO_ALLOC_PRIMARY_CHUNK_SIZE), alloc(JSContext::TEMP_LIFO_ALLOC_PRIMARY_CHUNK_SIZE),
parseGlobal(nullptr), parseGlobal(nullptr),
callback(callback), callbackData(callbackData), callback(callback), callbackData(callbackData),
scripts(cx), sourceObjects(cx),
overRecursed(false), outOfMemory(false) overRecursed(false), outOfMemory(false)
{ {
// Note that |cx| is the main thread context here but the parse task will
// run with a different, helper thread, context.
MOZ_ASSERT(!cx->helperThread());
MOZ_ALWAYS_TRUE(scripts.reserve(scripts.capacity())); MOZ_ALWAYS_TRUE(scripts.reserve(scripts.capacity()));
MOZ_ALWAYS_TRUE(sourceObjects.reserve(sourceObjects.capacity())); MOZ_ALWAYS_TRUE(sourceObjects.reserve(sourceObjects.capacity()));
} }
@ -441,6 +444,8 @@ ParseTask::ParseTask(ParseTaskKind kind, JSContext* cx,
bool bool
ParseTask::init(JSContext* cx, const ReadOnlyCompileOptions& options, JSObject* global) ParseTask::init(JSContext* cx, const ReadOnlyCompileOptions& options, JSObject* global)
{ {
MOZ_ASSERT(!cx->helperThread());
if (!this->options.copy(cx, options)) if (!this->options.copy(cx, options))
return false; return false;
@ -457,6 +462,8 @@ ParseTask::activate(JSRuntime* rt)
bool bool
ParseTask::finish(JSContext* cx) ParseTask::finish(JSContext* cx)
{ {
MOZ_ASSERT(!cx->helperThread());
for (auto& sourceObject : sourceObjects) { for (auto& sourceObject : sourceObjects) {
RootedScriptSourceObject sso(cx, sourceObject); RootedScriptSourceObject sso(cx, sourceObject);
if (!ScriptSourceObject::initFromOptions(cx, sso, options)) if (!ScriptSourceObject::initFromOptions(cx, sso, options))
@ -504,6 +511,8 @@ ScriptParseTask::ScriptParseTask(JSContext* cx, JS::SourceBufferHolder& srcBuf,
void void
ScriptParseTask::parse(JSContext* cx) ScriptParseTask::parse(JSContext* cx)
{ {
MOZ_ASSERT(cx->helperThread());
Rooted<ScriptSourceObject*> sourceObject(cx); Rooted<ScriptSourceObject*> sourceObject(cx);
ScopeKind scopeKind = options.nonSyntacticScope ? ScopeKind::NonSyntactic : ScopeKind::Global; ScopeKind scopeKind = options.nonSyntacticScope ? ScopeKind::NonSyntactic : ScopeKind::Global;
@ -526,6 +535,8 @@ ModuleParseTask::ModuleParseTask(JSContext* cx, JS::SourceBufferHolder& srcBuf,
void void
ModuleParseTask::parse(JSContext* cx) ModuleParseTask::parse(JSContext* cx)
{ {
MOZ_ASSERT(cx->helperThread());
Rooted<ScriptSourceObject*> sourceObject(cx); Rooted<ScriptSourceObject*> sourceObject(cx);
JSScript* script = frontend::CompileModule(cx, options, data, alloc, &sourceObject.get()); JSScript* script = frontend::CompileModule(cx, options, data, alloc, &sourceObject.get());
@ -545,6 +556,8 @@ ScriptDecodeTask::ScriptDecodeTask(JSContext* cx, const JS::TranscodeRange& rang
void void
ScriptDecodeTask::parse(JSContext* cx) ScriptDecodeTask::parse(JSContext* cx)
{ {
MOZ_ASSERT(cx->helperThread());
RootedScript resultScript(cx); RootedScript resultScript(cx);
Rooted<ScriptSourceObject*> sourceObject(cx); Rooted<ScriptSourceObject*> sourceObject(cx);
@ -570,6 +583,8 @@ BinASTDecodeTask::BinASTDecodeTask(JSContext* cx, const uint8_t* buf, size_t len
void void
BinASTDecodeTask::parse(JSContext* cx) BinASTDecodeTask::parse(JSContext* cx)
{ {
MOZ_ASSERT(cx->helperThread());
RootedScriptSourceObject sourceObject(cx); RootedScriptSourceObject sourceObject(cx);
JSScript* script = frontend::CompileGlobalBinASTScript(cx, alloc, options, JSScript* script = frontend::CompileGlobalBinASTScript(cx, alloc, options,
@ -594,9 +609,12 @@ MultiScriptsDecodeTask::MultiScriptsDecodeTask(JSContext* cx, JS::TranscodeSourc
void void
MultiScriptsDecodeTask::parse(JSContext* cx) MultiScriptsDecodeTask::parse(JSContext* cx)
{ {
MOZ_ASSERT(cx->helperThread());
if (!scripts.reserve(sources->length()) || if (!scripts.reserve(sources->length()) ||
!sourceObjects.reserve(sources->length())) !sourceObjects.reserve(sources->length()))
{ {
ReportOutOfMemory(cx); // This sets |outOfMemory|.
return; return;
} }
@ -1652,6 +1670,7 @@ bool
GlobalHelperThreadState::finishParseTask(JSContext* cx, ParseTaskKind kind, GlobalHelperThreadState::finishParseTask(JSContext* cx, ParseTaskKind kind,
JS::OffThreadToken* token, F&& finishCallback) JS::OffThreadToken* token, F&& finishCallback)
{ {
MOZ_ASSERT(!cx->helperThread());
MOZ_ASSERT(cx->realm()); MOZ_ASSERT(cx->realm());
Rooted<UniquePtr<ParseTask>> parseTask(cx, removeFinishedParseTask(kind, token)); Rooted<UniquePtr<ParseTask>> parseTask(cx, removeFinishedParseTask(kind, token));
@ -1729,14 +1748,16 @@ GlobalHelperThreadState::finishParseTask(JSContext* cx, ParseTaskKind kind,
{ {
size_t expectedLength = 0; size_t expectedLength = 0;
bool ok = finishParseTask(cx, kind, token, [&scripts, &expectedLength] (ParseTask* parseTask) { bool ok = finishParseTask(cx, kind, token, [cx, &scripts, &expectedLength] (ParseTask* parseTask) {
MOZ_ASSERT(parseTask->kind == ParseTaskKind::MultiScriptsDecode); MOZ_ASSERT(parseTask->kind == ParseTaskKind::MultiScriptsDecode);
auto task = static_cast<MultiScriptsDecodeTask*>(parseTask); auto task = static_cast<MultiScriptsDecodeTask*>(parseTask);
expectedLength = task->sources->length(); expectedLength = task->sources->length();
if (!scripts.reserve(task->scripts.length())) if (!scripts.reserve(task->scripts.length())) {
ReportOutOfMemory(cx);
return false; return false;
}
for (auto& script : task->scripts) for (auto& script : task->scripts)
scripts.infallibleAppend(script); scripts.infallibleAppend(script);

Просмотреть файл

@ -702,10 +702,10 @@ struct ParseTask : public mozilla::LinkedListElement<ParseTask>, public JS::OffT
// Holds the final scripts between the invocation of the callback and the // Holds the final scripts between the invocation of the callback and the
// point where FinishOffThreadScript is called, which will destroy the // point where FinishOffThreadScript is called, which will destroy the
// ParseTask. // ParseTask.
GCVector<JSScript*, 1> scripts; GCVector<JSScript*, 1, SystemAllocPolicy> scripts;
// Holds the ScriptSourceObjects generated for the script compilation. // Holds the ScriptSourceObjects generated for the script compilation.
GCVector<ScriptSourceObject*, 1> sourceObjects; GCVector<ScriptSourceObject*, 1, SystemAllocPolicy> sourceObjects;
// Any errors or warnings produced during compilation. These are reported // Any errors or warnings produced during compilation. These are reported
// when finishing the script. // when finishing the script.