
The isolate ownership API was [introduced recently](https://dart-review.googlesource.com/c/sdk/+/407700) to solve [some deadlock bugs](https://github.com/dart-lang/native/issues/1908) in native callbacks. A native callback is a call from native code into a Dart function. Currently all such callbacks must run that Dart function in the isolate that created the callback (called the target isolate). The only native callback primitives at the moment are `NativeCallable.isolateLocal` (blocking, but must be invoked from the same thread as the target isolate, and the target isolate must be currently entered on that thread) and `NativeCallable.listener` (non-blocking, can be invoked from any thread). To build blocking callbacks that can be called from any thread, we can use a `NativeCallable.listener`, and use a synchronization object like a mutex or a condition variable to block until the callback is complete. However, if we try to do this on the thread that is currently entered in the target isolate, we will deadlock: we invoke the listener, a message is sent to the target isolate, and we block waiting for the message to be handled, so we never pass control flow back to the isolate to handle the message, and never stop waiting. To fix this deadlock, Ffigen and Jnigen both have a mechanism that checks if we're on the target isolate's thread first: - If the native caller is already on the same thread as the target isolate, and the target isolate is entered: - Call the Dart function directly using `NativeCallable.isolateLocal` or similar - Otherwise, if the native caller is coming from a different thread: - Call the Dart function asynchronously using `NativeCallable.listener` or similar - Block until the callback finishes However, this neglects the case where we're on the target isolate's thread, but not entered into the isolate. This case happens in Flutter when the callback is invoked from the UI thread (or the platform thread when thread merging is enabled), and the target isolate is the root isolate. When the native callback is invoked, the root isolate is not entered, so we hit the second case: we send a message to the root isolate, and block to wait for a response. Since the root isolate is exclusively run on the UI thread, and we're blocking the UI thread, the message will never be handled, and we deadlock. The isolate ownership API fixes this by allowing the embedder to inform the VM that it will run a particular isolate exclusively on a particular thread, using `Dart_SetCurrentThreadOwnsIsolate`. Other native code can then query that ownership using `Dart_GetCurrentThreadOwnsIsolate`. This lets us add a third case to our conditional: - If the native caller is on the thread that is currently entered in the target isolate: - Call the Dart function directly using `NativeCallable.isolateLocal` or similar - Otherwise, if the native caller is on the thread that owns the target isolate - Enter the target isolate - Call the Dart function directly using `NativeCallable.isolateLocal `or similar - Exit the target isolate - Otherwise, the native caller is coming from an unrelated thread: - Call the Dart function asynchronously using `NativeCallable.listener` or similar - Block until the callback finishes **Note:** We don't need to set the ownership of VM managed threads, because they run in a thread pool exclusively used by the VM, so there's no way for native code to be executed on the thread (except by FFI, in which case we're entered into the isolate anyway). We only need this for Flutter's root isolate because work can be sent to the UI thread/platform thread using OS specific APIs like Android's `Looper.getMainLooper()`.
Flutter is Google's SDK for crafting beautiful, fast user experiences for mobile, web, and desktop from a single codebase. Flutter works with existing code, is used by developers and organizations around the world, and is free and open source.
Documentation
For announcements about new releases, follow the flutter-announce@googlegroups.com mailing list. Our documentation also tracks breaking changes across releases.
Terms of service
The Flutter tool may occasionally download resources from Google servers. By downloading or using the Flutter SDK, you agree to the Google Terms of Service: https://policies.google.com/terms
For example, when installed from GitHub (as opposed to from a prepackaged
archive), the Flutter tool will download the Dart SDK from Google servers
immediately when first run, as it is used to execute the flutter
tool itself.
This will also occur when Flutter is upgraded (e.g. by running the flutter upgrade
command).
About Flutter
We think Flutter will help you create beautiful, fast apps, with a productive, extensible and open development model, whether you're targeting iOS or Android, web, Windows, macOS, Linux or embedding it as the UI toolkit for a platform of your choice.
Beautiful user experiences
We want to enable designers to deliver their full creative vision without being forced to water it down due to limitations of the underlying framework. Flutter's layered architecture gives you control over every pixel on the screen and its powerful compositing capabilities let you overlay and animate graphics, video, text, and controls without limitation. Flutter includes a full set of widgets that deliver pixel-perfect experiences whether you're building for iOS (Cupertino) or other platforms (Material), along with support for customizing or creating entirely new visual components.
Fast results
Flutter is fast. It's powered by hardware-accelerated 2D graphics libraries like Skia (which underpins Chrome and Android) and Impeller. We architected Flutter to support glitch-free, jank-free graphics at the native speed of your device.
Flutter code is powered by the world-class Dart platform, which enables compilation to 32-bit and 64-bit ARM machine code for iOS and Android, JavaScript and WebAssembly for the web, as well as Intel x64 and ARM for desktop devices.
Productive development
Flutter offers stateful hot reload, allowing you to make changes to your code and see the results instantly without restarting your app or losing its state.
Extensible and open model
Flutter works with any development tool (or none at all), and also includes editor plug-ins for both Visual Studio Code and IntelliJ / Android Studio. Flutter provides tens of thousands of packages to speed your development, regardless of your target platform. And accessing other native code is easy, with support for both FFI (on Android, on iOS, on macOS, and on Windows) as well as platform-specific APIs.
Flutter is a fully open-source project, and we welcome contributions. Information on how to get started can be found in our contributor guide.