[client] wayland: marshal resize requests to the event-loop thread

setWindowSize is called from the render, frame and transport threads,
but shellResize issues shell requests and, with the libdecor GTK
plugin, renders the window decorations through pango, none of which is
thread safe. A frame thread exiting after such a call crashed in the
pango fontmap destructor during thread teardown.

Publish the requested size atomically and wake the event loop through
an eventfd registered in the epoll; the poll callback applies the size
on the event-loop thread. Concurrent requests coalesce into the most
recent one.
This commit is contained in:
Amit Mendapara
2026-08-11 18:11:03 +05:30
committed by Geoffrey McRae
parent 574e8edbae
commit c66a974f1d
3 changed files with 67 additions and 1 deletions

View File

@@ -23,6 +23,10 @@
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <unistd.h>
#include <wayland-client.h>
#include "app.h"
@@ -132,6 +136,29 @@ static const struct wp_fractional_scale_v1_listener fractionalScaleListener = {
.preferred_scale = fractionalScalePreferredScale,
};
static void resizePollCallback(uint32_t events, void * opaque)
{
eventfd_t value;
eventfd_read(wlWm.resizeEventFd, &value);
const uint64_t pending = atomic_exchange_explicit(&wlWm.pendingResize, 0,
memory_order_acquire);
if (!pending || !app_isRunning())
return;
wlWm.desktop->shellResize((int)(pending >> 32), (int)(uint32_t)pending);
}
static void resizeEventFdFree(void)
{
if (wlWm.resizeEventFd < 0)
return;
waylandPollUnregister(wlWm.resizeEventFd);
close(wlWm.resizeEventFd);
wlWm.resizeEventFd = -1;
}
bool waylandWindowInit(const char * title, const char * appId, bool fullscreen, bool maximize, bool borderless, bool resizable)
{
wlWm.scale = waylandScaleFromInt(1);
@@ -142,11 +169,29 @@ bool waylandWindowInit(const char * title, const char * appId, bool fullscreen,
DEBUG_ERROR("Failed to initialize event for waitFrame");
return false;
}
waylandSignalFrame(LG_DS_WAIT_FRAME_INTERRUPTED);
wlWm.resizeEventFd = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
if (wlWm.resizeEventFd < 0)
{
DEBUG_ERROR("Failed to create the resize eventfd: %s", strerror(errno));
return false;
}
if (!waylandPollRegister(wlWm.resizeEventFd, resizePollCallback, NULL,
EPOLLIN))
{
DEBUG_ERROR("Failed to register the resize eventfd");
close(wlWm.resizeEventFd);
wlWm.resizeEventFd = -1;
return false;
}
if (!wlWm.compositor)
{
DEBUG_ERROR("Compositor missing wl_compositor (version 3+), will not proceed");
resizeEventFdFree();
return false;
}
@@ -154,6 +199,7 @@ bool waylandWindowInit(const char * title, const char * appId, bool fullscreen,
if (!wlWm.surface)
{
DEBUG_ERROR("Failed to create wl_surface");
resizeEventFdFree();
return false;
}
@@ -176,7 +222,10 @@ bool waylandWindowInit(const char * title, const char * appId, bool fullscreen,
if (!wlWm.desktop->shellInit(wlWm.display, wlWm.surface,
title, appId, fullscreen, maximize, borderless, resizable))
{
resizeEventFdFree();
return false;
}
INTERLOCKED_SECTION(wlWm.surfaceLock,
{
@@ -190,6 +239,7 @@ bool waylandWindowInit(const char * title, const char * appId, bool fullscreen,
if (wl_display_roundtrip(wlWm.display) < 0)
{
DEBUG_ERROR("Failed waiting for the initial Wayland configure");
resizeEventFdFree();
return false;
}
}
@@ -199,6 +249,8 @@ bool waylandWindowInit(const char * title, const char * appId, bool fullscreen,
void waylandWindowFree(void)
{
resizeEventFdFree();
struct SurfaceOutput * output;
struct SurfaceOutput * temp;
wl_list_for_each_safe(output, temp, &wlWm.surfaceOutputs, link)
@@ -217,7 +269,10 @@ void waylandWindowFree(void)
void waylandSetWindowSize(int x, int y)
{
wlWm.desktop->shellResize(x, y);
// The shell resize must run on the event-loop thread; see resizePollCallback
atomic_store_explicit(&wlWm.pendingResize,
(uint64_t)(uint32_t)x << 32 | (uint32_t)y, memory_order_release);
eventfd_write(wlWm.resizeEventFd, 1);
}
bool waylandIsValidPointerPos(int x, int y)