use core::fmt;
use core::marker::PhantomData;
#[cfg(feature = "dispatch")]
#[cfg(feature = "NSThread")]
use core::mem::{self, ManuallyDrop};
#[cfg(feature = "NSThread")]
use core::panic::{RefUnwindSafe, UnwindSafe};
#[cfg(feature = "NSThread")]
use crate::Foundation::NSThread;
use objc2::mutability::IsMainThreadOnly;
use objc2::rc::Allocated;
use objc2::{msg_send_id, ClassType};
#[cfg(feature = "NSThread")]
unsafe impl Send for NSThread {}
#[cfg(feature = "NSThread")]
unsafe impl Sync for NSThread {}
#[cfg(feature = "NSThread")]
impl UnwindSafe for NSThread {}
#[cfg(feature = "NSThread")]
impl RefUnwindSafe for NSThread {}
#[cfg(feature = "NSThread")]
impl fmt::Debug for NSThread {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let obj: &crate::Foundation::NSObject = self;
fmt::Debug::fmt(obj, f)
}
}
#[cfg(feature = "NSThread")]
pub fn is_multi_threaded() -> bool {
NSThread::isMultiThreaded()
}
#[cfg(feature = "NSThread")]
pub fn is_main_thread() -> bool {
NSThread::isMainThread_class()
}
#[allow(unused)]
#[cfg(feature = "NSThread")]
fn make_multithreaded() {
let thread = unsafe { NSThread::new() };
unsafe { thread.start() };
}
#[cfg(feature = "dispatch")]
#[cfg(feature = "NSThread")]
pub fn run_on_main<F, R>(f: F) -> R
where
F: Send + FnOnce(MainThreadMarker) -> R,
R: Send,
{
if let Some(mtm) = MainThreadMarker::new() {
f(mtm)
} else {
dispatch::Queue::main().exec_sync(|| {
f(unsafe { MainThreadMarker::new_unchecked() })
})
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct MainThreadMarker {
_priv: PhantomData<*mut ()>,
}
impl MainThreadMarker {
#[cfg(feature = "NSThread")]
#[inline]
pub fn new() -> Option<Self> {
if NSThread::isMainThread_class() {
Some(unsafe { Self::new_unchecked() })
} else {
None
}
}
#[inline]
pub unsafe fn new_unchecked() -> Self {
Self { _priv: PhantomData }
}
#[inline]
pub fn alloc<T: ClassType>(self) -> Allocated<T> {
unsafe { msg_send_id![T::class(), alloc] }
}
#[deprecated = "Use the free-standing function `run_on_main` instead"]
#[cfg(feature = "dispatch")]
#[cfg(feature = "NSThread")]
pub fn run_on_main<F, R>(f: F) -> R
where
F: Send + FnOnce(MainThreadMarker) -> R,
R: Send,
{
run_on_main(f)
}
}
impl<T: ?Sized + IsMainThreadOnly> From<&T> for MainThreadMarker {
#[inline]
fn from(_obj: &T) -> Self {
unsafe { Self::new_unchecked() }
}
}
impl fmt::Debug for MainThreadMarker {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("MainThreadMarker").finish()
}
}
#[doc(alias = "@MainActor")]
#[cfg(feature = "dispatch")]
#[cfg(feature = "NSThread")]
pub struct MainThreadBound<T>(ManuallyDrop<T>);
#[cfg(feature = "dispatch")]
#[cfg(feature = "NSThread")]
unsafe impl<T> Send for MainThreadBound<T> {}
#[cfg(feature = "dispatch")]
#[cfg(feature = "NSThread")]
unsafe impl<T> Sync for MainThreadBound<T> {}
#[cfg(feature = "dispatch")]
#[cfg(feature = "NSThread")]
impl<T> Drop for MainThreadBound<T> {
fn drop(&mut self) {
if mem::needs_drop::<T>() {
run_on_main(|_mtm| {
let this = self;
unsafe { ManuallyDrop::drop(&mut this.0) };
})
}
}
}
#[cfg(feature = "dispatch")]
#[cfg(feature = "NSThread")]
impl<T> MainThreadBound<T> {
#[inline]
pub fn new(inner: T, _mtm: MainThreadMarker) -> Self {
Self(ManuallyDrop::new(inner))
}
#[inline]
pub fn get(&self, _mtm: MainThreadMarker) -> &T {
&self.0
}
#[inline]
pub fn get_mut(&mut self, _mtm: MainThreadMarker) -> &mut T {
&mut self.0
}
#[inline]
pub fn into_inner(self, _mtm: MainThreadMarker) -> T {
let mut this = ManuallyDrop::new(self);
unsafe { ManuallyDrop::take(&mut this.0) }
}
}
#[cfg(feature = "dispatch")]
#[cfg(feature = "NSThread")]
impl<T> MainThreadBound<T> {
#[inline]
pub fn get_on_main<F, R>(&self, f: F) -> R
where
F: Send + FnOnce(&T) -> R,
R: Send,
{
run_on_main(|mtm| f(self.get(mtm)))
}
#[inline]
pub fn get_on_main_mut<F, R>(&mut self, f: F) -> R
where
F: Send + FnOnce(&mut T) -> R,
R: Send,
{
run_on_main(|mtm| f(self.get_mut(mtm)))
}
}
#[cfg(feature = "dispatch")]
#[cfg(feature = "NSThread")]
impl<T> fmt::Debug for MainThreadBound<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("MainThreadBound").finish_non_exhaustive()
}
}