Fix continuous event polling in the GLFW event loop (flutter/engine#12320)

* Do not pass a milliseconds::max() timeout that will overflow when converted
  to nanoseconds
* Avoid holding the task_queue_mutex_ while calling glfwWaitEventsTimeout
* Use a signed type to hold the difference between a task's timestamp and
  the current engine time

Fixes https://github.com/flutter/flutter/issues/40281
This commit is contained in:
Jason Simmons
2019-09-17 13:54:38 -07:00
committed by GitHub
parent 7d72616400
commit c667b18e15
2 changed files with 11 additions and 7 deletions

View File

@@ -739,9 +739,10 @@ void FlutterDesktopWindowSetPixelRatioOverride(
bool FlutterDesktopRunWindowEventLoopWithTimeout(
FlutterDesktopWindowControllerRef controller,
uint32_t timeout_milliseconds) {
auto wait_duration = timeout_milliseconds == 0
? std::chrono::milliseconds::max()
: std::chrono::milliseconds(timeout_milliseconds);
std::chrono::nanoseconds wait_duration =
timeout_milliseconds == 0
? std::chrono::nanoseconds::max()
: std::chrono::milliseconds(timeout_milliseconds);
controller->event_loop->WaitForEvents(wait_duration);
return !glfwWindowShouldClose(controller->window.get());

View File

@@ -63,9 +63,12 @@ void GLFWEventLoop::WaitForEvents(std::chrono::nanoseconds max_wait) {
// Make sure the seconds are not integral.
using Seconds = std::chrono::duration<double, std::ratio<1>>;
std::lock_guard<std::mutex> lock(task_queue_mutex_);
const auto next_wake = task_queue_.empty() ? TaskTimePoint::max()
: task_queue_.top().fire_time;
TaskTimePoint next_wake;
{
std::lock_guard<std::mutex> lock(task_queue_mutex_);
next_wake = task_queue_.empty() ? TaskTimePoint::max()
: task_queue_.top().fire_time;
}
const auto duration_to_wait = std::chrono::duration_cast<Seconds>(
std::min(next_wake - now, max_wait));
@@ -84,7 +87,7 @@ void GLFWEventLoop::WaitForEvents(std::chrono::nanoseconds max_wait) {
GLFWEventLoop::TaskTimePoint GLFWEventLoop::TimePointFromFlutterTime(
uint64_t flutter_target_time_nanos) {
const auto now = TaskTimePoint::clock::now();
const auto flutter_duration =
const int64_t flutter_duration =
flutter_target_time_nanos - FlutterEngineGetCurrentTime();
return now + std::chrono::nanoseconds(flutter_duration);
}