Skip to content

Commit

Permalink
[refactor] make barrier,condvar,semaphore only avaiable with multitas…
Browse files Browse the repository at this point in the history
…k enabled
  • Loading branch information
hky1999 committed Oct 29, 2024
1 parent f74da97 commit 803d94a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,39 @@
//! The Condition Variable
//!
//! Implementation adapted from the 'RwLock' type of the standard library. See:
//! <https://doc.rust-lang.org/stable/std/sync/struct.Condvar.html>
//!
//! 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;
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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
36 changes: 0 additions & 36 deletions modules/axsync/src/condvar/mod.rs

This file was deleted.

54 changes: 0 additions & 54 deletions modules/axsync/src/condvar/no_thread.rs

This file was deleted.

14 changes: 7 additions & 7 deletions modules/axsync/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 803d94a

Please sign in to comment.