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: 158343 Delivered-To: patches@linaro.org Received: by 2002:a02:48:0:0:0:0:0 with SMTP id 69csp1175194jaa; Thu, 14 Feb 2019 02:28:20 -0800 (PST) X-Received: by 2002:a05:600c:2255:: with SMTP id a21mr2216572wmm.31.1550140100490; Thu, 14 Feb 2019 02:28:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1550140100; cv=none; d=google.com; s=arc-20160816; b=W+zgVQF7fhxLnklH/Me7bDQ9NuDC/+sbxvYD5NxSA0Lv0LgkxQNNqUA8oO93sv/H4Q D7TDDdoVfbV6yEzxpe7i4uKAGBabmgBJFCQfUbPR6WjTcgt1diAfZpjZDXOp8KJ8kDww l/uAwLYrxLKxzs2oVCaXV1pnH3VrnPieq0/dejtJ6CStlbD97BJ4canOzoQakv/7TD49 yjSv1JQL1lS+WYS1LAUXWWxL+hSSVoGMyF4E079A3yLYiJ/JgnxercVTGGKfYJZPl+uE FQPx8agWcTozZz0wcPTeiIE8lYI1iMyflTxLDoZxnmHHzTUtxm+iGwxbEsN2hFh9q8/5 0kbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:dkim-signature; bh=xgyqIteJDA8A8gslVlpPNbSOD5Z2HmWyq3Wfr3J3rwg=; b=OaqZUamHH5zrSwiK0jihzFf8UhEjHLfLouh+ULP4heiA5BZFWDo/pC24hA7vJ++V3o yQQbmpsk1QFh1P5NfXxsUDXM0XiAiA4BCLvpEKOJnBwCeaJstFrAIgMfxxlGScMvDl5W /xu6/DC/j1g/wXDwwdPrIJVXySt/AMx72S0s10TQ++WsOvmdiS8bghaDs8qMxdcc2SG8 tdBThmVJ9u/JIMadBuKmSPKwYHst+xNodIjo6qWt0YN0tQnxkxywSAEZg9meLVzfyubR 7g1Wcbn+/Sge1Kp1SefoVHuoEc1ttF7SYei4DiOA+jYqMnqCj/l979ZDmPxM3ZscM7m5 P7vQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="xqr5oQ/6"; spf=pass (google.com: domain of peter.maydell@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=peter.maydell@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id u13sor1326359wrq.41.2019.02.14.02.28.20 for (Google Transport Security); Thu, 14 Feb 2019 02:28:20 -0800 (PST) Received-SPF: pass (google.com: domain of peter.maydell@linaro.org designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b="xqr5oQ/6"; spf=pass (google.com: domain of peter.maydell@linaro.org designates 209.85.220.65 as permitted sender) smtp.mailfrom=peter.maydell@linaro.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org 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=YHqn8T8QjTbI1hpi7JrMX1nhRQPMyksnozLEVr0IYrYDYUedIVJ/Rgmwk3N0C0DnG8 C1WQs30JhVebE/8x2lfa7J1uuqNDKeU7p0xLPlAz4hypiqPChR8EQarXyWASSMcJM7nq PUQ2pmeQRQkJw4157hl6ncYQq28GE8xhs5AlJsROoMt/quPqInkOmI5l1VJErW3aB48h poMtfJMkwsIOd6sgKW5g3milI9rQJDlk0pb7zOu5siYTUrOkQgnIyVdrGgSWU+VBXqoF vmen7/VP1+HOqhPRGsstses8DWhNuGjuvqBWn5Ln+GIHum+a5WAI1OdZ8orDMYm3Coim +PcQ== X-Gm-Message-State: AHQUAubFZ0IwAhdX0k4wCIEHXa8oUHlhmIVyhVurdzO6BadyTlt5M+FO KvL/ZnjiAdJQ24lxxtmYmBHFfW0E 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) Return-Path: 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 Cc: patches@linaro.org, John Arbuckle , Roman Bolshakov , Berkus Decker , Gerd Hoffmann , Ben Hekster , BALATON Zoltan Subject: [PATCH v2 1/7] ui/cocoa: Ensure we have the iothread lock when calling into QEMU 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 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 --- ui/cocoa.m | 83 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 59 insertions(+), 24 deletions(-) -- 2.17.2 (Apple Git-113) Reviewed-by: Roman Bolshakov Tested-by: Roman Bolshakov 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(), '%'); }