forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dart_vm_lifecycle.h
93 lines (69 loc) · 2.62 KB
/
dart_vm_lifecycle.h
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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 FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_
#define FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_
#include <memory>
#include "flutter/fml/macros.h"
#include "flutter/lib/ui/isolate_name_server/isolate_name_server.h"
#include "flutter/runtime/dart_vm.h"
#include "flutter/runtime/service_protocol.h"
#include "third_party/dart/runtime/include/dart_tools_api.h"
namespace flutter {
// A strong reference to the Dart VM. There can only be one VM running in the
// process at any given time. A reference to the VM may only be obtained via the
// |Create| method. In case there is already a running instance of the VM in the
// process, a strong reference to that VM is obtained and the arguments to the
// |Create| call ignored. If there is no VM already running in the process, a VM
// is initialized in a thread safe manner and returned to the caller. The VM
// will shutdown only when all callers relinquish their references (by
// collecting their instances of this class).
//
// DartVMRef instances may be created on any thread.
class DartVMRef {
public:
[[nodiscard]] static DartVMRef Create(
const Settings& settings,
fml::RefPtr<const DartSnapshot> vm_snapshot = nullptr,
fml::RefPtr<const DartSnapshot> isolate_snapshot = nullptr);
DartVMRef(const DartVMRef&) = default;
DartVMRef(DartVMRef&&);
~DartVMRef();
// This is an inherently racy way to check if a VM instance is running and
// should not be used outside of unit-tests where there is a known threading
// model.
static bool IsInstanceRunning();
static std::shared_ptr<const DartVMData> GetVMData();
static std::shared_ptr<ServiceProtocol> GetServiceProtocol();
static std::shared_ptr<IsolateNameServer> GetIsolateNameServer();
explicit operator bool() const { return static_cast<bool>(vm_); }
DartVM* get() {
FML_DCHECK(vm_);
return vm_.get();
}
const DartVM* get() const {
FML_DCHECK(vm_);
return vm_.get();
}
DartVM* operator->() {
FML_DCHECK(vm_);
return vm_.get();
}
const DartVM* operator->() const {
FML_DCHECK(vm_);
return vm_.get();
}
// NOLINTNEXTLINE(google-runtime-operator)
DartVM* operator&() {
FML_DCHECK(vm_);
return vm_.get();
}
private:
friend class DartIsolate;
std::shared_ptr<DartVM> vm_;
explicit DartVMRef(std::shared_ptr<DartVM> vm);
// Only used by Dart Isolate to register itself with the VM.
static DartVM* GetRunningVM();
};
} // namespace flutter
#endif // FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_