Commit 0cd11cee authored by Nikolaos Papaspyrou's avatar Nikolaos Papaspyrou Committed by V8 LUCI CQ

[heap][cleanup] Refactor checking for main thread

This CL removes GCTracer::AssertMainThread and adds the more general
methods Heap::IsMainThread and Heap::IsSharedMainThread, to be used
in DCHECKs and elsewhere. It also introduces some const qualifiers.

Bug: v8:12425
Change-Id: Ibdec39ce77be704598ca0c8b440005dc27bd6997
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3650600Reviewed-by: 's avatarDominik Inführ <dinfuehr@chromium.org>
Commit-Queue: Nikolaos Papaspyrou <nikolaos@chromium.org>
Reviewed-by: 's avatarToon Verwaest <verwaest@chromium.org>
Cr-Commit-Position: refs/heads/main@{#80586}
parent 3ebf2052
......@@ -1157,7 +1157,7 @@ class V8_EXPORT_PRIVATE Isolate final : private HiddenFactory {
Heap* heap() { return &heap_; }
const Heap* heap() const { return &heap_; }
ReadOnlyHeap* read_only_heap() const { return read_only_heap_; }
static Isolate* FromHeap(Heap* heap) {
static Isolate* FromHeap(const Heap* heap) {
return reinterpret_cast<Isolate*>(reinterpret_cast<Address>(heap) -
OFFSET_OF(Isolate, heap_));
}
......
......@@ -8,6 +8,7 @@
#include "src/base/platform/platform.h"
#include "src/execution/isolate.h"
#include "src/heap/gc-tracer.h"
#include "src/heap/heap-inl.h"
namespace v8 {
namespace internal {
......@@ -37,9 +38,8 @@ GCTracer::Scope::Scope(GCTracer* tracer, ScopeId scope, ThreadKind thread_kind)
#ifdef V8_RUNTIME_CALL_STATS
if (V8_LIKELY(!TracingFlags::is_runtime_stats_enabled())) return;
if (thread_kind_ == ThreadKind::kMain) {
#if DEBUG
AssertMainThread();
#endif // DEBUG
DCHECK(tracer_->heap_->IsMainThread() ||
tracer_->heap_->IsSharedMainThread());
runtime_stats_ = tracer_->heap_->isolate_->counters()->runtime_call_stats();
runtime_stats_->Enter(&timer_, GCTracer::RCSCounterFromScope(scope));
} else {
......@@ -56,10 +56,8 @@ GCTracer::Scope::~Scope() {
tracer_->AddScopeSample(scope_, duration_ms);
if (thread_kind_ == ThreadKind::kMain) {
#if DEBUG
AssertMainThread();
#endif // DEBUG
DCHECK(tracer_->heap_->IsMainThread() ||
tracer_->heap_->IsSharedMainThread());
if (scope_ == ScopeId::MC_INCREMENTAL ||
scope_ == ScopeId::MC_INCREMENTAL_START ||
scope_ == ScopeId::MC_INCREMENTAL_FINALIZE) {
......
......@@ -45,19 +45,6 @@ CollectionEpoch next_epoch() {
}
} // namespace
#if DEBUG
void GCTracer::Scope::AssertMainThread() {
Isolate* isolate = tracer_->heap_->isolate();
Isolate* shared_isolate = isolate->shared_isolate();
ThreadId thread_id = ThreadId::Current();
// Either run on isolate's main thread or on the current main thread of the
// shared isolate during shared GCs.
DCHECK(isolate->thread_id() == thread_id ||
(shared_isolate && shared_isolate->thread_id() == thread_id));
}
#endif // DEBUG
GCTracer::Event::Event(Type type, State state,
GarbageCollectionReason gc_reason,
const char* collector_reason)
......
......@@ -102,10 +102,6 @@ class V8_EXPORT_PRIVATE GCTracer {
static constexpr int IncrementalOffset(ScopeId id);
private:
#if DEBUG
void AssertMainThread();
#endif // DEBUG
GCTracer* const tracer_;
const ScopeId scope_;
const ThreadKind thread_kind_;
......
......@@ -95,7 +95,18 @@ base::EnumSet<CodeFlushMode> Heap::GetCodeFlushMode(Isolate* isolate) {
return code_flush_mode;
}
Isolate* Heap::isolate() { return Isolate::FromHeap(this); }
Isolate* Heap::isolate() const { return Isolate::FromHeap(this); }
#ifdef DEBUG
bool Heap::IsMainThread() const {
return isolate()->thread_id() == ThreadId::Current();
}
bool Heap::IsSharedMainThread() const {
Isolate* shared_isolate = isolate()->shared_isolate();
return shared_isolate && shared_isolate->thread_id() == ThreadId::Current();
}
#endif
int64_t Heap::external_memory() { return external_memory_.total(); }
......
......@@ -893,7 +893,15 @@ class Heap {
inline ConcurrentAllocator* concurrent_allocator_for_maps();
inline Isolate* isolate();
inline Isolate* isolate() const;
#ifdef DEBUG
// Check if we run on isolate's main thread.
inline bool IsMainThread() const;
// Check if we run on the current main thread of the shared isolate during
// shared GC.
inline bool IsSharedMainThread() const;
#endif
MarkCompactCollector* mark_compact_collector() {
return mark_compact_collector_.get();
......
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