Bug 1704923 - Move helper thread APIs to their own header file r=sfink

This also tidies up the implementation a bit, moving
SetHelperThreadTaskCallback to the JS namespace and removing the unused
RunnableTask class.

Differential Revision: https://phabricator.services.mozilla.com/D116219
This commit is contained in:
Jon Coppeard 2021-06-03 10:24:12 +00:00
Родитель 970ec6ea0e
Коммит b484bb005d
10 изменённых файлов: 60 добавлений и 36 удалений

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

@ -0,0 +1,31 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: set ts=8 sts=2 et sw=2 tw=80:
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* API for supplying an external thread pool to run internal work off the main
* thread.
*/
#ifndef js_HelperThreadAPI_h
#define js_HelperThreadAPI_h
namespace JS {
/**
* Set callback to dispatch a tasks to an external thread pool.
*
* When the task runs it should call JS::RunHelperThreadTask.
*/
using HelperThreadTaskCallback = void (*)();
extern JS_PUBLIC_API void SetHelperThreadTaskCallback(
HelperThreadTaskCallback callback);
// Function to call from external thread pool to run a helper thread task.
extern JS_PUBLIC_API void RunHelperThreadTask();
} // namespace JS
#endif // js_HelperThreadAPI_h

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

@ -69,17 +69,6 @@ enum ThreadType {
THREAD_TYPE_MAX // Used to check shell function arguments
};
/*
* Threads need a universal way to dispatch from xpcom thread pools,
* so having objects inherit from this struct enables
* mozilla::HelperThreadPool's runnable handler to call runTask() on each type.
*/
struct RunnableTask {
virtual ThreadType threadType() = 0;
virtual void runTask() = 0;
virtual ~RunnableTask() = default;
};
namespace oom {
/*

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

@ -1905,15 +1905,6 @@ JS_PUBLIC_API void JS::AssertObjectBelongsToCurrentThread(JSObject* obj) {
MOZ_RELEASE_ASSERT(CurrentThreadCanAccessRuntime(rt));
}
// TODO:
// Bug 1630189: Windows PGO build will have a linking error for
// HelperThreadTaskCallback, use MOZ_NEVER_INLINE to prevent this. See
// https://bugzilla.mozilla.org/show_bug.cgi?id=1630189#c4
JS_PUBLIC_API MOZ_NEVER_INLINE void SetHelperThreadTaskCallback(
void (*callback)()) {
HelperThreadTaskCallback = callback;
}
JS_PUBLIC_API void JS::SetFilenameValidationCallback(
JS::FilenameValidationCallback cb) {
js::gFilenameValidationCallback = cb;

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

@ -193,16 +193,6 @@ extern JS_PUBLIC_API bool JS_IsBuiltinEvalFunction(JSFunction* fun);
/** True iff fun is the Function constructor. */
extern JS_PUBLIC_API bool JS_IsBuiltinFunctionConstructor(JSFunction* fun);
/**
* Set callback to send tasks to XPCOM thread pools
*/
JS_PUBLIC_API void SetHelperThreadTaskCallback(void (*callback)());
namespace JS {
// Function to call from external thread pool to run a helper thread task.
extern JS_PUBLIC_API void RunHelperThreadTask();
} // namespace JS
extern JS_PUBLIC_API const char* JS_GetImplementationVersion(void);
extern JS_PUBLIC_API void JS_SetWrapObjectCallbacks(

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

@ -158,6 +158,7 @@ EXPORTS.js += [
"../public/GCVector.h",
"../public/HashTable.h",
"../public/HeapAPI.h",
"../public/HelperThreadAPI.h",
"../public/Id.h",
"../public/Initialization.h",
"../public/JSON.h",

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

@ -22,6 +22,7 @@
#include "ds/Fifo.h"
#include "frontend/CompilationStencil.h" // CompilationStencil, ExtensibleCompilationStencil, CompilationGCOutput
#include "js/CompileOptions.h"
#include "js/HelperThreadAPI.h"
#include "js/TypeDecls.h"
#include "threading/ConditionVariable.h"
#include "threading/Thread.h"
@ -160,6 +161,11 @@ class GlobalHelperThreadState {
// This is used to get the HelperThreadTask that are currently running.
HelperThreadTaskVector helperTasks_;
// Callback to dispatch a task using an external thread pool. Set by
// JS::SetHelperThreadTaskCallback. If this is not set the internal thread
// pool is used.
JS::HelperThreadTaskCallback dispatchTaskCallback = nullptr;
bool isInitialized_ = false;
bool useInternalThreadPool_;
@ -199,6 +205,8 @@ class GlobalHelperThreadState {
void finish();
void finishThreads();
void setExternalTaskCallback(JS::HelperThreadTaskCallback callback);
[[nodiscard]] bool ensureContextList(size_t count,
const AutoLockHelperThreadState& lock);
JSContext* getFirstUnusedContext(AutoLockHelperThreadState& locked);

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

@ -21,6 +21,7 @@
#include "jit/JitRuntime.h"
#include "js/ContextOptions.h" // JS::ContextOptions
#include "js/friend/StackLimits.h" // js::ReportOverRecursed
#include "js/HelperThreadAPI.h"
#include "js/OffThreadScriptCompilation.h" // JS::OffThreadToken, JS::OffThreadCompileCallback
#include "js/SourceText.h"
#include "js/UniquePtr.h"
@ -133,6 +134,21 @@ void JS::SetProfilingThreadCallbacks(
HelperThreadState().unregisterThread = unregisterThread;
}
// Bug 1630189: Without MOZ_NEVER_INLINE, Windows PGO builds have a linking
// error for HelperThreadTaskCallback.
JS_PUBLIC_API MOZ_NEVER_INLINE void JS::SetHelperThreadTaskCallback(
HelperThreadTaskCallback callback) {
HelperThreadState().setExternalTaskCallback(callback);
}
void GlobalHelperThreadState::setExternalTaskCallback(
JS::HelperThreadTaskCallback callback) {
AutoLockHelperThreadState lock;
MOZ_ASSERT(!isInitialized(lock));
MOZ_ASSERT(!dispatchTaskCallback);
dispatchTaskCallback = callback;
}
bool js::StartOffThreadWasmCompile(wasm::CompileTask* task,
wasm::CompileMode mode) {
return HelperThreadState().submitTask(task, mode);
@ -1301,7 +1317,7 @@ bool GlobalHelperThreadState::ensureInitialized() {
return true;
}
useInternalThreadPool_ = HelperThreadTaskCallback == nullptr;
useInternalThreadPool_ = dispatchTaskCallback == nullptr;
for (size_t& i : runningTaskCount) {
i = 0;
@ -1358,6 +1374,8 @@ GlobalHelperThreadState::GlobalHelperThreadState()
unregisterThread(nullptr),
wasmTier2GeneratorsFinished_(0),
useInternalThreadPool_(true) {
MOZ_ASSERT(!gHelperThreadState);
cpuCount = ClampDefaultCPUCount(GetCPUCount());
threadCount = ThreadCountForCPUCount(cpuCount);
gcParallelThreadCount = threadCount;

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

@ -68,7 +68,6 @@ Atomic<JS::LargeAllocationFailureCallback> js::OnLargeAllocationFailure;
JS::FilenameValidationCallback js::gFilenameValidationCallback = nullptr;
namespace js {
void (*HelperThreadTaskCallback)();
#ifndef __wasi__
bool gCanUseExtraThreads = true;

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

@ -1181,10 +1181,6 @@ extern mozilla::Atomic<JS::BuildIdOp> GetBuildId;
extern JS::FilenameValidationCallback gFilenameValidationCallback;
// This callback is set by js::SetHelperThreadTaskCallback and may be null.
// See comment in jsapi.h.
extern void (*HelperThreadTaskCallback)();
} /* namespace js */
#endif /* vm_Runtime_h */

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

@ -41,6 +41,7 @@
#include "jsapi.h"
#include "js/ArrayBuffer.h"
#include "js/ContextOptions.h"
#include "js/HelperThreadAPI.h"
#include "js/Initialization.h"
#include "js/MemoryMetrics.h"
#include "js/OffThreadScriptCompilation.h"