Commit 19bad28d authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm][gc] Link Isolates from WasmEngine

First step towards GC of wasm code: Introduce a link to all Isolates
that use a WasmEngine.

R=mstarzinger@chromium.org

Bug: v8:8217
Change-Id: Ib7f4495e7c7e5cc9ad58293518c65738f23d664c
Reviewed-on: https://chromium-review.googlesource.com/1240335
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#56204}
parent 47945fa7
......@@ -2300,6 +2300,13 @@ void Isolate::UnregisterManagedPtrDestructor(ManagedPtrDestructor* destructor) {
destructor->next_ = nullptr;
}
void Isolate::SetWasmEngine(std::shared_ptr<wasm::WasmEngine> engine) {
DCHECK_NULL(wasm_engine_); // Only call once before {Init}.
wasm_engine_ = std::move(engine);
wasm_engine_->AddIsolate(this);
wasm::WasmCodeManager::InstallSamplingGCCallback(this);
}
// NOLINTNEXTLINE
Isolate::PerIsolateThreadData::~PerIsolateThreadData() {
#if defined(USE_SIMULATOR)
......@@ -2656,7 +2663,10 @@ void Isolate::Deinit() {
heap_.TearDown();
logger_->TearDown();
wasm_engine_.reset();
if (wasm_engine_) {
wasm_engine_->RemoveIsolate(this);
wasm_engine_.reset();
}
if (FLAG_embedded_builtins) {
if (DefaultEmbeddedBlob() == nullptr && embedded_blob() != nullptr) {
......@@ -2973,9 +2983,9 @@ bool Isolate::Init(StartupDeserializer* des) {
// Setup the wasm engine.
if (wasm_engine_ == nullptr) {
wasm_engine_ = wasm::WasmEngine::GetWasmEngine();
wasm::WasmCodeManager::InstallSamplingGCCallback(this);
SetWasmEngine(wasm::WasmEngine::GetWasmEngine());
}
DCHECK_NOT_NULL(wasm_engine_);
deoptimizer_data_ = new DeoptimizerData(heap());
......
......@@ -1509,10 +1509,7 @@ class Isolate : private HiddenFactory {
}
wasm::WasmEngine* wasm_engine() const { return wasm_engine_.get(); }
void set_wasm_engine(std::shared_ptr<wasm::WasmEngine> engine) {
DCHECK_NULL(wasm_engine_); // Only call once before {Init}.
wasm_engine_ = std::move(engine);
}
void SetWasmEngine(std::shared_ptr<wasm::WasmEngine> engine);
const v8::Context::BackupIncumbentScope* top_backup_incumbent_scope() const {
return top_backup_incumbent_scope_;
......
......@@ -24,6 +24,8 @@ WasmEngine::WasmEngine()
WasmEngine::~WasmEngine() {
// All AsyncCompileJobs have been canceled.
DCHECK(jobs_.empty());
// All Isolates have been deregistered.
DCHECK(isolates_.empty());
}
bool WasmEngine::SyncValidate(Isolate* isolate, const WasmFeatures& enabled,
......@@ -251,6 +253,7 @@ std::unique_ptr<AsyncCompileJob> WasmEngine::RemoveCompileJob(
bool WasmEngine::HasRunningCompileJob(Isolate* isolate) {
base::LockGuard<base::Mutex> guard(&mutex_);
DCHECK_EQ(1, isolates_.count(isolate));
for (auto& entry : jobs_) {
if (entry.first->isolate() == isolate) return true;
}
......@@ -259,6 +262,7 @@ bool WasmEngine::HasRunningCompileJob(Isolate* isolate) {
void WasmEngine::DeleteCompileJobsOnIsolate(Isolate* isolate) {
base::LockGuard<base::Mutex> guard(&mutex_);
DCHECK_EQ(1, isolates_.count(isolate));
for (auto it = jobs_.begin(); it != jobs_.end();) {
if (it->first->isolate() == isolate) {
it = jobs_.erase(it);
......@@ -268,6 +272,18 @@ void WasmEngine::DeleteCompileJobsOnIsolate(Isolate* isolate) {
}
}
void WasmEngine::AddIsolate(Isolate* isolate) {
base::LockGuard<base::Mutex> guard(&mutex_);
DCHECK_EQ(0, isolates_.count(isolate));
isolates_.insert(isolate);
}
void WasmEngine::RemoveIsolate(Isolate* isolate) {
base::LockGuard<base::Mutex> guard(&mutex_);
DCHECK_EQ(1, isolates_.count(isolate));
isolates_.erase(isolate);
}
namespace {
struct WasmEnginePointerConstructTrait final {
......@@ -286,16 +302,19 @@ base::LazyStaticInstance<std::shared_ptr<WasmEngine>,
} // namespace
// static
void WasmEngine::InitializeOncePerProcess() {
if (!FLAG_wasm_shared_engine) return;
global_wasm_engine.Pointer()->reset(new WasmEngine());
}
// static
void WasmEngine::GlobalTearDown() {
if (!FLAG_wasm_shared_engine) return;
global_wasm_engine.Pointer()->reset();
}
// static
std::shared_ptr<WasmEngine> WasmEngine::GetWasmEngine() {
if (FLAG_wasm_shared_engine) return global_wasm_engine.Get();
return std::shared_ptr<WasmEngine>(new WasmEngine());
......
......@@ -6,6 +6,7 @@
#define V8_WASM_WASM_ENGINE_H_
#include <memory>
#include <unordered_set>
#include "src/wasm/wasm-code-manager.h"
#include "src/wasm/wasm-memory.h"
......@@ -135,6 +136,10 @@ class V8_EXPORT_PRIVATE WasmEngine {
// for tearing down an isolate, or to clean it up to be reused.
void DeleteCompileJobsOnIsolate(Isolate* isolate);
// Manage the set of Isolates that use this WasmEngine.
void AddIsolate(Isolate* isolate);
void RemoveIsolate(Isolate* isolate);
// Call on process start and exit.
static void InitializeOncePerProcess();
static void GlobalTearDown();
......@@ -168,6 +173,9 @@ class V8_EXPORT_PRIVATE WasmEngine {
std::unique_ptr<CompilationStatistics> compilation_stats_;
std::unique_ptr<CodeTracer> code_tracer_;
// Set of isolates which use this WasmEngine. Used for cross-isolate GCs.
std::unordered_set<Isolate*> isolates_;
// End of fields protected by {mutex_}.
//////////////////////////////////////////////////////////////////////////////
......
......@@ -56,7 +56,7 @@ class SharedEngineIsolate {
public:
explicit SharedEngineIsolate(SharedEngine* engine)
: isolate_(v8::Isolate::Allocate()) {
isolate()->set_wasm_engine(engine->ExportEngineForSharing());
isolate()->SetWasmEngine(engine->ExportEngineForSharing());
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
v8::Isolate::Initialize(isolate_, create_params);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment