2020-04-06 06:23:44 +00:00
|
|
|
// 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 <memory>
|
|
|
|
|
|
|
|
#include "src/api/api.h"
|
|
|
|
#include "src/base/platform/condition-variable.h"
|
|
|
|
#include "src/base/platform/mutex.h"
|
|
|
|
#include "src/base/platform/semaphore.h"
|
|
|
|
#include "src/handles/handles-inl.h"
|
|
|
|
#include "src/handles/local-handles-inl.h"
|
|
|
|
#include "src/handles/persistent-handles.h"
|
|
|
|
#include "src/heap/heap.h"
|
2020-07-10 08:58:37 +00:00
|
|
|
#include "src/heap/local-heap-inl.h"
|
2020-04-06 06:23:44 +00:00
|
|
|
#include "src/heap/local-heap.h"
|
|
|
|
#include "src/heap/safepoint.h"
|
|
|
|
#include "src/objects/heap-number.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
#include "test/cctest/heap/heap-utils.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
static constexpr int kNumHandles = kHandleBlockSize * 2 + kHandleBlockSize / 2;
|
|
|
|
|
2020-06-10 13:45:24 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-04-06 06:23:44 +00:00
|
|
|
class PersistentHandlesThread final : public v8::base::Thread {
|
|
|
|
public:
|
|
|
|
PersistentHandlesThread(Heap* heap, std::vector<Handle<HeapNumber>> handles,
|
2020-07-10 08:58:37 +00:00
|
|
|
std::unique_ptr<PersistentHandles> ph,
|
|
|
|
HeapNumber number, base::Semaphore* sema_started,
|
2020-04-06 06:23:44 +00:00
|
|
|
base::Semaphore* sema_gc_finished)
|
|
|
|
: v8::base::Thread(base::Thread::Options("ThreadWithLocalHeap")),
|
|
|
|
heap_(heap),
|
|
|
|
handles_(std::move(handles)),
|
|
|
|
ph_(std::move(ph)),
|
2020-07-10 08:58:37 +00:00
|
|
|
number_(number),
|
2020-04-06 06:23:44 +00:00
|
|
|
sema_started_(sema_started),
|
|
|
|
sema_gc_finished_(sema_gc_finished) {}
|
|
|
|
|
|
|
|
void Run() override {
|
2020-10-14 11:47:05 +00:00
|
|
|
LocalHeap local_heap(heap_, ThreadKind::kBackground, std::move(ph_));
|
2020-10-15 10:57:22 +00:00
|
|
|
UnparkedScope unparked_scope(&local_heap);
|
2020-04-06 06:23:44 +00:00
|
|
|
LocalHandleScope scope(&local_heap);
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumHandles; i++) {
|
2020-07-10 08:58:37 +00:00
|
|
|
handles_.push_back(local_heap.NewPersistentHandle(number_));
|
2020-04-06 06:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sema_started_->Signal();
|
|
|
|
|
|
|
|
{
|
|
|
|
ParkedScope scope(&local_heap);
|
|
|
|
sema_gc_finished_->Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Handle<HeapNumber> handle : handles_) {
|
|
|
|
CHECK_EQ(42.0, handle->value());
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_EQ(handles_.size(), kNumHandles * 2);
|
|
|
|
|
|
|
|
CHECK(!ph_);
|
|
|
|
ph_ = local_heap.DetachPersistentHandles();
|
|
|
|
}
|
|
|
|
|
2020-10-09 14:10:42 +00:00
|
|
|
std::unique_ptr<PersistentHandles> DetachPersistentHandles() {
|
|
|
|
CHECK(ph_);
|
|
|
|
return std::move(ph_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-04-06 06:23:44 +00:00
|
|
|
Heap* heap_;
|
|
|
|
std::vector<Handle<HeapNumber>> handles_;
|
|
|
|
std::unique_ptr<PersistentHandles> ph_;
|
2020-07-10 08:58:37 +00:00
|
|
|
HeapNumber number_;
|
2020-04-06 06:23:44 +00:00
|
|
|
base::Semaphore* sema_started_;
|
|
|
|
base::Semaphore* sema_gc_finished_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(CreatePersistentHandles) {
|
2020-09-24 10:51:32 +00:00
|
|
|
heap::EnsureFlagLocalHeapsEnabled();
|
2020-04-06 06:23:44 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
|
|
|
std::unique_ptr<PersistentHandles> ph = isolate->NewPersistentHandles();
|
|
|
|
std::vector<Handle<HeapNumber>> handles;
|
|
|
|
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<HeapNumber> number = isolate->factory()->NewHeapNumber(42.0);
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumHandles; i++) {
|
2020-07-10 08:58:37 +00:00
|
|
|
handles.push_back(ph->NewHandle(number));
|
2020-04-06 06:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
base::Semaphore sema_started(0);
|
|
|
|
base::Semaphore sema_gc_finished(0);
|
|
|
|
|
|
|
|
// pass persistent handles to background thread
|
|
|
|
std::unique_ptr<PersistentHandlesThread> thread(new PersistentHandlesThread(
|
2020-07-10 08:58:37 +00:00
|
|
|
isolate->heap(), std::move(handles), std::move(ph), *number,
|
|
|
|
&sema_started, &sema_gc_finished));
|
2020-04-06 06:23:44 +00:00
|
|
|
CHECK(thread->Start());
|
|
|
|
|
|
|
|
sema_started.Wait();
|
|
|
|
|
|
|
|
CcTest::CollectAllGarbage();
|
|
|
|
sema_gc_finished.Signal();
|
|
|
|
|
|
|
|
thread->Join();
|
|
|
|
|
|
|
|
// get persistent handles back to main thread
|
2020-10-09 14:10:42 +00:00
|
|
|
ph = thread->DetachPersistentHandles();
|
2020-07-10 08:58:37 +00:00
|
|
|
ph->NewHandle(number);
|
2020-04-06 06:23:44 +00:00
|
|
|
}
|
|
|
|
|
2020-07-08 12:29:28 +00:00
|
|
|
TEST(DereferencePersistentHandle) {
|
2020-09-24 10:51:32 +00:00
|
|
|
heap::EnsureFlagLocalHeapsEnabled();
|
2020-07-08 12:29:28 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
|
|
|
std::unique_ptr<PersistentHandles> phs = isolate->NewPersistentHandles();
|
|
|
|
Handle<HeapNumber> ph;
|
|
|
|
{
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<HeapNumber> number = isolate->factory()->NewHeapNumber(42.0);
|
2020-07-10 08:58:37 +00:00
|
|
|
ph = phs->NewHandle(number);
|
2020-07-08 12:29:28 +00:00
|
|
|
}
|
|
|
|
{
|
2020-10-14 11:47:05 +00:00
|
|
|
LocalHeap local_heap(isolate->heap(), ThreadKind::kMain, std::move(phs));
|
2020-10-15 10:57:22 +00:00
|
|
|
UnparkedScope scope(&local_heap);
|
2020-07-08 12:29:28 +00:00
|
|
|
CHECK_EQ(42, ph->value());
|
|
|
|
DisallowHandleDereference disallow_scope;
|
|
|
|
CHECK_EQ(42, ph->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 21:00:28 +00:00
|
|
|
TEST(NewPersistentHandleFailsWhenParked) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
2020-10-14 11:47:05 +00:00
|
|
|
LocalHeap local_heap(isolate->heap(), ThreadKind::kMain);
|
2020-07-29 21:00:28 +00:00
|
|
|
// Fail here in debug mode: Persistent handles can't be created if local heap
|
|
|
|
// is parked
|
|
|
|
local_heap.NewPersistentHandle(Smi::FromInt(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(NewPersistentHandleFailsWhenParkedExplicit) {
|
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
2020-10-14 11:47:05 +00:00
|
|
|
LocalHeap local_heap(isolate->heap(), ThreadKind::kMain,
|
|
|
|
isolate->NewPersistentHandles());
|
2020-07-29 21:00:28 +00:00
|
|
|
// Fail here in debug mode: Persistent handles can't be created if local heap
|
|
|
|
// is parked
|
|
|
|
local_heap.NewPersistentHandle(Smi::FromInt(1));
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:45:24 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2020-04-06 06:23:44 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|