forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_runner.cc
52 lines (39 loc) · 1.37 KB
/
task_runner.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// 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);
}
bool TaskRunner::RunsTasksOnCurrentThread() {
if (!fml::MessageLoop::IsInitializedForCurrentThread()) {
return false;
}
return MessageLoop::GetCurrent().GetLoopImpl() == loop_;
}
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