safepoint.h 5.82 KB
Newer Older
1 2 3 4 5 6 7 8 9
// 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_SAFEPOINT_H_
#define V8_HEAP_SAFEPOINT_H_

#include "src/base/platform/condition-variable.h"
#include "src/base/platform/mutex.h"
10
#include "src/common/globals.h"
11
#include "src/handles/persistent-handles.h"
12
#include "src/heap/local-heap.h"
13
#include "src/objects/visitors.h"
14 15 16 17 18 19

namespace v8 {
namespace internal {

class Heap;
class LocalHeap;
20
class PerClientSafepointData;
21
class RootVisitor;
22

23 24
// Used to bring all threads with heap access in an isolate to a safepoint such
// that e.g. a garbage collection can be performed.
25
class IsolateSafepoint final {
26
 public:
27
  explicit IsolateSafepoint(Heap* heap);
28

29 30 31
  // Iterate handles in local heaps
  void Iterate(RootVisitor* visitor);

32 33 34
  // Iterate local heaps
  template <typename Callback>
  void IterateLocalHeaps(Callback callback) {
35
    AssertActive();
36 37 38 39 40 41
    for (LocalHeap* current = local_heaps_head_; current;
         current = current->next_) {
      callback(current);
    }
  }

42
  void AssertActive() { local_heaps_mutex_.AssertHeld(); }
43

44
  V8_EXPORT_PRIVATE void AssertMainThreadIsOnlyThread();
45

46 47 48
 private:
  class Barrier {
    base::Mutex mutex_;
49 50
    base::ConditionVariable cv_resume_;
    base::ConditionVariable cv_stopped_;
51 52
    bool armed_;

53
    size_t stopped_ = 0;
54 55 56

    bool IsArmed() { return armed_; }

57
   public:
58
    Barrier() : armed_(false), stopped_(0) {}
59 60 61

    void Arm();
    void Disarm();
62
    void WaitUntilRunningThreadsInSafepoint(size_t running);
63 64 65 66

    void WaitInSafepoint();
    void WaitInUnpark();
    void NotifyPark();
67 68
  };

69 70 71 72 73 74 75 76 77 78 79
  enum class IncludeMainThread { kYes, kNo };

  // Wait until unpark operation is safe again.
  void WaitInUnpark();

  // Enter the safepoint from a running thread.
  void WaitInSafepoint();

  // Running thread reached a safepoint by parking itself.
  void NotifyPark();

80
  // Methods for entering/leaving local safepoint scopes.
81
  void EnterLocalSafepointScope();
82
  void LeaveLocalSafepointScope();
83 84 85 86 87 88 89 90

  // Methods for entering/leaving global safepoint scopes.
  void TryInitiateGlobalSafepointScope(Isolate* initiator,
                                       PerClientSafepointData* client_data);
  void InitiateGlobalSafepointScope(Isolate* initiator,
                                    PerClientSafepointData* client_data);
  void InitiateGlobalSafepointScopeRaw(Isolate* initiator,
                                       PerClientSafepointData* client_data);
91 92
  void LeaveGlobalSafepointScope(Isolate* initiator);

93 94 95 96
  // Blocks until all running threads reached a safepoint.
  void WaitUntilRunningThreadsInSafepoint(
      const PerClientSafepointData* client_data);

97
  IncludeMainThread ShouldIncludeMainThread(Isolate* initiator);
98 99

  void LockMutex(LocalHeap* local_heap);
100

101 102
  size_t SetSafepointRequestedFlags(IncludeMainThread include_main_thread);
  void ClearSafepointRequestedFlags(IncludeMainThread include_main_thread);
103

104 105 106 107
  template <typename Callback>
  void AddLocalHeap(LocalHeap* local_heap, Callback callback) {
    // Safepoint holds this lock in order to stop threads from starting or
    // stopping.
108
    base::RecursiveMutexGuard guard(&local_heaps_mutex_);
109 110 111 112 113 114 115 116 117 118 119 120 121

    // Additional code protected from safepoint
    callback();

    // Add list to doubly-linked list
    if (local_heaps_head_) local_heaps_head_->prev_ = local_heap;
    local_heap->prev_ = nullptr;
    local_heap->next_ = local_heaps_head_;
    local_heaps_head_ = local_heap;
  }

  template <typename Callback>
  void RemoveLocalHeap(LocalHeap* local_heap, Callback callback) {
122
    base::RecursiveMutexGuard guard(&local_heaps_mutex_);
123 124 125 126 127 128 129 130 131 132 133

    // Additional code protected from safepoint
    callback();

    // Remove list from doubly-linked list
    if (local_heap->next_) local_heap->next_->prev_ = local_heap->prev_;
    if (local_heap->prev_)
      local_heap->prev_->next_ = local_heap->next_;
    else
      local_heaps_head_ = local_heap->next_;
  }
134

135 136 137
  Isolate* isolate() const;
  Isolate* shared_isolate() const;

138 139 140
  Barrier barrier_;
  Heap* heap_;

141 142 143
  // Mutex is used both for safepointing and adding/removing threads. A
  // RecursiveMutex is needed since we need to support nested SafepointScopes.
  base::RecursiveMutex local_heaps_mutex_;
144 145
  LocalHeap* local_heaps_head_;

146
  int active_safepoint_scopes_;
147

148
  friend class GlobalSafepoint;
149
  friend class GlobalSafepointScope;
150
  friend class Isolate;
151
  friend class LocalHeap;
152
  friend class SafepointScope;
153 154
};

155
class V8_NODISCARD SafepointScope {
156 157 158 159 160
 public:
  V8_EXPORT_PRIVATE explicit SafepointScope(Heap* heap);
  V8_EXPORT_PRIVATE ~SafepointScope();

 private:
161
  IsolateSafepoint* safepoint_;
162 163
};

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
// Used for reaching a global safepoint, a safepoint across all client isolates
// of the shared isolate.
class GlobalSafepoint final {
 public:
  explicit GlobalSafepoint(Isolate* isolate);

  void AppendClient(Isolate* client);
  void RemoveClient(Isolate* client);

  template <typename Callback>
  void IterateClientIsolates(Callback callback) {
    for (Isolate* current = clients_head_; current;
         current = current->global_safepoint_next_client_isolate_) {
      callback(current);
    }
  }

  void AssertNoClients();

183 184
  void AssertActive() { clients_mutex_.AssertHeld(); }

185 186 187 188 189 190 191 192 193 194
 private:
  void EnterGlobalSafepointScope(Isolate* initiator);
  void LeaveGlobalSafepointScope(Isolate* initiator);

  Isolate* const shared_isolate_;
  Heap* const shared_heap_;
  base::Mutex clients_mutex_;
  Isolate* clients_head_ = nullptr;

  friend class GlobalSafepointScope;
195
  friend class Isolate;
196 197 198 199 200 201 202 203 204 205 206 207
};

class V8_NODISCARD GlobalSafepointScope {
 public:
  V8_EXPORT_PRIVATE explicit GlobalSafepointScope(Isolate* initiator);
  V8_EXPORT_PRIVATE ~GlobalSafepointScope();

 private:
  Isolate* const initiator_;
  Isolate* const shared_isolate_;
};

208 209 210 211
}  // namespace internal
}  // namespace v8

#endif  // V8_HEAP_SAFEPOINT_H_