forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfl_task_runner.cc
More file actions
213 lines (166 loc) · 6.36 KB
/
fl_task_runner.cc
File metadata and controls
213 lines (166 loc) · 6.36 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// 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.
#include "flutter/shell/platform/linux/fl_task_runner.h"
#include "flutter/shell/platform/linux/fl_engine_private.h"
static constexpr int kMicrosecondsPerNanosecond = 1000;
static constexpr int kMillisecondsPerMicrosecond = 1000;
struct _FlTaskRunner {
GObject parent_instance;
FlEngine* engine;
GMutex mutex;
GCond cond;
guint timeout_source_id;
GList /*<FlTaskRunnerTask>*/* pending_tasks;
gboolean blocking_main_thread;
};
typedef struct _FlTaskRunnerTask {
// absolute time of task (based on g_get_monotonic_time)
gint64 task_time_micros;
std::function<void()> delegate;
} FlTaskRunnerTask;
G_DEFINE_TYPE(FlTaskRunner, fl_task_runner, G_TYPE_OBJECT)
// Removes expired tasks from the task queue and executes them.
// The execution is performed with mutex unlocked.
static void fl_task_runner_process_expired_tasks_locked(FlTaskRunner* self) {
GList* expired_tasks = nullptr;
gint64 current_time = g_get_monotonic_time();
GList* l = self->pending_tasks;
while (l != nullptr) {
FlTaskRunnerTask* task = static_cast<FlTaskRunnerTask*>(l->data);
if (task->task_time_micros <= current_time) {
GList* link = l;
l = l->next;
self->pending_tasks = g_list_remove_link(self->pending_tasks, link);
expired_tasks = g_list_concat(expired_tasks, link);
} else {
l = l->next;
}
}
g_mutex_unlock(&self->mutex);
l = expired_tasks;
while (l != nullptr && self->engine) {
FlTaskRunnerTask* task = static_cast<FlTaskRunnerTask*>(l->data);
task->delegate();
l = l->next;
}
g_list_free_full(expired_tasks, g_free);
g_mutex_lock(&self->mutex);
}
static void fl_task_runner_tasks_did_change_locked(FlTaskRunner* self);
// Invoked from a timeout source. Removes and executes expired tasks
// and reschedules timeout if needed.
static gboolean fl_task_runner_on_expired_timeout(gpointer data) {
FlTaskRunner* self = FL_TASK_RUNNER(data);
g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
(void)locker; // unused variable
g_object_ref(self);
self->timeout_source_id = 0;
fl_task_runner_process_expired_tasks_locked(self);
// reschedule timeout
fl_task_runner_tasks_did_change_locked(self);
g_object_unref(self);
return FALSE;
}
// Returns the absolute time of next expired task (in microseconds, based on
// g_get_monotonic_time). If no task is scheduled returns G_MAXINT64.
static gint64 fl_task_runner_next_task_expiration_time_locked(
FlTaskRunner* self) {
gint64 min_time = G_MAXINT64;
GList* l = self->pending_tasks;
while (l != nullptr) {
FlTaskRunnerTask* task = static_cast<FlTaskRunnerTask*>(l->data);
min_time = MIN(min_time, task->task_time_micros);
l = l->next;
}
return min_time;
}
static void fl_task_runner_tasks_did_change_locked(FlTaskRunner* self) {
if (self->blocking_main_thread) {
// Wake up blocked thread
g_cond_signal(&self->cond);
} else {
// Reschedule timeout
if (self->timeout_source_id != 0) {
g_source_remove(self->timeout_source_id);
self->timeout_source_id = 0;
}
gint64 min_time = fl_task_runner_next_task_expiration_time_locked(self);
if (min_time != G_MAXINT64) {
gint64 remaining = MAX(min_time - g_get_monotonic_time(), 0);
self->timeout_source_id =
g_timeout_add(remaining / kMillisecondsPerMicrosecond + 1,
fl_task_runner_on_expired_timeout, self);
}
}
}
static void engine_weak_notify_cb(gpointer user_data,
GObject* where_the_object_was) {
FlTaskRunner* self = FL_TASK_RUNNER(user_data);
self->engine = nullptr;
}
void fl_task_runner_dispose(GObject* object) {
FlTaskRunner* self = FL_TASK_RUNNER(object);
// this should never happen because the task runner is retained while blocking
// main thread
g_assert(!self->blocking_main_thread);
if (self->engine != nullptr) {
g_object_weak_unref(G_OBJECT(self->engine), engine_weak_notify_cb, self);
self->engine = nullptr;
}
g_mutex_clear(&self->mutex);
g_cond_clear(&self->cond);
g_list_free_full(self->pending_tasks, g_free);
if (self->timeout_source_id != 0) {
g_source_remove(self->timeout_source_id);
}
G_OBJECT_CLASS(fl_task_runner_parent_class)->dispose(object);
}
static void fl_task_runner_class_init(FlTaskRunnerClass* klass) {
G_OBJECT_CLASS(klass)->dispose = fl_task_runner_dispose;
}
static void fl_task_runner_init(FlTaskRunner* self) {
g_mutex_init(&self->mutex);
g_cond_init(&self->cond);
}
FlTaskRunner* fl_task_runner_new(FlEngine* engine) {
FlTaskRunner* res =
FL_TASK_RUNNER(g_object_new(fl_task_runner_get_type(), nullptr));
res->engine = engine;
g_object_weak_ref(G_OBJECT(engine), engine_weak_notify_cb, res);
return res;
}
void fl_task_runner_post_task(FlTaskRunner* self,
std::function<void()> delegate,
uint64_t target_time_nanos) {
g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
(void)locker; // unused variable
FlTaskRunnerTask* runner_task = g_new0(FlTaskRunnerTask, 1);
runner_task->delegate = std::move(delegate);
runner_task->task_time_micros =
target_time_nanos / kMicrosecondsPerNanosecond;
self->pending_tasks = g_list_append(self->pending_tasks, runner_task);
fl_task_runner_tasks_did_change_locked(self);
}
void fl_task_runner_block_main_thread(FlTaskRunner* self) {
g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
(void)locker; // unused variable
g_return_if_fail(self->blocking_main_thread == FALSE);
g_object_ref(self);
self->blocking_main_thread = true;
while (self->blocking_main_thread) {
g_cond_wait_until(&self->cond, &self->mutex,
fl_task_runner_next_task_expiration_time_locked(self));
fl_task_runner_process_expired_tasks_locked(self);
}
// Tasks might have changed in the meanwhile, reschedule timeout
fl_task_runner_tasks_did_change_locked(self);
g_object_unref(self);
}
void fl_task_runner_release_main_thread(FlTaskRunner* self) {
g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
(void)locker; // unused variable
g_return_if_fail(self->blocking_main_thread == TRUE);
self->blocking_main_thread = FALSE;
g_cond_signal(&self->cond);
}