socket.c3: optimize poll() duration wrapping (#1762)

* socket.c3: optimize poll() duration wrapping
This commit is contained in:
Bernd
2025-01-04 00:32:17 +01:00
committed by GitHub
parent eed806962d
commit ba5e2b7fa6

View File

@@ -52,23 +52,26 @@ struct Poll
PollEvents revents;
}
fn ulong! poll_ms(Poll[] polls, long timeout_ms)
<*
@param [inout] polls
@param timeout "duration to poll (clamped to CInt.max ms), or POLL_FOREVER."
*>
fn ulong! poll(Poll[] polls, Duration timeout)
{
return poll(polls, time::ms(timeout_ms)) @inline;
return poll_ms(polls, timeout.to_ms()) @inline;
}
<*
@param [inout] polls
@param timeout "duration to poll."
@param timeout_ms "duration to poll in ms or -1. Clamped to CInt.max"
*>
fn ulong! poll(Poll[] polls, Duration timeout)
fn ulong! poll_ms(Poll[] polls, long timeout_ms)
{
long time_ms = (timeout < 0) ? (long)POLL_FOREVER : timeout.to_ms();
if (time_ms > CInt.max) time_ms = CInt.max;
if (timeout_ms > CInt.max) timeout_ms = CInt.max;
$if env::WIN32:
CInt result = win32_WSAPoll((Win32_LPWSAPOLLFD)polls.ptr, (Win32_ULONG)polls.len, (CInt)time_ms);
CInt result = win32_WSAPoll((Win32_LPWSAPOLLFD)polls.ptr, (Win32_ULONG)polls.len, (CInt)timeout_ms);
$else
CInt result = os::poll((Posix_pollfd*)polls.ptr, (Posix_nfds_t)polls.len, (CInt)time_ms);
CInt result = os::poll((Posix_pollfd*)polls.ptr, (Posix_nfds_t)polls.len, (CInt)timeout_ms);
$endif
return result < 0 ? os::socket_error()? : (ulong)result;
}