mirror of
https://github.com/encounter/engine.git
synced 2026-03-30 11:09:55 -07:00
379028ab66
- Add the functionality to merge and unmerge MessageLoopTaskQueues This introduces a notion of a "owning" and "subsumed" queue ids. Owning queue will take care of the tasks submitted to both that and it's subsumed queue. - The tasks submitted still maintain the queue affinity - Same for the task observers - Also adds MergedQueuesRunner which grabs both the locks owner and subsumed queues in RAII fashion. - Also use task queue id to verify if we are running in the same thread. - This is to enable merging the backed message loop task queues to enable dynamic thread merging in IOS.
74 lines
1.8 KiB
C++
74 lines
1.8 KiB
C++
// 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.
|
|
|
|
#define FML_USED_ON_EMBEDDER
|
|
|
|
#include "flutter/fml/task_runner.h"
|
|
|
|
#include <utility>
|
|
|
|
#include "flutter/fml/logging.h"
|
|
#include "flutter/fml/message_loop.h"
|
|
#include "flutter/fml/message_loop_impl.h"
|
|
|
|
namespace fml {
|
|
|
|
TaskRunner::TaskRunner(fml::RefPtr<MessageLoopImpl> loop)
|
|
: loop_(std::move(loop)) {}
|
|
|
|
TaskRunner::~TaskRunner() = default;
|
|
|
|
void TaskRunner::PostTask(fml::closure task) {
|
|
loop_->PostTask(std::move(task), fml::TimePoint::Now());
|
|
}
|
|
|
|
void TaskRunner::PostTaskForTime(fml::closure task,
|
|
fml::TimePoint target_time) {
|
|
loop_->PostTask(std::move(task), target_time);
|
|
}
|
|
|
|
void TaskRunner::PostDelayedTask(fml::closure task, fml::TimeDelta delay) {
|
|
loop_->PostTask(std::move(task), fml::TimePoint::Now() + delay);
|
|
}
|
|
|
|
TaskQueueId TaskRunner::GetTaskQueueId() {
|
|
FML_DCHECK(loop_);
|
|
return loop_->GetTaskQueueId();
|
|
}
|
|
|
|
bool TaskRunner::RunsTasksOnCurrentThread() {
|
|
if (!fml::MessageLoop::IsInitializedForCurrentThread()) {
|
|
return false;
|
|
}
|
|
const auto current_queue_id =
|
|
MessageLoop::GetCurrent().GetLoopImpl()->GetTaskQueueId();
|
|
const auto loop_queue_id = loop_->GetTaskQueueId();
|
|
|
|
if (current_queue_id == loop_queue_id) {
|
|
return true;
|
|
}
|
|
|
|
auto queues = MessageLoopTaskQueues::GetInstance();
|
|
if (queues->Owns(current_queue_id, loop_queue_id)) {
|
|
return true;
|
|
}
|
|
if (queues->Owns(loop_queue_id, current_queue_id)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void TaskRunner::RunNowOrPostTask(fml::RefPtr<fml::TaskRunner> runner,
|
|
fml::closure task) {
|
|
FML_DCHECK(runner);
|
|
if (runner->RunsTasksOnCurrentThread()) {
|
|
task();
|
|
} else {
|
|
runner->PostTask(std::move(task));
|
|
}
|
|
}
|
|
|
|
} // namespace fml
|