On 17/3/22 12:56, Philippe Mathieu-Daudé wrote:
From: Paolo Bonzini <pbonz...@redhat.com>

Simplify the initialization dance by running qemu_init() in the main
thread before the Cocoa event loop starts.  The cocoa_display_init()
code that is post-applicationDidFinishLaunching: moves to the
application delegate itself, and the secondary thread only runs
the rest of qemu_main(), namely qemu_main_loop() and qemu_cleanup().

This fixes a case where addRemovableDevicesMenuItems() calls
qmp_query_block() while expecting the main thread to still hold
the BQL.  The newly-introduced assertions in the block layer
complain about this.

Signed-off-by: Paolo Bonzini <pbonz...@redhat.com>
Message-Id: <20220307151004.578069-1-pbonz...@redhat.com>
[PMD: Fixed trivial build failures & rebased]
Signed-off-by: Philippe Mathieu-Daudé <f4...@amsat.org>
---
  softmmu/main.c |  12 +++---
  ui/cocoa.m     | 114 ++++++++++++++++++++-----------------------------
  2 files changed, 54 insertions(+), 72 deletions(-)

diff --git a/ui/cocoa.m b/ui/cocoa.m
index 027c3053f7..2e5130d805 100644
--- a/ui/cocoa.m
+++ b/ui/cocoa.m
@@ -100,14 +100,13 @@ static int last_buttons;
  static int cursor_hide = 1;
  static int left_command_key_enabled = 1;
  static bool swap_opt_cmd;
+static bool full_screen;
+static bool full_grab;
+static bool have_cocoa_ui;
-static int gArgc;
-static char **gArgv;
  static bool stretch_video;
  static NSTextField *pauseLabel;
-static QemuSemaphore display_init_sem;
-static QemuSemaphore app_started_sem;
  static bool allow_events;
static NSInteger cbchangecount = -1;
@@ -115,6 +114,7 @@ static QemuClipboardInfo *cbinfo;
  static QemuClipboardPeer cbpeer;
  static QemuCocoaPasteboardTypeOwner *cbowner;
  static QemuEvent cbevent;
+static QemuThread main_thread;

This variable doesn't need to be in global scope, I'll
move it to applicationDidFinishLaunching().

@@ -1323,8 +1310,22 @@ static CGEventRef handleTapEvent(CGEventTapProxy proxy, 
CGEventType type, CGEven
  {
      COCOA_DEBUG("QemuCocoaAppController: applicationDidFinishLaunching\n");
      allow_events = true;
-    /* Tell cocoa_display_init to proceed */
-    qemu_sem_post(&app_started_sem);
+
+    // register vga output callbacks
+    register_displaychangelistener(&dcl);
+
+    qemu_clipboard_peer_register(&cbpeer);
+    qemu_mutex_unlock_iothread();
+    qemu_thread_create(&main_thread, "qemu_main_loop", call_qemu_main_loop,
+                       NULL, QEMU_THREAD_DETACHED);
+
+    if (full_screen) {
+        [NSApp activateIgnoringOtherApps: YES];
+        [self toggleFullScreen: nil];
+    }
+    if (full_grab) {
+        [self setFullGrab: nil];
+    }
  }

Reply via email to