Refactor Delayed Tasks to their own file (flutter/engine#9290)

* Refactor Delayed Tasks to their own class

* fix some comments

* Update BUILD.gn
This commit is contained in:
Kaushik Iska
2019-06-11 18:33:04 -07:00
committed by GitHub
parent 899cd81833
commit deaaaf3d53
6 changed files with 85 additions and 37 deletions

View File

@@ -100,6 +100,8 @@ FILE: ../../../flutter/fml/command_line_unittest.cc
FILE: ../../../flutter/fml/compiler_specific.h
FILE: ../../../flutter/fml/concurrent_message_loop.cc
FILE: ../../../flutter/fml/concurrent_message_loop.h
FILE: ../../../flutter/fml/delayed_task.cc
FILE: ../../../flutter/fml/delayed_task.h
FILE: ../../../flutter/fml/eintr_wrapper.h
FILE: ../../../flutter/fml/file.cc
FILE: ../../../flutter/fml/file.h

View File

@@ -16,6 +16,8 @@ source_set("fml") {
"compiler_specific.h",
"concurrent_message_loop.cc",
"concurrent_message_loop.h",
"delayed_task.cc",
"delayed_task.h",
"eintr_wrapper.h",
"file.cc",
"file.h",

View File

@@ -0,0 +1,35 @@
// 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/delayed_task.h"
namespace fml {
DelayedTask::DelayedTask(size_t order,
fml::closure task,
fml::TimePoint target_time)
: order_(order), task_(std::move(task)), target_time_(target_time) {}
DelayedTask::DelayedTask(const DelayedTask& other) = default;
DelayedTask::~DelayedTask() = default;
const fml::closure& DelayedTask::GetTask() const {
return task_;
}
fml::TimePoint DelayedTask::GetTargetTime() const {
return target_time_;
}
bool DelayedTask::operator>(const DelayedTask& other) const {
if (target_time_ == other.target_time_) {
return order_ > other.order_;
}
return target_time_ > other.target_time_;
}
} // namespace fml

View File

@@ -0,0 +1,41 @@
// 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_FML_DELAYED_TASK_H_
#define FLUTTER_FML_DELAYED_TASK_H_
#include "flutter/fml/closure.h"
#include "flutter/fml/time/time_point.h"
#include <queue>
namespace fml {
class DelayedTask {
public:
DelayedTask(size_t order, fml::closure task, fml::TimePoint target_time);
DelayedTask(const DelayedTask& other);
~DelayedTask();
const fml::closure& GetTask() const;
fml::TimePoint GetTargetTime() const;
bool operator>(const DelayedTask& other) const;
private:
size_t order_;
fml::closure task_;
fml::TimePoint target_time_;
};
using DelayedTaskQueue = std::priority_queue<DelayedTask,
std::deque<DelayedTask>,
std::greater<DelayedTask>>;
} // namespace fml
#endif // FLUTTER_FML_DELAYED_TASK_H_

View File

@@ -133,7 +133,7 @@ void MessageLoopImpl::RegisterTask(fml::closure task,
}
std::lock_guard<std::mutex> lock(delayed_tasks_mutex_);
delayed_tasks_.push({++order_, std::move(task), target_time});
WakeUp(delayed_tasks_.top().target_time);
WakeUp(delayed_tasks_.top().GetTargetTime());
}
void MessageLoopImpl::FlushTasks(FlushType type) {
@@ -158,10 +158,10 @@ void MessageLoopImpl::FlushTasks(FlushType type) {
auto now = fml::TimePoint::Now();
while (!delayed_tasks_.empty()) {
const auto& top = delayed_tasks_.top();
if (top.target_time > now) {
if (top.GetTargetTime() > now) {
break;
}
invocations.emplace_back(std::move(top.task));
invocations.emplace_back(std::move(top.GetTask()));
delayed_tasks_.pop();
if (type == FlushType::kSingle) {
break;
@@ -169,7 +169,7 @@ void MessageLoopImpl::FlushTasks(FlushType type) {
}
WakeUp(delayed_tasks_.empty() ? fml::TimePoint::Max()
: delayed_tasks_.top().target_time);
: delayed_tasks_.top().GetTargetTime());
}
for (const auto& invocation : invocations) {
@@ -189,13 +189,4 @@ void MessageLoopImpl::RunSingleExpiredTaskNow() {
FlushTasks(FlushType::kSingle);
}
MessageLoopImpl::DelayedTask::DelayedTask(size_t p_order,
fml::closure p_task,
fml::TimePoint p_target_time)
: order(p_order), task(std::move(p_task)), target_time(p_target_time) {}
MessageLoopImpl::DelayedTask::DelayedTask(const DelayedTask& other) = default;
MessageLoopImpl::DelayedTask::~DelayedTask() = default;
} // namespace fml

View File

@@ -13,6 +13,7 @@
#include <utility>
#include "flutter/fml/closure.h"
#include "flutter/fml/delayed_task.h"
#include "flutter/fml/macros.h"
#include "flutter/fml/memory/ref_counted.h"
#include "flutter/fml/message_loop.h"
@@ -58,30 +59,6 @@ class MessageLoopImpl : public fml::RefCountedThreadSafe<MessageLoopImpl> {
MessageLoopImpl();
private:
struct DelayedTask {
size_t order;
fml::closure task;
fml::TimePoint target_time;
DelayedTask(size_t p_order,
fml::closure p_task,
fml::TimePoint p_target_time);
DelayedTask(const DelayedTask& other);
~DelayedTask();
};
struct DelayedTaskCompare {
bool operator()(const DelayedTask& a, const DelayedTask& b) {
return a.target_time == b.target_time ? a.order > b.order
: a.target_time > b.target_time;
}
};
using DelayedTaskQueue = std::
priority_queue<DelayedTask, std::deque<DelayedTask>, DelayedTaskCompare>;
std::mutex tasks_flushing_mutex_;
std::mutex observers_mutex_;