Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui_ios: Reuse the cached main-thread marker (#65975)
Kirill Bulatovf1ccf949d59b
diff --git a/crates/gpui_apple/src/gpui_apple.rs b/crates/gpui_apple/src/gpui_apple.rsindex 6f39e63b762..f3ce55a202c 100644--- a/crates/gpui_apple/src/gpui_apple.rs+++ b/crates/gpui_apple/src/gpui_apple.rs@@ -16,6 +16,31 @@ mod open_type;#[cfg(feature = "font-kit")]mod text_system;+use objc2::MainThreadMarker;+pub use dispatcher::{AppleActivity, AppleDispatcher};#[cfg(feature = "font-kit")]pub use text_system::AppleTextSystem;++#[inline]+pub fn main_thread_marker() -> Option<MainThreadMarker> {+ thread_local! {+ static MAIN_THREAD_MARKER: Option<MainThreadMarker> = MainThreadMarker::new();+ }+ MAIN_THREAD_MARKER.with(|marker| *marker)+}++#[cfg(test)]+mod tests {+ use super::main_thread_marker;++ #[test]+ fn main_thread_marker_rejects_background_threads() {+ std::thread::spawn(|| {+ assert_eq!(main_thread_marker(), None);+ assert_eq!(main_thread_marker(), None);+ })+ .join()+ .expect("background thread panicked");+ }+}diff --git a/crates/gpui_ios/src/ios/application.rs b/crates/gpui_ios/src/ios/application.rsindex e6235cf7af8..addd040c86f 100644--- a/crates/gpui_ios/src/ios/application.rs+++ b/crates/gpui_ios/src/ios/application.rs@@ -1,8 +1,9 @@use super::{CallbackSlot, platform::IosPlatformState, window::IosWindowState};use gpui::{AppLifecyclePhase, WindowVisibility};+use gpui_apple::main_thread_marker;use objc2::{- ClassType, DefinedClass, MainThreadMarker, MainThreadOnly, Message, define_class, extern_class,- extern_methods, msg_send,+ ClassType, DefinedClass, MainThreadOnly, Message, define_class, extern_class, extern_methods,+ msg_send,rc::{Allocated, Retained},runtime::AnyObject,sel,@@ -47,7 +48,7 @@ pub(super) fn current_platform() -> Option<Rc<IosPlatformState>> {}pub(super) fn run(platform: &Rc<IosPlatformState>, on_finish_launching: Box<dyn FnOnce()>) {- let main_thread = MainThreadMarker::new().expect("UIKit requires the main thread");+ let main_thread = main_thread_marker().expect("UIKit requires the main thread");CURRENT_PLATFORM.with_borrow_mut(|current| {assert!(current.upgrade().is_none(),diff --git a/crates/gpui_ios/src/ios/display.rs b/crates/gpui_ios/src/ios/display.rsindex c142a31583d..6b8a00b3de7 100644--- a/crates/gpui_ios/src/ios/display.rs+++ b/crates/gpui_ios/src/ios/display.rs@@ -5,7 +5,8 @@use anyhow::Result;use gpui::{Bounds, DisplayId, Pixels, PlatformDisplay, px, size};-use objc2::{MainThreadMarker, rc::Retained};+use gpui_apple::main_thread_marker;+use objc2::rc::Retained;use objc2_core_foundation::CGRect;use objc2_ui_kit::UIScreen;use std::time::Duration;@@ -30,7 +31,7 @@ impl IosDisplay {#[allow(deprecated)] // Platform display queries can precede scene creation.pub fn main() -> Self {let screen =- UIScreen::mainScreen(MainThreadMarker::new().expect("UIKit requires the main thread"));+ UIScreen::mainScreen(main_thread_marker().expect("UIKit requires the main thread"));Self { screen }}@@ -38,7 +39,7 @@ impl IosDisplay {#[allow(deprecated)] // Preserve external screens even before they have a GPUI scene.pub fn all() -> impl Iterator<Item = Self> {let screens =- UIScreen::screens(MainThreadMarker::new().expect("UIKit requires the main thread"));+ UIScreen::screens(main_thread_marker().expect("UIKit requires the main thread"));screens.to_vec().into_iter().map(|screen| Self { screen })}diff --git a/crates/gpui_ios/src/ios/platform.rs b/crates/gpui_ios/src/ios/platform.rsindex b3c8121dc39..13538a3d3e2 100644--- a/crates/gpui_ios/src/ios/platform.rs+++ b/crates/gpui_ios/src/ios/platform.rs@@ -23,10 +23,10 @@ use gpui::{PlatformWindow, Result, Task, ThermalState, WindowAppearance, WindowParams,};use gpui_apple::{- AppleDispatcher, keychain,+ AppleDispatcher, keychain, main_thread_marker,thermal::{self, ThermalObserver},};-use objc2::{MainThreadMarker, MainThreadOnly, rc::Retained, runtime::ProtocolObject};+use objc2::{MainThreadOnly, rc::Retained, runtime::ProtocolObject};use objc2_foundation::{NSBundle, NSDictionary, NSNotification, NSNotificationCenter, NSObjectProtocol, NSString, NSURL,};@@ -122,7 +122,7 @@ impl IosPlatform {// Legacy hosts can create a GPUI window without supplying a scene.#[allow(deprecated)]UIApplication::sharedApplication(- MainThreadMarker::new().expect("UIKit requires the main thread"),+ main_thread_marker().expect("UIKit requires the main thread"),).keyWindow()}?;@@ -160,7 +160,7 @@ impl IosPlatform {fn open_url_with_system(url: &NSURL) {unsafe {let app = UIApplication::sharedApplication(- MainThreadMarker::new().expect("UIKit requires the main thread"),+ main_thread_marker().expect("UIKit requires the main thread"),);app.openURL_options_completionHandler(url, &NSDictionary::new(), None);}diff --git a/crates/gpui_ios/src/ios/window.rs b/crates/gpui_ios/src/ios/window.rsindex 685f575986d..e1556654f97 100644--- a/crates/gpui_ios/src/ios/window.rs+++ b/crates/gpui_ios/src/ios/window.rs@@ -20,7 +20,10 @@ use gpui::{TouchEvent, TouchId, TouchPhase, WindowAppearance, WindowBackgroundAppearance, WindowBounds,WindowControlArea, WindowInsets, WindowParams, WindowVisibility, px, size,};-use gpui_apple::metal_renderer::{Context as MetalContext, MetalRenderer};+use gpui_apple::{+ main_thread_marker,+ metal_renderer::{Context as MetalContext, MetalRenderer},+};use objc2::rc::Retained;use objc2::runtime::{AnyClass, AnyObject, ProtocolObject};use objc2::{@@ -301,7 +304,7 @@ impl IosWindow {let screen_bounds = screen.bounds();let scale_factor = screen.scale();- let main_thread = MainThreadMarker::new().expect("UIKit requires the main thread");+ let main_thread = main_thread_marker().expect("UIKit requires the main thread");// Create UIWindowlet window_scene = platform.application.window_scene();let window_scene = window_scene.as_deref();diff --git a/crates/gpui_macos/src/gpui_macos.rs b/crates/gpui_macos/src/gpui_macos.rsindex c74d0599c79..8732badd321 100644--- a/crates/gpui_macos/src/gpui_macos.rs+++ b/crates/gpui_macos/src/gpui_macos.rs@@ -28,7 +28,7 @@ mod window;mod window_appearance;use objc2::{- Encode, Encoding, MainThreadMarker, RefEncode,+ Encode, Encoding, RefEncode,rc::Retained,runtime::{AnyObject, Bool},};@@ -37,20 +37,13 @@ use std::{ffi::CStr, ops::Range};pub(crate) use display::*;pub(crate) use display_link::*;-pub(crate) use gpui_apple::{AppleActivity, AppleDispatcher};+pub(crate) use gpui_apple::{AppleActivity, AppleDispatcher, main_thread_marker};pub(crate) use keyboard::*;pub(crate) use platform::*;pub(crate) use window::*;pub use platform::MacPlatform;-fn main_thread_marker() -> Option<MainThreadMarker> {- thread_local! {- static MAIN_THREAD_MARKER: Option<MainThreadMarker> = MainThreadMarker::new();- }- MAIN_THREAD_MARKER.with(|marker| *marker)-}-trait BoolExt {fn to_objc(self) -> Bool;}@@ -131,18 +124,3 @@ unsafe impl RefEncode for NSRange {fn ns_string(string: &str) -> *mut AnyObject {Retained::autorelease_ptr(NSString::from_str(string)).cast()}--#[cfg(test)]-mod tests {- use super::main_thread_marker;-- #[test]- fn main_thread_marker_rejects_background_threads() {- std::thread::spawn(|| {- assert_eq!(main_thread_marker(), None);- assert_eq!(main_thread_marker(), None);- })- .join()- .expect("background thread panicked");- }-}