From nobody Sat Dec 28 21:58:00 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1734992264; cv=none; d=zohomail.com; s=zohoarc; b=Za0ZpgLeYJeAGiqKEQ477InWr3h+nAEGt7OK8gOTXT2OYdFYXTDCnRfMJ0P2V8MdOCHsaZTgx4DEB8Q82vQWX3f4/3iekHMph7ODHJnCcVGGJ/oBDUce4irOh2mnRMSgsQKzZHIyXDxxIj34g8XUPP+z5waUqgOlTQJrN25QSUg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1734992264; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Rr/XHRRV40edbEmNfwT52ZokvXLkO84xfJOIJy0vMbQ=; b=EbQUKuleYqLsJavRVAlKZUlzw+hYbLpZKeZEm3cnPARVoh3WJkrhLtE2npnj9aXaU/GUV0FKkBNMQ8tJuEIRrKHz8SSHP/q90cYx3+Wi7MrmEWqDPBDVqMMbjeP4Ax3kCKrVTEGt4eA5hBQDgBmbwOJ1LOG3xuvrjra2y4SGuWk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1734992263801965.2699601466003; Mon, 23 Dec 2024 14:17:43 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tPqjb-0001zI-My; Mon, 23 Dec 2024 17:17:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tPqjU-0001w7-6u for qemu-devel@nongnu.org; Mon, 23 Dec 2024 17:17:00 -0500 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tPqjQ-0006tn-Ob for qemu-devel@nongnu.org; Mon, 23 Dec 2024 17:16:59 -0500 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-385ddcfc97bso3999852f8f.1 for ; Mon, 23 Dec 2024 14:16:55 -0800 (PST) Received: from localhost.localdomain (h082218084190.host.wavenet.at. [82.218.84.190]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-436611fc161sm147561075e9.10.2024.12.23.14.16.52 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Mon, 23 Dec 2024 14:16:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=philjordan-eu.20230601.gappssmtp.com; s=20230601; t=1734992215; x=1735597015; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Rr/XHRRV40edbEmNfwT52ZokvXLkO84xfJOIJy0vMbQ=; b=NRvMTiq1NMkzitY5DONdFrMIVJH9ic0vX8To9bOjTnKfz9L/BliUlZ6WPpxzKaNL9W odgYmyZfGBTbUEKBLuT5SCF8cHM09Aog4jiXsVTdJqtaXe63y13bisDAnLg5Ry8M6cMQ /f2GBoAG+uquOV7ciRaJcr3rUswDJ4STDPY4S+gle3W3JTMr2v3JGUyohBQjaa0OrHtt qHu89tBknx8BGhCjuhqax7ezMWXKTFUB8GEFyJrakRrBWY4wI+UYc2nP4EYTlsYz8uV+ Q7IyNSzUueDE7VGTb0jizLFDQ1B0/mKKzqQy7YifYk7HGFwD9kP41cBGm6wc2W2+tJty f4Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734992215; x=1735597015; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Rr/XHRRV40edbEmNfwT52ZokvXLkO84xfJOIJy0vMbQ=; b=ey4AHsNkrioFqlNso6DSU5jJQBX+hFsZCTXD+YzqoVNQ5lnWxUPHcDjhVPOTqM2VRz sjUlDp35ZraDhGs5Dt21mB0um5KqnQHm5/CWRxK7O+/49d0ANPZMaVu/rxUPiFcdS5kY Oz+BXfGc2vUpCOaNqgPfLbHb5DlQD1h0ivtooeRsFuu0hD/l/KrRFDpvaGS7f8UQR+x0 4mxNxGxHdEH4X8iOiIryim/52okKZffxE3x7w5lzzWHVz2KDw14A+dLafU43iR29pIAE yOg2F/UoJNW8Ua0NkXsR8r9D2k4PkJrw9G8fDRwzZa80gBERutInxpbjQ87O1kox+AVx DdGg== X-Gm-Message-State: AOJu0YyTjhiYMKF9asRWC/mqpPqdLiufJQrniE06Z4bKbq1BjMNKWtXX V32XqJ1Q3kmI33DUyrSCgr/GuD74Aw1sbEf7C1n32ptAX/eqQgMBF/Krg0gjScCR6+FV/iGVYa9 o5g== X-Gm-Gg: ASbGncuQokoybpGyawhBDLhk1zjBhNFplSRdf7j75AOjquvbHXDu6/SSOPpabVS4EBT cQfMPk4yNvfKu3rUUwG/3uh8yDpA4p/xJSAtx9hMoTIxBGjvv5tf2Co4lMjqaeJYUvW8Tk+3AHp dzTHVT9OIqRCcDJuuJw9psUU3ePmw9X8+s+T/vAKuMIoDetQQ14YfKutTqecAlZ/DJC7qsYMNRL nJp8FaxNTO3UpqMNvJRY6BdRLPQHHstVlaMvhA8ArWJOYZ7eD2wQKOVgBqFVJYALwk2gi2AEBMA zBPGn1FTG6mgwBWGOFF5Ph33HA/NEWOA X-Google-Smtp-Source: AGHT+IFCrVYpHpigy8WBr8TGesn+9+isM/XlYIDcImV48bLt+rDmU70XUHdMMeeJC/5q23eIINgXYg== X-Received: by 2002:adf:a452:0:b0:38a:25d3:ba9e with SMTP id ffacd0b85a97d-38a2d04408fmr4504831f8f.2.1734992214698; Mon, 23 Dec 2024 14:16:54 -0800 (PST) From: Phil Dennis-Jordan To: qemu-devel@nongnu.org Cc: agraf@csgraf.de, phil@philjordan.eu, peter.maydell@linaro.org, pbonzini@redhat.com, rad@semihalf.com, quic_llindhol@quicinc.com, stefanha@redhat.com, mst@redhat.com, slp@redhat.com, richard.henderson@linaro.org, eduardo@habkost.net, marcel.apfelbaum@gmail.com, gaosong@loongson.cn, jiaxun.yang@flygoat.com, chenhuacai@kernel.org, kwolf@redhat.com, hreitz@redhat.com, philmd@linaro.org, shorne@gmail.com, palmer@dabbelt.com, alistair.francis@wdc.com, bmeng.cn@gmail.com, liwei1518@gmail.com, dbarboza@ventanamicro.com, zhiwei_liu@linux.alibaba.com, jcmvbkbc@gmail.com, marcandre.lureau@redhat.com, berrange@redhat.com, akihiko.odaki@daynix.com, qemu-arm@nongnu.org, qemu-block@nongnu.org, qemu-riscv@nongnu.org, balaton@eik.bme.hu Subject: [PATCH v16 01/14] ui & main loop: Redesign of system-specific main thread event handling Date: Mon, 23 Dec 2024 23:16:32 +0100 Message-Id: <20241223221645.29911-2-phil@philjordan.eu> X-Mailer: git-send-email 2.39.5 (Apple Git-154) In-Reply-To: <20241223221645.29911-1-phil@philjordan.eu> References: <20241223221645.29911-1-phil@philjordan.eu> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: neutral client-ip=2a00:1450:4864:20::435; envelope-from=phil@philjordan.eu; helo=mail-wr1-x435.google.com X-Spam_score_int: -10 X-Spam_score: -1.1 X-Spam_bar: - X-Spam_report: (-1.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NEUTRAL=0.779 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @philjordan-eu.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1734992268360116600 Content-Type: text/plain; charset="utf-8" macOS's Cocoa event handling must be done on the initial (main) thread of the process. Furthermore, if library or application code uses libdispatch, the main dispatch queue must be handling events on the main thread as well. So far, this has affected Qemu in both the Cocoa and SDL UIs, although in different ways: the Cocoa UI replaces the default qemu_main function with one that spins Qemu's internal main event loop off onto a background thread. SDL (which uses Cocoa internally) on the other hand uses a polling approach within Qemu's main event loop. Events are polled during the SDL UI's dpy_refresh callback, which happens to run on the main thread by default. As UIs are mutually exclusive, this works OK as long as nothing else needs platform-native event handling. In the next patch, a new device is introduced based on the ParavirtualizedGraphics.framework in macOS. This uses libdispatch internally, and only works when events are being handled on the main runloop. With the current system, it works when using either the Cocoa or the SDL UI. However, it does not when running headless. Moreover, any attempt to install a similar scheme to the Cocoa UI's main thread replacement fails when combined with the SDL UI. This change tidies up main thread management to be more flexible. * The qemu_main global function pointer is a custom function for the main thread, and it may now be NULL. When it is, the main thread runs the main Qemu loop. This represents the traditional setup. * When non-null, spawning the main Qemu event loop on a separate thread is now done centrally rather than inside the Cocoa UI code. * For most platforms, qemu_main is indeed NULL by default, but on Darwin, it defaults to a function that runs the CFRunLoop. * The Cocoa UI sets qemu_main to a function which runs the NSApplication event handling runloop, as is usual for a Cocoa app. * The SDL UI overrides the qemu_main function to NULL, thus specifying that Qemu's main loop must run on the main thread. * The GTK UI also overrides the qemu_main function to NULL. * For other UIs, or in the absence of UIs, the platform's default behaviour is followed. This means that on macOS, the platform's runloop events are always handled, regardless of chosen UI. The new PV graphics device will thus work in all configurations. There is no functional change on other operating systems. Implementing this via a global function pointer variable is a bit ugly, but it's probably worth investigating the existing UI thread rule violations in the SDL (e.g. #2537) and GTK+ back-ends. Fixing those issues might precipitate requirements similar but not identical to those of the Cocoa UI; hopefully we'll see some kind of pattern emerge, which can then be used as a basis for an overhaul. (In fact, it may turn out to be simplest to split the UI/native platform event thread from the QEMU main event loop on all platforms, with any UI or even none at all.) Signed-off-by: Phil Dennis-Jordan Reviewed-by: Akihiko Odaki Tested-by: Akihiko Odaki --- v5: * Simplified the way of setting/clearing the main loop by going back to setting qemu_main directly, but narrowing the scope of what it needs to do, and it can now be NULL. v6: * Folded function qemu_run_default_main_on_new_thread's code into main() * Removed whitespace changes left over on lines near code removed between v4 and v5 v9: * Set qemu_main to NULL for GTK UI as well. v10: * Added comments clarifying the functionality and purpose of qemu_main. v11: * Removed the qemu_main_fn typedef again. * Consolidation of main, qemu_default_main, and call_qemu_default_main so that the latter has been eliminated altogether. * Reinstated the #include directive, added comment saying why it's needed. * Improved the comment on the qemu_main global variable. * Expanded the commit message. v12: * More precise wording of code comments. v16: * Fixed merge conflict with recent sysemu->system path rename. include/qemu-main.h | 14 +++++++++++- system/main.c | 37 +++++++++++++++++++++++++++---- ui/cocoa.m | 54 +++++++++++---------------------------------- ui/gtk.c | 4 ++++ ui/sdl2.c | 4 ++++ 5 files changed, 67 insertions(+), 46 deletions(-) diff --git a/include/qemu-main.h b/include/qemu-main.h index 940960a7db..2ee83bedff 100644 --- a/include/qemu-main.h +++ b/include/qemu-main.h @@ -5,7 +5,19 @@ #ifndef QEMU_MAIN_H #define QEMU_MAIN_H =20 -int qemu_default_main(void); +/* + * The function to run on the main (initial) thread of the process. + * NULL means QEMU's main event loop. + * When non-NULL, QEMU's main event loop will run on a purposely created + * thread, after which the provided function pointer will be invoked on + * the initial thread. + * This is useful on platforms which treat the main thread as special + * (macOS/Darwin) and/or require all UI API calls to occur from the main + * thread. Those platforms can initialise it to a specific function, + * while UI implementations may reset it to NULL during their init if they + * will handle system and UI events on the main thread via QEMU's own main + * event loop. + */ extern int (*qemu_main)(void); =20 #endif /* QEMU_MAIN_H */ diff --git a/system/main.c b/system/main.c index 4923520741..9c9a1adc20 100644 --- a/system/main.c +++ b/system/main.c @@ -24,26 +24,55 @@ =20 #include "qemu/osdep.h" #include "qemu-main.h" +#include "qemu/main-loop.h" #include "system/system.h" =20 #ifdef CONFIG_SDL +/* + * SDL insists on wrapping the main() function with its own implementation= on + * some platforms; it does so via a macro that renames our main function, = so + * must be #included here even with no SDL code called from this f= ile. + */ #include #endif =20 -int qemu_default_main(void) +#ifdef CONFIG_DARWIN +#include +#endif + +static void *qemu_default_main(void *opaque) { int status; =20 + bql_lock(); status =3D qemu_main_loop(); qemu_cleanup(status); + bql_unlock(); =20 - return status; + exit(status); } =20 -int (*qemu_main)(void) =3D qemu_default_main; +int (*qemu_main)(void); + +#ifdef CONFIG_DARWIN +static int os_darwin_cfrunloop_main(void) +{ + CFRunLoopRun(); + g_assert_not_reached(); +} +int (*qemu_main)(void) =3D os_darwin_cfrunloop_main; +#endif =20 int main(int argc, char **argv) { qemu_init(argc, argv); - return qemu_main(); + bql_unlock(); + if (qemu_main) { + QemuThread main_loop_thread; + qemu_thread_create(&main_loop_thread, "qemu_main", + qemu_default_main, NULL, QEMU_THREAD_DETACHED); + return qemu_main(); + } else { + qemu_default_main(NULL); + } } diff --git a/ui/cocoa.m b/ui/cocoa.m index 3a88535374..23b7a736d7 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -73,6 +73,8 @@ int height; } QEMUScreen; =20 +@class QemuCocoaPasteboardTypeOwner; + static void cocoa_update(DisplayChangeListener *dcl, int x, int y, int w, int h); =20 @@ -107,6 +109,7 @@ static void cocoa_switch(DisplayChangeListener *dcl, static NSInteger cbchangecount =3D -1; static QemuClipboardInfo *cbinfo; static QemuEvent cbevent; +static QemuCocoaPasteboardTypeOwner *cbowner; =20 // Utility functions to run specified code block with the BQL held typedef void (^CodeBlock)(void); @@ -1326,8 +1329,10 @@ - (void) dealloc { COCOA_DEBUG("QemuCocoaAppController: dealloc\n"); =20 - if (cocoaView) - [cocoaView release]; + [cocoaView release]; + [cbowner release]; + cbowner =3D nil; + [super dealloc]; } =20 @@ -1943,8 +1948,6 @@ - (void)pasteboard:(NSPasteboard *)sender provideData= ForType:(NSPasteboardType)t =20 @end =20 -static QemuCocoaPasteboardTypeOwner *cbowner; - static void cocoa_clipboard_notify(Notifier *notifier, void *data); static void cocoa_clipboard_request(QemuClipboardInfo *info, QemuClipboardType type); @@ -2007,43 +2010,8 @@ static void cocoa_clipboard_request(QemuClipboardInf= o *info, } } =20 -/* - * The startup process for the OSX/Cocoa UI is complicated, because - * OSX insists that the UI runs on the initial main thread, and so we - * need to start a second thread which runs the qemu_default_main(): - * in main(): - * in cocoa_display_init(): - * assign cocoa_main to qemu_main - * create application, menus, etc - * in cocoa_main(): - * create qemu-main thread - * enter OSX run loop - */ - -static void *call_qemu_main(void *opaque) -{ - int status; - - COCOA_DEBUG("Second thread: calling qemu_default_main()\n"); - bql_lock(); - status =3D qemu_default_main(); - bql_unlock(); - COCOA_DEBUG("Second thread: qemu_default_main() returned, exiting\n"); - [cbowner release]; - exit(status); -} - static int cocoa_main(void) { - QemuThread thread; - - COCOA_DEBUG("Entered %s()\n", __func__); - - bql_unlock(); - qemu_thread_create(&thread, "qemu_main", call_qemu_main, - NULL, QEMU_THREAD_DETACHED); - - // Start the main event loop COCOA_DEBUG("Main thread: entering OSX run loop\n"); [NSApp run]; COCOA_DEBUG("Main thread: left OSX run loop, which should never happen= \n"); @@ -2125,8 +2093,6 @@ static void cocoa_display_init(DisplayState *ds, Disp= layOptions *opts) =20 COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n"); =20 - qemu_main =3D cocoa_main; - // Pull this console process up to being a fully-fledged graphical // app with a menubar and Dock icon ProcessSerialNumber psn =3D { 0, kCurrentProcess }; @@ -2190,6 +2156,12 @@ static void cocoa_display_init(DisplayState *ds, Dis= playOptions *opts) qemu_clipboard_peer_register(&cbpeer); =20 [pool release]; + + /* + * The Cocoa UI will run the NSApplication runloop on the main thread + * rather than the default Core Foundation one. + */ + qemu_main =3D cocoa_main; } =20 static QemuDisplay qemu_display_cocoa =3D { diff --git a/ui/gtk.c b/ui/gtk.c index 0d38c070e4..c023743148 100644 --- a/ui/gtk.c +++ b/ui/gtk.c @@ -38,6 +38,7 @@ #include "qemu/cutils.h" #include "qemu/error-report.h" #include "qemu/main-loop.h" +#include "qemu-main.h" =20 #include "ui/console.h" #include "ui/gtk.h" @@ -2485,6 +2486,9 @@ static void gtk_display_init(DisplayState *ds, Displa= yOptions *opts) #ifdef CONFIG_GTK_CLIPBOARD gd_clipboard_init(s); #endif /* CONFIG_GTK_CLIPBOARD */ + + /* GTK's event polling must happen on the main thread. */ + qemu_main =3D NULL; } =20 static void early_gtk_display_init(DisplayOptions *opts) diff --git a/ui/sdl2.c b/ui/sdl2.c index 1fb72f67a6..445eb1dd9f 100644 --- a/ui/sdl2.c +++ b/ui/sdl2.c @@ -34,6 +34,7 @@ #include "system/system.h" #include "ui/win32-kbd-hook.h" #include "qemu/log.h" +#include "qemu-main.h" =20 static int sdl2_num_outputs; static struct sdl2_console *sdl2_console; @@ -965,6 +966,9 @@ static void sdl2_display_init(DisplayState *ds, Display= Options *o) } =20 atexit(sdl_cleanup); + + /* SDL's event polling (in dpy_refresh) must happen on the main thread= . */ + qemu_main =3D NULL; } =20 static QemuDisplay qemu_display_sdl2 =3D { --=20 2.39.5 (Apple Git-154)