From f6b03f106c0cd2a225b528c972840204103e58a6 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Tue, 14 Jan 2020 19:42:15 +0100 Subject: [PATCH 1/5] wayland: Cancel display read before abortOnFatalError Calling wl_display_cancel_read immediately ensures that other readers waiting on us will have a chance to wake up and discover the error in a timely manner. --- glfw/wl_window.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glfw/wl_window.c b/glfw/wl_window.c index 505d84829..cdc6ef0cf 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -782,8 +782,8 @@ handleEvents(monotonic_t timeout) errno = 0; if (wl_display_flush(display) < 0 && errno != EAGAIN) { - abortOnFatalError(errno); wl_display_cancel_read(display); + abortOnFatalError(errno); return; } From 4acab650160f38d0d7c7121cf8f252c6a4d6c4ae Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Tue, 14 Jan 2020 19:42:51 +0100 Subject: [PATCH 2/5] wayland: Only cancel display read after prepare success The display reader count only increments when wl_display_prepare_read succeeds. Calling wl_display_cancel_read when wl_display_prepare_read has not succeeded results in a negative reader count, which does not have well-defined behavior. --- glfw/wl_window.c | 1 - 1 file changed, 1 deletion(-) diff --git a/glfw/wl_window.c b/glfw/wl_window.c index cdc6ef0cf..e82fda793 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -768,7 +768,6 @@ handleEvents(monotonic_t timeout) if (num_dispatched < 0) { if (errno == EAGAIN) continue; int last_error = wl_display_get_error(display); - wl_display_cancel_read(display); if (last_error) abortOnFatalError(last_error); return; } From bc24716476187453e61038eee6ed24f3d0821733 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Tue, 14 Jan 2020 19:54:48 +0100 Subject: [PATCH 3/5] wayland: errno does not have to be cleared As long as errno is only read on error from an errno-setting function, then there is no need to reset errno between uses. --- glfw/wl_window.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/glfw/wl_window.c b/glfw/wl_window.c index e82fda793..61b407dd8 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -763,7 +763,6 @@ handleEvents(monotonic_t timeout) while (wl_display_prepare_read(display) != 0) { while(1) { - errno = 0; int num_dispatched = wl_display_dispatch_pending(display); if (num_dispatched < 0) { if (errno == EAGAIN) continue; @@ -778,7 +777,6 @@ handleEvents(monotonic_t timeout) // If an error different from EAGAIN happens, we have likely been // disconnected from the Wayland session, try to handle that the best we // can. - errno = 0; if (wl_display_flush(display) < 0 && errno != EAGAIN) { wl_display_cancel_read(display); From 083b294659b33a43815b1db384ccac446f55efe0 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Tue, 14 Jan 2020 19:47:17 +0100 Subject: [PATCH 4/5] wayland: Simplify dispatch_pending error handling wl_display_dispatch_pending does not return EAGAIN, and always sets an error when it fails. This allows us to handle errors in a simpler manner. --- glfw/wl_window.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/glfw/wl_window.c b/glfw/wl_window.c index 61b407dd8..a31a37dad 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -762,15 +762,9 @@ handleEvents(monotonic_t timeout) EVDBG("starting handleEvents(%.2f)", monotonic_t_to_s_double(timeout)); while (wl_display_prepare_read(display) != 0) { - while(1) { - int num_dispatched = wl_display_dispatch_pending(display); - if (num_dispatched < 0) { - if (errno == EAGAIN) continue; - int last_error = wl_display_get_error(display); - if (last_error) abortOnFatalError(last_error); - return; - } - break; + if (wl_display_dispatch_pending(display) == -1) { + abortOnFatalError(wl_display_get_error(display)); + return; } } From ca7cab1a2d9f7d0e0ac2c1152251bde7ee8d10f5 Mon Sep 17 00:00:00 2001 From: Kenny Levinsen Date: Tue, 14 Jan 2020 20:06:13 +0100 Subject: [PATCH 5/5] wayland: Consistently use errno for errors --- glfw/wl_window.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/glfw/wl_window.c b/glfw/wl_window.c index a31a37dad..3d16da8ae 100644 --- a/glfw/wl_window.c +++ b/glfw/wl_window.c @@ -763,7 +763,7 @@ handleEvents(monotonic_t timeout) while (wl_display_prepare_read(display) != 0) { if (wl_display_dispatch_pending(display) == -1) { - abortOnFatalError(wl_display_get_error(display)); + abortOnFatalError(errno); return; } }