From 754179dacd4c8cbd06d6c646cda9c5e23edcb707 Mon Sep 17 00:00:00 2001 From: Simon Ser Date: Fri, 6 Nov 2020 13:37:26 +0100 Subject: [PATCH] backend/session: add a timeout waiting for GPUs If a GPU doesn't show up in 10s, bail out and return zero GPUs. --- backend/session/session.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/backend/session/session.c b/backend/session/session.c index a25e78f2..b58bd967 100644 --- a/backend/session/session.c +++ b/backend/session/session.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -17,6 +18,8 @@ #include "backend/session/session.h" #include "util/signal.h" +#define WAIT_GPU_TIMEOUT 10000 // ms + extern const struct session_impl session_libseat; extern const struct session_impl session_logind; extern const struct session_impl session_direct; @@ -322,6 +325,12 @@ static struct udev_enumerate *enumerate_drm_cards(struct udev *udev) { return en; } +static uint64_t get_current_time_ms(void) { + struct timespec ts = {0}; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; +} + struct find_gpus_add_handler { bool added; struct wl_listener listener; @@ -356,16 +365,24 @@ size_t wlr_session_find_gpus(struct wlr_session *session, handler.listener.notify = find_gpus_handle_add; wl_signal_add(&session->events.add_drm_card, &handler.listener); + uint64_t started_at = get_current_time_ms(); + uint64_t timeout = WAIT_GPU_TIMEOUT; struct wl_event_loop *event_loop = wl_display_get_event_loop(session->display); while (!handler.added) { - int ret = wl_event_loop_dispatch(event_loop, -1); + int ret = wl_event_loop_dispatch(event_loop, (int)timeout); if (ret < 0) { wlr_log_errno(WLR_ERROR, "Failed to wait for DRM card device: " "wl_event_loop_dispatch failed"); udev_enumerate_unref(en); return -1; } + + uint64_t now = get_current_time_ms(); + if (now >= started_at + WAIT_GPU_TIMEOUT) { + break; + } + timeout = started_at + WAIT_GPU_TIMEOUT - now; } wl_list_remove(&handler.listener.link);