forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WeakPersistentHandle migration (flutter#19843)
and roll Dart to 5278383.
- Loading branch information
Showing
22 changed files
with
488 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Signature: c55e82fda8f939e4cdbeb9c2c004e7d9 | ||
Signature: 51a79de001f82d05f084b671f6216a31 | ||
|
||
UNUSED LICENSES: | ||
|
||
|
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,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()) { | ||
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 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 { | ||
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 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.