| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef FLUTTER_FML_TASK_SOURCE_H_ |
| 6 | #define FLUTTER_FML_TASK_SOURCE_H_ |
| 7 | |
| 8 | #include "flutter/fml/delayed_task.h" |
| 9 | #include "flutter/fml/task_queue_id.h" |
| 10 | #include "flutter/fml/task_source_grade.h" |
| 11 | |
| 12 | namespace fml { |
| 13 | |
| 14 | class MessageLoopTaskQueues; |
| 15 | |
| 16 | /** |
| 17 | * A Source of tasks for the `MessageLoopTaskQueues` task dispatcher. This is a |
| 18 | * wrapper around a primary and secondary task heap with the difference between |
| 19 | * them being that the secondary task heap can be paused and resumed by the task |
| 20 | * dispatcher. `TaskSourceGrade` determines what task heap the task is assigned |
| 21 | * to. |
| 22 | * |
| 23 | * Registering Tasks |
| 24 | * ----------------- |
| 25 | * The task dispatcher associates a task source with each `TaskQueueID`. When |
| 26 | * the user of the task dispatcher registers a task, the task is in-turn |
| 27 | * registered with the `TaskSource` corresponding to the `TaskQueueID`. |
| 28 | * |
| 29 | * Processing Tasks |
| 30 | * ---------------- |
| 31 | * Task dispatcher provides the event loop a way to acquire tasks to run via |
| 32 | * `GetNextTaskToRun`. Task dispatcher asks the underlying `TaskSource` for the |
| 33 | * next task. |
| 34 | */ |
| 35 | class TaskSource { |
| 36 | public: |
| 37 | struct TopTask { |
| 38 | TaskQueueId task_queue_id; |
| 39 | const DelayedTask& task; |
| 40 | }; |
| 41 | |
| 42 | /// Construts a TaskSource with the given `task_queue_id`. |
| 43 | explicit TaskSource(TaskQueueId task_queue_id); |
| 44 | |
| 45 | ~TaskSource(); |
| 46 | |
| 47 | /// Drops the pending tasks from both primary and secondary task heaps. |
| 48 | void ShutDown(); |
| 49 | |
| 50 | /// Adds a task to the corresponding task heap as dictated by the |
| 51 | /// `TaskSourceGrade` of the `DelayedTask`. |
| 52 | void RegisterTask(const DelayedTask& task); |
| 53 | |
| 54 | /// Pops the task heap corresponding to the `TaskSourceGrade`. |
| 55 | void PopTask(TaskSourceGrade grade); |
| 56 | |
| 57 | /// Returns the number of pending tasks. Excludes the tasks from the secondary |
| 58 | /// heap if it's paused. |
| 59 | size_t GetNumPendingTasks() const; |
| 60 | |
| 61 | /// Returns true if `GetNumPendingTasks` is zero. |
| 62 | bool IsEmpty() const; |
| 63 | |
| 64 | /// Returns the top task based on scheduled time, taking into account whether |
| 65 | /// the secondary heap has been paused or not. |
| 66 | TopTask Top() const; |
| 67 | |
| 68 | /// Pause providing tasks from secondary task heap. |
| 69 | void PauseSecondary(); |
| 70 | |
| 71 | /// Resume providing tasks from secondary task heap. |
| 72 | void ResumeSecondary(); |
| 73 | |
| 74 | private: |
| 75 | const fml::TaskQueueId task_queue_id_; |
| 76 | fml::DelayedTaskQueue primary_task_queue_; |
| 77 | fml::DelayedTaskQueue secondary_task_queue_; |
| 78 | int secondary_pause_requests_ = 0; |
| 79 | |
| 80 | FML_DISALLOW_COPY_ASSIGN_AND_MOVE(TaskSource); |
| 81 | }; |
| 82 | |
| 83 | } // namespace fml |
| 84 | |
| 85 | #endif // FLUTTER_FML_TASK_SOURCE_H_ |
| 86 | |