-
Notifications
You must be signed in to change notification settings - Fork 26.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Platform Isolate #136314
Comments
I believe that perhaps this could solve the problem of low-level audio API callbacks in the system that today cannot be called with dart ffi |
@insinfo Only if the audio API calls those callbacks on the platform thread. Audio APIs often create their own internal thread dedicated to audio rendering, and in that case platform isolates wouldn't help. |
This is needed to prevent users from closing the platform isolate. Bug: flutter/flutter#136314 Change-Id: I98ab0a847dad245a1f1d6c16e96f2f4add39a84f TEST=runtime/vm/isolate_test.cc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/352760 Reviewed-by: Ryan Macnak <rmacnak@google.com> Commit-Queue: Liam Appelbe <liama@google.com>
This is a prototype of the [PlatformIsolate API](flutter/flutter#136314). **UPDATE (Jan 25):** The PR is ready for review. PTAL. The `PlatformIsolate` creation flow is: 1. `PlatformIsolate.spawn` running on parent isolate (platform_isolate.dart) a. Create `isolateReadyPort` b. `PlatformIsolateNativeApi::Spawn` (platform_isolate.cc) c. `DartIsolate::CreatePlatformIsolate` (dart_isolate.cc) d. Isolate created. Entry point invocation task dispatched to platform thread e. `PlatformIsolate.spawn` returns a `Future<Isolate>` 2. On the platform thread, `_platformIsolateMain` is invoked in the platform isolate a. Create `entryPointPort` b. Send `Isolate.current` metadata and `entryPointPort` back to the parent isolate via `isolateReadyPort` 3. Back in the parent isolate, `isolateReadyPort.handler` is invoked a. Send the user's `entryPoint` and `message` to the platform isolate via `entryPointPort` b. Use received isolate metadata to create a new `Isolate` representing the platform isolate and complete the `Future<Isolate>` 4. In the platform isolate, `entryPointPort.handler` is invoked a. Run the user's `entryPoint(message)` The engine shutdown flow is handled by `PlatformIsolateManager`, which maintains a set of running platform isolates.
I still have a problem with that |
@liamappelbe I wanted to use the I can see the However, it's missing in the Edit: I'm curious if this issue might be linked to the absence of an entry for The output of
|
Maybe. I'm not a regular contributor to the engine, so I'm not sure what plumbing is needed when a new file is added. I'm surprised this wasn't caught by CI though. @jason-simmons should I add platform_isolate.dart to dart_ui.gni? |
…package This makes the API available to Flutter framework users. See flutter/flutter#136314
Sent a PR to add it to |
…package (#51538) This makes the API available to Flutter framework users. See flutter/flutter#136314
The Platform Isolate API is now available on the master channel. I've tried calling the runOnPlatformThread function in a Flutter Counter app but the app crashed with this error:
It doesn't matter what you do in the function that you pass to the runOnPlatformThread function. Even calling it this way crashes the app with the above error: void _incrementCounter() async {
await runOnPlatformThread(() {});
// ...
} |
What platform did this happen on? From the screenshot in #136314 (comment) it looks like you're running on Windows. The I'm planning to add a Flutter engine flag that embedders can use to indicate whether they support this API. |
Yes, I tested this on Windows.
I see. Thanks for the explanation. |
@halildurmus What's your use case? Windows APIs aren't usually thread locked |
I think I've found a Win32 API (ShowCursor) that needs to be called on the platform thread in order to work (see dart-windows/win32#313). I've confirmed that it works (hides the cursor) if I call the API inside the Windows runner (windows\runner\main.cpp). I suspect there may be many similar APIs in the Win32 API that need to be called on the platform thread. |
Looked at this again and found a viable way to make the current platform isolates implementation work with the task runner design used on Linux and Windows. Proposed a PR: flutter/engine#51573 |
Not entirely sure whether the changes made in flutter/engine#51573 were intended to completely resolve the problems on Linux and Windows but after trying again, I still encountered the same error. I even gave it a shot on Linux, but it didn't work there either. |
No - that PR is not a complete solution. There is still the issue of supporting embedders where the platform thread does not have a message loop (see flutter/engine#48551 (comment)). With flutter/engine#51573 a call to But that only works because an internal debugging feature ( The Linux and Windows embedders will need to solve this in order to enable support for platform isolates. The message loop is needed because So until this is resolved I think it makes sense to also add the engine flag that embedders can use to declare whether they support platform isolates. |
…s available. Platform isolates are currently supported only on Android and iOS. See flutter/flutter#136314
…s available. (#51784) Platform isolates are currently supported only on Android and iOS. See flutter/flutter#136314
Wondering if there's a design doc or summary of what this means to Flutter developers? |
May I ask if after this feature is implemented, I will be able to call the native function from the thread where the win32 window was created? For example, SetWindowSubclass for win32 can only be called from the thread that created the win32 window. So I can't do it with pure dart code at the moment, I need to use platform channels for that. |
The feature is already implemented (as |
I've split off the remaining bits of work into their own issues. Closing this now. |
This is a tracking bug for the Platform Isolate feature. The design details are still being worked out, but the general idea is to provide a way of using FFI to invoke native APIs that can only be be called from the platform thread.
Some related bugs: #113099, #28310, dart-lang/sdk#52106, dart-lang/sdk#38315, dart-lang/sdk#42570, dart-lang/sdk#46943, dart-lang/native#463
Remaining work tasks after initial PR landed
runInPlatformThread()
from helper isolates: Allow callingrunOnPlatformThread()
from helper isolates #150584PlatformIsolateManager::RemovePlatformIsolate
andPlatformIsolateManager::ShutdownPlatformIsolates
, find a way of asserting that we're on the platform thread:PlatformIsolateManager
methods should assert they're called on the platform thread #150586The text was updated successfully, but these errors were encountered: