flutter_flutter/engine/src/flutter/runtime/platform_isolate_manager.h
Liam Appelbe 27ca389132 Experimental platform isolates API (flutter/engine#48551)
This is a prototype of the [PlatformIsolate
API](https://github.com/flutter/flutter/issues/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.
2024-03-04 16:47:41 +13:00

57 lines
1.9 KiB
C++

// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef FLUTTER_RUNTIME_PLATFORM_ISOLATE_MANAGER_H_
#define FLUTTER_RUNTIME_PLATFORM_ISOLATE_MANAGER_H_
#include <atomic>
#include <mutex>
#include <unordered_set>
#include "third_party/dart/runtime/include/dart_api.h"
namespace flutter {
/// Maintains a list of registered platform isolates, so that they can be
/// proactively shutdown as a group during shell shutdown.
class PlatformIsolateManager {
public:
/// Returns whether the PlatformIsolateManager is shutdown. New isolates
/// cannot be registered after the manager is shutdown. Must be called on the
/// platform thread.
bool HasShutdown();
/// Returns whether the PlatformIsolateManager is shutdown. New isolates
/// cannot be registered after the manager is shutdown. Callable from any
/// thread. The result may be obsolete immediately after the call.
bool HasShutdownMaybeFalseNegative();
/// Register an isolate in the list of platform isolates. Callable from any
/// thread.
bool RegisterPlatformIsolate(Dart_Isolate isolate);
/// Remove an isolate from the list of platform isolates. Must be called from
/// the platform thread.
void RemovePlatformIsolate(Dart_Isolate isolate);
/// Shuts down all registered isolates, and the manager itself. Must be called
/// from the platform thread.
void ShutdownPlatformIsolates();
/// Returns whether an isolate is registered. For testing only. Callable from
/// any thread.
bool IsRegisteredForTestingOnly(Dart_Isolate isolate);
private:
// This lock must be recursive because ShutdownPlatformIsolates indirectly
// calls RemovePlatformIsolate.
std::recursive_mutex lock_;
std::unordered_set<Dart_Isolate> platform_isolates_;
bool is_shutdown_ = false;
};
} // namespace flutter
#endif // FLUTTER_RUNTIME_PLATFORM_ISOLATE_MANAGER_H_