Skip to content
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

Make Dart_Post schedule on port's isolate #49524

Closed
gaaclarke opened this issue Jul 25, 2022 · 3 comments
Closed

Make Dart_Post schedule on port's isolate #49524

gaaclarke opened this issue Jul 25, 2022 · 3 comments
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.

Comments

@gaaclarke
Copy link
Contributor

This tracker is for issues related to:

  • Dart VM

There doesn't appear to be a way to post data from C onto an arbitrary isolate. What I'd like to do is:

class Router {
  public:
    void handlePlatformMessage(Dart_Handle channel, Dart_Handle message, Dart_Port port, Dart_Handle callback) {
      // Called on arbitrary isolate thread.
      std::shared_ptr<Handler> handler;
      {
        std::scoped_lock lock(mutex_);
        handler = _handlers[ToCppString(channel)];      
      }
      handler->thread->post([handler, port, callback]() {
        std::unique_ptr<Bytes> result = handler->exec(message);
        Dart_Handle dart_result = ZeroCopyConversion(result.release());
        Dart_Post(port, callback);
        Dart_Post(port, dart_result);
      });
    }
  private:
    std::mutex mutex_;
    std::map<std::string, std::shared_ptr<Handler>> handlers_;
};

I don't believe this is possible with today's API since Dart_Post requires that Dart_CurrentIsolate() != nullptr and one cannot safely call Dart_EnterIsolate safely from an arbitrary thread since it is an error to do so if another thread has already "entered" that isolate.

This could be implemented by making ports store their isolate so that Dart_Post can schedule with that isolate, thus removing the dependency on Dart_CurrentIsolate()'s value.

This is a required feature if we ever want to implement flutter/flutter#13937 since this would allow scheduling on an arbitrary isolate's event loop via the SendPort API.

cc @mkustermann

@lrhn lrhn added the area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. label Jul 25, 2022
@rmacnak-google
Copy link
Contributor

Dart_Post is for a sender running with a current isolate, which is the only way to even have Dart_Handles to serve as a message. There must a current isolate, but it does not need to be the isolate of the destination port.

Dart_PostCObject is for a sender running without an isolate, which necessarily can't have Dart_Handles and so must provide Dart_CObjects as the message instead.

@gaaclarke
Copy link
Contributor Author

Thanks, Dart_PostCObject is what I was looking for.

@gaaclarke gaaclarke reopened this Jul 25, 2022
copybara-service bot pushed a commit that referenced this issue Jul 25, 2022
fixes #49524

TEST=n/a, documentation

Change-Id: Ia5154e9c2c1d6f6cce46e9bceb49f315cb840bc3
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252681
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
@mkustermann
Copy link
Member

mkustermann commented Jul 26, 2022

This could be implemented by making ports store their isolate so that Dart_Post can schedule with that isolate, thus removing
the dependency on Dart_CurrentIsolate()'s value.

Ports do store the isolate which will process messages sent to the port. Though only as long as the isolate is alive, after it's death, posting messages to the port will be silently dropped. So the reference from Port to Isolate is in some sense a weak reference (and stored in our PortMap)

Thanks, Dart_PostCObject is what I was looking for.

To persist references to objects that the receiver can use (e.g. callback closure in your case) you can use persistent handles.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.
Projects
None yet
Development

No branches or pull requests

4 participants