]>
code.delx.au - pulseaudio/blob - src/polypcore/poll.c
4 Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
5 Copyright (C) 2005, Cendio AB.
6 This file is part of polypaudio.
7 Based on work for the GNU C Library.
9 polypaudio is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Library General Public License as published
11 by the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 polypaudio is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public
20 License along with polypaudio; If not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 /* Poll the file descriptors described by the NFDS structures starting at
26 FDS. If TIMEOUT is nonzero and not -1, allow TIMEOUT milliseconds for
27 an event to occur; if TIMEOUT is -1, block until an event occurs.
28 Returns the number of file descriptors with events, zero if timed out,
37 #ifdef HAVE_SYS_SELECT_H
38 #include <sys/select.h>
43 #ifndef HAVE_SYS_POLL_H
45 #include <polypcore/core-util.h>
49 int poll (struct pollfd
*fds
, unsigned long int nfds
, int timeout
) {
51 fd_set rset
, wset
, xset
;
69 * Windows does not support signals properly so waiting for them would
75 return select(0, NULL
, NULL
, NULL
, NULL
);
79 for (f
= fds
; f
< &fds
[nfds
]; ++f
) {
81 if (f
->events
& POLLIN
)
82 FD_SET (f
->fd
, &rset
);
83 if (f
->events
& POLLOUT
)
84 FD_SET (f
->fd
, &wset
);
85 if (f
->events
& POLLPRI
)
86 FD_SET (f
->fd
, &xset
);
87 if (f
->fd
> maxfd
&& (f
->events
& (POLLIN
|POLLOUT
|POLLPRI
)))
92 tv
.tv_sec
= timeout
/ 1000;
93 tv
.tv_usec
= (timeout
% 1000) * 1000;
95 ready
= select ((SELECT_TYPE_ARG1
) maxfd
+ 1, SELECT_TYPE_ARG234
&rset
,
96 SELECT_TYPE_ARG234
&wset
, SELECT_TYPE_ARG234
&xset
,
97 SELECT_TYPE_ARG5 (timeout
== -1 ? NULL
: &tv
));
98 if ((ready
== -1) && (errno
== EBADF
)) {
107 for (f
= fds
; f
< &fds
[nfds
]; ++f
) {
109 fd_set sngl_rset
, sngl_wset
, sngl_xset
;
111 FD_ZERO (&sngl_rset
);
112 FD_ZERO (&sngl_wset
);
113 FD_ZERO (&sngl_xset
);
115 if (f
->events
& POLLIN
)
116 FD_SET (f
->fd
, &sngl_rset
);
117 if (f
->events
& POLLOUT
)
118 FD_SET (f
->fd
, &sngl_wset
);
119 if (f
->events
& POLLPRI
)
120 FD_SET (f
->fd
, &sngl_xset
);
121 if (f
->events
& (POLLIN
|POLLOUT
|POLLPRI
)) {
122 struct timeval singl_tv
;
125 singl_tv
.tv_usec
= 0;
127 if (select((SELECT_TYPE_ARG1
) f
->fd
, SELECT_TYPE_ARG234
&rset
,
128 SELECT_TYPE_ARG234
&wset
, SELECT_TYPE_ARG234
&xset
,
129 SELECT_TYPE_ARG5
&singl_tv
) != -1) {
130 if (f
->events
& POLLIN
)
131 FD_SET (f
->fd
, &rset
);
132 if (f
->events
& POLLOUT
)
133 FD_SET (f
->fd
, &wset
);
134 if (f
->events
& POLLPRI
)
135 FD_SET (f
->fd
, &xset
);
136 if (f
->fd
> maxfd
&& (f
->events
& (POLLIN
|POLLOUT
|POLLPRI
)))
139 } else if (errno
== EBADF
)
140 f
->revents
|= POLLNVAL
;
146 /* Linux alters the tv struct... but it shouldn't matter here ...
147 * as we're going to be a little bit out anyway as we've just eaten
148 * more than a couple of cpu cycles above */
149 ready
= select ((SELECT_TYPE_ARG1
) maxfd
+ 1, SELECT_TYPE_ARG234
&rset
,
150 SELECT_TYPE_ARG234
&wset
, SELECT_TYPE_ARG234
&xset
,
151 SELECT_TYPE_ARG5 (timeout
== -1 ? NULL
: &tv
));
156 errno
= WSAGetLastError();
161 for (f
= fds
; f
< &fds
[nfds
]; ++f
) {
164 if (FD_ISSET (f
->fd
, &rset
)) {
165 /* support for POLLHUP. An hung up descriptor does not
166 increase the return value! */
167 if (recv (f
->fd
, data
, 64, MSG_PEEK
) == -1) {
168 if (errno
== ESHUTDOWN
|| errno
== ECONNRESET
||
169 errno
== ECONNABORTED
|| errno
== ENETRESET
) {
170 fprintf(stderr
, "Hangup\n");
171 f
->revents
|= POLLHUP
;
176 f
->revents
|= POLLIN
;
178 if (FD_ISSET (f
->fd
, &wset
))
179 f
->revents
|= POLLOUT
;
180 if (FD_ISSET (f
->fd
, &xset
))
181 f
->revents
|= POLLPRI
;
191 #endif /* HAVE_SYS_POLL_H */