mirror of
https://github.com/esp-rs/esp-hal.git
synced 2025-09-27 04:10:28 +00:00
Add a function to retrieve a semaphore's counter value (#4066)
This commit is contained in:
parent
6dd0edd492
commit
b9dd86258a
@ -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) };
|
||||
|
||||
|
@ -11,6 +11,7 @@ unsafe extern "Rust" {
|
||||
|
||||
fn esp_preempt_semaphore_take(semaphore: SemaphorePtr, timeout_us: Option<u32>) -> 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`.
|
||||
|
Loading…
x
Reference in New Issue
Block a user