SemaphoreImplementation

Trait SemaphoreImplementation 

Source
pub trait SemaphoreImplementation {
    // Required methods
    fn create(kind: SemaphoreKind) -> SemaphorePtr;
    unsafe fn delete(semaphore: SemaphorePtr);
    unsafe fn take(semaphore: SemaphorePtr, timeout_us: Option<u32>) -> bool;
    unsafe fn give(semaphore: SemaphorePtr) -> bool;
    unsafe fn try_give_from_isr(
        semaphore: SemaphorePtr,
        higher_prio_task_waken: Option<&mut bool>,
    ) -> bool;
    unsafe fn current_count(semaphore: SemaphorePtr) -> u32;
    unsafe fn try_take(semaphore: SemaphorePtr) -> bool;
    unsafe fn try_take_from_isr(
        semaphore: SemaphorePtr,
        higher_prio_task_waken: Option<&mut bool>,
    ) -> bool;
}
Expand description

A semaphore primitive.

The following snippet demonstrates the boilerplate necessary to implement a semaphore using the SemaphoreImplementation trait:

use esp_radio_rtos_driver::{
    register_semaphore_implementation,
    semaphore::{SemaphoreImplementation, SemaphoreKind, SemaphorePtr},
};

struct MySemaphore {
    // Semaphore implementation details
}

impl SemaphoreImplementation for MySemaphore {
    fn create(kind: SemaphoreKind) -> SemaphorePtr {
        unimplemented!()
    }

    unsafe fn delete(semaphore: SemaphorePtr) {
        unimplemented!()
    }

    unsafe fn take(semaphore: SemaphorePtr, timeout_us: Option<u32>) -> bool {
        unimplemented!()
    }

    unsafe fn give(semaphore: SemaphorePtr) -> bool {
        unimplemented!()
    }

    unsafe fn try_give_from_isr(
        semaphore: SemaphorePtr,
        higher_prio_task_waken: Option<&mut bool>,
    ) -> bool {
        unimplemented!()
    }

    unsafe fn current_count(semaphore: SemaphorePtr) -> u32 {
        unimplemented!()
    }

    unsafe fn try_take(semaphore: SemaphorePtr) -> bool {
        unimplemented!()
    }

    unsafe fn try_take_from_isr(
        semaphore: SemaphorePtr,
        higher_prio_task_waken: Option<&mut bool>,
    ) -> bool {
        unimplemented!()
    }
}

register_semaphore_implementation!(MySemaphore);

Required Methods§

Source

fn create(kind: SemaphoreKind) -> SemaphorePtr

Creates a new semaphore instance.

kind specifies the type of semaphore to create.

  • SemaphoreKind::Counting should create counting, non-recursive semaphores/mutexes.
  • SemaphoreKind::RecursiveMutex should create recursive mutexes.
Source

unsafe fn delete(semaphore: SemaphorePtr)

Deletes a semaphore instance.

§Safety

semaphore must be a pointer returned from Self::create.

Source

unsafe fn take(semaphore: SemaphorePtr, timeout_us: Option<u32>) -> bool

Increments the semaphore’s counter.

If a timeout is given, this function should block until either a semaphore could be taken, or the timeout has been reached. If no timeout is specified, the function should block indefinitely.

Recursive mutexes can be repeatedly taken by the same task.

The timeout is specified in microseconds.

This function returns true if the semaphore was taken, false if the timeout was reached.

§Safety

semaphore must be a pointer returned from Self::create.

Source

unsafe fn give(semaphore: SemaphorePtr) -> bool

Increments the semaphore’s counter.

This function returns true if the semaphore was given, false if the counter is at its maximum.

Recursive mutexes can not be given by a task other than the one that first locked it.

§Safety

semaphore must be a pointer returned from Self::create.

Source

unsafe fn try_give_from_isr( semaphore: SemaphorePtr, higher_prio_task_waken: Option<&mut bool>, ) -> bool

Attempts to increment the semaphore’s counter from an ISR.

This function returns true if the semaphore was given, false if the counter is at its maximum.

The higher_prio_task_waken parameter is an optional mutable reference to a boolean flag. If the flag is Some, the implementation may set it to true to request a context switch.

§Safety

semaphore must be a pointer returned from Self::create.

Source

unsafe fn current_count(semaphore: SemaphorePtr) -> u32

Returns the semaphore’s current counter value.

§Safety

semaphore must be a pointer returned from Self::create.

Source

unsafe fn try_take(semaphore: SemaphorePtr) -> bool

Attempts to decrement the semaphore’s counter.

If the counter is zero, this function must immediately return false.

§Safety

semaphore must be a pointer returned from Self::create.

Source

unsafe fn try_take_from_isr( semaphore: SemaphorePtr, higher_prio_task_waken: Option<&mut bool>, ) -> bool

Attempts to decrement the semaphore’s counter from an ISR.

If the counter is zero, this function must immediately return false.

The higher_prio_task_waken parameter is an optional mutable reference to a boolean flag. If the flag is Some, the implementation may set it to true to request a context switch.

§Safety

semaphore must be a pointer returned from Self::create.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§