#![allow(unused_imports, reason = "behavior changes in compilation context")]
use core::{
error::Error,
fmt,
sync::atomic::{AtomicBool, Ordering},
};
mod mutex;
pub use mutex::{MappedMutexGuard, Mutex, MutexGuard};
mod rwlock;
pub use rwlock::{
MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard,
};
mod once;
pub use once::Once;
mod condvar;
pub use condvar::Condvar;
pub struct Flag {
#[cfg(panic = "unwind")]
failed: AtomicBool,
}
impl Flag {
#[inline]
#[allow(clippy::new_without_default)]
pub const fn new() -> Flag {
Flag {
#[cfg(panic = "unwind")]
failed: AtomicBool::new(false),
}
}
#[inline]
pub fn borrow(&self) -> LockResult<()> {
if self.get() {
Err(PoisonError::new(()))
} else {
Ok(())
}
}
#[inline]
pub fn guard(&self) -> LockResult<Guard> {
let ret = Guard {
#[cfg(panic = "unwind")]
panicking: crate::panicking::panicking(),
};
if self.get() {
Err(PoisonError::new(ret))
} else {
Ok(ret)
}
}
#[inline]
#[allow(unused_variables, reason = "behavior changes in compilation context")]
pub fn done(&self, guard: &Guard) {
cfg_select! {
panic = "unwind" => {
if !guard.panicking && crate::panicking::panicking() {
self.failed.store(true, Ordering::Relaxed);
}
},
_ => {}
}
}
#[inline]
pub fn get(&self) -> bool {
cfg_select! {
panic = "unwind" => self.failed.load(Ordering::Relaxed),
_ => false,
}
}
#[inline]
pub fn clear(&self) {
#[cfg(panic = "unwind")]
self.failed.store(false, Ordering::Relaxed)
}
}
#[derive(Clone)]
pub struct Guard {
#[cfg(panic = "unwind")]
panicking: bool,
}
pub struct PoisonError<T> {
data: T,
#[cfg(not(panic = "unwind"))]
_never: core::convert::Infallible,
}
pub enum TryLockError<T> {
Poisoned(PoisonError<T>),
WouldBlock,
}
pub type LockResult<T> = Result<T, PoisonError<T>>;
pub type TryLockResult<Guard> = Result<Guard, TryLockError<Guard>>;
impl<T> fmt::Debug for PoisonError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PoisonError").finish_non_exhaustive()
}
}
impl<T> fmt::Display for PoisonError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
"poisoned lock: another task failed inside".fmt(f)
}
}
impl<T> Error for PoisonError<T> {
#[allow(deprecated)]
fn description(&self) -> &str {
"poisoned lock: another task failed inside"
}
}
impl<T> PoisonError<T> {
#[cfg(panic = "unwind")]
pub fn new(data: T) -> PoisonError<T> {
PoisonError { data }
}
#[cfg(not(panic = "unwind"))]
#[track_caller]
pub fn new(_data: T) -> PoisonError<T> {
panic!("PoisonError created in a pspsdk built with panic=\"abort\"")
}
pub fn into_inner(self) -> T {
self.data
}
pub fn get_ref(&self) -> &T {
&self.data
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.data
}
}
impl<T> From<PoisonError<T>> for TryLockError<T> {
fn from(err: PoisonError<T>) -> TryLockError<T> {
TryLockError::Poisoned(err)
}
}
impl<T> fmt::Debug for TryLockError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
#[cfg(panic = "unwind")]
TryLockError::Poisoned(..) => "Poisoned(..)".fmt(f),
#[cfg(not(panic = "unwind"))]
TryLockError::Poisoned(ref p) => match p._never {},
TryLockError::WouldBlock => "WouldBlock".fmt(f),
}
}
}
impl<T> fmt::Display for TryLockError<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
#[cfg(panic = "unwind")]
TryLockError::Poisoned(..) => "poisoned lock: another task failed inside",
#[cfg(not(panic = "unwind"))]
TryLockError::Poisoned(ref p) => match p._never {},
TryLockError::WouldBlock => "try_lock failed because the operation would block",
}
.fmt(f)
}
}
impl<T> Error for TryLockError<T> {
#[allow(deprecated)]
fn description(&self) -> &str {
match *self {
#[cfg(panic = "unwind")]
TryLockError::Poisoned(ref p) => p.description(),
#[cfg(not(panic = "unwind"))]
TryLockError::Poisoned(ref p) => match p._never {},
TryLockError::WouldBlock => "try_lock failed because the operation would block",
}
}
#[allow(deprecated)]
fn cause(&self) -> Option<&dyn Error> {
match *self {
#[cfg(panic = "unwind")]
TryLockError::Poisoned(ref p) => Some(p),
#[cfg(not(panic = "unwind"))]
TryLockError::Poisoned(ref p) => match p._never {},
_ => None,
}
}
}
pub fn map_result<T, U, F>(result: LockResult<T>, f: F) -> LockResult<U>
where
F: FnOnce(T) -> U,
{
match result {
Ok(t) => Ok(f(t)),
#[cfg(panic = "unwind")]
Err(PoisonError { data: guard, .. }) => Err(PoisonError::new(f(guard))),
}
}