From 8380066e70302d813200b1b31f5360ddcdb0829e Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Wed, 19 May 2021 14:49:01 -0700 Subject: [PATCH] updated the documenation for message loops a bit (flutter/engine#26257) --- engine/src/flutter/fml/message_loop.h | 19 ++++++++++++++-- engine/src/flutter/fml/message_loop_impl.h | 5 +++++ .../flutter/fml/message_loop_task_queues.h | 18 ++++++++++----- .../platform/android/message_loop_android.h | 4 ++++ engine/src/flutter/fml/task_runner.h | 22 +++++++++++++++++++ engine/src/flutter/fml/wakeable.h | 2 ++ 6 files changed, 62 insertions(+), 8 deletions(-) diff --git a/engine/src/flutter/fml/message_loop.h b/engine/src/flutter/fml/message_loop.h index 025f8059ec..ccc1f70b93 100644 --- a/engine/src/flutter/fml/message_loop.h +++ b/engine/src/flutter/fml/message_loop.h @@ -13,13 +13,23 @@ namespace fml { class TaskRunner; class MessageLoopImpl; +/// An event loop associated with a thread. +/// +/// This class is the generic front-end to the MessageLoop, differences in +/// implementation based on the running platform are in the subclasses of +/// flutter::MessageLoopImpl (ex flutter::MessageLoopAndroid). +/// +/// For scheduling events on the message loop see flutter::TaskRunner. +/// +/// \see fml::TaskRunner +/// \see fml::MessageLoopImpl +/// \see fml::MessageLoopTaskQueues +/// \see fml::Wakeable class MessageLoop { public: FML_EMBEDDER_ONLY static MessageLoop& GetCurrent(); - bool IsValid() const; - void Run(); void Terminate(); @@ -36,10 +46,15 @@ class MessageLoop { static void EnsureInitializedForCurrentThread(); + /// Returns true if \p EnsureInitializedForCurrentThread has been called on + /// this thread already. static bool IsInitializedForCurrentThread(); ~MessageLoop(); + /// Gets the unique identifier for the TaskQueue associated with the current + /// thread. + /// \see fml::MessageLoopTaskQueues static TaskQueueId GetCurrentTaskQueueId(); private: diff --git a/engine/src/flutter/fml/message_loop_impl.h b/engine/src/flutter/fml/message_loop_impl.h index 055423fad8..23a2eceb15 100644 --- a/engine/src/flutter/fml/message_loop_impl.h +++ b/engine/src/flutter/fml/message_loop_impl.h @@ -23,6 +23,11 @@ namespace fml { +/// An abstract class that represents the differences in implementation of a \p +/// fml::MessageLoop depending on the platform. +/// \see fml::MessageLoop +/// \see fml::MessageLoopAndroid +/// \see fml::MessageLoopDarwin class MessageLoopImpl : public Wakeable, public fml::RefCountedThreadSafe { public: diff --git a/engine/src/flutter/fml/message_loop_task_queues.h b/engine/src/flutter/fml/message_loop_task_queues.h index 3d7f2bcb87..da3362f1c3 100644 --- a/engine/src/flutter/fml/message_loop_task_queues.h +++ b/engine/src/flutter/fml/message_loop_task_queues.h @@ -22,8 +22,11 @@ namespace fml { static const TaskQueueId _kUnmerged = TaskQueueId(TaskQueueId::kUnmerged); -// This is keyed by the |TaskQueueId| and contains all the queue -// components that make up a single TaskQueue. +/// A collection of tasks and observers associated with one TaskQueue. +/// +/// Often a TaskQueue has a one-to-one relationship with a fml::MessageLoop, +/// this isn't the case when TaskQueues are merged via +/// \p fml::MessageLoopTaskQueues::Merge. class TaskQueueEntry { public: using TaskObservers = std::map; @@ -51,9 +54,12 @@ enum class FlushType { kAll, }; -// This class keeps track of all the tasks and observers that -// need to be run on it's MessageLoopImpl. This also wakes up the -// loop at the required times. +/// A singleton container for all tasks and observers associated with all +/// fml::MessageLoops. +/// +/// This also wakes up the loop at the required times. +/// \see fml::MessageLoop +/// \see fml::Wakeable class MessageLoopTaskQueues : public fml::RefCountedThreadSafe { public: @@ -115,7 +121,7 @@ class MessageLoopTaskQueues // Will return false if the owner has not been merged before. bool Unmerge(TaskQueueId owner); - // Returns true if owner owns the subsumed task queue. + /// Returns \p true if \p owner owns the \p subsumed task queue. bool Owns(TaskQueueId owner, TaskQueueId subsumed) const; // Returns the subsumed task queue if any or |TaskQueueId::kUnmerged| diff --git a/engine/src/flutter/fml/platform/android/message_loop_android.h b/engine/src/flutter/fml/platform/android/message_loop_android.h index b73b06e3db..6ff26be0fd 100644 --- a/engine/src/flutter/fml/platform/android/message_loop_android.h +++ b/engine/src/flutter/fml/platform/android/message_loop_android.h @@ -21,6 +21,10 @@ struct UniqueLooperTraits { static void Free(ALooper* value) { ::ALooper_release(value); } }; +/// Android implementation of \p MessageLoopImpl. +/// +/// This implemenation wraps usage of Android's \p looper. +/// \see https://developer.android.com/ndk/reference/group/looper class MessageLoopAndroid : public MessageLoopImpl { private: fml::UniqueObject looper_; diff --git a/engine/src/flutter/fml/task_runner.h b/engine/src/flutter/fml/task_runner.h index 5bb738e48b..d65c3e919b 100644 --- a/engine/src/flutter/fml/task_runner.h +++ b/engine/src/flutter/fml/task_runner.h @@ -16,11 +16,21 @@ 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, public BasicTaskRunner { public: @@ -31,12 +41,24 @@ class TaskRunner : public fml::RefCountedThreadSafe, 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 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(fml::RefPtr runner, const fml::closure& task); diff --git a/engine/src/flutter/fml/wakeable.h b/engine/src/flutter/fml/wakeable.h index 2b36139817..36408c0528 100644 --- a/engine/src/flutter/fml/wakeable.h +++ b/engine/src/flutter/fml/wakeable.h @@ -9,6 +9,8 @@ namespace fml { +/// Interface over the ability to \p WakeUp a \p fml::MessageLoopImpl. +/// \see fml::MessageLoopTaskQueues class Wakeable { public: virtual ~Wakeable() {}