pub struct Mutex<T> {
#[cfg(feature = "std")]
inner: std::sync::Mutex<T>,
#[cfg(not(feature = "std"))]
inner: crate::memory::allocator::Mutex<T>,
}
impl<T> Mutex<T> {
pub fn new(value: T) -> Self {
Mutex {
#[cfg(feature = "std")]
inner: std::sync::Mutex::new(value),
#[cfg(not(feature = "std"))]
inner: crate::memory::allocator::Mutex::new(value),
}
}
pub fn lock(&self) -> MutexGuard<'_, T> {
#[cfg(feature = "std")]
{
let guard = self.inner.lock().unwrap_or_else(|e| e.into_inner());
MutexGuard { inner: guard }
}
#[cfg(not(feature = "std"))]
{
let guard = try_lock!(self.inner);
MutexGuard { inner: guard }
}
}
pub fn try_lock(&self) -> Option<MutexGuard<'_, T>> {
#[cfg(feature = "std")]
{
match self.inner.try_lock() {
Ok(guard) => Some(MutexGuard { inner: guard }),
Err(_) => None,
}
}
#[cfg(not(feature = "std"))]
{
self.inner
.try_lock()
.ok()
.map(|guard| MutexGuard { inner: guard })
}
}
}
pub struct MutexGuard<'a, T> {
#[cfg(feature = "std")]
inner: std::sync::MutexGuard<'a, T>,
#[cfg(not(feature = "std"))]
inner: crate::memory::allocator::MutexGuard<'a, T>,
}
impl<'a, T> core::ops::Deref for MutexGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
&*self.inner
}
}
impl<'a, T> core::ops::DerefMut for MutexGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T {
&mut *self.inner
}
}
pub struct RwLock<T> {
#[cfg(feature = "std")]
inner: std::sync::RwLock<T>,
#[cfg(not(feature = "std"))]
inner: crate::memory::allocator::Mutex<T>, }
impl<T> RwLock<T> {
pub fn new(value: T) -> Self {
RwLock {
#[cfg(feature = "std")]
inner: std::sync::RwLock::new(value),
#[cfg(not(feature = "std"))]
inner: crate::memory::allocator::Mutex::new(value),
}
}
pub fn read(&self) -> RwLockReadGuard<'_, T> {
#[cfg(feature = "std")]
{
let guard = self.inner.read().unwrap_or_else(|e| e.into_inner());
RwLockReadGuard { inner: guard }
}
#[cfg(not(feature = "std"))]
{
let guard = try_lock!(self.inner);
RwLockReadGuard { inner: guard }
}
}
pub fn try_read(&self) -> Option<RwLockReadGuard<'_, T>> {
#[cfg(feature = "std")]
{
match self.inner.try_read() {
Ok(guard) => Some(RwLockReadGuard { inner: guard }),
Err(_) => None,
}
}
#[cfg(not(feature = "std"))]
{
self.inner
.try_lock()
.ok()
.map(|guard| RwLockReadGuard { inner: guard })
}
}
pub fn write(&self) -> RwLockWriteGuard<'_, T> {
#[cfg(feature = "std")]
{
let guard = self.inner.write().unwrap_or_else(|e| e.into_inner());
RwLockWriteGuard { inner: guard }
}
#[cfg(not(feature = "std"))]
{
let guard = try_lock!(self.inner);
RwLockWriteGuard { inner: guard }
}
}
pub fn try_write(&self) -> Option<RwLockWriteGuard<'_, T>> {
#[cfg(feature = "std")]
{
match self.inner.try_write() {
Ok(guard) => Some(RwLockWriteGuard { inner: guard }),
Err(_) => None,
}
}
#[cfg(not(feature = "std"))]
{
self.inner
.try_lock()
.ok()
.map(|guard| RwLockWriteGuard { inner: guard })
}
}
}
pub struct RwLockReadGuard<'a, T> {
#[cfg(feature = "std")]
inner: std::sync::RwLockReadGuard<'a, T>,
#[cfg(not(feature = "std"))]
inner: crate::memory::allocator::MutexGuard<'a, T>,
}
impl<'a, T> core::ops::Deref for RwLockReadGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
&*self.inner
}
}
pub struct RwLockWriteGuard<'a, T> {
#[cfg(feature = "std")]
inner: std::sync::RwLockWriteGuard<'a, T>,
#[cfg(not(feature = "std"))]
inner: crate::memory::allocator::MutexGuard<'a, T>,
}
impl<'a, T> core::ops::Deref for RwLockWriteGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
&*self.inner
}
}
impl<'a, T> core::ops::DerefMut for RwLockWriteGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T {
&mut *self.inner
}
}
#[cfg(feature = "std")]
pub use std::sync::Arc;
#[cfg(not(feature = "std"))]
pub use alloc::sync::Arc;