]>
sourceware.org Git - newlib-cygwin.git/blob - winsup/cygwin/poll.cc
1 /* poll.cc. Implements poll(2) via usage of select(2) call.
3 Copyright 2000, 2001, 2002 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
14 #include <sys/socket.h>
27 poll (struct pollfd
*fds
, unsigned int nfds
, int timeout
)
30 fd_set
*read_fds
, *write_fds
, *except_fds
;
31 struct timeval tv
= { timeout
/ 1000, (timeout
% 1000) * 1000 };
32 sigframe
thisframe (mainthread
);
34 for (unsigned int i
= 0; i
< nfds
; ++i
)
35 if (fds
[i
].fd
> max_fd
)
38 size_t fds_size
= howmany (max_fd
+ 1, NFDBITS
) * sizeof (fd_mask
);
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 (!read_fds
|| !write_fds
|| !except_fds
)
50 memset (read_fds
, 0, fds_size
);
51 memset (write_fds
, 0, fds_size
);
52 memset (except_fds
, 0, fds_size
);
55 for (unsigned int i
= 0; i
< nfds
; ++i
)
58 if (!cygheap
->fdtab
.not_open (fds
[i
].fd
))
60 if (fds
[i
].events
& POLLIN
)
61 FD_SET(fds
[i
].fd
, read_fds
);
62 if (fds
[i
].events
& POLLOUT
)
63 FD_SET(fds
[i
].fd
, write_fds
);
64 if (fds
[i
].events
& POLLPRI
)
65 FD_SET(fds
[i
].fd
, except_fds
);
67 else if (fds
[i
].fd
>= 0)
70 fds
[i
].revents
= POLLNVAL
;
77 int ret
= cygwin_select (max_fd
+ 1, read_fds
, write_fds
, except_fds
, timeout
< 0 ? NULL
: &tv
);
80 for (unsigned int i
= 0; i
< nfds
; ++i
)
84 if (cygheap
->fdtab
.not_open (fds
[i
].fd
))
85 fds
[i
].revents
= POLLHUP
;
88 if (FD_ISSET(fds
[i
].fd
, read_fds
))
91 fhandler_socket
*sock
=
92 cygheap
->fdtab
[fds
[i
].fd
]->is_socket ();
94 fds
[i
].revents
|= POLLIN
;
96 switch (sock
->recvfrom (peek
, sizeof (peek
), MSG_PEEK
,
99 case -1: /* Something weird happened */
100 fds
[i
].revents
|= POLLERR
;
102 case 0: /* Closed on the read side. */
103 fds
[i
].revents
|= POLLHUP
;
106 fds
[i
].revents
|= POLLIN
;
110 if (FD_ISSET(fds
[i
].fd
, write_fds
))
111 fds
[i
].revents
|= POLLOUT
;
112 if (FD_ISSET(fds
[i
].fd
, except_fds
))
113 fds
[i
].revents
|= POLLPRI
;
This page took 0.039033 seconds and 5 git commands to generate.