Skip to main content

Semaphore

Struct Semaphore 

Source
pub struct Semaphore(/* private fields */);
Expand description

Semaphore types for signaling and resource management. POSIX backend for SemaphoreFn. Built directly on a pthread_mutex_t + pthread_cond_t pair rather than sem_t, so signal() can be bounded by a user-supplied max_count and the mutex can use priority inheritance — neither of which plain POSIX unnamed semaphores support.

Fields (unnamed, accessed as self.0/self.1/self.2): the pthread handle, the current count, and the fixed maximum count.

Implementations§

Source§

impl Semaphore

Source

pub fn new(max_count: UBaseType, initial_count: UBaseType) -> Result<Self>

Creates a new semaphore with the given maximum and initial count. Semaphore::signal never raises the count above max_count; a binary semaphore is just max_count == 1.

§Examples
use osal_rs::os::*;

// Binary semaphore, starts "empty".
let sem = Semaphore::new(1, 0).unwrap();
assert_eq!(sem.wait_from_isr(), osal_rs::utils::OsalRsBool::False);

Methods from Deref<Target = SemaphoreHandle>§

Source

pub fn is_empty(&self) -> bool

Returns true if this handle is still in its never-initialized (or already-deleted) state, i.e. both the mutex and condition variable are all-zero.

§Examples
use osal_rs::os::types::ClockMonotonicHandle;

assert!(ClockMonotonicHandle::default().is_empty());

Trait Implementations§

Source§

impl Debug for Semaphore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for Semaphore

Source§

type Target = ClockMonotonicHandle

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Display for Semaphore

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Semaphore

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Semaphore for Semaphore

Source§

fn is_null(&self) -> bool

Returns true if this semaphore is never-initialized-or-already-deleted.

Unlike crate::os::EventGroupFn::is_null, the count is checked too, so a live semaphore that just happens to be momentarily empty (count == 0) is never mistaken for a deleted one.

§Examples
use osal_rs::os::*;

let mut sem = Semaphore::new(1, 0).unwrap();
assert!(!sem.is_null());

sem.delete();
assert!(sem.is_null());
Source§

fn wait(&self, ticks_to_wait: impl ToTick) -> OsalRsBool

Blocks until a unit is available or ticks_to_wait elapses (accepts any ToTick value, e.g. a raw tick count or a core::time::Duration; pass TickType::MAX ticks to wait forever), decrementing the count on success.

§Examples
use osal_rs::os::*;
use osal_rs::utils::OsalRsBool;
use core::time::Duration;

let sem = Semaphore::new(1, 1).unwrap();
assert_eq!(sem.wait(Duration::from_millis(100)), OsalRsBool::True);

// Count is now 0: waiting again times out instead of blocking forever.
assert_eq!(sem.wait(Duration::from_millis(10)), OsalRsBool::False);
Source§

fn wait_from_isr(&self) -> OsalRsBool

ISR-safe variant of Semaphore::wait. POSIX has no interrupt context of its own, so this never blocks (trylock instead of lock, and no timeout parameter); it returns OsalRsBool::False both when the mutex is contended and when the count is simply zero.

§Examples
use osal_rs::os::*;
use osal_rs::utils::OsalRsBool;

let sem = Semaphore::new(1, 1).unwrap();
assert_eq!(sem.wait_from_isr(), OsalRsBool::True);
assert_eq!(sem.wait_from_isr(), OsalRsBool::False);
Source§

fn signal(&self) -> OsalRsBool

Increments the count (unless already at max_count) and wakes any thread blocked in Semaphore::wait. Returns OsalRsBool::False if the semaphore was already at max_count.

§Examples
use osal_rs::os::*;
use osal_rs::utils::OsalRsBool;

let sem = Semaphore::new(1, 0).unwrap();
assert_eq!(sem.signal(), OsalRsBool::True);

// Already at max_count: signalling again fails.
assert_eq!(sem.signal(), OsalRsBool::False);
Source§

fn signal_from_isr(&self) -> OsalRsBool

ISR-safe variant of Semaphore::signal. Fails with OsalRsBool::False instead of blocking if the mutex is contended.

§Examples
use osal_rs::os::*;
use osal_rs::utils::OsalRsBool;

let sem = Semaphore::new(1, 0).unwrap();
assert_eq!(sem.signal_from_isr(), OsalRsBool::True);
Source§

fn delete(&mut self)

Destroys the underlying pthread objects and resets this semaphore to its “null” state. Safe to call more than once, and called automatically on Drop if not called explicitly.

§Examples
use osal_rs::os::*;

let mut sem = Semaphore::new(1, 0).unwrap();
sem.delete();
assert!(sem.is_null());
Source§

impl Send for Semaphore

Source§

impl Sync for Semaphore

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.