|
| 1 | +//! Solaris/Illumos event ports. |
| 2 | +
|
| 3 | +use crate::backend::{c, io::syscalls}; |
| 4 | +use crate::fd::{AsFd, AsRawFd, OwnedFd}; |
| 5 | +use crate::io; |
| 6 | + |
| 7 | +use super::PollFlags; |
| 8 | + |
| 9 | +use core::convert::TryInto; |
| 10 | +use core::time::Duration; |
| 11 | + |
| 12 | +/// The structure representing a port event. |
| 13 | +#[repr(transparent)] |
| 14 | +pub struct Event(pub(crate) c::port_event); |
| 15 | + |
| 16 | +impl Event { |
| 17 | + /// Get the events associated with this event. |
| 18 | + pub fn events(&self) -> i32 { |
| 19 | + self.0.portev_events |
| 20 | + } |
| 21 | + |
| 22 | + /// Get the event source associated with this event. |
| 23 | + pub fn object(&self) -> usize { |
| 24 | + self.0.portev_object |
| 25 | + } |
| 26 | + |
| 27 | + /// Get the userdata associated with this event. |
| 28 | + pub fn userdata(&self) -> *mut c::c_void { |
| 29 | + self.0.portev_user |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// `port_create()`- Creates a new port. |
| 34 | +/// |
| 35 | +/// # References |
| 36 | +/// |
| 37 | +/// - [OpenSolaris] |
| 38 | +/// - [illumos] |
| 39 | +/// |
| 40 | +/// [OpenSolaris]: https://www.unix.com/man-page/opensolaris/3C/port_create/ |
| 41 | +/// [illumos]: https://illumos.org/man/3C/port_create |
| 42 | +pub fn port_create() -> io::Result<OwnedFd> { |
| 43 | + syscalls::port_create() |
| 44 | +} |
| 45 | + |
| 46 | +/// `port_associate(_, PORT_SOURCE_FD, _, _, _)`- Associates a file descriptor |
| 47 | +/// with a port. |
| 48 | +/// |
| 49 | +/// # Safety |
| 50 | +/// |
| 51 | +/// Any `object`s passed into the `port` must be valid for the lifetime of the `port`. |
| 52 | +/// Logically, `port` keeps a borrowed reference to the `object` until it is |
| 53 | +/// removed via `port_dissociate_fd`. |
| 54 | +/// |
| 55 | +/// # References |
| 56 | +/// |
| 57 | +/// - [OpenSolaris] |
| 58 | +/// - [illumos] |
| 59 | +/// |
| 60 | +/// [OpenSolaris]: https://www.unix.com/man-page/opensolaris/3C/port_associate/ |
| 61 | +/// [illumos]: https://illumos.org/man/3C/port_associate |
| 62 | +pub unsafe fn port_associate_fd( |
| 63 | + port: impl AsFd, |
| 64 | + object: impl AsRawFd, |
| 65 | + events: PollFlags, |
| 66 | + userdata: *mut c::c_void, |
| 67 | +) -> io::Result<()> { |
| 68 | + syscalls::port_associate( |
| 69 | + port.as_fd(), |
| 70 | + c::PORT_SOURCE_FD, |
| 71 | + object.as_raw_fd() as _, |
| 72 | + events.bits() as _, |
| 73 | + userdata.cast(), |
| 74 | + ) |
| 75 | +} |
| 76 | + |
| 77 | +/// `port_dissociate(_, PORT_SOURCE_FD, _)`- Dissociates a file descriptor from |
| 78 | +/// a port. |
| 79 | +/// |
| 80 | +/// # Safety |
| 81 | +/// |
| 82 | +/// The file descriptor passed into this function must have been previously |
| 83 | +/// associated with the port via [`port_associate_fd`]. |
| 84 | +/// |
| 85 | +/// # References |
| 86 | +/// |
| 87 | +/// - [OpenSolaris] |
| 88 | +/// - [illumos] |
| 89 | +/// |
| 90 | +/// [OpenSolaris]: https://www.unix.com/man-page/opensolaris/3C/port_dissociate |
| 91 | +/// [illumos]: https://illumos.org/man/3C/port_dissociate |
| 92 | +pub unsafe fn port_dissociate_fd(port: impl AsFd, object: impl AsRawFd) -> io::Result<()> { |
| 93 | + syscalls::port_dissociate(port.as_fd(), c::PORT_SOURCE_FD, object.as_raw_fd() as _) |
| 94 | +} |
| 95 | + |
| 96 | +/// `port_get()`- Gets an event from a port. |
| 97 | +/// |
| 98 | +/// # References |
| 99 | +/// |
| 100 | +/// - [OpenSolaris] |
| 101 | +/// - [illumos] |
| 102 | +/// |
| 103 | +/// [OpenSolaris]: https://www.unix.com/man-page/opensolaris/3C/port_get/ |
| 104 | +/// [illumos]: https://illumos.org/man/3C/port_get |
| 105 | +pub fn port_get(port: impl AsFd, timeout: Option<Duration>) -> io::Result<Event> { |
| 106 | + let mut timeout = timeout.map(|timeout| c::timespec { |
| 107 | + tv_sec: timeout.as_secs().try_into().unwrap(), |
| 108 | + tv_nsec: timeout.subsec_nanos() as _, |
| 109 | + }); |
| 110 | + |
| 111 | + syscalls::port_get(port.as_fd(), timeout.as_mut()) |
| 112 | +} |
| 113 | + |
| 114 | +/// `port_getn()`- Gets multiple events from a port. |
| 115 | +/// |
| 116 | +/// # References |
| 117 | +/// |
| 118 | +/// - [OpenSolaris] |
| 119 | +/// - [illumos] |
| 120 | +/// |
| 121 | +/// [OpenSolaris]: https://www.unix.com/man-page/opensolaris/3C/port_getn/ |
| 122 | +/// [illumos]: https://illumos.org/man/3C/port_getn |
| 123 | +pub fn port_getn( |
| 124 | + port: impl AsFd, |
| 125 | + events: &mut Vec<Event>, |
| 126 | + min_events: usize, |
| 127 | + timeout: Option<Duration>, |
| 128 | +) -> io::Result<()> { |
| 129 | + events.clear(); |
| 130 | + |
| 131 | + let mut timeout = timeout.map(|timeout| c::timespec { |
| 132 | + tv_sec: timeout.as_secs().try_into().unwrap(), |
| 133 | + tv_nsec: timeout.subsec_nanos() as _, |
| 134 | + }); |
| 135 | + |
| 136 | + syscalls::port_getn( |
| 137 | + port.as_fd(), |
| 138 | + timeout.as_mut(), |
| 139 | + events, |
| 140 | + min_events.try_into().unwrap(), |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +/// `port_send()`- Sends an event to a port. |
| 145 | +/// |
| 146 | +/// # References |
| 147 | +/// |
| 148 | +/// - [OpenSolaris] |
| 149 | +/// - [illumos] |
| 150 | +/// |
| 151 | +/// [OpenSolaris]: https://www.unix.com/man-page/opensolaris/3C/port_send/ |
| 152 | +/// [illumos]: https://illumos.org/man/3C/port_send |
| 153 | +pub fn port_send(port: impl AsFd, events: i32, userdata: *mut c::c_void) -> io::Result<()> { |
| 154 | + syscalls::port_send(port.as_fd(), events, userdata.cast()) |
| 155 | +} |
0 commit comments