forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dart_weak_persistent_value.cc
70 lines (58 loc) · 2.08 KB
/
dart_weak_persistent_value.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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