summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/heap/local-heap-unittest.cc
blob: 919578f2fbb97f8afde335a7bb51387aca0108cd (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// 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"

#include "src/base/platform/condition-variable.h"
#include "src/base/platform/mutex.h"
#include "src/heap/heap.h"
#include "src/heap/parked-scope.h"
#include "src/heap/safepoint.h"
#include "test/unittests/test-utils.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace v8 {
namespace internal {

using LocalHeapTest = TestWithIsolate;

TEST_F(LocalHeapTest, Initialize) {
  Heap* heap = i_isolate()->heap();
  CHECK(heap->safepoint()->ContainsAnyLocalHeap());
}

TEST_F(LocalHeapTest, Current) {
  Heap* heap = i_isolate()->heap();

  CHECK_NULL(LocalHeap::Current());

  {
    LocalHeap lh(heap, ThreadKind::kMain);
    CHECK_NULL(LocalHeap::Current());
  }

  CHECK_NULL(LocalHeap::Current());

  {
    LocalHeap lh(heap, ThreadKind::kMain);
    CHECK_NULL(LocalHeap::Current());
  }

  CHECK_NULL(LocalHeap::Current());
}

namespace {
class BackgroundThread final : public v8::base::Thread {
 public:
  explicit BackgroundThread(Heap* heap)
      : v8::base::Thread(base::Thread::Options("BackgroundThread")),
        heap_(heap) {}

  void Run() override {
    CHECK_NULL(LocalHeap::Current());
    {
      LocalHeap lh(heap_, ThreadKind::kBackground);
      CHECK_EQ(&lh, LocalHeap::Current());
    }
    CHECK_NULL(LocalHeap::Current());
  }

  Heap* heap_;
};
}  // anonymous namespace

TEST_F(LocalHeapTest, CurrentBackground) {
  Heap* heap = i_isolate()->heap();
  CHECK_NULL(LocalHeap::Current());
  {
    LocalHeap lh(heap, ThreadKind::kMain);
    auto thread = std::make_unique<BackgroundThread>(heap);
    CHECK(thread->Start());
    CHECK_NULL(LocalHeap::Current());
    thread->Join();
    CHECK_NULL(LocalHeap::Current());
  }
  CHECK_NULL(LocalHeap::Current());
}

namespace {

class GCEpilogue {
 public:
  static void Callback(void* data) {
    reinterpret_cast<GCEpilogue*>(data)->was_invoked_ = true;
  }

  void NotifyStarted() {
    base::LockGuard<base::Mutex> lock_guard(&mutex_);
    started_ = true;
    cv_.NotifyOne();
  }

  void WaitUntilStarted() {
    base::LockGuard<base::Mutex> lock_guard(&mutex_);
    while (!started_) {
      cv_.Wait(&mutex_);
    }
  }
  void RequestStop() {
    base::LockGuard<base::Mutex> lock_guard(&mutex_);
    stop_requested_ = true;
  }

  bool StopRequested() {
    base::LockGuard<base::Mutex> lock_guard(&mutex_);
    return stop_requested_;
  }

  bool WasInvoked() { return was_invoked_; }

 private:
  bool was_invoked_ = false;
  bool started_ = false;
  bool stop_requested_ = false;
  base::Mutex mutex_;
  base::ConditionVariable cv_;
};

class BackgroundThreadForGCEpilogue final : public v8::base::Thread {
 public:
  explicit BackgroundThreadForGCEpilogue(Heap* heap, bool parked,
                                         GCEpilogue* epilogue)
      : v8::base::Thread(base::Thread::Options("BackgroundThread")),
        heap_(heap),
        parked_(parked),
        epilogue_(epilogue) {}

  void Run() override {
    LocalHeap lh(heap_, ThreadKind::kBackground);
    base::Optional<UnparkedScope> unparked_scope;
    if (!parked_) {
      unparked_scope.emplace(&lh);
    }
    {
      base::Optional<UnparkedScope> unparked_scope;
      if (parked_) unparked_scope.emplace(&lh);
      lh.AddGCEpilogueCallback(&GCEpilogue::Callback, epilogue_);
    }
    epilogue_->NotifyStarted();
    while (!epilogue_->StopRequested()) {
      lh.Safepoint();
    }
    {
      base::Optional<UnparkedScope> unparked_scope;
      if (parked_) unparked_scope.emplace(&lh);
      lh.RemoveGCEpilogueCallback(&GCEpilogue::Callback, epilogue_);
    }
  }

  Heap* heap_;
  bool parked_;
  GCEpilogue* epilogue_;
};

}  // anonymous namespace

TEST_F(LocalHeapTest, GCEpilogue) {
  Heap* heap = i_isolate()->heap();
  LocalHeap lh(heap, ThreadKind::kMain);
  std::array<GCEpilogue, 3> epilogue;
  {
    UnparkedScope unparked(&lh);
    lh.AddGCEpilogueCallback(&GCEpilogue::Callback, &epilogue[0]);
  }
  auto thread1 =
      std::make_unique<BackgroundThreadForGCEpilogue>(heap, true, &epilogue[1]);
  auto thread2 = std::make_unique<BackgroundThreadForGCEpilogue>(heap, false,
                                                                 &epilogue[2]);
  CHECK(thread1->Start());
  CHECK(thread2->Start());
  epilogue[1].WaitUntilStarted();
  epilogue[2].WaitUntilStarted();
  heap->PreciseCollectAllGarbage(Heap::kNoGCFlags,
                                 GarbageCollectionReason::kTesting);
  epilogue[1].RequestStop();
  epilogue[2].RequestStop();
  thread1->Join();
  thread2->Join();
  {
    UnparkedScope unparked(&lh);
    lh.RemoveGCEpilogueCallback(&GCEpilogue::Callback, &epilogue[0]);
  }
  for (auto& e : epilogue) {
    CHECK(e.WasInvoked());
  }
}

}  // namespace internal
}  // namespace v8