From 4e09bca9e34c226b6d7e34b4fa11248405fd988e Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Sun, 22 Oct 2023 19:50:38 +0200 Subject: Move everything into a new repo. --- poll.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 poll.c (limited to 'poll.c') diff --git a/poll.c b/poll.c new file mode 100644 index 0000000..5e02723 --- /dev/null +++ b/poll.c @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +int poll(struct pollfd *fds, size_t nfds, int timeout) { + (void)timeout; + int read_locks[nfds]; + int write_locks[nfds]; + int disconnect_locks[nfds]; + for (size_t i = 0; i < nfds; i++) { + if (fds[i].fd < 0) + continue; + vfs_fd_t *f = get_vfs_fd(fds[i].fd); + if (fds[i].events & POLLIN) + read_locks[i] = create_read_fdhalt(f); + if (fds[i].events & POLLOUT) + write_locks[i] = create_write_fdhalt(f); + if (fds[i].events & POLLHUP) + disconnect_locks[i] = create_disconnect_fdhalt(f); + } + + switch_task(); + + for (size_t i = 0; i < nfds; i++) { + if (fds[i].fd < 0) + continue; + if (fds[i].events & POLLIN) + unset_read_fdhalt(read_locks[i]); + if (fds[i].events & POLLOUT) + unset_write_fdhalt(write_locks[i]); + if (fds[i].events & POLLHUP) + unset_disconnect_fdhalt(disconnect_locks[i]); + } + for (size_t i = 0; i < nfds; i++) { + if (0 > fds[i].fd) { + fds[i].revents = 0; + continue; + } + vfs_fd_t *f = get_vfs_fd(fds[i].fd); + if (!f) { + if (fds[i].events & POLLHUP) + fds[i].revents |= POLLHUP; + } else { + if (f->inode->has_data && fds[i].events & POLLIN) + fds[i].revents |= POLLIN; + if (f->inode->can_write && fds[i].events & POLLOUT) + fds[i].revents |= POLLOUT; + if (!(f->inode->is_open) && fds[i].events & POLLHUP) + fds[i].revents |= POLLHUP; + } + } + return 0; +} -- cgit v1.2.3