mirror of
https://github.com/zed-industries/zed.git
synced 2026-12-03 14:01:30 +01:00
gpui_ios: Reuse the cached main-thread marker (#65975)
Follow-up to https://github.com/zed-industries/zed/pull/65934#issuecomment-6332065288 Release Notes: - [GPUI] N/A
This commit is contained in:
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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 UIWindow
|
||||
let window_scene = platform.application.window_scene();
|
||||
let window_scene = window_scene.as_deref();
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user