pub use sendable::{RefCount, Shared, ThreadSafetyMarker};
use crate::fx::unique::UniqueContext;
#[cfg(all(feature = "sendable", not(feature = "std")))]
compile_error!("Feature 'sendable' requires 'std' feature for thread synchronization primitives");
#[cfg(all(feature = "sendable", feature = "std"))]
mod sendable {
use alloc::sync::Arc;
use std::sync::Mutex;
pub trait ThreadSafetyMarker: Send {}
impl<T: Send> ThreadSafetyMarker for T {}
pub type RefCount<T> = Arc<Mutex<T>>;
pub type Shared<T> = Arc<T>;
pub fn ref_count<T>(value: T) -> RefCount<T> {
Arc::new(Mutex::new(value))
}
}
#[cfg(not(feature = "sendable"))]
mod sendable {
use alloc::rc::Rc;
use core::cell::RefCell;
pub trait ThreadSafetyMarker {}
impl<T> ThreadSafetyMarker for T {}
pub type RefCount<T> = Rc<RefCell<T>>;
pub type Shared<T> = Rc<T>;
pub fn ref_count<T>(value: T) -> RefCount<T> {
Rc::new(RefCell::new(value))
}
}
#[cfg(all(feature = "sendable", feature = "std"))]
pub(crate) fn acquire_mut(
ctx: &RefCount<UniqueContext>,
) -> std::sync::MutexGuard<'_, UniqueContext> {
ctx.lock().unwrap()
}
#[cfg(all(feature = "sendable", feature = "std"))]
pub(crate) fn acquire_ref(
ctx: &RefCount<UniqueContext>,
) -> std::sync::MutexGuard<'_, UniqueContext> {
ctx.lock().unwrap()
}
#[cfg(not(feature = "sendable"))]
pub(crate) fn acquire_mut(ctx: &RefCount<UniqueContext>) -> core::cell::RefMut<'_, UniqueContext> {
ctx.borrow_mut()
}
#[cfg(not(feature = "sendable"))]
pub(crate) fn acquire_ref(ctx: &RefCount<UniqueContext>) -> core::cell::Ref<'_, UniqueContext> {
ctx.borrow()
}
pub fn ref_count<T>(value: T) -> RefCount<T> {
sendable::ref_count(value)
}