Introduce sigtimedwait() on Windows

This commit is contained in:
Justine Tunney 2024-09-14 20:32:46 -07:00
parent 37e2660c7f
commit c260144843
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
17 changed files with 280 additions and 130 deletions

View file

@ -35,80 +35,14 @@
#include "libc/sysv/consts/sig.h"
#include "libc/sysv/errfuns.h"
/**
* Checks status on multiple file descriptors at once.
*
* This function is the same as saying:
*
* sigset_t old;
* sigprocmask(SIG_SETMASK, sigmask, &old);
* poll(fds, nfds, timeout);
* sigprocmask(SIG_SETMASK, old, 0);
*
* Except it happens atomically when the kernel supports doing that. On
* kernels such as XNU and NetBSD which don't, this wrapper will fall
* back to using the example above. If you need ironclad assurances of
* signal mask atomicity, then consider using pselect() which Cosmo Libc
* guarantees to be atomic on all supported platforms.
*
* Servers that need to handle an unbounded number of client connections
* should just create a separate thread for each client. poll(), ppoll()
* and select() aren't scalable i/o solutions on any platform.
*
* On Windows it's only possible to poll 64 file descriptors at a time;
* it's a limitation imposed by WSAPoll(). Cosmopolitan Libc's ppoll()
* polyfill can go higher in some cases; for example, It's possible to
* poll 64 sockets and 64 pipes/terminals at the same time. Furthermore,
* elements whose fd field is set to a negative number are ignored and
* will not count against this limit.
*
* One of the use cases for poll() is to quickly check if a number of
* file descriptors are valid. The canonical way to do this is to set
* events to 0 which prevents blocking and causes only the invalid,
* hangup, and error statuses to be checked.
*
* On XNU, the POLLHUP and POLLERR statuses aren't checked unless either
* POLLIN, POLLOUT, or POLLPRI are specified in the events field. Cosmo
* will however polyfill the checking of POLLNVAL on XNU with the events
* doesn't specify any of the above i/o events.
*
* When XNU and BSD OSes report POLLHUP, they will always set POLLIN too
* when POLLIN is requested, even in cases when there isn't unread data.
*
* @param fds[𝑖].fd should be a socket, input pipe, or conosle input
* and if it's a negative number then the entry is ignored, plus
* revents will be set to zero
* @param fds[𝑖].events flags can have POLLIN, POLLOUT, POLLPRI,
* POLLRDNORM, POLLWRNORM, POLLRDBAND, POLLWRBAND as well as
* POLLERR, POLLHUP, and POLLNVAL although the latter are
* always implied (assuming fd0) so they're ignored here
* @param timeout_ms if 0 means don't wait and negative waits forever
* @return number of `fds` whose revents field has been set to a nonzero
* number, 0 if the timeout elapsed without events, or -1 w/ errno
* @return fds[𝑖].revents is always zero initializaed and then will
* be populated with POLL{IN,OUT,PRI,HUP,ERR,NVAL} if something
* was determined about the file descriptor
* @param timeout if null will block indefinitely
* @param sigmask may be null in which case no mask change happens
* @raise EINVAL if we exceeded the 64 socket limit on Windows
* @raise ECANCELED if thread was cancelled in masked mode
* @raise EINVAL if `nfds` exceeded `RLIMIT_NOFILE`
* @raise ENOMEM on failure to allocate memory
* @raise EINVAL if `*timeout` is invalid
* @raise EINTR if signal was delivered
* @cancelationpoint
* @asyncsignalsafe
* @norestart
*/
int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
const sigset_t *sigmask) {
static int ppoll_impl(struct pollfd *fds, size_t nfds,
const struct timespec *timeout, const sigset_t *sigmask) {
int e, fdcount;
sigset_t oldmask;
struct timespec ts, *tsp;
BEGIN_CANCELATION_POINT;
// validate timeout
if (timeout && timeout->tv_nsec >= 1000000000)
if (timeout && timeout->tv_nsec >= 1000000000ull)
return einval();
// The OpenBSD poll() man pages claims it'll ignore POLLERR, POLLHUP,
@ -192,6 +126,78 @@ int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
}
}
return fdcount;
}
/**
* Checks status on multiple file descriptors at once.
*
* This function is the same as saying:
*
* sigset_t old;
* sigprocmask(SIG_SETMASK, sigmask, &old);
* poll(fds, nfds, timeout);
* sigprocmask(SIG_SETMASK, old, 0);
*
* Except it happens atomically when the kernel supports doing that. On
* kernels such as XNU and NetBSD which don't, this wrapper will fall
* back to using the example above. If you need ironclad assurances of
* signal mask atomicity, then consider using pselect() which Cosmo Libc
* guarantees to be atomic on all supported platforms.
*
* Servers that need to handle an unbounded number of client connections
* should just create a separate thread for each client. poll(), ppoll()
* and select() aren't scalable i/o solutions on any platform.
*
* On Windows it's only possible to poll 64 file descriptors at a time;
* it's a limitation imposed by WSAPoll(). Cosmopolitan Libc's ppoll()
* polyfill can go higher in some cases; for example, It's possible to
* poll 64 sockets and 64 pipes/terminals at the same time. Furthermore,
* elements whose fd field is set to a negative number are ignored and
* will not count against this limit.
*
* One of the use cases for poll() is to quickly check if a number of
* file descriptors are valid. The canonical way to do this is to set
* events to 0 which prevents blocking and causes only the invalid,
* hangup, and error statuses to be checked.
*
* On XNU, the POLLHUP and POLLERR statuses aren't checked unless either
* POLLIN, POLLOUT, or POLLPRI are specified in the events field. Cosmo
* will however polyfill the checking of POLLNVAL on XNU with the events
* doesn't specify any of the above i/o events.
*
* When XNU and BSD OSes report POLLHUP, they will always set POLLIN too
* when POLLIN is requested, even in cases when there isn't unread data.
*
* @param fds[𝑖].fd should be a socket, input pipe, or conosle input
* and if it's a negative number then the entry is ignored, plus
* revents will be set to zero
* @param fds[𝑖].events flags can have POLLIN, POLLOUT, POLLPRI,
* POLLRDNORM, POLLWRNORM, POLLRDBAND, POLLWRBAND as well as
* POLLERR, POLLHUP, and POLLNVAL although the latter are
* always implied (assuming fd0) so they're ignored here
* @param timeout_ms if 0 means don't wait and negative waits forever
* @return number of `fds` whose revents field has been set to a nonzero
* number, 0 if the timeout elapsed without events, or -1 w/ errno
* @return fds[𝑖].revents is always zero initializaed and then will
* be populated with POLL{IN,OUT,PRI,HUP,ERR,NVAL} if something
* was determined about the file descriptor
* @param timeout if null will block indefinitely
* @param sigmask may be null in which case no mask change happens
* @raise ECANCELED if thread was cancelled in masked mode
* @raise EINVAL if `nfds` exceeded `RLIMIT_NOFILE`
* @raise ENOMEM on failure to allocate memory
* @raise EINVAL if `*timeout` is invalid
* @raise EINTR if signal was delivered
* @cancelationpoint
* @asyncsignalsafe
* @norestart
*/
int ppoll(struct pollfd *fds, size_t nfds, const struct timespec *timeout,
const sigset_t *sigmask) {
int fdcount;
BEGIN_CANCELATION_POINT;
fdcount = ppoll_impl(fds, nfds, timeout, sigmask);
END_CANCELATION_POINT;
STRACE("ppoll(%s, %'zu, %s, %s) → %d% lm",
DescribePollFds(fdcount, fds, nfds), nfds,