diff --git a/esp-preempt/src/semaphore.rs b/esp-preempt/src/semaphore.rs index 868fbb9e5..f4a228f44 100644 --- a/esp-preempt/src/semaphore.rs +++ b/esp-preempt/src/semaphore.rs @@ -32,6 +32,10 @@ impl SemaphoreInner { false } } + + fn current_count(&mut self) -> u32 { + self.current + } } pub struct Semaphore { @@ -84,6 +88,10 @@ impl Semaphore { Self::yield_loop_with_timeout(timeout_us, || self.try_take()) } + pub fn current_count(&self) -> u32 { + self.inner.with(|sem| sem.current_count()) + } + pub fn give(&self) -> bool { self.inner.with(|sem| sem.try_give()) } @@ -112,6 +120,12 @@ impl SemaphoreImplementation for Semaphore { semaphore.give() } + unsafe fn current_count(semaphore: SemaphorePtr) -> u32 { + let semaphore = unsafe { Semaphore::from_ptr(semaphore) }; + + semaphore.current_count() + } + unsafe fn try_take(semaphore: SemaphorePtr) -> bool { let semaphore = unsafe { Semaphore::from_ptr(semaphore) }; diff --git a/esp-radio-preempt-driver/src/semaphore.rs b/esp-radio-preempt-driver/src/semaphore.rs index ab1d8511d..5ff393aa9 100644 --- a/esp-radio-preempt-driver/src/semaphore.rs +++ b/esp-radio-preempt-driver/src/semaphore.rs @@ -11,6 +11,7 @@ unsafe extern "Rust" { fn esp_preempt_semaphore_take(semaphore: SemaphorePtr, timeout_us: Option) -> bool; fn esp_preempt_semaphore_give(semaphore: SemaphorePtr) -> bool; + fn esp_preempt_semaphore_current_count(semaphore: SemaphorePtr) -> u32; fn esp_preempt_semaphore_try_take(semaphore: SemaphorePtr) -> bool; } @@ -51,6 +52,13 @@ pub trait SemaphoreImplementation { /// `semaphore` must be a pointer returned from [`Self::create`]. unsafe fn give(semaphore: SemaphorePtr) -> bool; + /// Returns the semaphore's current counter value. + /// + /// # Safety + /// + /// `semaphore` must be a pointer returned from [`Self::create`]. + unsafe fn current_count(semaphore: SemaphorePtr) -> u32; + /// Attempts to decrement the semaphore's counter. /// /// If the counter is zero, this function must immediately return `false`. @@ -93,6 +101,12 @@ macro_rules! register_semaphore_implementation { unsafe { <$t as $crate::semaphore::SemaphoreImplementation>::give(semaphore) } } + #[unsafe(no_mangle)] + #[inline] + fn esp_preempt_semaphore_current_count(semaphore: $crate::semaphore::SemaphorePtr) -> u32 { + unsafe { <$t as $crate::semaphore::SemaphoreImplementation>::current_count(semaphore) } + } + #[unsafe(no_mangle)] #[inline] fn esp_preempt_semaphore_try_take(semaphore: $crate::semaphore::SemaphorePtr) -> bool { @@ -157,6 +171,11 @@ impl SemaphoreHandle { unsafe { esp_preempt_semaphore_give(self.0) } } + /// Returns the current counter value. + pub fn current_count(&self) -> u32 { + unsafe { esp_preempt_semaphore_current_count(self.0) } + } + /// Attempts to decrement the semaphore's counter. /// /// If the counter is zero, this function returns `false`.