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