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§
Trait Implementations§
Source§impl Debug for NamedSemaphore
impl Debug for NamedSemaphore
Source§impl Drop for NamedSemaphore
impl Drop for NamedSemaphore
Source§impl SemaphoreInterface for NamedSemaphore
impl SemaphoreInterface for NamedSemaphore
Source§fn post(&self) -> Result<(), SemaphorePostError>
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>
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>
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>
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.
fn clock_type(&self) -> ClockType
impl Send for NamedSemaphore
impl Sync for NamedSemaphore
Auto Trait Implementations§
impl Freeze for NamedSemaphore
impl RefUnwindSafe for NamedSemaphore
impl Unpin for NamedSemaphore
impl UnwindSafe for NamedSemaphore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more