-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Expand file tree
/
Copy pathevent_loop.h
More file actions
88 lines (68 loc) · 2.67 KB
/
event_loop.h
File metadata and controls
88 lines (68 loc) · 2.67 KB
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 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_SHELL_PLATFORM_GLFW_EVENT_LOOP_H_
#define FLUTTER_SHELL_PLATFORM_GLFW_EVENT_LOOP_H_
#include <chrono>
#include <deque>
#include <functional>
#include <mutex>
#include <queue>
#include <thread>
#include "flutter/shell/platform/embedder/embedder.h"
namespace flutter {
// An abstract event loop.
class EventLoop {
public:
using TaskExpiredCallback = std::function<void(const FlutterTask*)>;
// Creates an event loop running on the given thread, calling
// |on_task_expired| to run tasks.
EventLoop(std::thread::id main_thread_id,
const TaskExpiredCallback& on_task_expired);
virtual ~EventLoop();
// Disallow copy.
EventLoop(const EventLoop&) = delete;
EventLoop& operator=(const EventLoop&) = delete;
// Returns if the current thread is the thread used by this event loop.
bool RunsTasksOnCurrentThread() const;
// Waits for the next event, processes it, and returns.
//
// Expired engine events, if any, are processed as well. The optional
// timeout should only be used when events not managed by this loop need to be
// processed in a polling manner.
void WaitForEvents(
std::chrono::nanoseconds max_wait = std::chrono::nanoseconds::max());
// Posts a Flutter engine task to the event loop for delayed execution.
void PostTask(FlutterTask flutter_task, uint64_t flutter_target_time_nanos);
protected:
using TaskTimePoint = std::chrono::steady_clock::time_point;
// Returns the timepoint corresponding to a Flutter task time.
static TaskTimePoint TimePointFromFlutterTime(
uint64_t flutter_target_time_nanos);
// Returns the mutex used to control the task queue. Subclasses may safely
// lock this mutex in the abstract methods below.
std::mutex& GetTaskQueueMutex() { return task_queue_mutex_; }
// Waits until the given time, or a Wake() call.
virtual void WaitUntil(const TaskTimePoint& time) = 0;
// Wakes the main thread from a WaitUntil call.
virtual void Wake() = 0;
struct Task {
uint64_t order;
TaskTimePoint fire_time;
FlutterTask task;
struct Comparer {
bool operator()(const Task& a, const Task& b) {
if (a.fire_time == b.fire_time) {
return a.order > b.order;
}
return a.fire_time > b.fire_time;
}
};
};
std::thread::id main_thread_id_;
TaskExpiredCallback on_task_expired_;
std::mutex task_queue_mutex_;
std::priority_queue<Task, std::deque<Task>, Task::Comparer> task_queue_;
};
} // namespace flutter
#endif // FLUTTER_SHELL_PLATFORM_GLFW_EVENT_LOOP_H_