Flutter Engine
event_loop.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_GLFW_EVENT_LOOP_H_
6 #define FLUTTER_SHELL_PLATFORM_GLFW_EVENT_LOOP_H_
7 
8 #include <chrono>
9 #include <deque>
10 #include <functional>
11 #include <mutex>
12 #include <queue>
13 #include <thread>
14 
15 #include "flutter/shell/platform/embedder/embedder.h"
16 
17 namespace flutter {
18 
19 // An abstract event loop.
20 class EventLoop {
21  public:
22  using TaskExpiredCallback = std::function<void(const FlutterTask*)>;
23 
24  // Creates an event loop running on the given thread, calling
25  // |on_task_expired| to run tasks.
26  EventLoop(std::thread::id main_thread_id,
27  const TaskExpiredCallback& on_task_expired);
28 
29  virtual ~EventLoop();
30 
31  // Disallow copy.
32  EventLoop(const EventLoop&) = delete;
33  EventLoop& operator=(const EventLoop&) = delete;
34 
35  // Returns if the current thread is the thread used by this event loop.
36  bool RunsTasksOnCurrentThread() const;
37 
38  // Waits for the next event, processes it, and returns.
39  //
40  // Expired engine events, if any, are processed as well. The optional
41  // timeout should only be used when events not managed by this loop need to be
42  // processed in a polling manner.
43  void WaitForEvents(
44  std::chrono::nanoseconds max_wait = std::chrono::nanoseconds::max());
45 
46  // Posts a Flutter engine task to the event loop for delayed execution.
47  void PostTask(FlutterTask flutter_task, uint64_t flutter_target_time_nanos);
48 
49  protected:
50  using TaskTimePoint = std::chrono::steady_clock::time_point;
51 
52  // Returns the timepoint corresponding to a Flutter task time.
54  uint64_t flutter_target_time_nanos);
55 
56  // Returns the mutex used to control the task queue. Subclasses may safely
57  // lock this mutex in the abstract methods below.
58  std::mutex& GetTaskQueueMutex() { return task_queue_mutex_; }
59 
60  // Waits until the given time, or a Wake() call.
61  virtual void WaitUntil(const TaskTimePoint& time) = 0;
62 
63  // Wakes the main thread from a WaitUntil call.
64  virtual void Wake() = 0;
65 
66  struct Task {
67  uint64_t order;
70 
71  struct Comparer {
72  bool operator()(const Task& a, const Task& b) {
73  if (a.fire_time == b.fire_time) {
74  return a.order > b.order;
75  }
76  return a.fire_time > b.fire_time;
77  }
78  };
79  };
82  std::mutex task_queue_mutex_;
83  std::priority_queue<Task, std::deque<Task>, Task::Comparer> task_queue_;
84 };
85 
86 } // namespace flutter
87 
88 #endif // FLUTTER_SHELL_PLATFORM_GLFW_EVENT_LOOP_H_
std::priority_queue< Task, std::deque< Task >, Task::Comparer > task_queue_
Definition: event_loop.h:83
std::thread::id main_thread_id_
Definition: event_loop.h:80
std::chrono::steady_clock::time_point TaskTimePoint
Definition: event_loop.h:50
bool operator()(const Task &a, const Task &b)
Definition: event_loop.h:72
std::function< void(const FlutterTask *)> TaskExpiredCallback
Definition: event_loop.h:22
TaskExpiredCallback on_task_expired_
Definition: event_loop.h:81
EventLoop(std::thread::id main_thread_id, const TaskExpiredCallback &on_task_expired)
Definition: event_loop.cc:12
virtual ~EventLoop()
bool RunsTasksOnCurrentThread() const
Definition: event_loop.cc:19
std::mutex & GetTaskQueueMutex()
Definition: event_loop.h:58
TaskTimePoint fire_time
Definition: event_loop.h:68
void PostTask(FlutterTask flutter_task, uint64_t flutter_target_time_nanos)
Definition: event_loop.cc:83
std::mutex task_queue_mutex_
Definition: event_loop.h:82
void WaitForEvents(std::chrono::nanoseconds max_wait=std::chrono::nanoseconds::max())
Definition: event_loop.cc:23
static TaskTimePoint TimePointFromFlutterTime(uint64_t flutter_target_time_nanos)
Definition: event_loop.cc:75
int32_t id
virtual void WaitUntil(const TaskTimePoint &time)=0
EventLoop & operator=(const EventLoop &)=delete
virtual void Wake()=0