Files
sdk/runtime/engine
Ivan Inozemtsev 8773b6618f Reland "Update libcxx and libcxxabi"
This is a reland of commit 3491f72880

Original change's description:
> Update libcxx and libcxxabi
>
> Add libc, required by libcxx.
>
> Based on https://github.com/flutter/flutter/pull/165621.
>
> Change-Id: I5b929dc81b2572e3b919a8d20e031f98196375e6
> Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/416220
> Auto-Submit: Ivan Inozemtsev <iinozemtsev@google.com>
> Reviewed-by: Ryan Macnak <rmacnak@google.com>
> Commit-Queue: Ivan Inozemtsev <iinozemtsev@google.com>

Change-Id: I9392f4cdc2ccb985085768c90bd293d353bf4c95
Cq-Include-Trybots: luci.dart.try:vm-aot-tsan-linux-release-x64-try,vm-tsan-linux-release-arm64-try,vm-msan-linux-release-x64-try,vm-ubsan-linux-release-arm64-try,vm-asan-linux-release-x64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421300
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Ivan Inozemtsev <iinozemtsev@google.com>
2025-04-09 09:04:51 -07:00
..
2025-04-09 09:04:51 -07:00

Dart Engine

The include/dart_engine.h provides additional functions to embed the Dart VM and to call Dart functions from kernel and AOT snapshots. It is not intended to be a full-featured API, and should be used together with dart_api.h.

It is intended for reusing of existing Dart code in non-Dart programs, and allows to use Dart snapshots as shared libraries: the caller can start one or several isolates from Dart snapshots and call Dart functions from it.

Comparing to dart_api.h it brings the following:

  • Full initialization of Dart VM, including initializing core libraries.
  • Easier handling of isolate messages
  • Lock-guarded functions to enter/exit isolates.

See examples in samples/embedder for usages of the API.

Handling isolate messages

In dart_api.h there are two possible ways to handle asynchronous isolate messages:

  • Dart_MessageNotifyCallback: Users can pass a callback to get notifications about messages to handle, but handling messages still requires entering an isolate, entering a scope, calling Dart_HandleMessage, and then leaving a scope and an isolate.
  • Dart_RunLoop: Users can invoke it on a separate thread, but then they won't be able to enter isolates from other threads.

As an alternative, dart_engine.h provides a simpler function DartEngine_HandleMessage, which encapsulates isolate and scope management, and it allows to specify per isolate / default message schedulers, which only need to schedule an execution DartEngine_HandleMessage with the right isolate.

Users can create their own DartEngine_MessageScheduler struct and pass it to DartEngine_SetMessageScheduler / DartEngine_SetDefaultMessageScheduler. See samples/embedder/run_timer_async.cc and samples/embedder/run_timer.cc examples.

Entering / leaving isolates

Because engine API uses its own message handling, it is important to use DartEngine_AcquireIsolate / DartEngine_ReleaseIsolate instead of Dart_EnterIsolate / Dart_ExitIsolate. DartEngine_AcquireIsolate blocks until an isolate can be entered by trying to obtain an internal lock (which is released by DartEngine_ReleaseIsolate), while Dart_EnterIsolate crashes if some other thread has entered the same isolate.