NamedSemaphore

Struct NamedSemaphore 

Source
pub struct NamedSemaphore { /* private fields */ }
Expand description

Represents a POSIX named semaphore - a semaphore with a corresponding file handle which can be opened by other processes. The filename corresponds to the semaphore name. A named semaphore is created by the NamedSemaphoreBuilder.

§Example

§In process 1


use iceoryx2_bb_posix::semaphore::*;
use iceoryx2_bb_posix::clock::*;
use core::time::Duration;
use iceoryx2_bb_system_types::file_name::FileName;
use iceoryx2_bb_container::semantic_string::*;

let name = FileName::new(b"mySemaphoreName").unwrap();
let semaphore = NamedSemaphoreBuilder::new(&name)
                    .creation_mode(CreationMode::PurgeAndCreate)
                    .permission(Permission::OWNER_ALL)
                    .create()
                    .expect("failed to create semaphore");

loop {
    nanosleep(Duration::from_secs(1));
    println!("trigger process 2");
    semaphore.post().expect("failed to trigger semaphore");
}

§In process 2


use iceoryx2_bb_posix::semaphore::*;
use iceoryx2_bb_system_types::file_name::FileName;
use iceoryx2_bb_container::semantic_string::*;

let name = FileName::new(b"mySemaphoreName").unwrap();
let semaphore = NamedSemaphoreBuilder::new(&name)
                    .open_existing()
                    .expect("failed to open semaphore");

loop {
    semaphore.blocking_wait().expect("failed to wait on semaphore");
    println!("process 1 has triggered me");
}

§Output

When both processes are running in two separate terminals one can observe that process 1 triggers process 2 every second.

Implementations§

Source§

impl NamedSemaphore

Source

pub fn name(&self) -> &FileName

Returns the name of the named semaphore

Trait Implementations§

Source§

impl Debug for NamedSemaphore

Source§

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

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

impl Drop for NamedSemaphore

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl SemaphoreInterface for NamedSemaphore

Source§

fn post(&self) -> Result<(), SemaphorePostError>

Increments the semaphore by one. If the semaphore already holds the maximum supported value another post call will lead to SemaphorePostError::Overflow.
Source§

fn blocking_wait(&self) -> Result<(), SemaphoreWaitError>

Decrements the semaphore by one. If the semaphore is zero it waits until a SemaphoreInterface::post() call incremented the semaphore by one. A semaphores internal value is always greater or equal to zero.
Source§

fn try_wait(&self) -> Result<bool, SemaphoreWaitError>

Tries to decrement the semaphore by one if it is greater zero and returns true. If the semaphores internal value is zero it returns false and does not decrement the semaphore.
Source§

fn timed_wait(&self, timeout: Duration) -> Result<bool, SemaphoreTimedWaitError>

Tries to decrement the semaphore until the decrement was successful and returns true or the timeout has passed and then returns false.
Source§

fn clock_type(&self) -> ClockType

Source§

impl Send for NamedSemaphore

Source§

impl Sync for NamedSemaphore

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<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.