Welcome to mirror list, hosted at ThFree Co, Russian Federation.

local-heap.h « heap « src « v8 « deps - github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 31c66bc2be5f0463bb54a3cb22bf071f84fecda8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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_LOCAL_HEAP_H_
#define V8_HEAP_LOCAL_HEAP_H_

#include <atomic>
#include <memory>

#include "src/base/platform/condition-variable.h"
#include "src/base/platform/mutex.h"
#include "src/execution/isolate.h"
#include "src/heap/concurrent-allocator.h"

namespace v8 {
namespace internal {

class Heap;
class Safepoint;
class LocalHandles;
class PersistentHandles;

class LocalHeap {
 public:
  V8_EXPORT_PRIVATE explicit LocalHeap(
      Heap* heap,
      std::unique_ptr<PersistentHandles> persistent_handles = nullptr);
  V8_EXPORT_PRIVATE ~LocalHeap();

  // Invoked by main thread to signal this thread that it needs to halt in a
  // safepoint.
  void RequestSafepoint();

  // Frequently invoked by local thread to check whether safepoint was requested
  // from the main thread.
  V8_EXPORT_PRIVATE void Safepoint();

  LocalHandles* handles() { return handles_.get(); }

  V8_EXPORT_PRIVATE Handle<Object> NewPersistentHandle(Address value);
  V8_EXPORT_PRIVATE std::unique_ptr<PersistentHandles>
  DetachPersistentHandles();

  bool IsParked();

  Heap* heap() { return heap_; }

  ConcurrentAllocator* old_space_allocator() { return &old_space_allocator_; }

 private:
  enum class ThreadState {
    // Threads in this state need to be stopped in a safepoint.
    Running,
    // Thread was parked, which means that the thread is not allowed to access
    // or manipulate the heap in any way.
    Parked,
    // Thread was stopped in a safepoint.
    Safepoint
  };

  V8_EXPORT_PRIVATE void Park();
  V8_EXPORT_PRIVATE void Unpark();
  void EnsureParkedBeforeDestruction();

  bool IsSafepointRequested();
  void ClearSafepointRequested();

  void EnterSafepoint();

  void FreeLinearAllocationArea();
  void MakeLinearAllocationAreaIterable();

  Heap* heap_;

  base::Mutex state_mutex_;
  base::ConditionVariable state_change_;
  ThreadState state_;

  std::atomic<bool> safepoint_requested_;

  bool allocation_failed_;

  LocalHeap* prev_;
  LocalHeap* next_;

  std::unique_ptr<LocalHandles> handles_;
  std::unique_ptr<PersistentHandles> persistent_handles_;

  ConcurrentAllocator old_space_allocator_;

  friend class Heap;
  friend class GlobalSafepoint;
  friend class ParkedScope;
  friend class ConcurrentAllocator;
};

class ParkedScope {
 public:
  explicit ParkedScope(LocalHeap* local_heap) : local_heap_(local_heap) {
    local_heap_->Park();
  }

  ~ParkedScope() { local_heap_->Unpark(); }

 private:
  LocalHeap* local_heap_;
};

}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_LOCAL_HEAP_H_