use core::ptr;
use crate::kernel::queue::{
queueQUEUE_TYPE_BINARY_SEMAPHORE, queueSEND_TO_BACK, xQueueCreateCountingSemaphoreStatic,
xQueueGenericCreateStatic, xQueueGenericSend, xQueueSemaphoreTake, QueueHandle_t,
StaticQueue_t,
};
#[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
use crate::kernel::queue::xQueueGenericCreate;
use crate::types::*;
pub struct BinarySemaphore {
handle: QueueHandle_t,
}
unsafe impl Sync for BinarySemaphore {}
unsafe impl Send for BinarySemaphore {}
impl BinarySemaphore {
#[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
pub fn new() -> Option<Self> {
let handle = unsafe { xQueueGenericCreate(1, 0, queueQUEUE_TYPE_BINARY_SEMAPHORE) };
if handle.is_null() {
None
} else {
Some(Self { handle })
}
}
pub fn new_static(sem_buffer: &'static mut StaticQueue_t) -> Option<Self> {
let handle = unsafe {
xQueueGenericCreateStatic(
1,
0,
ptr::null_mut(),
sem_buffer as *mut StaticQueue_t,
queueQUEUE_TYPE_BINARY_SEMAPHORE,
)
};
if handle.is_null() {
None
} else {
Some(Self { handle })
}
}
pub fn take(&self) -> bool {
unsafe { xQueueSemaphoreTake(self.handle, portMAX_DELAY) == pdTRUE }
}
pub fn try_take(&self) -> bool {
unsafe { xQueueSemaphoreTake(self.handle, 0) == pdTRUE }
}
pub fn take_timeout(&self, ticks: TickType_t) -> bool {
unsafe { xQueueSemaphoreTake(self.handle, ticks) == pdTRUE }
}
pub fn give(&self) -> bool {
unsafe {
xQueueGenericSend(self.handle, core::ptr::null(), 0, queueSEND_TO_BACK) == pdTRUE
}
}
pub unsafe fn raw_handle(&self) -> QueueHandle_t {
self.handle
}
}
pub struct CountingSemaphore {
handle: QueueHandle_t,
}
unsafe impl Sync for CountingSemaphore {}
unsafe impl Send for CountingSemaphore {}
impl CountingSemaphore {
#[cfg(any(feature = "alloc", feature = "heap-4", feature = "heap-5"))]
pub fn new(max_count: UBaseType_t, initial_count: UBaseType_t) -> Option<Self> {
let handle = unsafe {
crate::kernel::queue::xQueueCreateCountingSemaphore(max_count, initial_count)
};
if handle.is_null() {
None
} else {
Some(Self { handle })
}
}
pub fn new_static(
max_count: UBaseType_t,
initial_count: UBaseType_t,
sem_buffer: &'static mut StaticQueue_t,
) -> Option<Self> {
let handle = unsafe {
xQueueCreateCountingSemaphoreStatic(
max_count,
initial_count,
sem_buffer as *mut StaticQueue_t,
)
};
if handle.is_null() {
None
} else {
Some(Self { handle })
}
}
pub fn take(&self) -> bool {
unsafe { xQueueSemaphoreTake(self.handle, portMAX_DELAY) == pdTRUE }
}
pub fn try_take(&self) -> bool {
unsafe { xQueueSemaphoreTake(self.handle, 0) == pdTRUE }
}
pub fn take_timeout(&self, ticks: TickType_t) -> bool {
unsafe { xQueueSemaphoreTake(self.handle, ticks) == pdTRUE }
}
pub fn give(&self) -> bool {
unsafe {
xQueueGenericSend(self.handle, core::ptr::null(), 0, queueSEND_TO_BACK) == pdTRUE
}
}
#[cfg(feature = "trace-facility")]
pub fn count(&self) -> UBaseType_t {
unsafe { crate::kernel::queue::uxQueueMessagesWaiting(self.handle) }
}
pub unsafe fn raw_handle(&self) -> QueueHandle_t {
self.handle
}
}