1 /* poll.cc. Implements poll(2) via usage of select(2) call.
3 Copyright 2000, 2001 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
26 poll (struct pollfd *fds, unsigned int nfds, int timeout)
29 fd_set *open_fds, *read_fds, *write_fds, *except_fds;
30 struct timeval tv = { timeout / 1000, (timeout % 1000) * 1000 };
31 sigframe thisframe (mainthread);
33 for (unsigned int i = 0; i < nfds; ++i)
34 if (fds[i].fd > max_fd)
37 size_t fds_size = howmany(max_fd + 1, NFDBITS) * sizeof (fd_mask);
39 open_fds = (fd_set *) alloca (fds_size);
40 read_fds = (fd_set *) alloca (fds_size);
41 write_fds = (fd_set *) alloca (fds_size);
42 except_fds = (fd_set *) alloca (fds_size);
44 if (!open_fds || !read_fds || !write_fds || !except_fds)
50 memset (open_fds, 0, fds_size);
51 memset (read_fds, 0, fds_size);
52 memset (write_fds, 0, fds_size);
53 memset (except_fds, 0, fds_size);
55 bool valid_fds = false;
56 for (unsigned int i = 0; i < nfds; ++i)
57 if (!cygheap->fdtab.not_open (fds[i].fd))
61 FD_SET (fds[i].fd, open_fds);
62 if (fds[i].events & POLLIN)
63 FD_SET (fds[i].fd, read_fds);
64 if (fds[i].events & POLLOUT)
65 FD_SET (fds[i].fd, write_fds);
66 if (fds[i].events & POLLPRI)
67 FD_SET (fds[i].fd, except_fds);
70 fds[i].revents = POLLNVAL;
74 ret = cygwin_select (max_fd + 1, read_fds, write_fds, except_fds,
75 timeout < 0 ? NULL : &tv);
77 for (unsigned int i = 0; i < nfds; ++i)
79 if (fds[i].revents == POLLNVAL && ret >= 0)
81 else if (cygheap->fdtab.not_open(fds[i].fd))
82 fds[i].revents = POLLHUP;
84 fds[i].revents = POLLERR;
88 if (FD_ISSET (fds[i].fd, read_fds))
89 fds[i].revents |= POLLIN;
90 if (FD_ISSET (fds[i].fd, write_fds))
91 fds[i].revents |= POLLOUT;
92 if (FD_ISSET (fds[i].fd, except_fds))
93 fds[i].revents |= POLLPRI;