local-heap.cc 3.12 KB
Newer Older
1 2 3 4 5
// 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.

#include "src/heap/local-heap.h"
6 7 8 9

#include <memory>

#include "src/base/platform/mutex.h"
10
#include "src/handles/local-handles.h"
11
#include "src/heap/heap-inl.h"
12
#include "src/heap/safepoint.h"
13 14 15 16

namespace v8 {
namespace internal {

17 18
LocalHeap::LocalHeap(Heap* heap,
                     std::unique_ptr<PersistentHandles> persistent_handles)
19 20
    : heap_(heap),
      state_(ThreadState::Running),
21
      safepoint_requested_(false),
22
      allocation_failed_(false),
23
      prev_(nullptr),
24
      next_(nullptr),
25
      handles_(new LocalHandles),
26 27
      persistent_handles_(std::move(persistent_handles)),
      old_space_allocator_(this, heap->old_space()) {
28
  heap_->safepoint()->AddLocalHeap(this);
29 30 31
  if (persistent_handles_) {
    persistent_handles_->Attach(this);
  }
32 33
}

34
LocalHeap::~LocalHeap() {
35 36 37
  // Give up LAB before parking thread
  old_space_allocator_.FreeLinearAllocationArea();

38 39 40
  // Park thread since removing the local heap could block.
  EnsureParkedBeforeDestruction();

41
  heap_->safepoint()->RemoveLocalHeap(this);
42
}
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
Handle<Object> LocalHeap::NewPersistentHandle(Address value) {
  if (!persistent_handles_) {
    persistent_handles_.reset(
        heap_->isolate()->NewPersistentHandles().release());
  }
  return persistent_handles_->NewHandle(value);
}

std::unique_ptr<PersistentHandles> LocalHeap::DetachPersistentHandles() {
  if (persistent_handles_) persistent_handles_->Detach();
  return std::move(persistent_handles_);
}

bool LocalHeap::IsParked() {
  base::MutexGuard guard(&state_mutex_);
  return state_ == ThreadState::Parked;
}

62 63 64 65
void LocalHeap::Park() {
  base::MutexGuard guard(&state_mutex_);
  CHECK(state_ == ThreadState::Running);
  state_ = ThreadState::Parked;
66
  state_change_.NotifyAll();
67 68 69 70 71 72 73 74
}

void LocalHeap::Unpark() {
  base::MutexGuard guard(&state_mutex_);
  CHECK(state_ == ThreadState::Parked);
  state_ = ThreadState::Running;
}

75 76 77 78 79 80
void LocalHeap::EnsureParkedBeforeDestruction() {
  base::MutexGuard guard(&state_mutex_);
  state_ = ThreadState::Parked;
  state_change_.NotifyAll();
}

81 82 83 84 85 86 87 88 89 90
void LocalHeap::RequestSafepoint() {
  safepoint_requested_.store(true, std::memory_order_relaxed);
}

bool LocalHeap::IsSafepointRequested() {
  return safepoint_requested_.load(std::memory_order_relaxed);
}

void LocalHeap::Safepoint() {
  if (IsSafepointRequested()) {
91
    ClearSafepointRequested();
92 93 94 95 96 97 98 99
    EnterSafepoint();
  }
}

void LocalHeap::ClearSafepointRequested() {
  safepoint_requested_.store(false, std::memory_order_relaxed);
}

100
void LocalHeap::EnterSafepoint() { heap_->safepoint()->EnterFromThread(this); }
101

102 103 104 105
void LocalHeap::FreeLinearAllocationArea() {
  old_space_allocator_.FreeLinearAllocationArea();
}

106 107 108 109
void LocalHeap::MakeLinearAllocationAreaIterable() {
  old_space_allocator_.MakeLinearAllocationAreaIterable();
}

110 111 112 113 114 115 116 117
void LocalHeap::MarkLinearAllocationAreaBlack() {
  old_space_allocator_.MarkLinearAllocationAreaBlack();
}

void LocalHeap::UnmarkLinearAllocationArea() {
  old_space_allocator_.UnmarkLinearAllocationArea();
}

118 119
}  // namespace internal
}  // namespace v8