Skip to contentSkip to conversation

cocoa: run qemu_init in the main thread

RFC Superseded Philippe Mathieu-Daudé 4 participants 9 messages Mar 16, 2022 – Mar 17, 2022

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

RFC v2 1/1 cocoa: run qemu_init in the main thread

From: Paolo Bonzini <[email protected]>

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.

Message-Id: <[email protected]>

[PMD: Fixed trivial build failures & rebased]

---
Cc: Akihiko Odaki <[email protected]>
Cc: Peter Maydell <[email protected]>
Cc: Emanuele Giuseppe Esposito <[email protected]>

v1: https://lore.kernel.org/qemu-devel/[email protected]/

---
 softmmu/main.c |  12 ++--
 ui/cocoa.m     | 153 ++++++++++++++++++++++---------------------------
 2 files changed, 74 insertions(+), 91 deletions(-)
softmmu/main.c +7 -5
index 639c67ff48..0c4384e980 100644
@@ -39,16 +39,18 @@ int main(int argc, char **argv)
 #endif
 #endif /* CONFIG_SDL */
 
-#ifdef CONFIG_COCOA
-#undef main
-#define main qemu_main
-#endif /* CONFIG_COCOA */
-
+#ifndef CONFIG_COCOA
 int main(int argc, char **argv, char **envp)
 {
+    /*
+     * ui/cocoa.m relies on this being the exact content of main(),
+     * because it has to run everything after qemu_init in a secondary
+     * thread.
+     */
     qemu_init(argc, argv, envp);
     qemu_main_loop();
     qemu_cleanup();
 
     return 0;
 }
+#endif /* CONFIG_COCOA */
ui/cocoa.m +67 -86

236 lines not on this page · raw message

Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · Philippe Mathieu-Daudé

From: Paolo Bonzini <[email protected]>

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().

Quoted 4 lines

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.

I was thinking that it may be better to let softmmu/main.c do the details if it involves the internals of qemu_main() like qemu_main_loop().

More concretely, softmmu/main.c would provide a function to register a function pointer to take over the main thread. main() implemented in softmmu/main.c would call qemu_init(). If a function pointer gets registered in qemu_init(), it would create a thread for main loop and call the registered function pointer. Otherwise, it would directly call qemu_main_loop().

It would be a semantically appropriate division of ui/cocoa.m and softmmu/main.c. It would also be beneficial for end-users as it would also allow to isolate ui/cocoa.m into a separate module when --enable-modules in the future. (With "In the future", I mean sometime when we have time to hack Meson build files and some details we cannot fill by 7.0.)

Regards,
Akihiko Odaki

Quoted 295 lines

Message-Id: <[email protected]>

[PMD: Fixed trivial build failures & rebased]

---
Cc: Akihiko Odaki <[email protected]>
Cc: Peter Maydell <[email protected]>
Cc: Emanuele Giuseppe Esposito <[email protected]>

v1: https://lore.kernel.org/qemu-devel/[email protected]/

---
  softmmu/main.c |  12 ++--
  ui/cocoa.m     | 153 ++++++++++++++++++++++---------------------------
  2 files changed, 74 insertions(+), 91 deletions(-)
softmmu/main.c +7 -5
index 639c67ff48..0c4384e980 100644
@@ -39,16 +39,18 @@ int main(int argc, char **argv)
  #endif
  #endif /* CONFIG_SDL */
 
-#ifdef CONFIG_COCOA
-#undef main
-#define main qemu_main
-#endif /* CONFIG_COCOA */
-
+#ifndef CONFIG_COCOA
  int main(int argc, char **argv, char **envp)
  {
+    /*
+     * ui/cocoa.m relies on this being the exact content of main(),
+     * because it has to run everything after qemu_init in a secondary
+     * thread.
+     */
      qemu_init(argc, argv, envp);
      qemu_main_loop();
      qemu_cleanup();
 
      return 0;
  }
+#endif /* CONFIG_COCOA */
ui/cocoa.m +67 -86
index cb6e7c41dc..e69ce97f44 100644
@@ -75,6 +75,9 @@ typedef struct {
      int height;
  } QEMUScreen;
 
+@interface QemuCocoaPasteboardTypeOwner : NSObject<NSPasteboardTypeOwner>
+@end
+
  static void cocoa_update(DisplayChangeListener *dcl,
                           int x, int y, int w, int h);
 
@@ -97,20 +100,23 @@ 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;
  static QemuClipboardInfo *cbinfo;
  static QemuEvent cbevent;
 
+static QemuCocoaPasteboardTypeOwner *cbowner;
+static QemuClipboardPeer cbpeer;
+static QemuThread main_thread;
+
  // Utility functions to run specified code block with iothread lock held
  typedef void (^CodeBlock)(void);
  typedef bool (^BoolCodeBlock)(void);
@@ -142,6 +148,33 @@ static bool bool_with_iothread_lock(BoolCodeBlock block)
      return val;
  }
 
+/*
+ * 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 qemu_main_loop():
+ *
+ * Initial thread:                    2nd thread:
+ * in main():
+ *  qemu_init()
+ *  create application, menus, etc
+ *  enter OSX run loop
+ * in applicationDidFinishLaunching:
+ *  fullscreen if needed
+ *  create main loop thread
+ *                                    call qemu_main_loop()
+ */
+
+static void *call_qemu_main_loop(void *opaque)
+{
+    COCOA_DEBUG("Second thread: calling qemu_main()\n");
+    qemu_mutex_lock_iothread();
+    qemu_main_loop();
+    COCOA_DEBUG("Second thread: qemu_main_loop() returned, exiting\n");
+    qemu_cleanup();
+    [cbowner release];
+    exit(0);
+}
+
  // Mac to QKeyCode conversion
  static const int mac_to_qkeycode_map[] = {
      [kVK_ANSI_A] = Q_KEY_CODE_A,
@@ -780,9 +813,7 @@ static CGEventRef handleTapEvent(CGEventTapProxy proxy, CGEventType type, CGEven
          /*
           * Just let OSX have all events that arrive before
           * applicationDidFinishLaunching.
-         * This avoids a deadlock on the iothread lock, which cocoa_display_init()
-         * will not drop until after the app_started_sem is posted. (In theory
-         * there should not be any such events, but OSX Catalina now emits some.)
+         * This may not be needed anymore?
           */
          return false;
      }
@@ -1280,8 +1311,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];
+    }
  }
 
  - (void)applicationWillTerminate:(NSNotification *)aNotification
@@ -1804,9 +1849,6 @@ static void addRemovableDevicesMenuItems(void)
      qapi_free_BlockInfoList(pointerToFree);
  }
 
-@interface QemuCocoaPasteboardTypeOwner : NSObject<NSPasteboardTypeOwner>
-@end
-
  @implementation QemuCocoaPasteboardTypeOwner
 
  - (void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSPasteboardType)type
@@ -1841,8 +1883,6 @@ static void addRemovableDevicesMenuItems(void)
 
  @end
 
-static QemuCocoaPasteboardTypeOwner *cbowner;
-
  static void cocoa_clipboard_notify(Notifier *notifier, void *data);
  static void cocoa_clipboard_request(QemuClipboardInfo *info,
                                      QemuClipboardType type);
@@ -1903,60 +1943,18 @@ static void cocoa_clipboard_request(QemuClipboardInfo *info,
      }
  }
 
-/*
- * 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 vl.c qemu_main():
- *
- * Initial thread:                    2nd thread:
- * in main():
- *  create qemu-main thread
- *  wait on display_init semaphore
- *                                    call qemu_main()
- *                                    ...
- *                                    in cocoa_display_init():
- *                                     post the display_init semaphore
- *                                     wait on app_started semaphore
- *  create application, menus, etc
- *  enter OSX run loop
- * in applicationDidFinishLaunching:
- *  post app_started semaphore
- *                                     tell main thread to fullscreen if needed
- *                                    [...]
- *                                    run qemu main-loop
- *
- * We do this in two stages so that we don't do the creation of the
- * GUI application menus and so on for command line options like --help
- * where we want to just print text to stdout and exit immediately.
- */
-
-static void *call_qemu_main(void *opaque)
+int main(int argc, char **argv, char **envp)
  {
-    int status;
-
-    COCOA_DEBUG("Second thread: calling qemu_main()\n");
-    status = qemu_main(gArgc, gArgv, *_NSGetEnviron());
-    COCOA_DEBUG("Second thread: qemu_main() returned, exiting\n");
-    [cbowner release];
-    exit(status);
-}
-
-int main (int argc, char **argv) {
-    QemuThread thread;
-
      COCOA_DEBUG("Entered main()\n");
-    gArgc = argc;
-    gArgv = argv;
+    qemu_event_init(&cbevent, false);
 
-    qemu_sem_init(&display_init_sem, 0);
-    qemu_sem_init(&app_started_sem, 0);
-
-    qemu_thread_create(&thread, "qemu_main", call_qemu_main,
-                       NULL, QEMU_THREAD_DETACHED);
-
-    COCOA_DEBUG("Main thread: waiting for display_init_sem\n");
-    qemu_sem_wait(&display_init_sem);
-    COCOA_DEBUG("Main thread: initializing app\n");
+    /* Takes iothread lock, released in applicationDidFinishLaunching:.  */
+    qemu_init(argc, argv, envp);
+    if (!have_cocoa_ui) {
+         qemu_main_loop();
+         qemu_cleanup();
+         return 0;
+    }
 
      NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
@@ -1978,6 +1976,7 @@ int main (int argc, char **argv) {
       */
      add_console_menu_entries();
      addRemovableDevicesMenuItems();
+    cbowner = [[QemuCocoaPasteboardTypeOwner alloc] init];
 
      // Create an Application controller
      QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
@@ -2071,24 +2070,13 @@ static void cocoa_refresh(DisplayChangeListener *dcl)
  static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
  {
      COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
+    have_cocoa_ui = 1;
 
-    /* Tell main thread to go ahead and create the app and enter the run loop */
-    qemu_sem_post(&display_init_sem);
-    qemu_sem_wait(&app_started_sem);
-    COCOA_DEBUG("cocoa_display_init: app start completed\n");
-
-    QemuCocoaAppController *controller = (QemuCocoaAppController *)[[NSApplication sharedApplication] delegate];
-    /* if fullscreen mode is to be used */
      if (opts->has_full_screen && opts->full_screen) {
-        dispatch_async(dispatch_get_main_queue(), ^{
-            [NSApp activateIgnoringOtherApps: YES];
-            [controller toggleFullScreen: nil];
-        });
+        full_screen = 1;
      }
      if (opts->u.cocoa.has_full_grab && opts->u.cocoa.full_grab) {
-        dispatch_async(dispatch_get_main_queue(), ^{
-            [controller setFullGrab: nil];
-        });
+        full_grab = 1;
      }
 
      if (opts->has_show_cursor && opts->show_cursor) {
@@ -2101,13 +2089,6 @@ static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
      if (opts->u.cocoa.has_left_command_key && !opts->u.cocoa.left_command_key) {
          left_command_key_enabled = 0;
      }
-
-    // register vga output callbacks
-    register_displaychangelistener(&dcl);
-
-    qemu_event_init(&cbevent, false);
-    cbowner = [[QemuCocoaPasteboardTypeOwner alloc] init];
-    qemu_clipboard_peer_register(&cbpeer);
  }
 
  static QemuDisplay qemu_display_cocoa = {
#2 lore raw
Quoted 7 lines · Akihiko Odaki

I was thinking that it may be better to let softmmu/main.c do the details if it involves the internals of qemu_main() like qemu_main_loop().

More concretely, softmmu/main.c would provide a function to register a function pointer to take over the main thread. main() implemented in softmmu/main.c would call qemu_init(). If a function pointer gets registered in qemu_init(), it would create a thread for main loop and call the registered function pointer. Otherwise, it would directly call qemu_main_loop().

It would be a semantically appropriate division of ui/cocoa.m and softmmu/main.c. It would also be beneficial for end-users as it would also allow to isolate ui/cocoa.m into a separate module when --enable-modules in the future. (With "In the future", I mean sometime
when we have time to hack Meson build files and some details we cannot fill by 7.0.)

I would like this for 7.1.

Basically rename qemu_main_loop to qemu_default_main_loop, and cocoa_display_init would do

    qemu_main_loop = qemu_cocoa_main_loop;

qemu_cocoa_main_loop would include the bulk of the current main of ui/cocoa.m. Seems like a good idea.

Paolo

#3 lore raw
Quoted 21 lines · Paolo Bonzini
Quoted 10 lines · Akihiko Odaki

I was thinking that it may be better to let softmmu/main.c do the details if it involves the internals of qemu_main() like qemu_main_loop().

More concretely, softmmu/main.c would provide a function to register a function pointer to take over the main thread. main() implemented in softmmu/main.c would call qemu_init(). If a function pointer gets registered in qemu_init(), it would create a thread for main loop and call the registered function pointer. Otherwise, it would directly call qemu_main_loop().

Quoted 6 lines

It would be a semantically appropriate division of ui/cocoa.m and softmmu/main.c. It would also be beneficial for end-users as it would also allow to isolate ui/cocoa.m into a separate module when --enable-modules in the future. (With "In the future", I mean sometime when we have time to hack Meson build files and some details we cannot fill by 7.0.)

I would like this for 7.1.

Quoted 7 lines

Basically rename qemu_main_loop to qemu_default_main_loop, and cocoa_display_init would do

     qemu_main_loop = qemu_cocoa_main_loop;

qemu_cocoa_main_loop would include the bulk of the current main of ui/cocoa.m. Seems like a good idea.

Speaking of 7.1, is cocoa currently completely broken, ie in need of an interim fix for 7.0 ? If so, which of the various patches/approaches should it be ?

thanks
-- PMM

#4 lore raw
Quoted 26 lines · Peter Maydell
Quoted 24 lines · Paolo Bonzini
Quoted 10 lines · Akihiko Odaki

I was thinking that it may be better to let softmmu/main.c do the details if it involves the internals of qemu_main() like qemu_main_loop().

More concretely, softmmu/main.c would provide a function to register a function pointer to take over the main thread. main() implemented in softmmu/main.c would call qemu_init(). If a function pointer gets registered in qemu_init(), it would create a thread for main loop and call the registered function pointer. Otherwise, it would directly call qemu_main_loop().

Quoted 6 lines

It would be a semantically appropriate division of ui/cocoa.m and softmmu/main.c. It would also be beneficial for end-users as it would also allow to isolate ui/cocoa.m into a separate module when --enable-modules in the future. (With "In the future", I mean sometime when we have time to hack Meson build files and some details we cannot fill by 7.0.)

I would like this for 7.1.

Basically rename qemu_main_loop to qemu_default_main_loop, and cocoa_display_init would do

Quoted 8 lines
Quoted 4 lines
     qemu_main_loop = qemu_cocoa_main_loop;

qemu_cocoa_main_loop would include the bulk of the current main of ui/cocoa.m. Seems like a good idea.

Speaking of 7.1, is cocoa currently completely broken, ie in need of an interim fix for 7.0 ? If so, which of the various patches/approaches should it be ?

To answer the first half of my question, yes, the cocoa UI is currently completely broken as it asserts on startup.

-- PMM

#5 lore raw
Quoted 6 lines · Peter Maydell
Quoted 3 lines

Speaking of 7.1, is cocoa currently completely broken, ie in need of an interim fix for 7.0 ? If so, which of the various patches/approaches should it be ?

To answer the first half of my question, yes, the cocoa UI is currently completely broken as it asserts on startup.

I think this patch is a good interim fix for 7.0 and an improvement in general. Any other changes can be deferred to 7.1. For example it's trivial for Cocoa not to block GTK+/SDL anymore, but no need to have that in 7.0. Same for removing main() from ui/cocoa.m.

Paolo

#6 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 274 lines · Philippe Mathieu-Daudé

From: Paolo Bonzini <[email protected]>

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.

Message-Id: <[email protected]>

[PMD: Fixed trivial build failures & rebased]

---
Cc: Akihiko Odaki <[email protected]>
Cc: Peter Maydell <[email protected]>
Cc: Emanuele Giuseppe Esposito <[email protected]>

v1: https://lore.kernel.org/qemu-devel/[email protected]/

---
  softmmu/main.c |  12 ++--
  ui/cocoa.m     | 153 ++++++++++++++++++++++---------------------------
  2 files changed, 74 insertions(+), 91 deletions(-)
softmmu/main.c +7 -5
index 639c67ff48..0c4384e980 100644
@@ -39,16 +39,18 @@ int main(int argc, char **argv)
  #endif
  #endif /* CONFIG_SDL */
 
-#ifdef CONFIG_COCOA
-#undef main
-#define main qemu_main
-#endif /* CONFIG_COCOA */
-
+#ifndef CONFIG_COCOA
  int main(int argc, char **argv, char **envp)
  {
+    /*
+     * ui/cocoa.m relies on this being the exact content of main(),
+     * because it has to run everything after qemu_init in a secondary
+     * thread.
+     */
      qemu_init(argc, argv, envp);
      qemu_main_loop();
      qemu_cleanup();
 
      return 0;
  }
+#endif /* CONFIG_COCOA */
ui/cocoa.m +65 -68
index cb6e7c41dc..e69ce97f44 100644
@@ -75,6 +75,9 @@ typedef struct {
      int height;
  } QEMUScreen;
 
+@interface QemuCocoaPasteboardTypeOwner : NSObject<NSPasteboardTypeOwner>
+@end
+
  static void cocoa_update(DisplayChangeListener *dcl,
                           int x, int y, int w, int h);
 
@@ -97,20 +100,23 @@ 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;
  static QemuClipboardInfo *cbinfo;
  static QemuEvent cbevent;
 
+static QemuCocoaPasteboardTypeOwner *cbowner;
+static QemuClipboardPeer cbpeer;
+static QemuThread main_thread;
+
  // Utility functions to run specified code block with iothread lock held
  typedef void (^CodeBlock)(void);
  typedef bool (^BoolCodeBlock)(void);
@@ -142,6 +148,33 @@ static bool bool_with_iothread_lock(BoolCodeBlock block)
      return val;
  }
 
+/*
+ * 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 qemu_main_loop():
+ *
+ * Initial thread:                    2nd thread:
+ * in main():
+ *  qemu_init()
+ *  create application, menus, etc
+ *  enter OSX run loop
+ * in applicationDidFinishLaunching:
+ *  fullscreen if needed
+ *  create main loop thread
+ *                                    call qemu_main_loop()
+ */
+
+static void *call_qemu_main_loop(void *opaque)
+{
+    COCOA_DEBUG("Second thread: calling qemu_main()\n");
+    qemu_mutex_lock_iothread();
+    qemu_main_loop();
+    COCOA_DEBUG("Second thread: qemu_main_loop() returned, exiting\n");
+    qemu_cleanup();
+    [cbowner release];
+    exit(0);
+}
+
  // Mac to QKeyCode conversion
  static const int mac_to_qkeycode_map[] = {
      [kVK_ANSI_A] = Q_KEY_CODE_A,
@@ -780,9 +813,7 @@ static CGEventRef handleTapEvent(CGEventTapProxy proxy, CGEventType type, CGEven
          /*
           * Just let OSX have all events that arrive before
           * applicationDidFinishLaunching.
-         * This avoids a deadlock on the iothread lock, which cocoa_display_init()
-         * will not drop until after the app_started_sem is posted. (In theory
-         * there should not be any such events, but OSX Catalina now emits some.)
+         * This may not be needed anymore?
           */
          return false;
      }
@@ -1280,8 +1311,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];
+    }
  }
 
  - (void)applicationWillTerminate:(NSNotification *)aNotification
@@ -1804,9 +1849,6 @@ static void addRemovableDevicesMenuItems(void)
      qapi_free_BlockInfoList(pointerToFree);
  }
 
-@interface QemuCocoaPasteboardTypeOwner : NSObject<NSPasteboardTypeOwner>
-@end
-
  @implementation QemuCocoaPasteboardTypeOwner
 
  - (void)pasteboard:(NSPasteboard *)sender provideDataForType:(NSPasteboardType)type
@@ -1841,8 +1883,6 @@ static void addRemovableDevicesMenuItems(void)
 
  @end
 
-static QemuCocoaPasteboardTypeOwner *cbowner;
-
  static void cocoa_clipboard_notify(Notifier *notifier, void *data);
  static void cocoa_clipboard_request(QemuClipboardInfo *info,
                                      QemuClipboardType type);
@@ -1903,60 +1943,18 @@ static void cocoa_clipboard_request(QemuClipboardInfo *info,
      }
  }
 
-/*
- * 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 vl.c qemu_main():
- *
- * Initial thread:                    2nd thread:
- * in main():
- *  create qemu-main thread
- *  wait on display_init semaphore
- *                                    call qemu_main()
- *                                    ...
- *                                    in cocoa_display_init():
- *                                     post the display_init semaphore
- *                                     wait on app_started semaphore
- *  create application, menus, etc
- *  enter OSX run loop
- * in applicationDidFinishLaunching:
- *  post app_started semaphore
- *                                     tell main thread to fullscreen if needed
- *                                    [...]
- *                                    run qemu main-loop
- *
- * We do this in two stages so that we don't do the creation of the
- * GUI application menus and so on for command line options like --help
- * where we want to just print text to stdout and exit immediately.
- */
-
-static void *call_qemu_main(void *opaque)
+int main(int argc, char **argv, char **envp)
  {
-    int status;
-
-    COCOA_DEBUG("Second thread: calling qemu_main()\n");
-    status = qemu_main(gArgc, gArgv, *_NSGetEnviron());
-    COCOA_DEBUG("Second thread: qemu_main() returned, exiting\n");
-    [cbowner release];
-    exit(status);
-}
-
-int main (int argc, char **argv) {
-    QemuThread thread;
-
      COCOA_DEBUG("Entered main()\n");
-    gArgc = argc;
-    gArgv = argv;
+    qemu_event_init(&cbevent, false);
 
-    qemu_sem_init(&display_init_sem, 0);
-    qemu_sem_init(&app_started_sem, 0);
-
-    qemu_thread_create(&thread, "qemu_main", call_qemu_main,
-                       NULL, QEMU_THREAD_DETACHED);
-
-    COCOA_DEBUG("Main thread: waiting for display_init_sem\n");
-    qemu_sem_wait(&display_init_sem);
-    COCOA_DEBUG("Main thread: initializing app\n");
+    /* Takes iothread lock, released in applicationDidFinishLaunching:.  */
+    qemu_init(argc, argv, envp);
+    if (!have_cocoa_ui) {
+         qemu_main_loop();
+         qemu_cleanup();
+         return 0;
+    }
 
      NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
@@ -1978,6 +1976,7 @@ int main (int argc, char **argv) {
       */
      add_console_menu_entries();
      addRemovableDevicesMenuItems();
+    cbowner = [[QemuCocoaPasteboardTypeOwner alloc] init];
 
      // Create an Application controller
      QemuCocoaAppController *appController = [[QemuCocoaAppController alloc] init];
@@ -2071,24 +2070,13 @@ static void cocoa_refresh(DisplayChangeListener *dcl)
  static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
  {
      COCOA_DEBUG("qemu_cocoa: cocoa_display_init\n");
+    have_cocoa_ui = 1;
 
-    /* Tell main thread to go ahead and create the app and enter the run loop */
-    qemu_sem_post(&display_init_sem);
-    qemu_sem_wait(&app_started_sem);

ui/cocoa.m static void cocoa_refresh(DisplayChangeListener *dcl) · line 2076

Quoted 10 lines
ui/cocoa.m +1 -8
-    COCOA_DEBUG("cocoa_display_init: app start completed\n");
-
-    QemuCocoaAppController *controller = (QemuCocoaAppController *)[[NSApplication sharedApplication] delegate];
-    /* if fullscreen mode is to be used */
      if (opts->has_full_screen && opts->full_screen) {
-        dispatch_async(dispatch_get_main_queue(), ^{
-            [NSApp activateIgnoringOtherApps: YES];
-            [controller toggleFullScreen: nil];
-        });
+        full_screen = 1;

We could just save opts and use it later. sdl2 does this.

Regards,
Akihiko Odaki

Quoted 23 lines
ui/cocoa.m +1 -10
      }
      if (opts->u.cocoa.has_full_grab && opts->u.cocoa.full_grab) {
-        dispatch_async(dispatch_get_main_queue(), ^{
-            [controller setFullGrab: nil];
-        });
+        full_grab = 1;
      }
 
      if (opts->has_show_cursor && opts->show_cursor) {
@@ -2101,13 +2089,6 @@ static void cocoa_display_init(DisplayState *ds, DisplayOptions *opts)
      if (opts->u.cocoa.has_left_command_key && !opts->u.cocoa.left_command_key) {
          left_command_key_enabled = 0;
      }
-
-    // register vga output callbacks
-    register_displaychangelistener(&dcl);
-
-    qemu_event_init(&cbevent, false);
-    cbowner = [[QemuCocoaPasteboardTypeOwner alloc] init];
-    qemu_clipboard_peer_register(&cbpeer);
  }
 
  static QemuDisplay qemu_display_cocoa = {
#7 lore raw
In reply to Philippe Mathieu-Daudé

Just one change to aid future reading of the code, possibly. Move this line:

Quoted 1 line · Philippe Mathieu-Daudé

+ qemu_event_init(&cbevent, false);

just before

+ cbowner = [[QemuCocoaPasteboardTypeOwner alloc] init];

i.e. the place where it was before the patch, in cocoa_display_init.

Paolo

#8 lore raw
Quoted 10 lines · Paolo Bonzini

Just one change to aid future reading of the code, possibly.  Move this line:

Quoted 1 line · Philippe Mathieu-Daudé

+    qemu_event_init(&cbevent, false);

just before

+    cbowner = [[QemuCocoaPasteboardTypeOwner alloc] init];

i.e. the place where it was before the patch, in cocoa_display_init.

OK. I'll also include this change (s/cocoa_display_init/applicationDidFinishLaunching):

@@ -611,17 +611,17 @@
- (void) updateUIInfo
{
if (!allow_events) {
/*
* Don't try to tell QEMU about UI information in the application
* startup phase -- we haven't yet registered dcl with the QEMU UI
* layer, and also trying to take the iothread lock would
deadlock.

-         * When cocoa_display_init() does register the dcl, the UI layer
-         * will call cocoa_switch(), which will call updateUIInfo, so
-         * we don't lose any information here.
+         * When applicationDidFinishLaunching() does register the dcl, the
+         * UI layer will call cocoa_switch(), which will call updateUIInfo,
+         * so we don't lose any information here.
          */
         return;
     }