From patchwork Thu Feb 14 10:28:10 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 1041980 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=nongnu.org (client-ip=209.51.188.17; helo=lists.gnu.org; envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=linaro.org Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=linaro.org header.i=@linaro.org header.b="xqr5oQ/6"; dkim-atps=neutral Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 440Xqn49jYz9sMr for ; Thu, 14 Feb 2019 21:36:20 +1100 (AEDT) Received: from localhost ([127.0.0.1]:45802 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1guENM-0002L9-4K for incoming@patchwork.ozlabs.org; Thu, 14 Feb 2019 05:36:16 -0500 Received: from eggs.gnu.org ([209.51.188.92]:46110) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1guEML-000297-HI for qemu-devel@nongnu.org; Thu, 14 Feb 2019 05:35:14 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1guEFw-0003ya-Ow for qemu-devel@nongnu.org; Thu, 14 Feb 2019 05:28:38 -0500 Received: from mail-wr1-x442.google.com ([2a00:1450:4864:20::442]:39242) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1guEFs-0003lG-Vk for qemu-devel@nongnu.org; Thu, 14 Feb 2019 05:28:35 -0500 Received: by mail-wr1-x442.google.com with SMTP id l5so4766616wrw.6 for ; Thu, 14 Feb 2019 02:28:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=xgyqIteJDA8A8gslVlpPNbSOD5Z2HmWyq3Wfr3J3rwg=; b=xqr5oQ/6nnE/XdEIaa+knRVd4CX4wnBfmNtGz+ilqUkq1TNcXyFkFdlJHzhK4PK5Ox 5v0Y2qyeb5kPrcU9njykfhw5hL7BxQ5qnKzqiasJm3C1+mcxK0Az9yWKUZUzSx1OcdE3 08ZD59SNLEGfJEsd9Ou4Gbb4qonFJBCqJEUqfgVZDLYNwpiRU9pAuwuOvyTdn+R886N7 pjKk0KXgMk90uZnBt5zQvM1QWMGMISCPMtYHzzlw67ckd6uBQh0vPSAkwqg/PBBgu59s 7hA4G3WTZS0LqyjrIzy2jXzcATPwoEmyjonn5rIi1yfimuzQBX5IOY3xhtR0Ouuu0u2q 0yhA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=xgyqIteJDA8A8gslVlpPNbSOD5Z2HmWyq3Wfr3J3rwg=; b=SKl+rK/FH5jUPPk5WtN6Y8Dz9X+gILBREe+nTUzfPsIAjAIvytCXkC78U4cYWIqR3E i/KEZZJs6sr+YyvoZjzDnHcwkzsCiuUpCs49hRY2R/yOHIlzWGxPLWYoJBLAUNVonrLL 7Hrfcu19/i/PFS2nFYLVT71hAPjh+PVT58jTrCmZv8qIzkz2x04vuiQpzix12IrPpdB8 4VQ0E+uKwZVzm05fPjPZ6pPUEdvYVeiN3szQ1HcBjxFpG6NgDEujuUblZ+q7o7I/HvSt R7rnNDSCHbNYR1bhDUWykhQY4arhJlhTVjCTIKhqVpCpjEEviBmrsBfteLHzf/UPNT7e /JsA== X-Gm-Message-State: AHQUAuaBoRs4g/9gf0J8xlltyMpV3rpeqv1Jb3qK6FzaXWJgxtj6IYtO 1oIAJ2Jzu3ri1olXrt+5RRe/PW0Lj24VIg== X-Google-Smtp-Source: AHgI3IZxc1AqWvP2h82cepY8T41DHS6laA9IJXeyPCSYipOFCw57KdjD0mDyDelvsWIxqYaFjqq7fQ== X-Received: by 2002:adf:e548:: with SMTP id z8mr2336220wrm.52.1550140100078; Thu, 14 Feb 2019 02:28:20 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [81.2.115.148]) by smtp.gmail.com with ESMTPSA id o5sm6473817wrh.34.2019.02.14.02.28.18 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 14 Feb 2019 02:28:19 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Date: Thu, 14 Feb 2019 10:28:10 +0000 Message-Id: <20190214102816.3393-2-peter.maydell@linaro.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190214102816.3393-1-peter.maydell@linaro.org> References: <20190214102816.3393-1-peter.maydell@linaro.org> MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::442 Subject: [Qemu-devel] [PATCH v2 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: , Cc: patches@linaro.org, John Arbuckle , Roman Bolshakov , Gerd Hoffmann , Ben Hekster , Berkus Decker Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: "Qemu-devel" 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 --- ui/cocoa.m | 83 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/ui/cocoa.m b/ui/cocoa.m index e2567d6946..2931c751fd 100644 --- a/ui/cocoa.m +++ b/ui/cocoa.m @@ -129,6 +129,21 @@ NSTextField *pauseLabel; NSArray * supportedImageFileTypes; +// Utility function to run specified code block with iothread lock held +typedef void (^CodeBlock)(void); + +static void with_iothread_lock(CodeBlock block) +{ + bool locked = 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[] = { [kVK_ANSI_A] = Q_KEY_CODE_A, @@ -306,6 +321,7 @@ - (void) ungrabMouse; - (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 @@ - (void) handleMonitorInput:(NSEvent *)event - (void) handleEvent:(NSEvent *)event { - COCOA_DEBUG("QemuCocoaView: handleEvent\n"); + with_iothread_lock(^{ + [self handleEventLocked:event]; + }); +} +- (void) handleEventLocked:(NSEvent *)event +{ + COCOA_DEBUG("QemuCocoaView: handleEvent\n"); int buttons = 0; int keycode = 0; bool mouse_event = false; @@ -945,15 +967,18 @@ - (QEMUScreen) gscreen {return screen;} */ - (void) raiseAllKeys { - int index; const int max_index = ARRAY_SIZE(modifiers_state); - for (index = 0; index < max_index; index++) { - if (modifiers_state[index]) { - modifiers_state[index] = 0; - qemu_input_event_send_key_qcode(dcl->con, index, false); - } - } + with_iothread_lock(^{ + int index; + + for (index = 0; index < max_index; index++) { + if (modifiers_state[index]) { + modifiers_state[index] = 0; + qemu_input_event_send_key_qcode(dcl->con, index, false); + } + } + }); } @end @@ -1215,13 +1240,17 @@ - (void)removePause /* Restarts QEMU */ - (void)restartQEMU:(id)sender { - qmp_system_reset(NULL); + with_iothread_lock(^{ + qmp_system_reset(NULL); + }); } /* Powers down QEMU */ - (void)powerDownQEMU:(id)sender { - qmp_system_powerdown(NULL); + with_iothread_lock(^{ + qmp_system_powerdown(NULL); + }); } /* Ejects the media. @@ -1237,9 +1266,11 @@ - (void)ejectDeviceMedia:(id)sender return; } - Error *err = NULL; - qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding], - false, NULL, false, false, &err); + __block Error *err = NULL; + with_iothread_lock(^{ + qmp_eject(true, [drive cStringUsingEncoding: NSASCIIStringEncoding], + false, NULL, false, false, &err); + }); handleAnyDeviceErrors(err); } @@ -1271,16 +1302,18 @@ - (void)changeDeviceMedia:(id)sender return; } - Error *err = NULL; - qmp_blockdev_change_medium(true, - [drive cStringUsingEncoding: - NSASCIIStringEncoding], - false, NULL, - [file cStringUsingEncoding: - NSASCIIStringEncoding], - true, "raw", - false, 0, - &err); + __block Error *err = 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 +1452,9 @@ - (void)adjustSpeed:(id)sender // get the throttle percentage throttle_pct = [sender tag]; - 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(), '%'); }