Skip to main content

safa_abi/
poll.rs

1#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
2#[repr(transparent)]
3pub struct PollEvents(u16);
4
5impl PollEvents {
6    /// Waiting for no events, ignored.
7    pub const NONE: Self = Self(0);
8    /// Waiting for data to be available to read.
9    pub const DATA_AVAILABLE: Self = Self(1 << 0);
10    /// Waiting for the resource to be writable without blocking.
11    pub const CAN_WRITE: Self = Self(1 << 1);
12    /// The given resource is disconnected, usually is returned and not awaited for.
13    /// reads may still be possible if there is data available.
14    pub const DISCONNECTED: Self = Self(1 << 2);
15    /// Waiting for all events.
16    pub const ALL: Self = Self(u16::MAX);
17
18    pub const fn contains(&self, other: Self) -> bool {
19        self.0 & other.0 == other.0
20    }
21
22    pub const fn intersects(&self, other: Self) -> bool {
23        self.0 & other.0 != 0
24    }
25
26    pub const fn is_empty(&self) -> bool {
27        self.0 == 0
28    }
29
30    pub const fn union(&self, other: Self) -> Self {
31        Self(self.0 | other.0)
32    }
33
34    pub const fn intersection(&self, other: Self) -> Self {
35        Self(self.0 & other.0)
36    }
37
38    pub const fn difference(&self, other: Self) -> Self {
39        Self(self.0 & !other.0)
40    }
41}
42
43/// The layout of a single entry passed to [`crate::syscalls::SyscallTable::SysIOPoll`].
44#[derive(Debug, Clone, Copy, PartialEq, Eq)]
45#[repr(C)]
46pub struct PollEntry {
47    resource: u32,
48    events: PollEvents,
49    returned_events: PollEvents,
50}
51
52impl PollEntry {
53    pub const fn new(resource: u32, events: PollEvents) -> Self {
54        Self {
55            resource,
56            events,
57            returned_events: PollEvents::NONE,
58        }
59    }
60
61    /// Returns the resource ID associated with this poll entry.
62    pub const fn resource(&self) -> u32 {
63        self.resource
64    }
65
66    /// Returns the events associated with this poll entry.
67    pub const fn events(&self) -> PollEvents {
68        self.events
69    }
70
71    /// Returns the events that were returned by the poll operation.
72    pub const fn returned_events(&self) -> PollEvents {
73        self.returned_events
74    }
75
76    /// Returns a mutable reference to the events that were returned by the poll operation.
77    pub const fn returned_events_mut(&mut self) -> &mut PollEvents {
78        &mut self.returned_events
79    }
80}