This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Migrate Dart_WeakPersistentHandle uses and roll Dart #19843
Merged
dcharkes
merged 1 commit into
flutter:master
from
dcharkes:weak-persistent-handle-migration
Nov 3, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| Signature: c55e82fda8f939e4cdbeb9c2c004e7d9 | ||
| Signature: 51a79de001f82d05f084b671f6216a31 | ||
|
|
||
| UNUSED LICENSES: | ||
|
|
||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,70 @@ | ||
| // Copyright 2013 The Flutter 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 "tonic/dart_weak_persistent_value.h" | ||
|
|
||
| #include "tonic/dart_state.h" | ||
| #include "tonic/scopes/dart_isolate_scope.h" | ||
|
|
||
| namespace tonic { | ||
|
|
||
| DartWeakPersistentValue::DartWeakPersistentValue() : handle_(nullptr) {} | ||
|
|
||
| DartWeakPersistentValue::~DartWeakPersistentValue() { | ||
| Clear(); | ||
| } | ||
|
|
||
| void DartWeakPersistentValue::Set(DartState* dart_state, | ||
| Dart_Handle object, | ||
| void* peer, | ||
| intptr_t external_allocation_size, | ||
| Dart_HandleFinalizer callback) { | ||
| TONIC_DCHECK(is_empty()); | ||
| dart_state_ = dart_state->GetWeakPtr(); | ||
| handle_ = Dart_NewWeakPersistentHandle(object, peer, external_allocation_size, | ||
| callback); | ||
| } | ||
|
|
||
| void DartWeakPersistentValue::Clear() { | ||
| if (!handle_) { | ||
| return; | ||
| } | ||
|
|
||
| auto dart_state = dart_state_.lock(); | ||
| if (!dart_state) { | ||
| // The DartVM that the handle used to belong to has been shut down and that | ||
| // handle has already been deleted. | ||
| handle_ = nullptr; | ||
| return; | ||
| } | ||
|
|
||
| // The DartVM frees the handles during shutdown and calls the finalizers. | ||
| // Freeing handles during shutdown would fail. | ||
| if (!dart_state->IsShuttingDown()) { | ||
dcharkes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (Dart_CurrentIsolateGroup()) { | ||
| Dart_DeleteWeakPersistentHandle(handle_); | ||
| } else { | ||
| // If we are not on the mutator thread, this will fail. The caller must | ||
| // ensure to be on the mutator thread. | ||
| DartIsolateScope scope(dart_state->isolate()); | ||
| Dart_DeleteWeakPersistentHandle(handle_); | ||
| } | ||
| } | ||
| // If it's shutting down, the handle will be deleted already. | ||
|
|
||
| dart_state_.reset(); | ||
| handle_ = nullptr; | ||
| } | ||
|
|
||
| Dart_Handle DartWeakPersistentValue::Get() { | ||
| auto dart_state = dart_state_.lock(); | ||
| TONIC_DCHECK(dart_state); | ||
| TONIC_DCHECK(!dart_state->IsShuttingDown()); | ||
| if (!handle_) { | ||
| return nullptr; | ||
| } | ||
| return Dart_HandleFromWeakPersistent(handle_); | ||
| } | ||
|
|
||
| } // namespace tonic | ||
This file contains hidden or 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,48 @@ | ||
| // Copyright 2013 The Flutter 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 LIB_TONIC_DART_WEAK_PERSISTENT_VALUE_H_ | ||
| #define LIB_TONIC_DART_WEAK_PERSISTENT_VALUE_H_ | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "third_party/dart/runtime/include/dart_api.h" | ||
| #include "tonic/common/macros.h" | ||
|
|
||
| namespace tonic { | ||
| class DartState; | ||
|
|
||
| // DartWeakPersistentValue is a bookkeeping class to help pair calls to | ||
| // Dart_NewWeakPersistentHandle with Dart_DeleteWeakPersistentHandle even in | ||
| // the case if IsolateGroup shutdown. Consider using this class instead of | ||
| // holding a Dart_PersistentHandle directly so that you don't leak the | ||
| // Dart_WeakPersistentHandle. | ||
| class DartWeakPersistentValue { | ||
dcharkes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public: | ||
| DartWeakPersistentValue(); | ||
| ~DartWeakPersistentValue(); | ||
|
|
||
| Dart_WeakPersistentHandle value() const { return handle_; } | ||
| bool is_empty() const { return handle_ == nullptr; } | ||
|
|
||
| void Set(DartState* dart_state, | ||
| Dart_Handle object, | ||
| void* peer, | ||
| intptr_t external_allocation_size, | ||
| Dart_HandleFinalizer callback); | ||
| void Clear(); | ||
| Dart_Handle Get(); | ||
|
|
||
| const std::weak_ptr<DartState>& dart_state() const { return dart_state_; } | ||
|
|
||
| private: | ||
| std::weak_ptr<DartState> dart_state_; | ||
| Dart_WeakPersistentHandle handle_; | ||
|
|
||
| TONIC_DISALLOW_COPY_AND_ASSIGN(DartWeakPersistentValue); | ||
| }; | ||
|
|
||
| } // namespace tonic | ||
|
|
||
| #endif // LIB_TONIC_DART_WEAK_PERSISTENT_VALUE_H_ | ||
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.