-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow embedders to specify their own task runner interfaces.
Currently, all Flutter threads are managed by the engine itself. This works for all threads except the platform thread. On this thread, the engine cannot see the underlying event multiplexing mechanism. Using the new task runner interfaces, the engine can relinquish the task of setting up the event multiplexing mechanism and instead have the embedder provide one for it during setup. This sceme is only wired up for the platform thread. But, the eventual goal is to expose this message loop interoperability for all threads.
- Loading branch information
1 parent
039e6f6
commit f484779
Showing
15 changed files
with
679 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// 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. | ||
|
||
#ifndef FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SAFE_ACCESS_H_ | ||
#define FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SAFE_ACCESS_H_ | ||
|
||
#include <type_traits> | ||
|
||
#define SAFE_ACCESS(pointer, member, default_value) \ | ||
([=]() { \ | ||
if (offsetof(std::remove_pointer<decltype(pointer)>::type, member) + \ | ||
sizeof(pointer->member) <= \ | ||
pointer->struct_size) { \ | ||
return pointer->member; \ | ||
} \ | ||
return static_cast<decltype(pointer->member)>((default_value)); \ | ||
})() | ||
|
||
#endif // FLUTTER_SHELL_PLATFORM_EMBEDDER_EMBEDDER_SAFE_ACCESS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// 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/embedder/embedder_task_runner.h" | ||
|
||
#include "flutter/fml/message_loop_impl.h" | ||
|
||
namespace shell { | ||
|
||
EmbedderTaskRunner::EmbedderTaskRunner(DispatchTable table) | ||
: TaskRunner(nullptr /* loop implemenation*/), | ||
dispatch_table_(std::move(table)) { | ||
FML_DCHECK(dispatch_table_.post_task_callback); | ||
FML_DCHECK(dispatch_table_.runs_task_on_current_thread_callback); | ||
} | ||
|
||
EmbedderTaskRunner::~EmbedderTaskRunner() = default; | ||
|
||
void EmbedderTaskRunner::PostTask(fml::closure task) { | ||
PostTaskForTime(task, fml::TimePoint::Now()); | ||
} | ||
|
||
void EmbedderTaskRunner::PostTaskForTime(fml::closure task, | ||
fml::TimePoint target_time) { | ||
if (!task) { | ||
return; | ||
} | ||
|
||
uint64_t baton = 0; | ||
|
||
{ | ||
// Release the lock before the jump via the dispatch table. | ||
std::lock_guard<std::mutex> lock(tasks_mutex_); | ||
baton = ++last_baton_; | ||
pending_tasks_[baton] = task; | ||
} | ||
|
||
dispatch_table_.post_task_callback(this, baton, target_time); | ||
} | ||
|
||
void EmbedderTaskRunner::PostDelayedTask(fml::closure task, | ||
fml::TimeDelta delay) { | ||
PostTaskForTime(task, fml::TimePoint::Now() + delay); | ||
} | ||
|
||
bool EmbedderTaskRunner::RunsTasksOnCurrentThread() { | ||
return dispatch_table_.runs_task_on_current_thread_callback(); | ||
} | ||
|
||
bool EmbedderTaskRunner::PostTask(uint64_t baton) { | ||
fml::closure task; | ||
|
||
{ | ||
std::lock_guard<std::mutex> lock(tasks_mutex_); | ||
auto found = pending_tasks_.find(baton); | ||
if (found == pending_tasks_.end()) { | ||
FML_LOG(ERROR) << "Embedder attempted to post an unknown task."; | ||
return false; | ||
} | ||
task = found->second; | ||
pending_tasks_.erase(found); | ||
|
||
// Let go of the tasks mutex befor executing the task. | ||
} | ||
|
||
FML_DCHECK(task); | ||
task(); | ||
return true; | ||
} | ||
|
||
} // namespace shell |
Oops, something went wrong.