forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage_loop_impl.h
88 lines (62 loc) · 2.25 KB
/
message_loop_impl.h
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2017 The Chromium 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_MESSAGE_LOOP_IMPL_H_
#define FLUTTER_FML_MESSAGE_LOOP_IMPL_H_
#include <atomic>
#include <deque>
#include <queue>
#include <set>
#include <utility>
#include "flutter/fml/message_loop.h"
#include "lib/fxl/functional/closure.h"
#include "lib/fxl/macros.h"
#include "lib/fxl/memory/ref_counted.h"
#include "lib/fxl/synchronization/mutex.h"
#include "lib/fxl/synchronization/thread_annotations.h"
#include "lib/fxl/time/time_point.h"
namespace fml {
class MessageLoopImpl : public fxl::RefCountedThreadSafe<MessageLoopImpl> {
public:
static fxl::RefPtr<MessageLoopImpl> Create();
virtual ~MessageLoopImpl();
virtual void Run() = 0;
virtual void Terminate() = 0;
virtual void WakeUp(fxl::TimePoint time_point) = 0;
void PostTask(fxl::Closure task, fxl::TimePoint target_time);
void AddTaskObserver(TaskObserver* observer);
void RemoveTaskObserver(TaskObserver* observer);
void DoRun();
void DoTerminate();
protected:
MessageLoopImpl();
void RunExpiredTasksNow();
private:
struct DelayedTask {
size_t order;
fxl::Closure task;
fxl::TimePoint target_time;
DelayedTask(size_t p_order,
fxl::Closure p_task,
fxl::TimePoint p_target_time)
: order(p_order), task(std::move(p_task)), target_time(p_target_time) {}
};
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::set<TaskObserver*> task_observers_;
fxl::Mutex delayed_tasks_mutex_;
DelayedTaskQueue delayed_tasks_ FXL_GUARDED_BY(delayed_tasks_mutex_);
size_t order_ FXL_GUARDED_BY(delayed_tasks_mutex_);
std::atomic_bool terminated_;
void RegisterTask(fxl::Closure task, fxl::TimePoint target_time);
void RunExpiredTasks();
FXL_DISALLOW_COPY_AND_ASSIGN(MessageLoopImpl);
};
} // namespace fml
#endif // FLUTTER_FML_MESSAGE_LOOP_IMPL_H_