diff --git a/modules/axsync/src/condvar/multitask.rs b/modules/axsync/src/condvar.rs similarity index 89% rename from modules/axsync/src/condvar/multitask.rs rename to modules/axsync/src/condvar.rs index 45e5858f40..9a73a23e4d 100644 --- a/modules/axsync/src/condvar/multitask.rs +++ b/modules/axsync/src/condvar.rs @@ -1,3 +1,13 @@ +//! The Condition Variable +//! +//! Implementation adapted from the 'RwLock' type of the standard library. See: +//! +//! +//! Note: [`Condvar`] is not available when the `multitask` feature is disabled. + +#[cfg(test)] +mod tests; + use core::fmt; use core::sync::atomic::AtomicU32; use core::sync::atomic::Ordering::Relaxed; @@ -5,9 +15,25 @@ use core::time::Duration; use axtask::WaitQueue; -use crate::condvar::WaitTimeoutResult; use crate::{mutex, MutexGuard}; +/// A type indicating whether a timed wait on a condition variable returned +/// due to a time out or not. +/// +/// It is returned by the [`wait_timeout`] method. +/// +/// [`wait_timeout`]: Condvar::wait_timeout +#[derive(Debug, PartialEq, Eq, Copy, Clone)] +pub struct WaitTimeoutResult(bool); + +impl WaitTimeoutResult { + /// Returns `true` if the wait was known to have timed out. + #[must_use] + pub fn timed_out(&self) -> bool { + self.0 + } +} + /// A Condition Variable /// /// Condition variables represent the ability to block a thread such that it @@ -156,6 +182,10 @@ impl Condvar { (mutex.lock(), WaitTimeoutResult(!success)) } + /// Waits on this condition variable for a notification, timing out after a + /// specified duration. + /// + /// This function is not available when the `irq` feature is disabled. #[cfg(not(feature = "irq"))] pub fn wait_timeout<'a, T>( &self, @@ -210,6 +240,10 @@ impl Condvar { } } + /// Waits on this condition variable for a notification, timing out after a + /// specified duration. + /// + /// This function is not available when the `irq` feature is disabled. #[cfg(not(feature = "irq"))] pub fn wait_timeout_while<'a, T, F>( &self, diff --git a/modules/axsync/src/condvar/mod.rs b/modules/axsync/src/condvar/mod.rs deleted file mode 100644 index 707918b9f9..0000000000 --- a/modules/axsync/src/condvar/mod.rs +++ /dev/null @@ -1,36 +0,0 @@ -//! The Condition Variable -//! -//! Implementation adapted from the 'RwLock' type of the standard library. See: -//! -//! -//! Note: [`Condvar`] is not available when the `multitask` feature is disabled. - -#[cfg(not(feature = "multitask"))] -mod no_thread; -#[cfg(not(feature = "multitask"))] -pub use no_thread::Condvar; - -#[cfg(feature = "multitask")] -mod multitask; -#[cfg(feature = "multitask")] -pub use multitask::Condvar; - -/// A type indicating whether a timed wait on a condition variable returned -/// due to a time out or not. -/// -/// It is returned by the [`wait_timeout`] method. -/// -/// [`wait_timeout`]: Condvar::wait_timeout -#[derive(Debug, PartialEq, Eq, Copy, Clone)] -pub struct WaitTimeoutResult(bool); - -impl WaitTimeoutResult { - /// Returns `true` if the wait was known to have timed out. - #[must_use] - pub fn timed_out(&self) -> bool { - self.0 - } -} - -#[cfg(test)] -mod tests; diff --git a/modules/axsync/src/condvar/no_thread.rs b/modules/axsync/src/condvar/no_thread.rs deleted file mode 100644 index 6c4f493ea2..0000000000 --- a/modules/axsync/src/condvar/no_thread.rs +++ /dev/null @@ -1,54 +0,0 @@ -//! Dummy implementation of `Condvar` for single-threaded environments. -use crate::MutexGuard; - -pub struct Condvar {} - -impl Condvar { - #[inline] - pub const fn new() -> Condvar { - Condvar {} - } - - #[inline] - pub fn notify_one(&self) {} - - #[inline] - pub fn notify_all(&self) {} - - pub fn wait<'a, T>(&self, _guard: MutexGuard<'a, T>) -> MutexGuard<'a, T> { - panic!("condvar wait not supported") - } - - pub fn wait_while<'a, T, F>( - &self, - mut _guard: MutexGuard<'a, T>, - mut _condition: F, - ) -> MutexGuard<'a, T> - where - F: FnMut(&mut T) -> bool, - { - panic!("condvar wait_while not supported") - } - - #[cfg(feature = "irq")] - pub fn wait_timeout<'a, T>( - &self, - _guard: MutexGuard<'a, T>, - _dur: core::time::Duration, - ) -> (MutexGuard<'a, T>, WaitTimeoutResult) { - panic!("condvar wait_timeout not supported") - } - - #[cfg(feature = "irq")] - pub fn wait_timeout_while<'a, T, F>( - &self, - mut _guard: MutexGuard<'a, T>, - _dur: core::time::Duration, - mut _condition: F, - ) -> (MutexGuard<'a, T>, WaitTimeoutResult) - where - F: FnMut(&mut T) -> bool, - { - panic!("condvar wait_timeout_while not supported") - } -} diff --git a/modules/axsync/src/lib.rs b/modules/axsync/src/lib.rs index 508a6af7cd..ce4cc861ef 100644 --- a/modules/axsync/src/lib.rs +++ b/modules/axsync/src/lib.rs @@ -22,26 +22,26 @@ pub use kspin as spin; cfg_if::cfg_if! { if #[cfg(feature = "multitask")] { + mod barrier; + mod condvar; mod mutex; + mod semaphore; + + pub use self::barrier::{Barrier, BarrierWaitResult}; + pub use self::condvar::Condvar; #[doc(cfg(feature = "multitask"))] pub use self::mutex::{Mutex, MutexGuard}; + pub use semaphore::Semaphore; } else { #[doc(cfg(not(feature = "multitask")))] pub use kspin::{SpinNoIrq as Mutex, SpinNoIrqGuard as MutexGuard}; } } -mod barrier; -mod condvar; mod rwlock; -mod semaphore; - -pub use self::barrier::{Barrier, BarrierWaitResult}; -pub use self::condvar::Condvar; pub use self::rwlock::{ MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLock, RwLockReadGuard, RwLockWriteGuard, }; -pub use semaphore::Semaphore; #[cfg(test)] mod tests {