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