From 6daddf514628c383980aba0442d54e8a370bc56e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Mon, 7 Jun 2021 17:35:27 -0600 Subject: [PATCH] Allow platform references to be sent to background threads Co-Authored-By: Max Brunsfeld --- gpui/src/app.rs | 12 ++++++------ gpui/src/platform.rs | 2 +- gpui/src/platform/mac.rs | 6 +++--- gpui/src/platform/mac/platform.rs | 3 +++ gpui/src/platform/test.rs | 7 ++++--- 5 files changed, 17 insertions(+), 13 deletions(-) diff --git a/gpui/src/app.rs b/gpui/src/app.rs index e7a6412087..052fafd11c 100644 --- a/gpui/src/app.rs +++ b/gpui/src/app.rs @@ -120,7 +120,7 @@ impl App { let foreground = Rc::new(executor::Foreground::test()); let cx = Rc::new(RefCell::new(MutableAppContext::new( foreground, - Rc::new(platform), + Arc::new(platform), Rc::new(main_thread_platform), asset_source, ))); @@ -134,7 +134,7 @@ impl App { Fn: FnOnce(TestAppContext) -> F, F: Future, { - let platform = Rc::new(platform::test::platform()); + let platform = Arc::new(platform::test::platform()); let main_thread_platform = Rc::new(platform::test::main_thread_platform()); let foreground = Rc::new(executor::Foreground::test()); let cx = TestAppContext { @@ -361,7 +361,7 @@ impl TestAppContext { self.cx.borrow().cx.font_cache.clone() } - pub fn platform(&self) -> Rc { + pub fn platform(&self) -> Arc { self.cx.borrow().platform.clone() } @@ -530,7 +530,7 @@ type GlobalActionCallback = dyn FnMut(&dyn Any, &mut MutableAppContext); pub struct MutableAppContext { weak_self: Option>>, main_thread_platform: Rc, - platform: Rc, + platform: Arc, assets: Arc, cx: AppContext, actions: HashMap>>>, @@ -553,7 +553,7 @@ pub struct MutableAppContext { impl MutableAppContext { fn new( foreground: Rc, - platform: Rc, + platform: Arc, main_thread_platform: Rc, asset_source: impl AssetSource, ) -> Self { @@ -594,7 +594,7 @@ impl MutableAppContext { App(self.weak_self.as_ref().unwrap().upgrade().unwrap()) } - pub fn platform(&self) -> Rc { + pub fn platform(&self) -> Arc { self.platform.clone() } diff --git a/gpui/src/platform.rs b/gpui/src/platform.rs index c8ea76a2fb..187dfea1de 100644 --- a/gpui/src/platform.rs +++ b/gpui/src/platform.rs @@ -48,7 +48,7 @@ pub(crate) trait MainThreadPlatform { ); } -pub trait Platform { +pub trait Platform: Send + Sync { fn dispatcher(&self) -> Arc; fn fonts(&self) -> Arc; diff --git a/gpui/src/platform/mac.rs b/gpui/src/platform/mac.rs index 67dc9d22dc..1fbe3e8465 100644 --- a/gpui/src/platform/mac.rs +++ b/gpui/src/platform/mac.rs @@ -12,15 +12,15 @@ use cocoa::base::{BOOL, NO, YES}; pub use dispatcher::Dispatcher; pub use fonts::FontSystem; use platform::{MacMainThreadPlatform, MacPlatform}; -use std::rc::Rc; +use std::{rc::Rc, sync::Arc}; use window::Window; pub(crate) fn main_thread_platform() -> Rc { Rc::new(MacMainThreadPlatform::default()) } -pub(crate) fn platform() -> Rc { - Rc::new(MacPlatform::new()) +pub(crate) fn platform() -> Arc { + Arc::new(MacPlatform::new()) } trait BoolExt { diff --git a/gpui/src/platform/mac/platform.rs b/gpui/src/platform/mac/platform.rs index 99efc3235f..33f26a3680 100644 --- a/gpui/src/platform/mac/platform.rs +++ b/gpui/src/platform/mac/platform.rs @@ -329,6 +329,9 @@ impl MacPlatform { } } +unsafe impl Send for MacPlatform {} +unsafe impl Sync for MacPlatform {} + impl platform::Platform for MacPlatform { fn dispatcher(&self) -> Arc { self.dispatcher.clone() diff --git a/gpui/src/platform/test.rs b/gpui/src/platform/test.rs index bbbd7486d4..cecf83e673 100644 --- a/gpui/src/platform/test.rs +++ b/gpui/src/platform/test.rs @@ -1,4 +1,5 @@ use crate::ClipboardItem; +use parking_lot::Mutex; use pathfinder_geometry::vector::Vector2F; use std::{ any::Any, @@ -11,7 +12,7 @@ use std::{ pub(crate) struct Platform { dispatcher: Arc, fonts: Arc, - current_clipboard_item: RefCell>, + current_clipboard_item: Mutex>, } #[derive(Default)] @@ -114,11 +115,11 @@ impl super::Platform for Platform { fn quit(&self) {} fn write_to_clipboard(&self, item: ClipboardItem) { - *self.current_clipboard_item.borrow_mut() = Some(item); + *self.current_clipboard_item.lock() = Some(item); } fn read_from_clipboard(&self) -> Option { - self.current_clipboard_item.borrow().clone() + self.current_clipboard_item.lock().clone() } }