Skip to content

Commit 0168574

Browse files
Add a port_getn_query function. (#1216)
* Add a `port_getn_query` function. The solarish [`port_getn` function] special-cases a `max` argument value of 0 to be a query of the number of events available. #1215 added a special-case to protect the code from doing a resize in that case. And in case users actually do want to do a query, this PR adds a new `port_getn_query` function that passes a zero. [`port_getn` function]: https://illumos.org/man/3C/port_getn * Fix errors. * Update src/event/port.rs Co-authored-by: 王宇逸 <[email protected]> --------- Co-authored-by: 王宇逸 <[email protected]>
1 parent cac5c92 commit 0168574

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

Diff for: src/backend/libc/event/syscalls.rs

+22
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,13 @@ pub(crate) fn port_getn(
314314
events: &mut Vec<Event>,
315315
mut nget: u32,
316316
) -> io::Result<()> {
317+
// `port_getn` special-cases a max value of 0 to be a query that returns
318+
// the number of events. We don't want to do the `set_len` in that case, so
319+
// so bail out early if needed.
317320
if events.capacity() == 0 {
318321
return Ok(());
319322
}
323+
320324
let timeout = timeout.map_or(null_mut(), as_mut_ptr);
321325
unsafe {
322326
ret(c::port_getn(
@@ -336,6 +340,24 @@ pub(crate) fn port_getn(
336340
Ok(())
337341
}
338342

343+
#[cfg(solarish)]
344+
pub(crate) fn port_getn_query(port: BorrowedFd<'_>) -> io::Result<u32> {
345+
let mut nget: u32 = 0;
346+
347+
// Pass a `max` of 0 to query the number of available events.
348+
unsafe {
349+
ret(c::port_getn(
350+
borrowed_fd(port),
351+
null_mut(),
352+
0,
353+
&mut nget,
354+
null_mut(),
355+
))?;
356+
}
357+
358+
Ok(nget)
359+
}
360+
339361
#[cfg(solarish)]
340362
pub(crate) fn port_send(
341363
port: BorrowedFd<'_>,

Diff for: src/event/port.rs

+22
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ pub fn port_get(port: impl AsFd, timeout: Option<Duration>) -> io::Result<Event>
110110
/// `port_getn(port, events, min_events, timeout)`—Gets multiple events from a
111111
/// port.
112112
///
113+
/// This requests up to a max of `events.capacity()` events, and then resizes
114+
/// `events` to the number of events retrieved. If `events.capacity()` is 0,
115+
/// this does nothing and returns immediately.
116+
///
117+
/// To query the number of events without retrieving any, use
118+
/// [`port_getn_query`].
119+
///
113120
/// # References
114121
/// - [OpenSolaris]
115122
/// - [illumos]
@@ -138,6 +145,21 @@ pub fn port_getn(
138145
)
139146
}
140147

148+
/// `port_getn(port, NULL, 0, NULL)`—Queries the number of events
149+
/// available from a port.
150+
///
151+
/// To retrieve the events, use [`port_getn`].
152+
///
153+
/// # References
154+
/// - [OpenSolaris]
155+
/// - [illumos]
156+
///
157+
/// [OpenSolaris]: https://www.unix.com/man-page/opensolaris/3C/port_getn/
158+
/// [illumos]: https://illumos.org/man/3C/port_getn
159+
pub fn port_getn_query(port: impl AsFd) -> io::Result<u32> {
160+
syscalls::port_getn_query(port.as_fd())
161+
}
162+
141163
/// `port_send(port, events, userdata)`—Sends an event to a port.
142164
///
143165
/// # References

0 commit comments

Comments
 (0)