forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_loop_impl.h
89 lines (63 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
89
// 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 <map>
#include <mutex>
#include <queue>
#include <utility>
#include "flutter/fml/macros.h"
#include "flutter/fml/message_loop.h"
#include "lib/fxl/functional/closure.h"
#include "lib/fxl/memory/ref_counted.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(intptr_t key, fxl::Closure callback);
void RemoveTaskObserver(intptr_t key);
void DoRun();
void DoTerminate();
// Exposed for the embedder shell which allows clients to poll for events
// instead of dedicating a thread to the message loop.
void RunExpiredTasksNow();
protected:
MessageLoopImpl();
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::map<intptr_t, fxl::Closure> task_observers_;
std::mutex delayed_tasks_mutex_;
DelayedTaskQueue delayed_tasks_;
size_t order_;
std::atomic_bool terminated_;
void RegisterTask(fxl::Closure task, fxl::TimePoint target_time);
void RunExpiredTasks();
FML_DISALLOW_COPY_AND_ASSIGN(MessageLoopImpl);
};
} // namespace fml
#endif // FLUTTER_FML_MESSAGE_LOOP_IMPL_H_