qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH v3 1/3] ui/cocoa: Run qemu_init in the main thread


From: Paolo Bonzini
Subject: Re: [PATCH v3 1/3] ui/cocoa: Run qemu_init in the main thread
Date: Sun, 17 Jul 2022 05:18:07 +0200



Il sab 16 lug 2022, 22:42 Akihiko Odaki <akihiko.odaki@gmail.com> ha scritto:
> Is this documented anywhere?

Thread-unsafe classes (and more importantly, main thread only classes)
are rather exceptional, and they are listed at:
https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html

Being thread-safe is not the same thing as deferring to the main thread though. 

Here are a couple of examples of how things can go wrong, depending on the implementation of Cocoa:

- applicationWillTerminate is a notification, and as such will run in the main thread, but will applicationShouldTerminate also be called only from the main thread? If not, is it safe to show the alert in verifyQuit?

- is the secondary thread still in terminate: when applicationWillTerminate runs, and is it waiting for applicationWillTerminate to finish? If so, qemu_thread_join will deadlock (because the waited thread will not exit until applicationWillTerminate returns)

In both cases the problem is not thread-unsafety of Cocoa, but thread-unsafety of the QEMU application delegate.

Paolo 


Regards,
Akihiko Odaki

>
> Paolo
>
> Regards,
>> Akihiko Odaki
>>
>>>
>>> - here:
>>>
>>>       /*
>>>        * Nothing more to do in the QEMU thread, ask the application
>>>        * to exit.
>>>        */
>>>       dispatch_async(dispatch_get_main_queue(), ^{
>>>           [NSApp terminate: nil];
>>>       });
>>>
>>> - in verifyQuit:
>>>
>>>       if([alert runModal] == NSAlertSecondButtonReturn) {
>>>           with_iothread_lock(^{
>>>               shutdown_action = SHUTDOWN_ACTION_POWEROFF;
>>>               qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
>>>           });
>>>       }
>>>       /* and just return void */
>>>
>>> - applicationShouldTerminate: should be just
>>>
>>>       if (qatomic_read(&qemu_main_terminating)) {
>>>           return NSTerminateNow;
>>>       } else {
>>>           dispatch_async(dispatch_get_main_queue(), ^{
>>>               [self verifyQuit];
>>>           });
>>>       }
>>>       return NSTerminateCancel;
>>>
>>> - applicationWillTerminate: can be just the qemu_thread_join
>>>
>>> Paolo
>>
>>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]