Flutter Engine
 
Loading...
Searching...
No Matches
embedder_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
6
9
10namespace flutter {
11
12std::atomic_intptr_t EmbedderTaskRunner::next_unique_id_ = 0;
13
15 size_t embedder_identifier)
16 : TaskRunner(nullptr /* loop implemenation*/),
17 embedder_identifier_(embedder_identifier),
18 dispatch_table_(std::move(table)),
19 placeholder_id_(fml::TaskQueueId(fml::TaskQueueId::kInvalid)),
20 unique_id_(next_unique_id_++) {
21 FML_DCHECK(dispatch_table_.post_task_callback);
23 FML_DCHECK(dispatch_table_.destruction_callback);
24}
25
29
31 return embedder_identifier_;
32}
33
35 PostTaskForTime(task, fml::TimePoint::Now());
36}
37
38void EmbedderTaskRunner::PostTaskForTime(const fml::closure& task,
39 fml::TimePoint target_time) {
40 if (!task) {
41 return;
42 }
43
44 uint64_t baton = 0;
45
46 {
47 // Release the lock before the jump via the dispatch table.
48 std::scoped_lock lock(tasks_mutex_);
49 baton = ++last_baton_;
50 pending_tasks_[baton] = task;
51 }
52
53 dispatch_table_.post_task_callback(this, baton, target_time);
54}
55
56void EmbedderTaskRunner::PostDelayedTask(const fml::closure& task,
57 fml::TimeDelta delay) {
58 PostTaskForTime(task, fml::TimePoint::Now() + delay);
59}
60
61bool EmbedderTaskRunner::RunsTasksOnCurrentThread() {
62 return dispatch_table_.runs_task_on_current_thread_callback();
63}
64
65bool EmbedderTaskRunner::PostTask(uint64_t baton) {
66 fml::closure task;
67
68 {
69 std::scoped_lock lock(tasks_mutex_);
70 auto found = pending_tasks_.find(baton);
71 if (found == pending_tasks_.end()) {
72 FML_LOG(ERROR) << "Embedder attempted to post an unknown task.";
73 return false;
74 }
75 task = found->second;
76 pending_tasks_.erase(found);
77
78 // Let go of the tasks mutex befor executing the task.
79 }
80
81 FML_DCHECK(task);
82 task();
83 return true;
84}
85
86// |fml::TaskRunner|
87fml::TaskQueueId EmbedderTaskRunner::GetTaskQueueId() {
88 return placeholder_id_;
89}
90
91} // namespace flutter
size_t GetEmbedderIdentifier() const
The unique identifier provided by the embedder for the task runner. Embedders whose dispatch tables s...
EmbedderTaskRunner(DispatchTable table, size_t embedder_identifier)
Create a task runner with a dispatch table for delegation of task runner responsibility to the embedd...
static TimePoint Now()
Definition time_point.cc:49
#define FML_LOG(severity)
Definition logging.h:101
#define FML_DCHECK(condition)
Definition logging.h:122
std::function< void()> closure
Definition closure.h:14
Definition ref_ptr.h:261
std::function< void(EmbedderTaskRunner *task_runner, uint64_t task_baton, fml::TimePoint target_time)> post_task_callback
std::function< bool(void)> runs_task_on_current_thread_callback
std::function< void()> destruction_callback
Performs user-designated cleanup on destruction.