Commit dc45361e authored by Dominik Inführ's avatar Dominik Inführ Committed by Commit Bot

Reland "[heap] Introduce LocalIsolate for main thread"

This is a reland of e95e1b62

After landing https://crrev.com/c/2546682, this CL can be relanded
without changes.

Original change's description:
> [heap] Introduce LocalIsolate for main thread
>
> Add a LocalIsolate for the main thread to Isolate. This LocalIsolate is
> kept alive during the whole lifetime of the Isolate. The main thread
> LocalIsolate starts in the Running state in contrast to the background
> thread LocalIsolates (those start in Parked).
>
> Code paths in Turbofan that used to create a LocalIsolate on the main
> thread can now simply use the main thread LocalIsolate.
>
> LocalIsolate for the main thread will help in reducing differences
> between the main and background threads. The goal is that the main
> thread behaves more like a background thread.
>
> The main thread LocalIsolate should also make it simpler to share code
> between main thread and background threads by using LocalIsolate for
> both.
>
> Bug: v8:10315
> Change-Id: I7fd61d305a6fd7079e2319d75c291c1021e70018
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2509593
> Reviewed-by: Simon Zünd <szuend@chromium.org>
> Reviewed-by: Yang Guo <yangguo@chromium.org>
> Reviewed-by: Ulan Degenbaev <ulan@chromium.org>
> Reviewed-by: Leszek Swirski <leszeks@chromium.org>
> Reviewed-by: Georg Neis <neis@chromium.org>
> Commit-Queue: Dominik Inführ <dinfuehr@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#71226}

Bug: v8:10315
Change-Id: I418b1217aeac4f3c44a0aa514dea9864f8a58656
TBR: szuend@chromium.org, yangguo@chromium.org, ulan@chromium.org, leszeks@chromium.org, neis@chromium.org
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2543399Reviewed-by: 's avatarDominik Inführ <dinfuehr@chromium.org>
Commit-Queue: Dominik Inführ <dinfuehr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#71274}
parent e9f1abae
...@@ -2717,6 +2717,7 @@ v8_source_set("v8_base_without_compiler") { ...@@ -2717,6 +2717,7 @@ v8_source_set("v8_base_without_compiler") {
"src/heap/paged-spaces.cc", "src/heap/paged-spaces.cc",
"src/heap/paged-spaces.h", "src/heap/paged-spaces.h",
"src/heap/parallel-work-item.h", "src/heap/parallel-work-item.h",
"src/heap/parked-scope.h",
"src/heap/read-only-heap-inl.h", "src/heap/read-only-heap-inl.h",
"src/heap/read-only-heap.cc", "src/heap/read-only-heap.cc",
"src/heap/read-only-heap.h", "src/heap/read-only-heap.h",
......
...@@ -25,6 +25,7 @@ include_rules = [ ...@@ -25,6 +25,7 @@ include_rules = [
# TODO(v8:10496): Don't expose memory chunk outside of heap/. # TODO(v8:10496): Don't expose memory chunk outside of heap/.
"+src/heap/memory-chunk.h", "+src/heap/memory-chunk.h",
"+src/heap/memory-chunk-inl.h", "+src/heap/memory-chunk-inl.h",
"+src/heap/parked-scope.h",
"+src/heap/read-only-heap-inl.h", "+src/heap/read-only-heap-inl.h",
"+src/heap/read-only-heap.h", "+src/heap/read-only-heap.h",
"+src/heap/safepoint.h", "+src/heap/safepoint.h",
......
...@@ -37,6 +37,7 @@ ...@@ -37,6 +37,7 @@
#include "src/heap/local-factory-inl.h" #include "src/heap/local-factory-inl.h"
#include "src/heap/local-heap-inl.h" #include "src/heap/local-heap-inl.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "src/init/bootstrapper.h" #include "src/init/bootstrapper.h"
#include "src/interpreter/interpreter.h" #include "src/interpreter/interpreter.h"
#include "src/logging/log-inl.h" #include "src/logging/log-inl.h"
...@@ -968,9 +969,10 @@ bool GetOptimizedCodeNow(OptimizedCompilationJob* job, Isolate* isolate, ...@@ -968,9 +969,10 @@ bool GetOptimizedCodeNow(OptimizedCompilationJob* job, Isolate* isolate,
} }
{ {
LocalIsolate local_isolate(isolate, ThreadKind::kMain); // Park main thread here to be in the same state as background threads.
ParkedScope parked_scope(isolate->main_thread_local_isolate());
if (job->ExecuteJob(isolate->counters()->runtime_call_stats(), if (job->ExecuteJob(isolate->counters()->runtime_call_stats(),
&local_isolate)) { isolate->main_thread_local_isolate())) {
CompilerTracer::TraceAbortedJob(isolate, compilation_info); CompilerTracer::TraceAbortedJob(isolate, compilation_info);
return false; return false;
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "src/execution/isolate.h" #include "src/execution/isolate.h"
#include "src/execution/local-isolate.h" #include "src/execution/local-isolate.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "src/init/v8.h" #include "src/init/v8.h"
#include "src/logging/counters.h" #include "src/logging/counters.h"
#include "src/logging/log.h" #include "src/logging/log.h"
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include "src/handles/handles.h" #include "src/handles/handles.h"
#include "src/handles/persistent-handles.h" #include "src/handles/persistent-handles.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "src/interpreter/bytecode-array-accessor.h" #include "src/interpreter/bytecode-array-accessor.h"
#include "src/objects/code-kind.h" #include "src/objects/code-kind.h"
#include "src/objects/feedback-vector.h" #include "src/objects/feedback-vector.h"
......
...@@ -3034,8 +3034,8 @@ MaybeHandle<Code> Pipeline::GenerateCodeForTesting( ...@@ -3034,8 +3034,8 @@ MaybeHandle<Code> Pipeline::GenerateCodeForTesting(
} }
{ {
LocalIsolate local_isolate(isolate, ThreadKind::kMain); LocalIsolateScope local_isolate_scope(data.broker(), info,
LocalIsolateScope local_isolate_scope(data.broker(), info, &local_isolate); isolate->main_thread_local_isolate());
if (data.broker()->is_concurrent_inlining()) { if (data.broker()->is_concurrent_inlining()) {
if (!pipeline.CreateGraph()) return MaybeHandle<Code>(); if (!pipeline.CreateGraph()) return MaybeHandle<Code>();
} }
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include "src/diagnostics/compilation-statistics.h" #include "src/diagnostics/compilation-statistics.h"
#include "src/execution/frames-inl.h" #include "src/execution/frames-inl.h"
#include "src/execution/isolate-inl.h" #include "src/execution/isolate-inl.h"
#include "src/execution/local-isolate.h"
#include "src/execution/messages.h" #include "src/execution/messages.h"
#include "src/execution/microtask-queue.h" #include "src/execution/microtask-queue.h"
#include "src/execution/protectors-inl.h" #include "src/execution/protectors-inl.h"
...@@ -2910,6 +2911,7 @@ void Isolate::Delete(Isolate* isolate) { ...@@ -2910,6 +2911,7 @@ void Isolate::Delete(Isolate* isolate) {
Isolate* saved_isolate = reinterpret_cast<Isolate*>( Isolate* saved_isolate = reinterpret_cast<Isolate*>(
base::Thread::GetThreadLocal(isolate->isolate_key_)); base::Thread::GetThreadLocal(isolate->isolate_key_));
SetIsolateThreadLocals(isolate, nullptr); SetIsolateThreadLocals(isolate, nullptr);
isolate->set_thread_id(ThreadId::Current());
isolate->Deinit(); isolate->Deinit();
...@@ -3108,6 +3110,8 @@ void Isolate::Deinit() { ...@@ -3108,6 +3110,8 @@ void Isolate::Deinit() {
// This stops cancelable tasks (i.e. concurrent marking tasks) // This stops cancelable tasks (i.e. concurrent marking tasks)
cancelable_task_manager()->CancelAndWait(); cancelable_task_manager()->CancelAndWait();
main_thread_local_isolate_.reset();
heap_.TearDown(); heap_.TearDown();
FILE* logfile = logger_->TearDownAndGetLogFile(); FILE* logfile = logger_->TearDownAndGetLogFile();
if (logfile != nullptr) base::Fclose(logfile); if (logfile != nullptr) base::Fclose(logfile);
...@@ -3539,6 +3543,10 @@ bool Isolate::Init(SnapshotData* startup_snapshot_data, ...@@ -3539,6 +3543,10 @@ bool Isolate::Init(SnapshotData* startup_snapshot_data,
ReadOnlyHeap::SetUp(this, read_only_snapshot_data, can_rehash); ReadOnlyHeap::SetUp(this, read_only_snapshot_data, can_rehash);
heap_.SetUpSpaces(); heap_.SetUpSpaces();
// Create LocalIsolate/LocalHeap for the main thread and set state to Running.
main_thread_local_isolate_.reset(new LocalIsolate(this, ThreadKind::kMain));
main_thread_local_heap()->Unpark();
isolate_data_.external_reference_table()->Init(this); isolate_data_.external_reference_table()->Init(this);
// Setup the wasm engine. // Setup the wasm engine.
...@@ -4755,6 +4763,10 @@ void Isolate::RemoveContextIdCallback(const v8::WeakCallbackInfo<void>& data) { ...@@ -4755,6 +4763,10 @@ void Isolate::RemoveContextIdCallback(const v8::WeakCallbackInfo<void>& data) {
isolate->recorder_context_id_map_.erase(context_id); isolate->recorder_context_id_map_.erase(context_id);
} }
LocalHeap* Isolate::main_thread_local_heap() {
return main_thread_local_isolate()->heap();
}
// |chunk| is either a Page or an executable LargePage. // |chunk| is either a Page or an executable LargePage.
void Isolate::RemoveCodeMemoryChunk(MemoryChunk* chunk) { void Isolate::RemoveCodeMemoryChunk(MemoryChunk* chunk) {
// We only keep track of individual code pages/allocations if we are on arm32, // We only keep track of individual code pages/allocations if we are on arm32,
......
...@@ -1625,6 +1625,12 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { ...@@ -1625,6 +1625,12 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
MaybeLocal<v8::Context> GetContextFromRecorderContextId( MaybeLocal<v8::Context> GetContextFromRecorderContextId(
v8::metrics::Recorder::ContextId id); v8::metrics::Recorder::ContextId id);
LocalIsolate* main_thread_local_isolate() {
return main_thread_local_isolate_.get();
}
LocalHeap* main_thread_local_heap();
#ifdef V8_HEAP_SANDBOX #ifdef V8_HEAP_SANDBOX
ExternalPointerTable& external_pointer_table() { ExternalPointerTable& external_pointer_table() {
return isolate_data_.external_pointer_table_; return isolate_data_.external_pointer_table_;
...@@ -1961,6 +1967,8 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory { ...@@ -1961,6 +1967,8 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
bool promise_hook_or_debug_is_active_or_async_event_delegate_ = false; bool promise_hook_or_debug_is_active_or_async_event_delegate_ = false;
int async_task_count_ = 0; int async_task_count_ = 0;
std::unique_ptr<LocalIsolate> main_thread_local_isolate_;
v8::Isolate::AbortOnUncaughtExceptionCallback v8::Isolate::AbortOnUncaughtExceptionCallback
abort_on_uncaught_exception_callback_ = nullptr; abort_on_uncaught_exception_callback_ = nullptr;
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include "src/api/api.h" #include "src/api/api.h"
#include "src/base/logging.h" #include "src/base/logging.h"
#include "src/codegen/optimized-compilation-info.h" #include "src/codegen/optimized-compilation-info.h"
#include "src/execution/isolate.h"
#include "src/execution/thread-id.h"
#include "src/handles/maybe-handles.h" #include "src/handles/maybe-handles.h"
#include "src/objects/objects-inl.h" #include "src/objects/objects-inl.h"
#include "src/roots/roots-inl.h" #include "src/roots/roots-inl.h"
...@@ -43,8 +45,19 @@ bool HandleBase::IsDereferenceAllowed() const { ...@@ -43,8 +45,19 @@ bool HandleBase::IsDereferenceAllowed() const {
} }
if (isolate->IsBuiltinsTableHandleLocation(location_)) return true; if (isolate->IsBuiltinsTableHandleLocation(location_)) return true;
LocalHeap* local_heap = LocalHeap::Current(); if (FLAG_local_heaps) {
if (FLAG_local_heaps && local_heap) { LocalHeap* local_heap = LocalHeap::Current();
if (local_heap == nullptr) {
// We are on the main thread and can thus load the LocalHeap from the
// Isolate itself. The DCHECK makes sure that we are not accidentally
// dereferencing a handle on a background thread without an active
// LocalHeap.
DCHECK_EQ(ThreadId::Current(), isolate->thread_id());
local_heap = isolate->main_thread_local_heap();
}
// Local heap can't access handles when parked
if (!local_heap->IsHandleDereferenceAllowed()) { if (!local_heap->IsHandleDereferenceAllowed()) {
StdoutStream{} << "Cannot dereference handle owned by " StdoutStream{} << "Cannot dereference handle owned by "
<< "non-running local heap\n"; << "non-running local heap\n";
......
...@@ -23,8 +23,9 @@ AllocationResult ConcurrentAllocator::AllocateRaw(int object_size, ...@@ -23,8 +23,9 @@ AllocationResult ConcurrentAllocator::AllocateRaw(int object_size,
// TODO(dinfuehr): Add support for allocation observers // TODO(dinfuehr): Add support for allocation observers
CHECK(FLAG_concurrent_allocation); CHECK(FLAG_concurrent_allocation);
// Ensure that we are on the right thread #ifdef DEBUG
DCHECK_EQ(LocalHeap::Current(), local_heap_); local_heap_->VerifyCurrent();
#endif
if (object_size > kMaxLabObjectSize) { if (object_size > kMaxLabObjectSize) {
return AllocateOutsideLab(object_size, alignment, origin); return AllocateOutsideLab(object_size, alignment, origin);
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/marking.h" #include "src/heap/marking.h"
#include "src/heap/memory-chunk.h" #include "src/heap/memory-chunk.h"
#include "src/heap/parked-scope.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
......
...@@ -16,7 +16,7 @@ AllocationResult LocalHeap::AllocateRaw(int size_in_bytes, AllocationType type, ...@@ -16,7 +16,7 @@ AllocationResult LocalHeap::AllocateRaw(int size_in_bytes, AllocationType type,
AllocationOrigin origin, AllocationOrigin origin,
AllocationAlignment alignment) { AllocationAlignment alignment) {
#if DEBUG #if DEBUG
DCHECK_EQ(LocalHeap::Current(), this); VerifyCurrent();
DCHECK(AllowHandleAllocation::IsAllowed()); DCHECK(AllowHandleAllocation::IsAllowed());
DCHECK(AllowHeapAllocation::IsAllowed()); DCHECK(AllowHeapAllocation::IsAllowed());
DCHECK_IMPLIES(type == AllocationType::kCode || type == AllocationType::kMap, DCHECK_IMPLIES(type == AllocationType::kCode || type == AllocationType::kMap,
......
...@@ -6,13 +6,16 @@ ...@@ -6,13 +6,16 @@
#include <memory> #include <memory>
#include "src/base/logging.h"
#include "src/base/platform/mutex.h" #include "src/base/platform/mutex.h"
#include "src/common/globals.h" #include "src/common/globals.h"
#include "src/execution/isolate.h"
#include "src/handles/local-handles.h" #include "src/handles/local-handles.h"
#include "src/heap/heap-inl.h" #include "src/heap/heap-inl.h"
#include "src/heap/heap-write-barrier.h" #include "src/heap/heap-write-barrier.h"
#include "src/heap/local-heap-inl.h" #include "src/heap/local-heap-inl.h"
#include "src/heap/marking-barrier.h" #include "src/heap/marking-barrier.h"
#include "src/heap/parked-scope.h"
#include "src/heap/safepoint.h" #include "src/heap/safepoint.h"
namespace v8 { namespace v8 {
...@@ -24,6 +27,17 @@ thread_local LocalHeap* current_local_heap = nullptr; ...@@ -24,6 +27,17 @@ thread_local LocalHeap* current_local_heap = nullptr;
LocalHeap* LocalHeap::Current() { return current_local_heap; } LocalHeap* LocalHeap::Current() { return current_local_heap; }
#ifdef DEBUG
void LocalHeap::VerifyCurrent() {
LocalHeap* current = LocalHeap::Current();
if (is_main_thread())
DCHECK_NULL(current);
else
DCHECK_EQ(current, this);
}
#endif
LocalHeap::LocalHeap(Heap* heap, ThreadKind kind, LocalHeap::LocalHeap(Heap* heap, ThreadKind kind,
std::unique_ptr<PersistentHandles> persistent_handles) std::unique_ptr<PersistentHandles> persistent_handles)
: heap_(heap), : heap_(heap),
...@@ -38,7 +52,7 @@ LocalHeap::LocalHeap(Heap* heap, ThreadKind kind, ...@@ -38,7 +52,7 @@ LocalHeap::LocalHeap(Heap* heap, ThreadKind kind,
marking_barrier_(new MarkingBarrier(this)), marking_barrier_(new MarkingBarrier(this)),
old_space_allocator_(this, heap->old_space()) { old_space_allocator_(this, heap->old_space()) {
heap_->safepoint()->AddLocalHeap(this, [this] { heap_->safepoint()->AddLocalHeap(this, [this] {
if (FLAG_local_heaps) { if (FLAG_local_heaps && !is_main_thread()) {
WriteBarrier::SetForThread(marking_barrier_.get()); WriteBarrier::SetForThread(marking_barrier_.get());
if (heap_->incremental_marking()->IsMarking()) { if (heap_->incremental_marking()->IsMarking()) {
marking_barrier_->Activate( marking_barrier_->Activate(
...@@ -51,7 +65,7 @@ LocalHeap::LocalHeap(Heap* heap, ThreadKind kind, ...@@ -51,7 +65,7 @@ LocalHeap::LocalHeap(Heap* heap, ThreadKind kind,
persistent_handles_->Attach(this); persistent_handles_->Attach(this);
} }
DCHECK_NULL(current_local_heap); DCHECK_NULL(current_local_heap);
current_local_heap = this; if (!is_main_thread()) current_local_heap = this;
} }
LocalHeap::~LocalHeap() { LocalHeap::~LocalHeap() {
...@@ -61,14 +75,16 @@ LocalHeap::~LocalHeap() { ...@@ -61,14 +75,16 @@ LocalHeap::~LocalHeap() {
heap_->safepoint()->RemoveLocalHeap(this, [this] { heap_->safepoint()->RemoveLocalHeap(this, [this] {
old_space_allocator_.FreeLinearAllocationArea(); old_space_allocator_.FreeLinearAllocationArea();
if (FLAG_local_heaps) { if (FLAG_local_heaps && !is_main_thread()) {
marking_barrier_->Publish(); marking_barrier_->Publish();
WriteBarrier::ClearForThread(marking_barrier_.get()); WriteBarrier::ClearForThread(marking_barrier_.get());
} }
}); });
DCHECK_EQ(current_local_heap, this); if (!is_main_thread()) {
current_local_heap = nullptr; DCHECK_EQ(current_local_heap, this);
current_local_heap = nullptr;
}
} }
void LocalHeap::EnsurePersistentHandles() { void LocalHeap::EnsurePersistentHandles() {
...@@ -101,13 +117,17 @@ bool LocalHeap::ContainsLocalHandle(Address* location) { ...@@ -101,13 +117,17 @@ bool LocalHeap::ContainsLocalHandle(Address* location) {
} }
bool LocalHeap::IsHandleDereferenceAllowed() { bool LocalHeap::IsHandleDereferenceAllowed() {
DCHECK_EQ(LocalHeap::Current(), this); #ifdef DEBUG
VerifyCurrent();
#endif
return state_ == ThreadState::Running; return state_ == ThreadState::Running;
} }
#endif #endif
bool LocalHeap::IsParked() { bool LocalHeap::IsParked() {
DCHECK_EQ(LocalHeap::Current(), this); #ifdef DEBUG
VerifyCurrent();
#endif
return state_ == ThreadState::Parked; return state_ == ThreadState::Parked;
} }
......
...@@ -111,6 +111,10 @@ class V8_EXPORT_PRIVATE LocalHeap { ...@@ -111,6 +111,10 @@ class V8_EXPORT_PRIVATE LocalHeap {
// with the current thread. // with the current thread.
static LocalHeap* Current(); static LocalHeap* Current();
#ifdef DEBUG
void VerifyCurrent();
#endif
// Allocate an uninitialized object. // Allocate an uninitialized object.
V8_WARN_UNUSED_RESULT inline AllocationResult AllocateRaw( V8_WARN_UNUSED_RESULT inline AllocationResult AllocateRaw(
int size_in_bytes, AllocationType allocation, int size_in_bytes, AllocationType allocation,
...@@ -185,47 +189,7 @@ class V8_EXPORT_PRIVATE LocalHeap { ...@@ -185,47 +189,7 @@ class V8_EXPORT_PRIVATE LocalHeap {
friend class ParkedScope; friend class ParkedScope;
friend class UnparkedScope; friend class UnparkedScope;
friend class ConcurrentAllocator; friend class ConcurrentAllocator;
}; friend class Isolate;
// Scope that explicitly parks LocalHeap prohibiting access to the heap and the
// creation of Handles.
class ParkedScope {
public:
explicit ParkedScope(LocalHeap* local_heap) : local_heap_(local_heap) {
local_heap_->Park();
}
~ParkedScope() { local_heap_->Unpark(); }
private:
LocalHeap* const local_heap_;
};
// Scope that explicitly unparks LocalHeap allowing access to the heap and the
// creation of Handles.
class UnparkedScope {
public:
explicit UnparkedScope(LocalHeap* local_heap) : local_heap_(local_heap) {
local_heap_->Unpark();
}
~UnparkedScope() { local_heap_->Park(); }
private:
LocalHeap* const local_heap_;
};
class ParkedMutexGuard {
base::Mutex* guard_;
public:
explicit ParkedMutexGuard(LocalHeap* local_heap, base::Mutex* guard)
: guard_(guard) {
ParkedScope scope(local_heap);
guard_->Lock();
}
~ParkedMutexGuard() { guard_->Unlock(); }
}; };
} // namespace internal } // namespace internal
......
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_HEAP_PARKED_SCOPE_H_
#define V8_HEAP_PARKED_SCOPE_H_
#include "src/base/platform/mutex.h"
#include "src/execution/local-isolate.h"
#include "src/heap/local-heap.h"
namespace v8 {
namespace internal {
// Scope that explicitly parks a thread, prohibiting access to the heap and the
// creation of handles.
class ParkedScope {
public:
explicit ParkedScope(LocalIsolate* local_isolate)
: ParkedScope(local_isolate->heap()) {}
explicit ParkedScope(LocalHeap* local_heap) : local_heap_(local_heap) {
local_heap_->Park();
}
~ParkedScope() { local_heap_->Unpark(); }
private:
LocalHeap* const local_heap_;
};
// Scope that explicitly unparks a thread, allowing access to the heap and the
// creation of handles.
class UnparkedScope {
public:
explicit UnparkedScope(LocalIsolate* local_isolate)
: UnparkedScope(local_isolate->heap()) {}
explicit UnparkedScope(LocalHeap* local_heap) : local_heap_(local_heap) {
local_heap_->Unpark();
}
~UnparkedScope() { local_heap_->Park(); }
private:
LocalHeap* const local_heap_;
};
class ParkedMutexGuard {
base::Mutex* guard_;
public:
explicit ParkedMutexGuard(LocalIsolate* local_isolate, base::Mutex* guard)
: ParkedMutexGuard(local_isolate->heap(), guard) {}
explicit ParkedMutexGuard(LocalHeap* local_heap, base::Mutex* guard)
: guard_(guard) {
ParkedScope scope(local_heap);
guard_->Lock();
}
~ParkedMutexGuard() { guard_->Unlock(); }
};
} // namespace internal
} // namespace v8
#endif // V8_HEAP_PARKED_SCOPE_H_
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "src/heap/safepoint.h" #include "src/heap/safepoint.h"
#include "src/base/logging.h"
#include "src/handles/local-handles.h" #include "src/handles/local-handles.h"
#include "src/handles/persistent-handles.h" #include "src/handles/persistent-handles.h"
#include "src/heap/gc-tracer.h" #include "src/heap/gc-tracer.h"
...@@ -25,13 +26,13 @@ void GlobalSafepoint::EnterSafepointScope() { ...@@ -25,13 +26,13 @@ void GlobalSafepoint::EnterSafepointScope() {
TRACE_GC(heap_->tracer(), GCTracer::Scope::STOP_THE_WORLD); TRACE_GC(heap_->tracer(), GCTracer::Scope::STOP_THE_WORLD);
local_heaps_mutex_.Lock(); local_heaps_mutex_.Lock();
local_heap_of_this_thread_ = LocalHeap::Current();
barrier_.Arm(); barrier_.Arm();
DCHECK_NULL(LocalHeap::Current());
for (LocalHeap* current = local_heaps_head_; current; for (LocalHeap* current = local_heaps_head_; current;
current = current->next_) { current = current->next_) {
if (current == local_heap_of_this_thread_) { if (current->is_main_thread()) {
continue; continue;
} }
current->RequestSafepoint(); current->RequestSafepoint();
...@@ -39,8 +40,7 @@ void GlobalSafepoint::EnterSafepointScope() { ...@@ -39,8 +40,7 @@ void GlobalSafepoint::EnterSafepointScope() {
for (LocalHeap* current = local_heaps_head_; current; for (LocalHeap* current = local_heaps_head_; current;
current = current->next_) { current = current->next_) {
if (current == local_heap_of_this_thread_) { if (current->is_main_thread()) {
DCHECK(current->is_main_thread());
continue; continue;
} }
DCHECK(!current->is_main_thread()); DCHECK(!current->is_main_thread());
...@@ -58,11 +58,11 @@ void GlobalSafepoint::LeaveSafepointScope() { ...@@ -58,11 +58,11 @@ void GlobalSafepoint::LeaveSafepointScope() {
DCHECK_GT(active_safepoint_scopes_, 0); DCHECK_GT(active_safepoint_scopes_, 0);
if (--active_safepoint_scopes_ > 0) return; if (--active_safepoint_scopes_ > 0) return;
DCHECK_EQ(local_heap_of_this_thread_, LocalHeap::Current()); DCHECK_NULL(LocalHeap::Current());
for (LocalHeap* current = local_heaps_head_; current; for (LocalHeap* current = local_heaps_head_; current;
current = current->next_) { current = current->next_) {
if (current == local_heap_of_this_thread_) { if (current->is_main_thread()) {
continue; continue;
} }
current->state_mutex_.Unlock(); current->state_mutex_.Unlock();
...@@ -70,7 +70,6 @@ void GlobalSafepoint::LeaveSafepointScope() { ...@@ -70,7 +70,6 @@ void GlobalSafepoint::LeaveSafepointScope() {
barrier_.Disarm(); barrier_.Disarm();
local_heap_of_this_thread_ = nullptr;
local_heaps_mutex_.Unlock(); local_heaps_mutex_.Unlock();
} }
......
...@@ -101,8 +101,6 @@ class GlobalSafepoint { ...@@ -101,8 +101,6 @@ class GlobalSafepoint {
int active_safepoint_scopes_; int active_safepoint_scopes_;
LocalHeap* local_heap_of_this_thread_;
friend class SafepointScope; friend class SafepointScope;
friend class LocalHeap; friend class LocalHeap;
friend class PersistentHandles; friend class PersistentHandles;
......
...@@ -145,6 +145,7 @@ V8InspectorSessionImpl::V8InspectorSessionImpl(V8InspectorImpl* inspector, ...@@ -145,6 +145,7 @@ V8InspectorSessionImpl::V8InspectorSessionImpl(V8InspectorImpl* inspector,
} }
V8InspectorSessionImpl::~V8InspectorSessionImpl() { V8InspectorSessionImpl::~V8InspectorSessionImpl() {
v8::Isolate::Scope scope(m_inspector->isolate());
discardInjectedScripts(); discardInjectedScripts();
m_consoleAgent->disable(); m_consoleAgent->disable();
m_profilerAgent->disable(); m_profilerAgent->disable();
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "src/heap/concurrent-allocator-inl.h" #include "src/heap/concurrent-allocator-inl.h"
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/local-heap-inl.h" #include "src/heap/local-heap-inl.h"
#include "src/heap/parked-scope.h"
#include "src/heap/safepoint.h" #include "src/heap/safepoint.h"
#include "src/objects/heap-number.h" #include "src/objects/heap-number.h"
#include "src/objects/heap-object.h" #include "src/objects/heap-object.h"
...@@ -126,11 +127,7 @@ UNINITIALIZED_TEST(ConcurrentAllocationInOldSpaceFromMainThread) { ...@@ -126,11 +127,7 @@ UNINITIALIZED_TEST(ConcurrentAllocationInOldSpaceFromMainThread) {
v8::Isolate* isolate = v8::Isolate::New(create_params); v8::Isolate* isolate = v8::Isolate::New(create_params);
Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate); Isolate* i_isolate = reinterpret_cast<Isolate*>(isolate);
{ AllocateSomeObjects(i_isolate->main_thread_local_heap());
LocalHeap local_heap(i_isolate->heap(), ThreadKind::kMain);
UnparkedScope unparked_scope(&local_heap);
AllocateSomeObjects(&local_heap);
}
isolate->Dispose(); isolate->Dispose();
} }
......
...@@ -47,6 +47,7 @@ ...@@ -47,6 +47,7 @@
#include "src/heap/mark-compact.h" #include "src/heap/mark-compact.h"
#include "src/heap/memory-chunk.h" #include "src/heap/memory-chunk.h"
#include "src/heap/memory-reducer.h" #include "src/heap/memory-reducer.h"
#include "src/heap/parked-scope.h"
#include "src/heap/remembered-set-inl.h" #include "src/heap/remembered-set-inl.h"
#include "src/heap/safepoint.h" #include "src/heap/safepoint.h"
#include "src/ic/ic.h" #include "src/ic/ic.h"
...@@ -7118,12 +7119,11 @@ TEST(GarbageCollectionWithLocalHeap) { ...@@ -7118,12 +7119,11 @@ TEST(GarbageCollectionWithLocalHeap) {
ManualGCScope manual_gc_scope; ManualGCScope manual_gc_scope;
CcTest::InitializeVM(); CcTest::InitializeVM();
Heap* heap = CcTest::i_isolate()->heap(); LocalHeap* local_heap = CcTest::i_isolate()->main_thread_local_heap();
LocalHeap local_heap(heap, ThreadKind::kMain);
UnparkedScope unparked_scope(&local_heap);
CcTest::CollectGarbage(OLD_SPACE); CcTest::CollectGarbage(OLD_SPACE);
{ ParkedScope parked_scope(&local_heap); }
{ ParkedScope parked_scope(local_heap); }
CcTest::CollectGarbage(OLD_SPACE); CcTest::CollectGarbage(OLD_SPACE);
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/local-heap-inl.h" #include "src/heap/local-heap-inl.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "test/cctest/cctest.h" #include "test/cctest/cctest.h"
#include "test/cctest/heap/heap-utils.h" #include "test/cctest/heap/heap-utils.h"
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "src/handles/persistent-handles.h" #include "src/handles/persistent-handles.h"
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "test/cctest/cctest.h" #include "test/cctest/cctest.h"
#include "test/cctest/heap/heap-utils.h" #include "test/cctest/heap/heap-utils.h"
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/local-heap-inl.h" #include "src/heap/local-heap-inl.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "test/cctest/cctest.h" #include "test/cctest/cctest.h"
#include "test/cctest/heap/heap-utils.h" #include "test/cctest/heap/heap-utils.h"
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "src/handles/persistent-handles.h" #include "src/handles/persistent-handles.h"
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "src/objects/contexts.h" #include "src/objects/contexts.h"
#include "test/cctest/cctest.h" #include "test/cctest/cctest.h"
#include "test/cctest/heap/heap-utils.h" #include "test/cctest/heap/heap-utils.h"
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/local-heap-inl.h" #include "src/heap/local-heap-inl.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "test/cctest/cctest.h" #include "test/cctest/cctest.h"
#include "test/cctest/heap/heap-utils.h" #include "test/cctest/heap/heap-utils.h"
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "src/handles/persistent-handles.h" #include "src/handles/persistent-handles.h"
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "src/objects/transitions-inl.h" #include "src/objects/transitions-inl.h"
#include "test/cctest/cctest.h" #include "test/cctest/cctest.h"
#include "test/cctest/heap/heap-utils.h" #include "test/cctest/heap/heap-utils.h"
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "src/handles/local-handles.h" #include "src/handles/local-handles.h"
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "src/heap/safepoint.h" #include "src/heap/safepoint.h"
#include "src/objects/heap-number.h" #include "src/objects/heap-number.h"
#include "test/cctest/cctest.h" #include "test/cctest/cctest.h"
...@@ -102,11 +103,7 @@ TEST(CreateLocalHandlesWithoutLocalHandleScope) { ...@@ -102,11 +103,7 @@ TEST(CreateLocalHandlesWithoutLocalHandleScope) {
CcTest::InitializeVM(); CcTest::InitializeVM();
Isolate* isolate = CcTest::i_isolate(); Isolate* isolate = CcTest::i_isolate();
{ handle(Smi::FromInt(17), isolate->main_thread_local_heap());
LocalHeap local_heap(isolate->heap(), ThreadKind::kMain);
UnparkedScope scope(&local_heap);
handle(Smi::FromInt(17), &local_heap);
}
} }
TEST(DereferenceLocalHandle) { TEST(DereferenceLocalHandle) {
...@@ -124,7 +121,8 @@ TEST(DereferenceLocalHandle) { ...@@ -124,7 +121,8 @@ TEST(DereferenceLocalHandle) {
ph = phs->NewHandle(number); ph = phs->NewHandle(number);
} }
{ {
LocalHeap local_heap(isolate->heap(), ThreadKind::kMain, std::move(phs)); LocalHeap local_heap(isolate->heap(), ThreadKind::kBackground,
std::move(phs));
UnparkedScope unparked_scope(&local_heap); UnparkedScope unparked_scope(&local_heap);
LocalHandleScope scope(&local_heap); LocalHandleScope scope(&local_heap);
Handle<HeapNumber> local_number = handle(*ph, &local_heap); Handle<HeapNumber> local_number = handle(*ph, &local_heap);
......
...@@ -54,13 +54,15 @@ class DeoptimizeCodeThread : public v8::base::Thread { ...@@ -54,13 +54,15 @@ class DeoptimizeCodeThread : public v8::base::Thread {
void Run() override { void Run() override {
v8::Locker locker(isolate_); v8::Locker locker(isolate_);
isolate_->Enter(); isolate_->Enter();
v8::HandleScope handle_scope(isolate_); {
v8::Local<v8::Context> context = v8::HandleScope handle_scope(isolate_);
v8::Local<v8::Context>::New(isolate_, context_); v8::Local<v8::Context> context =
v8::Context::Scope context_scope(context); v8::Local<v8::Context>::New(isolate_, context_);
// This code triggers deoptimization of some function that will be v8::Context::Scope context_scope(context);
// used in a different thread. // This code triggers deoptimization of some function that will be
CompileRun(source_); // used in a different thread.
CompileRun(source_);
}
isolate_->Exit(); isolate_->Exit();
} }
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/local-heap-inl.h" #include "src/heap/local-heap-inl.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "src/heap/safepoint.h" #include "src/heap/safepoint.h"
#include "src/objects/heap-number.h" #include "src/objects/heap-number.h"
#include "test/cctest/cctest.h" #include "test/cctest/cctest.h"
...@@ -129,7 +130,8 @@ TEST(DereferencePersistentHandle) { ...@@ -129,7 +130,8 @@ TEST(DereferencePersistentHandle) {
ph = phs->NewHandle(number); ph = phs->NewHandle(number);
} }
{ {
LocalHeap local_heap(isolate->heap(), ThreadKind::kMain, std::move(phs)); LocalHeap local_heap(isolate->heap(), ThreadKind::kBackground,
std::move(phs));
UnparkedScope scope(&local_heap); UnparkedScope scope(&local_heap);
CHECK_EQ(42, ph->value()); CHECK_EQ(42, ph->value());
DisallowHandleDereference disallow_scope; DisallowHandleDereference disallow_scope;
...@@ -141,7 +143,7 @@ TEST(NewPersistentHandleFailsWhenParked) { ...@@ -141,7 +143,7 @@ TEST(NewPersistentHandleFailsWhenParked) {
CcTest::InitializeVM(); CcTest::InitializeVM();
Isolate* isolate = CcTest::i_isolate(); Isolate* isolate = CcTest::i_isolate();
LocalHeap local_heap(isolate->heap(), ThreadKind::kMain); LocalHeap local_heap(isolate->heap(), ThreadKind::kBackground);
// Fail here in debug mode: Persistent handles can't be created if local heap // Fail here in debug mode: Persistent handles can't be created if local heap
// is parked // is parked
local_heap.NewPersistentHandle(Smi::FromInt(1)); local_heap.NewPersistentHandle(Smi::FromInt(1));
...@@ -151,7 +153,7 @@ TEST(NewPersistentHandleFailsWhenParkedExplicit) { ...@@ -151,7 +153,7 @@ TEST(NewPersistentHandleFailsWhenParkedExplicit) {
CcTest::InitializeVM(); CcTest::InitializeVM();
Isolate* isolate = CcTest::i_isolate(); Isolate* isolate = CcTest::i_isolate();
LocalHeap local_heap(isolate->heap(), ThreadKind::kMain, LocalHeap local_heap(isolate->heap(), ThreadKind::kBackground,
isolate->NewPersistentHandles()); isolate->NewPersistentHandles());
// Fail here in debug mode: Persistent handles can't be created if local heap // Fail here in debug mode: Persistent handles can't be created if local heap
// is parked // is parked
......
...@@ -38,6 +38,7 @@ class CompilerDispatcherTestFlags { ...@@ -38,6 +38,7 @@ class CompilerDispatcherTestFlags {
FLAG_single_threaded = true; FLAG_single_threaded = true;
FlagList::EnforceFlagImplications(); FlagList::EnforceFlagImplications();
FLAG_compiler_dispatcher = true; FLAG_compiler_dispatcher = true;
FLAG_finalize_streaming_on_background = false;
} }
static void RestoreFlags() { static void RestoreFlags() {
......
...@@ -62,8 +62,7 @@ class LocalFactoryTest : public TestWithIsolateAndZone { ...@@ -62,8 +62,7 @@ class LocalFactoryTest : public TestWithIsolateAndZone {
isolate(), true, construct_language_mode(FLAG_use_strict), isolate(), true, construct_language_mode(FLAG_use_strict),
REPLMode::kNo), REPLMode::kNo),
&state_), &state_),
local_isolate_(isolate(), ThreadKind::kMain), local_isolate_(isolate()->main_thread_local_isolate()) {
unparked_scope_(local_isolate_.heap()) {
FLAG_concurrent_allocation = true; FLAG_concurrent_allocation = true;
} }
...@@ -107,15 +106,14 @@ class LocalFactoryTest : public TestWithIsolateAndZone { ...@@ -107,15 +106,14 @@ class LocalFactoryTest : public TestWithIsolateAndZone {
Handle<Script> script() { return script_; } Handle<Script> script() { return script_; }
LocalIsolate* local_isolate() { return &local_isolate_; } LocalIsolate* local_isolate() { return local_isolate_; }
LocalFactory* local_factory() { return local_isolate()->factory(); } LocalFactory* local_factory() { return local_isolate()->factory(); }
private: private:
SaveFlags save_flags_; SaveFlags save_flags_;
UnoptimizedCompileState state_; UnoptimizedCompileState state_;
ParseInfo parse_info_; ParseInfo parse_info_;
LocalIsolate local_isolate_; LocalIsolate* local_isolate_;
UnparkedScope unparked_scope_;
Handle<String> source_string_; Handle<String> source_string_;
Handle<Script> script_; Handle<Script> script_;
}; };
......
...@@ -15,15 +15,7 @@ using LocalHeapTest = TestWithIsolate; ...@@ -15,15 +15,7 @@ using LocalHeapTest = TestWithIsolate;
TEST_F(LocalHeapTest, Initialize) { TEST_F(LocalHeapTest, Initialize) {
Heap* heap = i_isolate()->heap(); Heap* heap = i_isolate()->heap();
CHECK(heap->safepoint()->ContainsAnyLocalHeap());
CHECK(!heap->safepoint()->ContainsAnyLocalHeap());
{
LocalHeap lh(heap, ThreadKind::kMain);
CHECK(heap->safepoint()->ContainsLocalHeap(&lh));
}
CHECK(!heap->safepoint()->ContainsAnyLocalHeap());
} }
TEST_F(LocalHeapTest, Current) { TEST_F(LocalHeapTest, Current) {
...@@ -33,14 +25,14 @@ TEST_F(LocalHeapTest, Current) { ...@@ -33,14 +25,14 @@ TEST_F(LocalHeapTest, Current) {
{ {
LocalHeap lh(heap, ThreadKind::kMain); LocalHeap lh(heap, ThreadKind::kMain);
CHECK_EQ(&lh, LocalHeap::Current()); CHECK_NULL(LocalHeap::Current());
} }
CHECK_NULL(LocalHeap::Current()); CHECK_NULL(LocalHeap::Current());
{ {
LocalHeap lh(heap, ThreadKind::kMain); LocalHeap lh(heap, ThreadKind::kMain);
CHECK_EQ(&lh, LocalHeap::Current()); CHECK_NULL(LocalHeap::Current());
} }
CHECK_NULL(LocalHeap::Current()); CHECK_NULL(LocalHeap::Current());
...@@ -73,9 +65,9 @@ TEST_F(LocalHeapTest, CurrentBackground) { ...@@ -73,9 +65,9 @@ TEST_F(LocalHeapTest, CurrentBackground) {
LocalHeap lh(heap, ThreadKind::kMain); LocalHeap lh(heap, ThreadKind::kMain);
auto thread = std::make_unique<BackgroundThread>(heap); auto thread = std::make_unique<BackgroundThread>(heap);
CHECK(thread->Start()); CHECK(thread->Start());
CHECK_EQ(&lh, LocalHeap::Current()); CHECK_NULL(LocalHeap::Current());
thread->Join(); thread->Join();
CHECK_EQ(&lh, LocalHeap::Current()); CHECK_NULL(LocalHeap::Current());
} }
CHECK_NULL(LocalHeap::Current()); CHECK_NULL(LocalHeap::Current());
} }
......
...@@ -3,10 +3,12 @@ ...@@ -3,10 +3,12 @@
// found in the LICENSE file. // found in the LICENSE file.
#include "src/heap/safepoint.h" #include "src/heap/safepoint.h"
#include "src/base/platform/mutex.h" #include "src/base/platform/mutex.h"
#include "src/base/platform/platform.h" #include "src/base/platform/platform.h"
#include "src/heap/heap.h" #include "src/heap/heap.h"
#include "src/heap/local-heap.h" #include "src/heap/local-heap.h"
#include "src/heap/parked-scope.h"
#include "test/unittests/test-utils.h" #include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
...@@ -144,25 +146,5 @@ TEST_F(SafepointTest, StopRunningThreads) { ...@@ -144,25 +146,5 @@ TEST_F(SafepointTest, StopRunningThreads) {
CHECK_EQ(safepoint_count, kRuns * kSafepoints); CHECK_EQ(safepoint_count, kRuns * kSafepoints);
} }
TEST_F(SafepointTest, SkipLocalHeapOfThisThread) {
EnsureFlagLocalHeapsEnabled();
Heap* heap = i_isolate()->heap();
LocalHeap local_heap(heap, ThreadKind::kMain);
UnparkedScope unparked_scope(&local_heap);
{
SafepointScope scope(heap);
local_heap.Safepoint();
}
{
ParkedScope parked_scope(&local_heap);
SafepointScope scope(heap);
local_heap.Safepoint();
}
{
SafepointScope scope(heap);
local_heap.Safepoint();
}
}
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
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