| Flutter Linux Embedder |

fl_task_runner.cc
Go to the documentation of this file.
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 #include "flutter/shell/platform/linux/fl_task_runner.h"
6 #include "flutter/shell/platform/linux/fl_engine_private.h"
7
8 static constexpr int kMicrosecondsPerNanosecond = 1000;
9 static constexpr int kMillisecondsPerMicrosecond = 1000;
10
11 struct _FlTaskRunner {
12 GObject parent_instance;
13
14 GWeakRef engine;
15
16 GMutex mutex;
17 GCond cond;
18
19 guint timeout_source_id;
20 GList /*<FlTaskRunnerTask>*/* pending_tasks;
21 gboolean blocking_main_thread;
22 };
23
24 typedef struct _FlTaskRunnerTask {
25 // absolute time of task (based on g_get_monotonic_time).
26 gint64 task_time_micros;
27
28 // flutter task to execute if schedule through
29 // fl_task_runner_post_flutter_task.
30 FlutterTask task;
31 } FlTaskRunnerTask;
32
33 G_DEFINE_TYPE(FlTaskRunner, fl_task_runner, G_TYPE_OBJECT)
34
35 // Removes expired tasks from the task queue and executes them.
36 // The execution is performed with mutex unlocked.
37 static void fl_task_runner_process_expired_tasks_locked(FlTaskRunner* self) {
38 GList* expired_tasks = nullptr;
39
40 gint64 current_time = g_get_monotonic_time();
41
42 GList* l = self->pending_tasks;
43 while (l != nullptr) {
44 FlTaskRunnerTask* task = static_cast<FlTaskRunnerTask*>(l->data);
45 if (task->task_time_micros <= current_time) {
46 GList* link = l;
47 l = l->next;
48 self->pending_tasks = g_list_remove_link(self->pending_tasks, link);
49 expired_tasks = g_list_concat(expired_tasks, link);
50 } else {
51 l = l->next;
52 }
53 }
54
55 g_mutex_unlock(&self->mutex);
56
57 g_autoptr(FlEngine) engine = FL_ENGINE(g_weak_ref_get(&self->engine));
58 if (engine != nullptr) {
59 l = expired_tasks;
60 while (l != nullptr) {
61 FlTaskRunnerTask* task = static_cast<FlTaskRunnerTask*>(l->data);
62 fl_engine_execute_task(engine, &task->task);
63 l = l->next;
64 }
65 }
66
67 g_list_free_full(expired_tasks, g_free);
68
69 g_mutex_lock(&self->mutex);
70 }
71
72 static void fl_task_runner_tasks_did_change_locked(FlTaskRunner* self);
73
74 // Invoked from a timeout source. Removes and executes expired tasks
75 // and reschedules timeout if needed.
76 static gboolean fl_task_runner_on_expired_timeout(gpointer data) {
77 FlTaskRunner* self = FL_TASK_RUNNER(data);
78
79 g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
80 (void)locker; // unused variable
81
82 g_object_ref(self);
83
84 self->timeout_source_id = 0;
85 fl_task_runner_process_expired_tasks_locked(self);
86
87 // reschedule timeout
88 fl_task_runner_tasks_did_change_locked(self);
89
90 g_object_unref(self);
91
92 return FALSE;
93 }
94
95 // Returns the absolute time of next expired task (in microseconds, based on
96 // g_get_monotonic_time). If no task is scheduled returns G_MAXINT64.
97 static gint64 fl_task_runner_next_task_expiration_time_locked(
98 FlTaskRunner* self) {
99 gint64 min_time = G_MAXINT64;
100 GList* l = self->pending_tasks;
101 while (l != nullptr) {
102 FlTaskRunnerTask* task = static_cast<FlTaskRunnerTask*>(l->data);
103 min_time = MIN(min_time, task->task_time_micros);
104 l = l->next;
105 }
106 return min_time;
107 }
108
109 static void fl_task_runner_tasks_did_change_locked(FlTaskRunner* self) {
110 if (self->blocking_main_thread) {
111 // Wake up blocked thread
112 g_cond_signal(&self->cond);
113 } else {
114 // Reschedule timeout
115 if (self->timeout_source_id != 0) {
116 g_source_remove(self->timeout_source_id);
117 self->timeout_source_id = 0;
118 }
119 gint64 min_time = fl_task_runner_next_task_expiration_time_locked(self);
120 if (min_time != G_MAXINT64) {
121 gint64 remaining = MAX(min_time - g_get_monotonic_time(), 0);
122 self->timeout_source_id =
123 g_timeout_add(remaining / kMillisecondsPerMicrosecond + 1,
124 fl_task_runner_on_expired_timeout, self);
125 }
126 }
127 }
128
129 void fl_task_runner_dispose(GObject* object) {
130 FlTaskRunner* self = FL_TASK_RUNNER(object);
131
132 // this should never happen because the task runner is retained while blocking
133 // main thread
134 g_assert(!self->blocking_main_thread);
135
136 g_weak_ref_clear(&self->engine);
137 g_mutex_clear(&self->mutex);
138 g_cond_clear(&self->cond);
139
140 g_list_free_full(self->pending_tasks, g_free);
141 if (self->timeout_source_id != 0) {
142 g_source_remove(self->timeout_source_id);
143 }
144
145 G_OBJECT_CLASS(fl_task_runner_parent_class)->dispose(object);
146 }
147
148 static void fl_task_runner_class_init(FlTaskRunnerClass* klass) {
149 G_OBJECT_CLASS(klass)->dispose = fl_task_runner_dispose;
150 }
151
152 static void fl_task_runner_init(FlTaskRunner* self) {
153 g_mutex_init(&self->mutex);
154 g_cond_init(&self->cond);
155 }
156
157 FlTaskRunner* fl_task_runner_new(FlEngine* engine) {
158 FlTaskRunner* self =
159 FL_TASK_RUNNER(g_object_new(fl_task_runner_get_type(), nullptr));
160 g_weak_ref_init(&self->engine, G_OBJECT(engine));
161 return self;
162 }
163
164 void fl_task_runner_post_flutter_task(FlTaskRunner* self,
165 FlutterTask task,
166 uint64_t target_time_nanos) {
167 g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
168 (void)locker; // unused variable
169
170 FlTaskRunnerTask* runner_task = g_new0(FlTaskRunnerTask, 1);
171 runner_task->task = task;
172 runner_task->task_time_micros =
173 target_time_nanos / kMicrosecondsPerNanosecond;
174
175 self->pending_tasks = g_list_append(self->pending_tasks, runner_task);
176 fl_task_runner_tasks_did_change_locked(self);
177 }
178
179 void fl_task_runner_block_main_thread(FlTaskRunner* self) {
180 g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
181 (void)locker; // unused variable
182
183 g_return_if_fail(self->blocking_main_thread == FALSE);
184
185 g_object_ref(self);
186
187 self->blocking_main_thread = true;
188 while (self->blocking_main_thread) {
189 g_cond_wait_until(&self->cond, &self->mutex,
190 fl_task_runner_next_task_expiration_time_locked(self));
191 fl_task_runner_process_expired_tasks_locked(self);
192 }
193
194 // Tasks might have changed in the meanwhile, reschedule timeout
195 fl_task_runner_tasks_did_change_locked(self);
196
197 g_object_unref(self);
198 }
199
200 void fl_task_runner_release_main_thread(FlTaskRunner* self) {
201 g_autoptr(GMutexLocker) locker = g_mutex_locker_new(&self->mutex);
202 (void)locker; // unused variable
203
204 g_return_if_fail(self->blocking_main_thread == TRUE);
205
206 self->blocking_main_thread = FALSE;
207 g_cond_signal(&self->cond);
208 }
G_DEFINE_TYPE
G_DEFINE_TYPE(FlBasicMessageChannelResponseHandle, fl_basic_message_channel_response_handle, G_TYPE_OBJECT) static void fl_basic_message_channel_response_handle_dispose(GObject *object)
Definition: fl_basic_message_channel.cc:37
g_autoptr
g_autoptr(GMutexLocker) locker
TRUE
return TRUE
Definition: fl_compositor_software.cc:69
self
g_cond_signal & self
Definition: fl_compositor_software.cc:67
fl_engine_execute_task
void fl_engine_execute_task(FlEngine *self, FlutterTask *task)
Definition: fl_engine.cc:1425
fl_engine_private.h
fl_task_runner_dispose
void fl_task_runner_dispose(GObject *object)
Definition: fl_task_runner.cc:129
fl_task_runner_process_expired_tasks_locked
static void fl_task_runner_process_expired_tasks_locked(FlTaskRunner *self)
Definition: fl_task_runner.cc:37
fl_task_runner_post_flutter_task
void fl_task_runner_post_flutter_task(FlTaskRunner *self, FlutterTask task, uint64_t target_time_nanos)
Definition: fl_task_runner.cc:164
FlTaskRunnerTask
struct _FlTaskRunnerTask FlTaskRunnerTask
fl_task_runner_next_task_expiration_time_locked
static gint64 fl_task_runner_next_task_expiration_time_locked(FlTaskRunner *self)
Definition: fl_task_runner.cc:97
fl_task_runner_release_main_thread
void fl_task_runner_release_main_thread(FlTaskRunner *self)
Definition: fl_task_runner.cc:200
kMillisecondsPerMicrosecond
static constexpr int kMillisecondsPerMicrosecond
Definition: fl_task_runner.cc:9
fl_task_runner_class_init
static void fl_task_runner_class_init(FlTaskRunnerClass *klass)
Definition: fl_task_runner.cc:148
fl_task_runner_tasks_did_change_locked
static void fl_task_runner_tasks_did_change_locked(FlTaskRunner *self)
Definition: fl_task_runner.cc:109
fl_task_runner_block_main_thread
void fl_task_runner_block_main_thread(FlTaskRunner *self)
Definition: fl_task_runner.cc:179
fl_task_runner_on_expired_timeout
static gboolean fl_task_runner_on_expired_timeout(gpointer data)
Definition: fl_task_runner.cc:76
kMicrosecondsPerNanosecond
static constexpr int kMicrosecondsPerNanosecond
Definition: fl_task_runner.cc:8
fl_task_runner_new
FlTaskRunner * fl_task_runner_new(FlEngine *engine)
Definition: fl_task_runner.cc:157
fl_task_runner_init
static void fl_task_runner_init(FlTaskRunner *self)
Definition: fl_task_runner.cc:152
fl_task_runner.h
_FlTaskRunner
Definition: fl_task_runner.cc:11
_FlTaskRunner::blocking_main_thread
gboolean blocking_main_thread
Definition: fl_task_runner.cc:21
_FlTaskRunner::timeout_source_id
guint timeout_source_id
Definition: fl_task_runner.cc:19
_FlTaskRunner::pending_tasks
GList * pending_tasks
Definition: fl_task_runner.cc:20
_FlTaskRunner::engine
GWeakRef engine
Definition: fl_task_runner.cc:14
_FlTaskRunner::mutex
GMutex mutex
Definition: fl_task_runner.cc:16
_FlTaskRunner::parent_instance
GObject parent_instance
Definition: fl_task_runner.cc:12
_FlTaskRunner::cond
GCond cond
Definition: fl_task_runner.cc:17
_FlTaskRunnerTask
Definition: fl_task_runner.cc:24
_FlTaskRunnerTask::task
FlutterTask task
Definition: fl_task_runner.cc:30
_FlTaskRunnerTask::task_time_micros
gint64 task_time_micros
Definition: fl_task_runner.cc:26