Skip to content

Commit b9dd862

Browse files
authored
Add a function to retrieve a semaphore's counter value (#4066)
1 parent 6dd0edd commit b9dd862

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

esp-preempt/src/semaphore.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ impl SemaphoreInner {
3232
false
3333
}
3434
}
35+
36+
fn current_count(&mut self) -> u32 {
37+
self.current
38+
}
3539
}
3640

3741
pub struct Semaphore {
@@ -84,6 +88,10 @@ impl Semaphore {
8488
Self::yield_loop_with_timeout(timeout_us, || self.try_take())
8589
}
8690

91+
pub fn current_count(&self) -> u32 {
92+
self.inner.with(|sem| sem.current_count())
93+
}
94+
8795
pub fn give(&self) -> bool {
8896
self.inner.with(|sem| sem.try_give())
8997
}
@@ -112,6 +120,12 @@ impl SemaphoreImplementation for Semaphore {
112120
semaphore.give()
113121
}
114122

123+
unsafe fn current_count(semaphore: SemaphorePtr) -> u32 {
124+
let semaphore = unsafe { Semaphore::from_ptr(semaphore) };
125+
126+
semaphore.current_count()
127+
}
128+
115129
unsafe fn try_take(semaphore: SemaphorePtr) -> bool {
116130
let semaphore = unsafe { Semaphore::from_ptr(semaphore) };
117131

esp-radio-preempt-driver/src/semaphore.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ unsafe extern "Rust" {
1111

1212
fn esp_preempt_semaphore_take(semaphore: SemaphorePtr, timeout_us: Option<u32>) -> bool;
1313
fn esp_preempt_semaphore_give(semaphore: SemaphorePtr) -> bool;
14+
fn esp_preempt_semaphore_current_count(semaphore: SemaphorePtr) -> u32;
1415

1516
fn esp_preempt_semaphore_try_take(semaphore: SemaphorePtr) -> bool;
1617
}
@@ -51,6 +52,13 @@ pub trait SemaphoreImplementation {
5152
/// `semaphore` must be a pointer returned from [`Self::create`].
5253
unsafe fn give(semaphore: SemaphorePtr) -> bool;
5354

55+
/// Returns the semaphore's current counter value.
56+
///
57+
/// # Safety
58+
///
59+
/// `semaphore` must be a pointer returned from [`Self::create`].
60+
unsafe fn current_count(semaphore: SemaphorePtr) -> u32;
61+
5462
/// Attempts to decrement the semaphore's counter.
5563
///
5664
/// If the counter is zero, this function must immediately return `false`.
@@ -93,6 +101,12 @@ macro_rules! register_semaphore_implementation {
93101
unsafe { <$t as $crate::semaphore::SemaphoreImplementation>::give(semaphore) }
94102
}
95103

104+
#[unsafe(no_mangle)]
105+
#[inline]
106+
fn esp_preempt_semaphore_current_count(semaphore: $crate::semaphore::SemaphorePtr) -> u32 {
107+
unsafe { <$t as $crate::semaphore::SemaphoreImplementation>::current_count(semaphore) }
108+
}
109+
96110
#[unsafe(no_mangle)]
97111
#[inline]
98112
fn esp_preempt_semaphore_try_take(semaphore: $crate::semaphore::SemaphorePtr) -> bool {
@@ -157,6 +171,11 @@ impl SemaphoreHandle {
157171
unsafe { esp_preempt_semaphore_give(self.0) }
158172
}
159173

174+
/// Returns the current counter value.
175+
pub fn current_count(&self) -> u32 {
176+
unsafe { esp_preempt_semaphore_current_count(self.0) }
177+
}
178+
160179
/// Attempts to decrement the semaphore's counter.
161180
///
162181
/// If the counter is zero, this function returns `false`.

0 commit comments

Comments
 (0)