From f5cb4d8e1266eb6c184a01b3fe20da3de13f09bb Mon Sep 17 00:00:00 2001 From: Jinlei Li Date: Mon, 6 Jun 2022 15:29:16 +0800 Subject: [PATCH] metal: check if in the main thread when calling `create_surface` --- wgpu-hal/src/metal/surface.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/wgpu-hal/src/metal/surface.rs b/wgpu-hal/src/metal/surface.rs index a0ca46ab358..820805505a5 100644 --- a/wgpu-hal/src/metal/surface.rs +++ b/wgpu-hal/src/metal/surface.rs @@ -9,7 +9,7 @@ use objc::{ declare::ClassDecl, msg_send, rc::autoreleasepool, - runtime::{Class, Object, Sel, BOOL, YES}, + runtime::{Class, Object, Sel, BOOL, NO, YES}, sel, sel_impl, }; use parking_lot::Mutex; @@ -84,6 +84,11 @@ impl super::Surface { panic!("window does not have a valid contentView"); } + let is_main_thread: BOOL = msg_send![class!(NSThread), isMainThread]; + if is_main_thread == NO { + panic!("create_surface cannot be called in non-ui thread."); + } + let main_layer: *mut Object = msg_send![view, layer]; let class = class!(CAMetalLayer); let is_valid_layer: BOOL = msg_send![main_layer, isKindOfClass: class];