Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
dart_vm_lifecycle.h
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_
6#define FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_
7
8#include <memory>
9
10#include "flutter/fml/macros.h"
11#include "flutter/lib/ui/isolate_name_server/isolate_name_server.h"
12#include "flutter/runtime/dart_vm.h"
13#include "flutter/runtime/service_protocol.h"
14#include "third_party/dart/runtime/include/dart_tools_api.h"
15
16namespace flutter {
17
18// A strong reference to the Dart VM. There can only be one VM running in the
19// process at any given time. A reference to the VM may only be obtained via the
20// |Create| method. In case there is already a running instance of the VM in the
21// process, a strong reference to that VM is obtained and the arguments to the
22// |Create| call ignored. If there is no VM already running in the process, a VM
23// is initialized in a thread safe manner and returned to the caller. The VM
24// will shutdown only when all callers relinquish their references (by
25// collecting their instances of this class).
26//
27// DartVMRef instances may be created on any thread.
28class DartVMRef {
29 public:
30 [[nodiscard]] static DartVMRef Create(
31 const Settings& settings,
32 fml::RefPtr<const DartSnapshot> vm_snapshot = nullptr,
33 fml::RefPtr<const DartSnapshot> isolate_snapshot = nullptr);
34
35 DartVMRef(const DartVMRef&) = default;
36
38
39 ~DartVMRef();
40
41 // This is an inherently racy way to check if a VM instance is running and
42 // should not be used outside of unit-tests where there is a known threading
43 // model.
44 static bool IsInstanceRunning();
45
46 static std::shared_ptr<const DartVMData> GetVMData();
47
48 static std::shared_ptr<ServiceProtocol> GetServiceProtocol();
49
50 static std::shared_ptr<IsolateNameServer> GetIsolateNameServer();
51
52 explicit operator bool() const { return static_cast<bool>(vm_); }
53
55 FML_DCHECK(vm_);
56 return vm_.get();
57 }
58
59 const DartVM* get() const {
60 FML_DCHECK(vm_);
61 return vm_.get();
62 }
63
65 FML_DCHECK(vm_);
66 return vm_.get();
67 }
68
69 const DartVM* operator->() const {
70 FML_DCHECK(vm_);
71 return vm_.get();
72 }
73
74 // NOLINTNEXTLINE(google-runtime-operator)
76 FML_DCHECK(vm_);
77 return vm_.get();
78 }
79
80 private:
81 friend class DartIsolate;
82
83 std::shared_ptr<DartVM> vm_;
84
85 explicit DartVMRef(std::shared_ptr<DartVM> vm);
86
87 // Only used by Dart Isolate to register itself with the VM.
88 static DartVM* GetRunningVM();
89};
90
91} // namespace flutter
92
93#endif // FLUTTER_RUNTIME_DART_VM_LIFECYCLE_H_
static sk_sp< Effect > Create()
Represents an instance of a live isolate. An isolate is a separate Dart execution context....
const DartVM * get() const
const DartVM * operator->() const
DartVMRef(const DartVMRef &)=default
static std::shared_ptr< IsolateNameServer > GetIsolateNameServer()
static bool IsInstanceRunning()
static std::shared_ptr< ServiceProtocol > GetServiceProtocol()
static std::shared_ptr< const DartVMData > GetVMData()
DartVMRef(DartVMRef &&)
Describes a running instance of the Dart VM. There may only be one running instance of the Dart VM in...
Definition dart_vm.h:61
#define FML_DCHECK(condition)
Definition logging.h:103