mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
Fixes https://github.com/flutter/flutter/issues/152337 Introduces `ui_task_runner` field on `FlutterCustomTaskRunners`. This lets the embedder to specify task runner for UI isolate tasks, allowing for merging platform and UI threads. With custom UI task runner there is no `MessageLoop` anymore for the UI thread, so the message loop task observer can no longer be used to flush microtask queue. Instead the microtask queue is flushed at the end of each `FlutterEngineRunTask` invocation if needed. This is handled internally by the embedder. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md --------- Co-authored-by: Jonah Williams <jonahwilliams@google.com> Co-authored-by: Loïc Sharma <737941+loic-sharma@users.noreply.github.com>
90 lines
3.1 KiB
C++
90 lines
3.1 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_FML_TASK_RUNNER_H_
|
|
#define FLUTTER_FML_TASK_RUNNER_H_
|
|
|
|
#include "flutter/fml/closure.h"
|
|
#include "flutter/fml/macros.h"
|
|
#include "flutter/fml/memory/ref_counted.h"
|
|
#include "flutter/fml/memory/ref_ptr.h"
|
|
#include "flutter/fml/message_loop_task_queues.h"
|
|
#include "flutter/fml/time/time_point.h"
|
|
|
|
namespace fml {
|
|
|
|
class MessageLoopImpl;
|
|
|
|
/// An interface over the ability to schedule tasks on a \p TaskRunner.
|
|
class BasicTaskRunner {
|
|
public:
|
|
/// Schedules \p task to be executed on the TaskRunner's associated event
|
|
/// loop.
|
|
virtual void PostTask(const fml::closure& task) = 0;
|
|
};
|
|
|
|
/// The object for scheduling tasks on a \p fml::MessageLoop.
|
|
///
|
|
/// Typically there is one \p TaskRunner associated with each thread. When one
|
|
/// wants to execute an operation on that thread they post a task to the
|
|
/// TaskRunner.
|
|
///
|
|
/// \see fml::MessageLoop
|
|
class TaskRunner : public fml::RefCountedThreadSafe<TaskRunner>,
|
|
public BasicTaskRunner {
|
|
public:
|
|
virtual ~TaskRunner();
|
|
|
|
virtual void PostTask(const fml::closure& task) override;
|
|
|
|
virtual void PostTaskForTime(const fml::closure& task,
|
|
fml::TimePoint target_time);
|
|
|
|
/// Schedules a task to be run on the MessageLoop after the time \p delay has
|
|
/// passed.
|
|
/// \note There is latency between when the task is schedule and actually
|
|
/// executed so that the actual execution time is: now + delay +
|
|
/// message_loop_latency, where message_loop_latency is undefined and could be
|
|
/// tens of milliseconds.
|
|
virtual void PostDelayedTask(const fml::closure& task, fml::TimeDelta delay);
|
|
|
|
/// Returns \p true when the current executing thread's TaskRunner matches
|
|
/// this instance.
|
|
virtual bool RunsTasksOnCurrentThread();
|
|
|
|
/// Returns the unique identifier associated with the TaskRunner.
|
|
/// \see fml::MessageLoopTaskQueues
|
|
///
|
|
/// Will be TaskQueueId::kInvalid for embedder supplied task runners
|
|
/// that are not associated with a task queue.
|
|
virtual TaskQueueId GetTaskQueueId();
|
|
|
|
/// Executes the \p task directly if the TaskRunner \p runner is the
|
|
/// TaskRunner associated with the current executing thread.
|
|
static void RunNowOrPostTask(const fml::RefPtr<fml::TaskRunner>& runner,
|
|
const fml::closure& task);
|
|
|
|
/// Like RunNowOrPostTask, except that if the task can be immediately
|
|
/// executed, an empty task will still be posted to the runner afterwards.
|
|
///
|
|
/// This is used to ensure that messages posted to Dart from the platform
|
|
/// thread always flush the Dart event loop.
|
|
static void RunNowAndFlushMessages(const fml::RefPtr<fml::TaskRunner>& runner,
|
|
const fml::closure& task);
|
|
|
|
protected:
|
|
explicit TaskRunner(fml::RefPtr<MessageLoopImpl> loop);
|
|
|
|
private:
|
|
fml::RefPtr<MessageLoopImpl> loop_;
|
|
|
|
FML_FRIEND_MAKE_REF_COUNTED(TaskRunner);
|
|
FML_FRIEND_REF_COUNTED_THREAD_SAFE(TaskRunner);
|
|
FML_DISALLOW_COPY_AND_ASSIGN(TaskRunner);
|
|
};
|
|
|
|
} // namespace fml
|
|
|
|
#endif // FLUTTER_FML_TASK_RUNNER_H_
|