Skip to main content

iceoryx2_cal/event/
mod.rs

1// Copyright (c) 2023 Contributors to the Eclipse Foundation
2//
3// See the NOTICE file(s) distributed with this work for additional
4// information regarding copyright ownership.
5//
6// This program and the accompanying materials are made available under the
7// terms of the Apache Software License 2.0 which is available at
8// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
9// which is available at https://opensource.org/licenses/MIT.
10//
11// SPDX-License-Identifier: Apache-2.0 OR MIT
12
13pub mod common;
14pub mod id_tracker;
15pub mod process_local_socketpair;
16pub mod recommended;
17pub mod sem_bitset_posix_shared_memory;
18pub mod sem_bitset_process_local;
19pub mod signal_mechanism;
20pub mod unix_datagram_socket;
21
22use core::{fmt::Debug, time::Duration};
23
24pub use crate::named_concept::{NamedConcept, NamedConceptBuilder, NamedConceptMgmt};
25use iceoryx2_bb_elementary_traits::testing::abandonable::Abandonable;
26pub use iceoryx2_bb_system_types::file_name::*;
27pub use iceoryx2_bb_system_types::path::Path;
28
29#[derive(Debug, PartialEq, Eq, Clone, Copy)]
30pub enum NotifierNotifyError {
31    Interrupt,
32    FailedToDeliverSignal,
33    TriggerIdOutOfBounds,
34    Disconnected,
35    InternalFailure,
36}
37
38impl core::fmt::Display for NotifierNotifyError {
39    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
40        write!(f, "{}::{:?}", stringify!(Self), self)
41    }
42}
43
44impl core::error::Error for NotifierNotifyError {}
45
46#[derive(Debug, PartialEq, Eq, Clone, Copy)]
47pub enum NotifierCreateError {
48    Interrupt,
49    DoesNotExist,
50    InsufficientPermissions,
51    VersionMismatch,
52    InitializationNotYetFinalized,
53    InternalFailure,
54}
55
56impl core::fmt::Display for NotifierCreateError {
57    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
58        write!(f, "{}::{:?}", stringify!(Self), self)
59    }
60}
61
62impl core::error::Error for NotifierCreateError {}
63
64#[derive(Debug, PartialEq, Eq, Clone, Copy)]
65pub enum ListenerWaitError {
66    ContractViolation,
67    InternalFailure,
68    InterruptSignal,
69}
70
71impl core::fmt::Display for ListenerWaitError {
72    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
73        write!(f, "{}::{:?}", stringify!(Self), self)
74    }
75}
76
77impl core::error::Error for ListenerWaitError {}
78
79#[derive(Debug, PartialEq, Eq, Clone, Copy)]
80pub enum ListenerCreateError {
81    AlreadyExists,
82    InsufficientPermissions,
83    InternalFailure,
84}
85
86impl core::fmt::Display for ListenerCreateError {
87    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
88        write!(f, "{}::{:?}", stringify!(Self), self)
89    }
90}
91
92impl core::error::Error for ListenerCreateError {}
93
94#[derive(Default, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
95pub struct TriggerId(usize);
96
97impl TriggerId {
98    pub const fn new(value: usize) -> Self {
99        Self(value)
100    }
101
102    pub const fn as_value(&self) -> usize {
103        self.0
104    }
105}
106
107pub trait Notifier: NamedConcept + Debug + Send + Abandonable {
108    fn trigger_id_max(&self) -> TriggerId {
109        TriggerId::new(usize::MAX)
110    }
111    fn notify(&self, id: TriggerId) -> Result<(), NotifierNotifyError>;
112}
113
114pub trait NotifierBuilder<T: Event>: NamedConceptBuilder<T> + Debug {
115    fn timeout(self, timeout: Duration) -> Self;
116    fn open(self) -> Result<T::Notifier, NotifierCreateError>;
117}
118
119pub trait Listener: NamedConcept + Debug + Send + Abandonable {
120    const IS_FILE_DESCRIPTOR_BASED: bool = false;
121
122    fn try_wait_one(&self) -> Result<Option<TriggerId>, ListenerWaitError>;
123    fn timed_wait_one(&self, timeout: Duration) -> Result<Option<TriggerId>, ListenerWaitError>;
124    fn blocking_wait_one(&self) -> Result<Option<TriggerId>, ListenerWaitError>;
125
126    fn try_wait_all<F: FnMut(TriggerId)>(&self, callback: F) -> Result<(), ListenerWaitError>;
127    fn timed_wait_all<F: FnMut(TriggerId)>(
128        &self,
129        callback: F,
130        timeout: Duration,
131    ) -> Result<(), ListenerWaitError>;
132    fn blocking_wait_all<F: FnMut(TriggerId)>(&self, callback: F) -> Result<(), ListenerWaitError>;
133}
134
135pub trait ListenerBuilder<T: Event>: NamedConceptBuilder<T> + Debug {
136    fn trigger_id_max(self, id: TriggerId) -> Self;
137    fn create(self) -> Result<T::Listener, ListenerCreateError>;
138}
139
140pub trait Event: Sized + NamedConceptMgmt + Debug {
141    type Notifier: Notifier;
142    type NotifierBuilder: NotifierBuilder<Self>;
143    type Listener: Listener;
144    type ListenerBuilder: ListenerBuilder<Self>;
145
146    /// The default suffix of every event
147    fn default_suffix() -> FileName {
148        unsafe { FileName::new_unchecked(b".event") }
149    }
150
151    fn has_trigger_id_limit() -> bool {
152        false
153    }
154}