From nobody Sun Nov 9 19:09:40 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1551718311270165.0287561037327; Mon, 4 Mar 2019 08:51:51 -0800 (PST) Received: from localhost ([127.0.0.1]:57167 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h0qod-0005SQ-3A for importer@patchew.org; Mon, 04 Mar 2019 11:51:47 -0500 Received: from eggs.gnu.org ([209.51.188.92]:52459) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1h0qmy-0004MZ-6A for qemu-devel@nongnu.org; Mon, 04 Mar 2019 11:50:05 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1h0qmx-0005gV-3h for qemu-devel@nongnu.org; Mon, 04 Mar 2019 11:50:04 -0500 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]:32899) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1h0qmw-0005fk-SW for qemu-devel@nongnu.org; Mon, 04 Mar 2019 11:50:03 -0500 Received: by mail-wr1-x435.google.com with SMTP id i12so6325037wrw.0 for ; Mon, 04 Mar 2019 08:50:02 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id x74sm7238018wmf.22.2019.03.04.08.50.00 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 04 Mar 2019 08:50:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=7Kc1Pn/mGVzIiCkYEhD4KuVg7QlLrkOtfZT0u/xpGkA=; b=k2uP13zxLtrbY8fBn8UrJv0Ug/eWuh1IhwYlsVYz+OchyTpVIoC4knRnEuYyzo4fPV vYqJLF1Q+Ap7sxKG4Gisc3j4cgXna/Pw0yUMzavr4YOhZqtwKbeAsnPtEdUZP7dU4BCG /KeAjpVsYKS4uQHh3E9ec6qnOw6vPAU0jffgkVGnga7CIiw6WClLhLnvuyXx2QmcekRt x7EXIzcCc33yQaRP2cYGDqKoZCF7Y0nzkHLglBJK2I/5TS5YQw6gzlwzFB8LmP7G3Rtk VZmBBVMrZoWKEJ7GTO4SKU9YZ3Mr0SdRUMgX0MijmXYyfwqXhCMmU2KABsi0+556j1fo pXDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7Kc1Pn/mGVzIiCkYEhD4KuVg7QlLrkOtfZT0u/xpGkA=; b=fb8SMOJdWyRRdMBK2UsP+aUq68/KxT+m/j8vr0mmEFyTHN0+xxA1vq/IWaNXlswc2H t5xWqirMYCz6mJzR3ejTxUv2LUo95A720xrOg48Yb5WMyoaMahduIFpZpFr2rnap2Ewk h2hd7hfuxsXw+3CUcLgHsmRzdNPa+yjcb79T5eutHwzh+n62u+hCgi9QapfQRQEVGs7p o+dFCzALS+Jzm2M3tse2e3dIHLo/zttQYzmfDJknaq6kcTF8omuViywXbmFqjcyf/W46 chxdrMtW6DhYvFDDZ7qdCc9IGivGF6/cWlxqGkGUBGx9EZYQ5m/+ZEM2YkhP+E04E4Q/ Fn2A== X-Gm-Message-State: APjAAAV51fEVD3MZ2QA6QOALedgQ0snZh/k9RIgJwuSU6fQDKt0xOZXM SIdIj7ZkqKK5cazvb50+zt6McRaTIfc= X-Google-Smtp-Source: APXvYqybjJEGnnfz5arGA/fWhL2pTTyhf56QIbnaizrPHr9A5AnqRhJ3XzBdmrX3TuORF+xYINdFXQ== X-Received: by 2002:adf:9167:: with SMTP id j94mr14117313wrj.106.1551718201417; Mon, 04 Mar 2019 08:50:01 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Date: Mon, 4 Mar 2019 16:49:52 +0000 Message-Id: <20190304164958.9362-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190304164958.9362-1-peter.maydell@linaro.org> References: <20190304164958.9362-1-peter.maydell@linaro.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::435 Subject: [Qemu-devel] [PULL 1/7] ui/cocoa: Ensure we have the iothread lock when calling into QEMU X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 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" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The Cocoa UI should run on the main thread; this is enforced in OSX Mojave. In order to be able to run on the main thread, we need to make sure we hold the iothread lock whenever we call into various QEMU UI midlayer functions. Signed-off-by: Peter Maydell Reviewed-by: Roman Bolshakov Tested-by: Roman Bolshakov Message-id: 20190225102433.22401-2-peter.maydell@linaro.org Message-id: 20190214102816.3393-2-peter.maydell@linaro.org --- ui/cocoa.m | 91 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 26 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index e2567d69466..f1171c48654 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -129,6 +129,21 @@ bool stretch_video; NSTextField *pauseLabel; NSArray * supportedImageFileTypes; =20 +// Utility function to run specified code block with iothread lock held +typedef void (^CodeBlock)(void); + +static void with_iothread_lock(CodeBlock block) +{ + bool locked =3D qemu_mutex_iothread_locked(); + if (!locked) { + qemu_mutex_lock_iothread(); + } + block(); + if (!locked) { + qemu_mutex_unlock_iothread(); + } +} + // Mac to QKeyCode conversion const int mac_to_qkeycode_map[] =3D { [kVK_ANSI_A] =3D Q_KEY_CODE_A, @@ -306,6 +321,7 @@ static void handleAnyDeviceErrors(Error * err) - (void) toggleFullScreen:(id)sender; - (void) handleMonitorInput:(NSEvent *)event; - (void) handleEvent:(NSEvent *)event; +- (void) handleEventLocked:(NSEvent *)event; - (void) setAbsoluteEnabled:(BOOL)tIsAbsoluteEnabled; /* The state surrounding mouse grabbing is potentially confusing. * isAbsoluteEnabled tracks qemu_input_is_absolute() [ie "is the emulated @@ -649,8 +665,14 @@ QemuCocoaView *cocoaView; =20 - (void) handleEvent:(NSEvent *)event { - COCOA_DEBUG("QemuCocoaView: handleEvent\n"); + with_iothread_lock(^{ + [self handleEventLocked:event]; + }); +} =20 +- (void) handleEventLocked:(NSEvent *)event +{ + COCOA_DEBUG("QemuCocoaView: handleEvent\n"); int buttons =3D 0; int keycode =3D 0; bool mouse_event =3D false; @@ -945,15 +967,18 @@ QemuCocoaView *cocoaView; */ - (void) raiseAllKeys { - int index; const int max_index =3D ARRAY_SIZE(modifiers_state); =20 - for (index =3D 0; index < max_index; index++) { - if (modifiers_state[index]) { - modifiers_state[index] =3D 0; - qemu_input_event_send_key_qcode(dcl->con, index, false); - } - } + with_iothread_lock(^{ + int index; + + for (index =3D 0; index < max_index; index++) { + if (modifiers_state[index]) { + modifiers_state[index] =3D 0; + qemu_input_event_send_key_qcode(dcl->con, index, false); + } + } + }); } @end =20 @@ -1178,7 +1203,9 @@ QemuCocoaView *cocoaView; /* Pause the guest */ - (void)pauseQEMU:(id)sender { - qmp_stop(NULL); + with_iothread_lock(^{ + qmp_stop(NULL); + }); [sender setEnabled: NO]; [[[sender menu] itemWithTitle: @"Resume"] setEnabled: YES]; [self displayPause]; @@ -1187,7 +1214,9 @@ QemuCocoaView *cocoaView; /* Resume running the guest operating system */ - (void)resumeQEMU:(id) sender { - qmp_cont(NULL); + with_iothread_lock(^{ + qmp_cont(NULL); + }); [sender setEnabled: NO]; [[[sender menu] itemWithTitle: @"Pause"] setEnabled: YES]; [self removePause]; @@ -1215,13 +1244,17 @@ QemuCocoaView *cocoaView; /* Restarts QEMU */ - (void)restartQEMU:(id)sender { - qmp_system_reset(NULL); + with_iothread_lock(^{ + qmp_system_reset(NULL); + }); } =20 /* Powers down QEMU */ - (void)powerDownQEMU:(id)sender { - qmp_system_powerdown(NULL); + with_iothread_lock(^{ + qmp_system_powerdown(NULL); + }); } =20 /* Ejects the media. @@ -1237,9 +1270,11 @@ QemuCocoaView *cocoaView; return; } =20 - Error *err =3D NULL; - qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding], - false, NULL, false, false, &err); + __block Error *err =3D NULL; + with_iothread_lock(^{ + qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding= ], + false, NULL, false, false, &err); + }); handleAnyDeviceErrors(err); } =20 @@ -1271,16 +1306,18 @@ QemuCocoaView *cocoaView; return; } =20 - Error *err =3D NULL; - qmp_blockdev_change_medium(true, - [drive cStringUsingEncoding: - NSASCIIStringEncoding], - false, NULL, - [file cStringUsingEncoding: - NSASCIIStringEncoding], - true, "raw", - false, 0, - &err); + __block Error *err =3D NULL; + with_iothread_lock(^{ + qmp_blockdev_change_medium(true, + [drive cStringUsingEncoding: + NSASCIIStringEncoding], + false, NULL, + [file cStringUsingEncoding: + NSASCIIStringEncoding], + true, "raw", + false, 0, + &err); + }); handleAnyDeviceErrors(err); } } @@ -1419,7 +1456,9 @@ QemuCocoaView *cocoaView; // get the throttle percentage throttle_pct =3D [sender tag]; =20 - cpu_throttle_set(throttle_pct); + with_iothread_lock(^{ + cpu_throttle_set(throttle_pct); + }); COCOA_DEBUG("cpu throttling at %d%c\n", cpu_throttle_get_percentage(),= '%'); } =20 --=20 2.20.1