Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui_macos: Cache the main-thread marker per thread (#65934)

Kirill Bulatov2d6c664dc25c
diff --git a/crates/gpui_macos/src/display.rs b/crates/gpui_macos/src/display.rs
index 25d9d05ce89..df87dedbf70 100644
--- a/crates/gpui_macos/src/display.rs
+++ b/crates/gpui_macos/src/display.rs
@@ -1,3 +1,4 @@
+use crate::main_thread_marker;
use anyhow::Result;
use core_foundation::base::CFRelease;
use core_foundation::uuid::{CFUUIDGetUUIDBytes, CFUUIDRef};
@@ -5,7 +6,7 @@ use core_graphics::display::{
CGDirectDisplayID, CGDisplay, CGDisplayBounds, CGGetActiveDisplayList,
};
use gpui::{Bounds, DisplayId, Pixels, PlatformDisplay, point, px, refresh_interval_from_hz, size};
-use objc2::{MainThreadMarker, msg_send, rc::Retained, sel};
+use objc2::{msg_send, rc::Retained, sel};
use objc2_app_kit::NSScreen;
use objc2_foundation::{NSObjectProtocol, ns_string};
use std::time::Duration;
@@ -32,7 +33,7 @@ impl MacDisplay {
// The following is what Chromium does too:
//
// https://chromium.googlesource.com/chromium/src/+/66.0.3359.158/ui/display/mac/screen_mac.mm#56
- let main_thread = MainThreadMarker::new().expect("displays require the main thread");
+ let main_thread = main_thread_marker().expect("displays require the main thread");
let screens = NSScreen::screens(main_thread);
let screen = screens.firstObject().expect("no primary screen");
let screen_number = screen
@@ -166,7 +167,7 @@ impl PlatformDisplay for MacDisplay {
impl MacDisplay {
/// Find the NSScreen corresponding to this display
fn get_nsscreen(&self) -> Option<Retained<NSScreen>> {
- let main_thread = MainThreadMarker::new().expect("displays require the main thread");
+ let main_thread = main_thread_marker().expect("displays require the main thread");
NSScreen::screens(main_thread).into_iter().find(|screen| {
screen
.deviceDescription()
diff --git a/crates/gpui_macos/src/display_link.rs b/crates/gpui_macos/src/display_link.rs
index cf26d7eb869..658b9f24ace 100644
--- a/crates/gpui_macos/src/display_link.rs
+++ b/crates/gpui_macos/src/display_link.rs
@@ -50,6 +50,7 @@
//! is a std implementation detail, not a guarantee; if it changes, the cost
//! is added latency under contention, not incorrectness.)
+use crate::main_thread_marker;
use anyhow::Result;
use core_graphics::display::CGDirectDisplayID;
use dispatch2::{
@@ -113,7 +114,7 @@ fn lock_registry() -> MutexGuard<'static, Registry> {
fn debug_assert_main_thread() {
debug_assert!(
- objc2::MainThreadMarker::new().is_some(),
+ main_thread_marker().is_some(),
"display link registry mutations must happen on the main thread; \
the registry's lock ordering and state consistency depend on it"
);
diff --git a/crates/gpui_macos/src/gpui_macos.rs b/crates/gpui_macos/src/gpui_macos.rs
index b4ee9e03b37..c74d0599c79 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, RefEncode,
+ Encode, Encoding, MainThreadMarker, RefEncode,
rc::Retained,
runtime::{AnyObject, Bool},
};
@@ -44,6 +44,13 @@ 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;
}
@@ -124,3 +131,18 @@ 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");
+ }
+}
diff --git a/crates/gpui_macos/src/platform.rs b/crates/gpui_macos/src/platform.rs
index 9e99dd6e184..a5ece106ca5 100644
--- a/crates/gpui_macos/src/platform.rs
+++ b/crates/gpui_macos/src/platform.rs
@@ -1,7 +1,7 @@
use crate::{
AppleActivity, AppleDispatcher, BoolExt, MacDisplay, MacKeyboardLayout, MacKeyboardMapper,
- MacWindow, events::key_to_native, ns_string, pasteboard::Pasteboard, renderer,
- set_active_window_cursor_style,
+ MacWindow, events::key_to_native, main_thread_marker, ns_string, pasteboard::Pasteboard,
+ renderer, set_active_window_cursor_style,
};
use anyhow::{Context as _, anyhow};
use block2::RcBlock;
@@ -214,7 +214,7 @@ fn native_activation_policy(policy: ActivationPolicy) -> NSApplicationActivation
impl MacPlatform {
pub fn new(headless: bool) -> Self {
- let marker = MainThreadMarker::new().expect("Mac platform not created on main thread");
+ let marker = main_thread_marker().expect("Mac platform not created on main thread");
let dispatcher = Arc::new(AppleDispatcher::new());
#[cfg(feature = "font-kit")]