2020-03-25 10:09:07 +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"
|
2020-07-28 19:02:51 +00:00
|
|
|
#include "src/common/globals.h"
|
2020-03-25 10:09:07 +00:00
|
|
|
#include "src/handles/handles-inl.h"
|
|
|
|
#include "src/handles/local-handles-inl.h"
|
2020-07-28 19:02:51 +00:00
|
|
|
#include "src/handles/local-handles.h"
|
2020-03-25 10:09:07 +00:00
|
|
|
#include "src/heap/heap.h"
|
|
|
|
#include "src/heap/local-heap.h"
|
2020-11-17 10:16:09 +00:00
|
|
|
#include "src/heap/parked-scope.h"
|
2020-03-25 10:09:07 +00:00
|
|
|
#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 {
|
|
|
|
|
2020-06-10 13:45:24 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-03-25 10:09:07 +00:00
|
|
|
class LocalHandlesThread final : public v8::base::Thread {
|
|
|
|
public:
|
|
|
|
LocalHandlesThread(Heap* heap, Address object, base::Semaphore* sema_started,
|
|
|
|
base::Semaphore* sema_gc_finished)
|
|
|
|
: v8::base::Thread(base::Thread::Options("ThreadWithLocalHeap")),
|
|
|
|
heap_(heap),
|
|
|
|
object_(object),
|
|
|
|
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);
|
2020-10-15 10:57:22 +00:00
|
|
|
UnparkedScope unparked_scope(&local_heap);
|
2020-03-25 10:09:07 +00:00
|
|
|
LocalHandleScope scope(&local_heap);
|
|
|
|
|
|
|
|
static constexpr int kNumHandles =
|
|
|
|
kHandleBlockSize * 2 + kHandleBlockSize / 2;
|
|
|
|
|
|
|
|
std::vector<Handle<HeapNumber>> handles;
|
|
|
|
handles.reserve(kNumHandles);
|
|
|
|
|
|
|
|
for (int i = 0; i < kNumHandles; i++) {
|
|
|
|
Handle<HeapNumber> number = handle(
|
|
|
|
HeapNumber::cast(HeapObject::FromAddress(object_)), &local_heap);
|
|
|
|
handles.push_back(number);
|
|
|
|
}
|
|
|
|
|
|
|
|
sema_started_->Signal();
|
|
|
|
|
|
|
|
{
|
|
|
|
ParkedScope scope(&local_heap);
|
|
|
|
sema_gc_finished_->Wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Handle<HeapNumber> handle : handles) {
|
|
|
|
CHECK_EQ(42.0, handle->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Heap* heap_;
|
|
|
|
Address object_;
|
|
|
|
base::Semaphore* sema_started_;
|
|
|
|
base::Semaphore* sema_gc_finished_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(CreateLocalHandles) {
|
2020-09-24 10:51:32 +00:00
|
|
|
heap::EnsureFlagLocalHeapsEnabled();
|
2020-03-25 10:09:07 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
|
|
|
Address object = kNullAddress;
|
|
|
|
|
|
|
|
{
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<HeapNumber> number = isolate->factory()->NewHeapNumber(42.0);
|
|
|
|
object = (*number).address();
|
|
|
|
}
|
|
|
|
|
|
|
|
base::Semaphore sema_started(0);
|
|
|
|
base::Semaphore sema_gc_finished(0);
|
|
|
|
|
|
|
|
std::unique_ptr<LocalHandlesThread> thread(new LocalHandlesThread(
|
|
|
|
isolate->heap(), object, &sema_started, &sema_gc_finished));
|
|
|
|
CHECK(thread->Start());
|
|
|
|
|
|
|
|
sema_started.Wait();
|
|
|
|
|
|
|
|
CcTest::CollectAllGarbage();
|
|
|
|
sema_gc_finished.Signal();
|
|
|
|
|
|
|
|
thread->Join();
|
|
|
|
}
|
|
|
|
|
2020-07-28 19:02:51 +00:00
|
|
|
TEST(CreateLocalHandlesWithoutLocalHandleScope) {
|
2020-09-24 10:51:32 +00:00
|
|
|
heap::EnsureFlagLocalHeapsEnabled();
|
2020-07-28 19:02:51 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
2020-11-17 10:16:09 +00:00
|
|
|
handle(Smi::FromInt(17), isolate->main_thread_local_heap());
|
2020-07-28 19:02:51 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 09:01:57 +00:00
|
|
|
TEST(DereferenceLocalHandle) {
|
2020-09-24 10:51:32 +00:00
|
|
|
heap::EnsureFlagLocalHeapsEnabled();
|
2020-07-27 09:01:57 +00:00
|
|
|
CcTest::InitializeVM();
|
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
|
|
|
|
|
|
|
// Create a PersistentHandle to create the LocalHandle, and thus not have a
|
|
|
|
// HandleScope present to override the LocalHandleScope.
|
|
|
|
std::unique_ptr<PersistentHandles> phs = isolate->NewPersistentHandles();
|
|
|
|
Handle<HeapNumber> ph;
|
|
|
|
{
|
|
|
|
HandleScope handle_scope(isolate);
|
|
|
|
Handle<HeapNumber> number = isolate->factory()->NewHeapNumber(42.0);
|
|
|
|
ph = phs->NewHandle(number);
|
|
|
|
}
|
|
|
|
{
|
2020-11-17 10:16:09 +00:00
|
|
|
LocalHeap local_heap(isolate->heap(), ThreadKind::kBackground,
|
|
|
|
std::move(phs));
|
2020-10-15 10:57:22 +00:00
|
|
|
UnparkedScope unparked_scope(&local_heap);
|
2020-07-27 09:01:57 +00:00
|
|
|
LocalHandleScope scope(&local_heap);
|
|
|
|
Handle<HeapNumber> local_number = handle(*ph, &local_heap);
|
|
|
|
CHECK_EQ(42, local_number->value());
|
|
|
|
DisallowHandleDereference disallow_scope;
|
|
|
|
CHECK_EQ(42, local_number->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-10 13:45:24 +00:00
|
|
|
} // anonymous namespace
|
|
|
|
|
2020-03-25 10:09:07 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|