-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Original commit message: Reland "[weakrefs] Schedule FinalizationGroup cleanup tasks from within V8" Deprecate the following explicit FinalizationGroup APIs in favor of automatic handling of FinalizationGroup cleanup callbacks: - v8::Isolate::SetHostCleanupFinalizationGroupCallback - v8::FinaliationGroup::Cleanup If no HostCleanupFinalizationGroupCallback is set, then FinalizationGroup cleanup callbacks are automatically scheduled by V8 itself as non-nestable foreground tasks. When a Context being disposed, all FinalizationGroups that are associated with it are removed from the dirty list, cancelling scheduled cleanup. This is a reland of 31d8ff7ac5f4a91099f2f06f01e43e9e7aa79bc4 Bug: v8:8179, v8:10190 Change-Id: I704ecf48aeebac1dc2c05ea1c052f6a2560ae332 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2045723 Commit-Queue: Shu-yu Guo <[email protected]> Reviewed-by: Ulan Degenbaev <[email protected]> Reviewed-by: Ross McIlroy <[email protected]> Cr-Commit-Position: refs/heads/master@{#66208} Refs: v8/v8@55a01ec PR-URL: #32885 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: Gerhard Stöbich <[email protected]> Reviewed-By: Beth Griggs <[email protected]>
- Loading branch information
1 parent
da728c4
commit 1f02617
Showing
21 changed files
with
328 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// 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/finalization-group-cleanup-task.h" | ||
|
||
#include "src/execution/frames.h" | ||
#include "src/execution/interrupts-scope.h" | ||
#include "src/execution/stack-guard.h" | ||
#include "src/execution/v8threads.h" | ||
#include "src/heap/heap-inl.h" | ||
#include "src/objects/js-weak-refs-inl.h" | ||
#include "src/tracing/trace-event.h" | ||
|
||
namespace v8 { | ||
namespace internal { | ||
|
||
FinalizationGroupCleanupTask::FinalizationGroupCleanupTask(Heap* heap) | ||
: heap_(heap) {} | ||
|
||
void FinalizationGroupCleanupTask::SlowAssertNoActiveJavaScript() { | ||
#ifdef ENABLE_SLOW_DCHECKS | ||
class NoActiveJavaScript : public ThreadVisitor { | ||
public: | ||
void VisitThread(Isolate* isolate, ThreadLocalTop* top) override { | ||
for (StackFrameIterator it(isolate, top); !it.done(); it.Advance()) { | ||
DCHECK(!it.frame()->is_java_script()); | ||
} | ||
} | ||
}; | ||
NoActiveJavaScript no_active_js_visitor; | ||
Isolate* isolate = heap_->isolate(); | ||
no_active_js_visitor.VisitThread(isolate, isolate->thread_local_top()); | ||
isolate->thread_manager()->IterateArchivedThreads(&no_active_js_visitor); | ||
#endif // ENABLE_SLOW_DCHECKS | ||
} | ||
|
||
void FinalizationGroupCleanupTask::Run() { | ||
Isolate* isolate = heap_->isolate(); | ||
DCHECK(!isolate->host_cleanup_finalization_group_callback()); | ||
SlowAssertNoActiveJavaScript(); | ||
|
||
TRACE_EVENT_CALL_STATS_SCOPED(isolate, "v8", | ||
"V8.FinalizationGroupCleanupTask"); | ||
|
||
HandleScope handle_scope(isolate); | ||
Handle<JSFinalizationGroup> finalization_group; | ||
// There could be no dirty FinalizationGroups. When a context is disposed by | ||
// the embedder, its FinalizationGroups are removed from the dirty list. | ||
if (!heap_->TakeOneDirtyJSFinalizationGroup().ToHandle(&finalization_group)) { | ||
return; | ||
} | ||
finalization_group->set_scheduled_for_cleanup(false); | ||
|
||
// Since FinalizationGroup cleanup callbacks are scheduled by V8, enter the | ||
// FinalizationGroup's context. | ||
Handle<Context> context(Context::cast(finalization_group->native_context()), | ||
isolate); | ||
Handle<Object> callback(finalization_group->cleanup(), isolate); | ||
v8::Context::Scope context_scope(v8::Utils::ToLocal(context)); | ||
v8::TryCatch catcher(reinterpret_cast<v8::Isolate*>(isolate)); | ||
catcher.SetVerbose(true); | ||
|
||
// Exceptions are reported via the message handler. This is ensured by the | ||
// verbose TryCatch. | ||
InvokeFinalizationGroupCleanupFromTask(context, finalization_group, callback); | ||
|
||
// Repost if there are remaining dirty FinalizationGroups. | ||
heap_->set_is_finalization_group_cleanup_task_posted(false); | ||
heap_->PostFinalizationGroupCleanupTaskIfNeeded(); | ||
} | ||
|
||
} // namespace internal | ||
} // namespace v8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// 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_FINALIZATION_GROUP_CLEANUP_TASK_H_ | ||
#define V8_HEAP_FINALIZATION_GROUP_CLEANUP_TASK_H_ | ||
|
||
#include "include/v8-platform.h" | ||
#include "src/objects/js-weak-refs.h" | ||
|
||
namespace v8 { | ||
namespace internal { | ||
|
||
// The GC schedules a cleanup task when the dirty FinalizationGroup list is | ||
// non-empty. The task processes a single FinalizationGroup and posts another | ||
// cleanup task if there are remaining dirty FinalizationGroups on the list. | ||
class FinalizationGroupCleanupTask : public Task { | ||
public: | ||
explicit FinalizationGroupCleanupTask(Heap* heap); | ||
~FinalizationGroupCleanupTask() override = default; | ||
|
||
void Run() override; | ||
|
||
private: | ||
FinalizationGroupCleanupTask(const FinalizationGroupCleanupTask&) = delete; | ||
void operator=(const FinalizationGroupCleanupTask&) = delete; | ||
|
||
void SlowAssertNoActiveJavaScript(); | ||
|
||
Heap* heap_; | ||
}; | ||
|
||
} // namespace internal | ||
} // namespace v8 | ||
|
||
#endif // V8_HEAP_FINALIZATION_GROUP_CLEANUP_TASK_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.