Skip to content

Commit

Permalink
Refactor Delayed Tasks to their own file (flutter#9290)
Browse files Browse the repository at this point in the history
* Refactor Delayed Tasks to their own class

* fix some comments

* Update BUILD.gn
  • Loading branch information
iskakaushik committed Jun 12, 2019
1 parent 9baf589 commit 87c26ae
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 37 deletions.
2 changes: 2 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions fml/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
35 changes: 35 additions & 0 deletions fml/delayed_task.cc
Original file line number Diff line number Diff line change
@@ -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
41 changes: 41 additions & 0 deletions fml/delayed_task.h
Original file line number Diff line number Diff line change
@@ -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_
17 changes: 4 additions & 13 deletions fml/message_loop_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -158,18 +158,18 @@ 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;
}
}

WakeUp(delayed_tasks_.empty() ? fml::TimePoint::Max()
: delayed_tasks_.top().target_time);
: delayed_tasks_.top().GetTargetTime());
}

for (const auto& invocation : invocations) {
Expand All @@ -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
25 changes: 1 addition & 24 deletions fml/message_loop_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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_;
Expand Down

0 comments on commit 87c26ae

Please sign in to comment.