| 1 |
//! Windows system calls in the `event` module. |
| 2 |
|
| 3 |
use crate::backend::c; |
| 4 |
use crate::backend::conv::ret_c_int; |
| 5 |
use crate::event::{FdSetElement, PollFd, Timespec}; |
| 6 |
use crate::io; |
| 7 |
|
| 8 |
pub(crate) fn poll(fds: &mut [PollFd<'_>], timeout: Option<&Timespec>) -> io::Result<usize> { |
| 9 |
let nfds = fds |
| 10 |
.len() |
| 11 |
.try_into() |
| 12 |
.map_err(|_convert_err| io::Errno::INVAL)?; |
| 13 |
|
| 14 |
let timeout = match timeout { |
| 15 |
None => -1, |
| 16 |
Some(timeout) => timeout.as_c_int_millis().ok_or(io::Errno::INVAL)?, |
| 17 |
}; |
| 18 |
|
| 19 |
ret_c_int(unsafe { c::poll(fds.as_mut_ptr().cast(), nfds, timeout) }) |
| 20 |
.map(|nready| nready as usize) |
| 21 |
} |
| 22 |
|
| 23 |
pub(crate) fn select( |
| 24 |
nfds: i32, |
| 25 |
readfds: Option<&mut [FdSetElement]>, |
| 26 |
writefds: Option<&mut [FdSetElement]>, |
| 27 |
exceptfds: Option<&mut [FdSetElement]>, |
| 28 |
timeout: Option<&crate::timespec::Timespec>, |
| 29 |
) -> io::Result<i32> { |
| 30 |
use core::ptr::{null, null_mut}; |
| 31 |
|
| 32 |
let readfds = match readfds { |
| 33 |
Some(readfds) => { |
| 34 |
assert!(readfds.len() >= readfds[0].0 as usize); |
| 35 |
readfds.as_mut_ptr() |
| 36 |
} |
| 37 |
None => null_mut(), |
| 38 |
}; |
| 39 |
let writefds = match writefds { |
| 40 |
Some(writefds) => { |
| 41 |
assert!(writefds.len() >= writefds[0].0 as usize); |
| 42 |
writefds.as_mut_ptr() |
| 43 |
} |
| 44 |
None => null_mut(), |
| 45 |
}; |
| 46 |
let exceptfds = match exceptfds { |
| 47 |
Some(exceptfds) => { |
| 48 |
assert!(exceptfds.len() >= exceptfds[0].0 as usize); |
| 49 |
exceptfds.as_mut_ptr() |
| 50 |
} |
| 51 |
None => null_mut(), |
| 52 |
}; |
| 53 |
|
| 54 |
let timeout_data; |
| 55 |
let timeout_ptr = match timeout { |
| 56 |
Some(timeout) => { |
| 57 |
// Convert from `Timespec` to `TIMEVAL`. |
| 58 |
timeout_data = c::TIMEVAL { |
| 59 |
tv_sec: timeout |
| 60 |
.tv_sec |
| 61 |
.try_into() |
| 62 |
.map_err(|_| io::Errno::OPNOTSUPP)?, |
| 63 |
tv_usec: ((timeout.tv_nsec + 999) / 1000) as _, |
| 64 |
}; |
| 65 |
&timeout_data |
| 66 |
} |
| 67 |
None => null(), |
| 68 |
}; |
| 69 |
|
| 70 |
unsafe { |
| 71 |
ret_c_int(c::select( |
| 72 |
nfds, |
| 73 |
readfds.cast(), |
| 74 |
writefds.cast(), |
| 75 |
exceptfds.cast(), |
| 76 |
timeout_ptr, |
| 77 |
)) |
| 78 |
} |
| 79 |
} |