Skip to content

Commit

Permalink
Fix continuous event polling in the GLFW event loop
Browse files Browse the repository at this point in the history
* 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 flutter/flutter#40281
  • Loading branch information
jason-simmons committed Sep 17, 2019
1 parent 7ab6956 commit 8789dd4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion shell/platform/glfw/flutter_glfw.cc
Expand Up @@ -740,7 +740,7 @@ bool FlutterDesktopRunWindowEventLoopWithTimeout(
FlutterDesktopWindowControllerRef controller,
uint32_t timeout_milliseconds) {
auto wait_duration = timeout_milliseconds == 0
? std::chrono::milliseconds::max()
? std::chrono::nanoseconds::max()
: std::chrono::milliseconds(timeout_milliseconds);
controller->event_loop->WaitForEvents(wait_duration);

Expand Down
11 changes: 7 additions & 4 deletions shell/platform/glfw/glfw_event_loop.cc
Expand Up @@ -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));
Expand All @@ -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);
}
Expand Down

0 comments on commit 8789dd4

Please sign in to comment.