Flutter Engine
flutter::DartVMRef Class Reference

#include <dart_vm_lifecycle.h>

Public Member Functions

 DartVMRef (DartVMRef &&)
 
 ~DartVMRef ()
 
 operator bool () const
 
DartVMget ()
 
DartVMoperator-> ()
 
DartVMoperator & ()
 

Static Public Member Functions

static DartVMRef Create (Settings settings, fml::RefPtr< DartSnapshot > vm_snapshot=nullptr, fml::RefPtr< DartSnapshot > isolate_snapshot=nullptr)
 
static bool IsInstanceRunning ()
 
static std::shared_ptr< const DartVMDataGetVMData ()
 
static std::shared_ptr< ServiceProtocolGetServiceProtocol ()
 
static std::shared_ptr< IsolateNameServerGetIsolateNameServer ()
 

Friends

class DartIsolate
 

Detailed Description

Definition at line 27 of file dart_vm_lifecycle.h.

Constructor & Destructor Documentation

◆ DartVMRef()

flutter::DartVMRef::DartVMRef ( DartVMRef &&  )
default

◆ ~DartVMRef()

flutter::DartVMRef::~DartVMRef ( )

Definition at line 32 of file dart_vm_lifecycle.cc.

32  {
33  if (!vm_) {
34  // If there is no valid VM (possible via a move), there is no way that the
35  // decrement on the shared pointer can cause a collection. Avoid acquiring
36  // the lifecycle lock in this case. This is just working around a
37  // pessimization and not required for correctness.
38  return;
39  }
40  std::scoped_lock lifecycle_lock(gVMMutex);
41  vm_.reset();
42 }
static std::mutex gVMMutex

Member Function Documentation

◆ Create()

DartVMRef flutter::DartVMRef::Create ( Settings  settings,
fml::RefPtr< DartSnapshot vm_snapshot = nullptr,
fml::RefPtr< DartSnapshot isolate_snapshot = nullptr 
)
static

Definition at line 44 of file dart_vm_lifecycle.cc.

References FML_CHECK, FML_DLOG, FML_LOG, and flutter::Settings::leak_vm.

Referenced by flutter::BM_PlatformMessageResponseDartComplete(), flutter::Shell::Create(), flutter_runner::CreateWithContentsOfFile(), flutter_runner::Engine::Engine(), and flutter::testing::TEST_F().

46  {
47  std::scoped_lock lifecycle_lock(gVMMutex);
48 
49  if (!settings.leak_vm) {
51  << "Launch settings indicated that the VM should shut down in the "
52  "process when done but a previous launch asked the VM to leak in "
53  "the same process. For proper VM shutdown, all VM launches must "
54  "indicate that they should shut down when done.";
55  }
56 
57  // If there is already a running VM in the process, grab a strong reference to
58  // it.
59  if (auto vm = gVM.lock()) {
60  FML_DLOG(WARNING) << "Attempted to create a VM in a process where one was "
61  "already running. Ignoring arguments for current VM "
62  "create call and reusing the old VM.";
63  // There was already a running VM in the process,
64  return DartVMRef{std::move(vm)};
65  }
66 
67  std::scoped_lock dependents_lock(gVMDependentsMutex);
68 
69  gVMData.reset();
70  gVMServiceProtocol.reset();
71  gVMIsolateNameServer.reset();
72  gVM.reset();
73 
74  // If there is no VM in the process. Initialize one, hold the weak reference
75  // and pass a strong reference to the caller.
76  auto isolate_name_server = std::make_shared<IsolateNameServer>();
77  auto vm = DartVM::Create(std::move(settings), //
78  std::move(vm_snapshot), //
79  std::move(isolate_snapshot), //
80  isolate_name_server //
81  );
82 
83  if (!vm) {
84  FML_LOG(ERROR) << "Could not create Dart VM instance.";
85  return {nullptr};
86  }
87 
88  gVMData = vm->GetVMData();
89  gVMServiceProtocol = vm->GetServiceProtocol();
90  gVMIsolateNameServer = isolate_name_server;
91  gVM = vm;
92 
93  if (settings.leak_vm) {
94  gVMLeak = new std::shared_ptr<DartVM>(vm);
95  }
96 
97  return DartVMRef{std::move(vm)};
98 }
static std::mutex gVMMutex
static std::weak_ptr< ServiceProtocol > gVMServiceProtocol
static std::weak_ptr< IsolateNameServer > gVMIsolateNameServer
#define FML_LOG(severity)
Definition: logging.h:65
static std::weak_ptr< DartVM > gVM
static std::weak_ptr< const DartVMData > gVMData
DartVMRef(DartVMRef &&)
#define FML_CHECK(condition)
Definition: logging.h:68
#define FML_DLOG(severity)
Definition: logging.h:85
static std::mutex gVMDependentsMutex
static std::shared_ptr< DartVM > * gVMLeak

◆ get()

DartVM* flutter::DartVMRef::get ( )
inline

Definition at line 51 of file dart_vm_lifecycle.h.

References FML_DCHECK.

51  {
52  FML_DCHECK(vm_);
53  return vm_.get();
54  }
#define FML_DCHECK(condition)
Definition: logging.h:86

◆ GetIsolateNameServer()

std::shared_ptr< IsolateNameServer > flutter::DartVMRef::GetIsolateNameServer ( )
static

Definition at line 115 of file dart_vm_lifecycle.cc.

References FML_CHECK.

Referenced by flutter::DartIsolate::CreateRunningRootIsolate().

115  {
116  std::scoped_lock lock(gVMDependentsMutex);
117  return gVMIsolateNameServer.lock();
118 }
static std::weak_ptr< IsolateNameServer > gVMIsolateNameServer
static std::mutex gVMDependentsMutex

◆ GetServiceProtocol()

std::shared_ptr< ServiceProtocol > flutter::DartVMRef::GetServiceProtocol ( )
static

Definition at line 110 of file dart_vm_lifecycle.cc.

Referenced by flutter::DartIsolate::Shutdown().

110  {
111  std::scoped_lock lock(gVMDependentsMutex);
112  return gVMServiceProtocol.lock();
113 }
static std::weak_ptr< ServiceProtocol > gVMServiceProtocol
static std::mutex gVMDependentsMutex

◆ GetVMData()

std::shared_ptr< const DartVMData > flutter::DartVMRef::GetVMData ( )
static

Definition at line 105 of file dart_vm_lifecycle.cc.

Referenced by flutter::testing::RunDartCodeInIsolateOnUITaskRunner(), and flutter::DartIsolate::Shutdown().

105  {
106  std::scoped_lock lock(gVMDependentsMutex);
107  return gVMData.lock();
108 }
static std::weak_ptr< const DartVMData > gVMData
static std::mutex gVMDependentsMutex

◆ IsInstanceRunning()

bool flutter::DartVMRef::IsInstanceRunning ( )
static

Definition at line 100 of file dart_vm_lifecycle.cc.

Referenced by flutter::testing::TEST_F().

100  {
101  std::scoped_lock lock(gVMMutex);
102  return !gVM.expired();
103 }
static std::mutex gVMMutex
static std::weak_ptr< DartVM > gVM

◆ operator &()

DartVM* flutter::DartVMRef::operator& ( )
inline

Definition at line 61 of file dart_vm_lifecycle.h.

References FML_DCHECK.

61  {
62  FML_DCHECK(vm_);
63  return vm_.get();
64  }
#define FML_DCHECK(condition)
Definition: logging.h:86

◆ operator bool()

flutter::DartVMRef::operator bool ( ) const
inline

Definition at line 49 of file dart_vm_lifecycle.h.

49 { return static_cast<bool>(vm_); }

◆ operator->()

DartVM* flutter::DartVMRef::operator-> ( )
inline

Definition at line 56 of file dart_vm_lifecycle.h.

References FML_DCHECK.

56  {
57  FML_DCHECK(vm_);
58  return vm_.get();
59  }
#define FML_DCHECK(condition)
Definition: logging.h:86

Friends And Related Function Documentation

◆ DartIsolate

friend class DartIsolate
friend

Definition at line 67 of file dart_vm_lifecycle.h.


The documentation for this class was generated from the following files: