Flutter Engine
 
Loading...
Searching...
No Matches
embedder_task_runner.h
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#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_
6#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_
7
8#include <mutex>
9#include <unordered_map>
10
11#include "flutter/fml/macros.h"
13
14namespace flutter {
15
16//------------------------------------------------------------------------------
17/// A task runner which delegates responsibility of task execution to an
18/// embedder. This is done by managing a dispatch table to the embedder.
19///
20class EmbedderTaskRunner final : public fml::TaskRunner {
21 public:
22 //----------------------------------------------------------------------------
23 /// @brief A
24 ///
26 //--------------------------------------------------------------------------
27 /// Delegates responsibility of deferred task execution to the embedder.
28 /// Once the embedder gets the task, it must call
29 /// `EmbedderTaskRunner::PostTask` with the supplied `task_baton` on the
30 /// correct thread after the tasks `target_time` point expires.
31 ///
32 std::function<void(EmbedderTaskRunner* task_runner,
33 uint64_t task_baton,
34 fml::TimePoint target_time)>
36 //--------------------------------------------------------------------------
37 /// Asks the embedder if tasks posted to it on this task runner via the
38 /// `post_task_callback` will be executed (after task expiry) on the calling
39 /// thread.
40 ///
41 std::function<bool(void)> runs_task_on_current_thread_callback;
42
43 //--------------------------------------------------------------------------
44 /// Performs user-designated cleanup on destruction.
45 std::function<void()> destruction_callback;
46 };
47
48 //----------------------------------------------------------------------------
49 /// @brief Create a task runner with a dispatch table for delegation of
50 /// task runner responsibility to the embedder. When embedders
51 /// specify task runner dispatch tables that service tasks on the
52 /// same thread, they also must ensure that their
53 /// `embedder_idetifier`s match. This allows the engine to
54 /// determine task runner equality without actually posting tasks
55 /// to the task runner.
56 ///
57 /// @param[in] table The task runner dispatch table.
58 /// @param[in] embedder_identifier The embedder identifier
59 ///
60 EmbedderTaskRunner(DispatchTable table, size_t embedder_identifier);
61
62 // |fml::TaskRunner|
63 ~EmbedderTaskRunner() override;
64
65 //----------------------------------------------------------------------------
66 /// @brief The unique identifier provided by the embedder for the task
67 /// runner. Embedders whose dispatch tables service tasks on the
68 /// same underlying OS thread must ensure that their identifiers
69 /// match. This allows the engine to determine task runner
70 /// equality without posting tasks on the thread.
71 ///
72 /// @return The embedder identifier.
73 ///
74 size_t GetEmbedderIdentifier() const;
75
76 bool PostTask(uint64_t baton);
77
78 intptr_t unique_id() const { return unique_id_; }
79
80 private:
81 const size_t embedder_identifier_;
82 DispatchTable dispatch_table_;
83 std::mutex tasks_mutex_;
84 uint64_t last_baton_ = 0;
85 std::unordered_map<uint64_t, fml::closure> pending_tasks_;
86 fml::TaskQueueId placeholder_id_;
87 intptr_t unique_id_;
88
89 static std::atomic_intptr_t next_unique_id_;
90
91 // |fml::TaskRunner|
92 void PostTask(const fml::closure& task) override;
93
94 // |fml::TaskRunner|
95 void PostTaskForTime(const fml::closure& task,
96 fml::TimePoint target_time) override;
97
98 // |fml::TaskRunner|
99 void PostDelayedTask(const fml::closure& task, fml::TimeDelta delay) override;
100
101 // |fml::TaskRunner|
102 bool RunsTasksOnCurrentThread() override;
103
104 // |fml::TaskRunner|
105 fml::TaskQueueId GetTaskQueueId() override;
106
108};
109
110} // namespace flutter
111
112#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_TASK_RUNNER_H_
size_t GetEmbedderIdentifier() const
The unique identifier provided by the embedder for the task runner. Embedders whose dispatch tables s...
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
std::function< void()> closure
Definition closure.h:14
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.