From 011b9da48ddc11062ca058670ac47947b3c220c0 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Thu, 30 Oct 2025 12:45:04 -0400 Subject: Make window->wake() thread-safe Signed-off-by: Andrew Opalach --- src/poll_common.c | 22 ++++++++++------------ src/poll_common.h | 4 ++-- src/window_wayland.c | 4 ++-- src/window_x11.c | 4 ++-- 4 files changed, 16 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/poll_common.c b/src/poll_common.c index c5d43c4..0b8cb79 100644 --- a/src/poll_common.c +++ b/src/poll_common.c @@ -33,27 +33,25 @@ bool stl_poll_common(struct stl_window *window, struct nn_pollfd *fds, bool bloc return !window->closed; } #ifdef STELA_PAUSE -void stl_wake_common(struct stl_window *window, struct nn_pollfd *fds) +// wake() must be thread-safe. +void stl_wake_common(struct nn_pollfd *fds) { - window->pending_refresh = true; - // If we are already in poll, don't try to wake again instead - // just ensure we refresh. - if (!(fds[0].revents & POLLIN || fds[1].revents & POLLIN)) { - s64 val = 1; - ssize_t ret = nn_write(fds[1].fd, &val, sizeof(s64)); - al_assert(ret == sizeof(s64)); - } + s64 val = 1; + ssize_t ret = nn_write(fds[1].fd, &val, sizeof(s64)); + al_assert(ret == sizeof(s64)); } #endif #endif -bool stl_process_events_common(struct nn_pollfd *fds) +bool stl_process_events_common(struct stl_window *window, struct nn_pollfd *fds) { #ifdef STELA_POLL_INLINE #ifdef STELA_PAUSE if (fds[1].revents & POLLIN) { - s64 val = nn_read(fds[1].fd, &val, sizeof(s64)); - al_assert(val == sizeof(s64)); + s64 val; + ssize_t ret = nn_read(fds[1].fd, &val, sizeof(s64)); + al_assert(ret == sizeof(s64)); + window->pending_refresh = true; } #endif return (fds[0].revents & POLLIN); diff --git a/src/poll_common.h b/src/poll_common.h index 3ad6d5e..597fa73 100644 --- a/src/poll_common.h +++ b/src/poll_common.h @@ -7,7 +7,7 @@ #ifdef STELA_POLL_INLINE bool stl_poll_common(struct stl_window *window, struct nn_pollfd *fds, bool block); #ifdef STELA_PAUSE -void stl_wake_common(struct stl_window *window, struct nn_pollfd *fds); +void stl_wake_common(struct nn_pollfd *fds); #endif #endif -bool stl_process_events_common(struct nn_pollfd *fds); +bool stl_process_events_common(struct stl_window *window, struct nn_pollfd *fds); diff --git a/src/window_wayland.c b/src/window_wayland.c index f4ceb90..4591827 100644 --- a/src/window_wayland.c +++ b/src/window_wayland.c @@ -1117,7 +1117,7 @@ static bool window_wayland_poll(struct stl_window *window, bool block) static void window_wayland_wake(struct stl_window *window) { struct stl_window_wayland *wl = (struct stl_window_wayland *)window; - stl_wake_common(&wl->w, wl->fds); + stl_wake_common(wl->fds); } #endif #endif @@ -1125,7 +1125,7 @@ static void window_wayland_wake(struct stl_window *window) static void window_wayland_process_events(struct stl_window *window) { struct stl_window_wayland *wl = (struct stl_window_wayland *)window; - if (stl_process_events_common(wl->fds)) { + if (stl_process_events_common(&wl->w, wl->fds)) { wl_display_read_events(wl->display); wl_display_dispatch_pending(wl->display); } else { diff --git a/src/window_x11.c b/src/window_x11.c index 4f399c2..2f84f19 100644 --- a/src/window_x11.c +++ b/src/window_x11.c @@ -369,7 +369,7 @@ static bool window_x11_poll(struct stl_window *window, bool block) static void window_x11_wake(struct stl_window *window) { struct stl_window_x11 *xw = (struct stl_window_x11 *)window; - stl_wake_common(&xw->w, xw->fds); + stl_wake_common(xw->fds); } #endif #endif @@ -595,7 +595,7 @@ static void handle_next_xevent(struct stl_window_x11 *xw) static void window_x11_process_events(struct stl_window *window) { struct stl_window_x11 *xw = (struct stl_window_x11 *)window; - stl_process_events_common(xw->fds); + stl_process_events_common(&xw->w, xw->fds); // It seems for some reason POLLIN doesn't get set even when there are events. XPending(global.x11_d); while (QLength(global.x11_d) > 0) { -- cgit v1.2.3-101-g0448