6e33c95463
Adds shared libraries for embedding Dart VM and new API (runtime/engine/include/dart_engine.h). TEST=tests/standalone/embedder_samples_test.dart Cq-Include-Trybots: luci.dart.try:vm-aot-android-release-arm64c-try,vm-aot-android-release-arm_x64-try,vm-aot-asan-linux-release-x64-try,vm-aot-dwarf-linux-product-x64-try,vm-aot-dyn-linux-debug-x64-try,vm-aot-linux-debug-simarm_x64-try,vm-aot-linux-debug-simriscv32-try,vm-aot-linux-debug-simriscv64-try,vm-aot-linux-debug-x64-try,vm-aot-linux-debug-x64c-try,vm-aot-linux-product-x64-try,vm-aot-linux-release-arm64-try,vm-aot-linux-release-simarm_x64-try,vm-aot-linux-release-x64-try,vm-aot-mac-product-arm64-try,vm-aot-mac-release-arm64-try,vm-aot-mac-release-x64-try,vm-aot-msan-linux-release-x64-try,vm-aot-obfuscate-linux-release-x64-try,vm-aot-optimization-level-linux-release-x64-try,vm-aot-tsan-linux-release-x64-try,vm-aot-ubsan-linux-release-x64-try,vm-aot-win-debug-x64-try,vm-aot-win-debug-x64c-try,vm-aot-win-product-x64-try,vm-aot-win-release-x64-try,vm-appjit-linux-debug-x64-try,vm-appjit-linux-product-x64-try,vm-appjit-linux-release-x64-try,vm-asan-linux-release-arm64-try,vm-asan-linux-release-x64-try,vm-checked-mac-release-arm64-try,vm-eager-optimization-linux-release-ia32-try,vm-eager-optimization-linux-release-x64-try,vm-ffi-android-debug-arm-try,vm-ffi-android-debug-arm64c-try,vm-ffi-android-product-arm-try,vm-ffi-android-product-arm64c-try,vm-ffi-android-release-arm-try,vm-ffi-android-release-arm64c-try,vm-ffi-qemu-linux-release-arm-try,vm-ffi-qemu-linux-release-riscv64-try,vm-fuchsia-release-arm64-try,vm-fuchsia-release-x64-try,vm-gcc-linux-try,vm-linux-debug-ia32-try,vm-linux-debug-simriscv32-try,vm-linux-debug-simriscv64-try,vm-linux-debug-x64-try,vm-linux-debug-x64c-try,vm-linux-release-arm64-try,vm-linux-release-ia32-try,vm-linux-release-simarm-try,vm-linux-release-x64-try,vm-mac-debug-arm64-try,vm-mac-debug-x64-try,vm-mac-release-arm64-try,vm-mac-release-x64-try,vm-msan-linux-release-arm64-try,vm-msan-linux-release-x64-try,vm-msvc-windows-try,vm-reload-linux-debug-x64-try,vm-reload-linux-release-x64-try,vm-reload-rollback-linux-debug-x64-try,vm-reload-rollback-linux-release-x64-try,vm-tsan-linux-release-arm64-try,vm-tsan-linux-release-x64-try,vm-ubsan-linux-release-arm64-try,vm-ubsan-linux-release-x64-try,vm-win-debug-x64-try,vm-win-debug-x64c-try,vm-win-release-ia32-try,vm-win-release-x64-try Change-Id: Ia4e4d1b871ddef515cfb2f4639bdaa9fe3676936 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/402860 Reviewed-by: Slava Egorov <vegorov@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com> Commit-Queue: Martin Kustermann <kustermann@google.com>
135 lines
4.6 KiB
C++
135 lines
4.6 KiB
C++
// Copyright (c) 2025, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#ifndef RUNTIME_ENGINE_ENGINE_H_
|
|
#define RUNTIME_ENGINE_ENGINE_H_
|
|
|
|
#include <memory>
|
|
#include <queue>
|
|
#include <unordered_map>
|
|
#include <vector>
|
|
#include "include/dart_engine.h"
|
|
#include "platform/synchronization.h"
|
|
|
|
namespace dart {
|
|
namespace engine {
|
|
|
|
// Manages Dart VM initialization/shutdown, isolates, and all state that needs
|
|
// to be disposed (e.g. buffers of loaded snapshots).
|
|
//
|
|
// Since Dart VM has a global state, there should be only one instance of this
|
|
// class (accessible through Engine::instance()).
|
|
class Engine {
|
|
public:
|
|
// Loads Kernel snapshot from given file path.
|
|
//
|
|
// Just reads all bytes into a single buffer, owned by this class.
|
|
DartEngine_SnapshotData KernelFromFile(const char* path, char** error);
|
|
|
|
// Loads AOT snapshot from given file path.
|
|
//
|
|
// Opens given AOT snapshot as a shared library and loads buffer symbols from
|
|
// it.
|
|
DartEngine_SnapshotData AotFromFile(const char* path, char** error);
|
|
|
|
// Starts an isolate from a snapshot.
|
|
//
|
|
// Starting an isolate also routes message notify callback to NotifyMessage.
|
|
Dart_Isolate StartIsolate(const DartEngine_SnapshotData snapshot,
|
|
char** error);
|
|
|
|
// Acquires a lock for an isolate, so that the thread can enter it.
|
|
void LockIsolate(Dart_Isolate isolate);
|
|
|
|
// Releases the lock obtained by LockIsolate.
|
|
void UnlockIsolate(Dart_Isolate isolate);
|
|
|
|
// Called by Dart VM via Engine::MessageNotifyCallback.
|
|
//
|
|
// Calls isolate-specific message scheduler (if any), default message
|
|
// scheduler, or ignores the message.
|
|
void NotifyMessage(Dart_Isolate isolate);
|
|
|
|
// Calls Dart_HandleMessage, managing an isolate lock and Dart scope.
|
|
void HandleMessage(Dart_Isolate isolate);
|
|
|
|
// Sets a callback to be called when Dart_HandleMessage returns an error.
|
|
void SetHandleMessageErrorCallback(
|
|
DartEngine_HandleMessageErrorCallback callback);
|
|
|
|
// Sets a message scheduler for a given isolate.
|
|
void SetDefaultMessageScheduler(DartEngine_MessageScheduler scheduler);
|
|
|
|
// Sets default message scheduler for isolates without dedicated
|
|
// message scheduler.
|
|
void SetMessageScheduler(DartEngine_MessageScheduler scheduler,
|
|
Dart_Isolate isolate);
|
|
|
|
// Initializes embedder and partially initializes Dart VM.
|
|
//
|
|
// Full initialization happens only when the user starts the first isolate, as
|
|
// in case of AOT snapshots we don't have vm snapshot data and vm snapshot
|
|
// instuructions before the user makes the first call to StartIsolate.
|
|
bool Initialize(char** error);
|
|
|
|
// Shuts down the engine.
|
|
//
|
|
// Once shut down, it is not supposed to be re-initialized.
|
|
void Shutdown();
|
|
|
|
static Engine* instance();
|
|
// Passed to Dart_SetMessageNotifyCallback.
|
|
static void MessageNotifyCallback(Dart_Isolate isolate);
|
|
// Passed to user-previded message scheduler callback.
|
|
static void HandleMessageCallback(Dart_Isolate isolate);
|
|
|
|
private:
|
|
// Set to false once shutdown starts.
|
|
bool is_running_ = false;
|
|
|
|
// Controls initalization and shutdown, and prevents deadlocks when delivering
|
|
// messages during shutdown.
|
|
Mutex engine_lifecycle_;
|
|
|
|
// Guards access to engine state.
|
|
Mutex engine_state_;
|
|
|
|
// Whether Engine::Initialize is called.
|
|
bool initialized_ = false;
|
|
|
|
// Added because of AOT mode, because in AOT mode Dart VM cannot be fully
|
|
// initialized until we get vm snapshot data, which we typically get when the
|
|
// user starts the first isolate.
|
|
bool first_isolate_started_ = false;
|
|
|
|
// Snapshots, whose buffers are owned by the engine.
|
|
// and are freed on Shutdown.
|
|
std::vector<DartEngine_SnapshotData> owned_snapshots_;
|
|
|
|
// Loaded dynamic libraries. Dynamic libraries are loaded
|
|
// when reading AOT snapshots.
|
|
std::vector<void*> loaded_libraries_;
|
|
|
|
// All isolates, started via Engine::StartIsolate.
|
|
std::vector<Dart_Isolate> isolates_;
|
|
|
|
// Stores per-isolate mutexes, used by Engine::LockIsolate/UnlockIsolate.
|
|
std::unordered_map<Dart_Isolate, Mutex> mutexes_;
|
|
|
|
// Default scheduler.
|
|
DartEngine_MessageScheduler default_scheduler_;
|
|
// Per-isolate message schedulers.
|
|
std::unordered_map<Dart_Isolate, DartEngine_MessageScheduler> schedulers_;
|
|
|
|
// Callback to notify about Dart_HandleMessage errors.
|
|
DartEngine_HandleMessageErrorCallback handle_message_error_callback_;
|
|
|
|
// Helper function to get an element from mutexes_.
|
|
Mutex& MutexForIsolate(Dart_Isolate isolate);
|
|
};
|
|
|
|
} // namespace engine
|
|
} // namespace dart
|
|
#endif // RUNTIME_ENGINE_ENGINE_H_
|