| ... |
... |
@@ -317,14 +317,23 @@ static void poll_init_timeout(timeout *tv, Time t) |
|
317
|
317
|
|
|
318
|
318
|
static void poll_init_fdset(fdset *fds, int fd)
|
|
319
|
319
|
{
|
|
320
|
|
- /* select() overwrites the fdset so we must rebuild it every time. */
|
|
321
|
|
- FD_ZERO(&fds->selectfds);
|
|
322
|
|
- FD_SET(fd, &fds->selectfds);
|
|
|
320
|
+ /* select() modifies the fd_set: it uses the same fd_set for reporting as
|
|
|
321
|
+ * for input. Thus we must rebuild it every time. We can optimise this
|
|
|
322
|
+ * rebuilding somewhat however if we rely on select() not modifying the
|
|
|
323
|
+ * bits that we didn't ask it to look at. So we can zero the fd_set just
|
|
|
324
|
+ * once, and then only reset the single bit for the single fd, before each
|
|
|
325
|
+ * call to selct().
|
|
|
326
|
+ */
|
|
323
|
327
|
fds->fd = fd;
|
|
|
328
|
+ FD_ZERO(&fds->selectfds);
|
|
324
|
329
|
}
|
|
325
|
330
|
|
|
326
|
331
|
static int poll_no_timeout(fdset *fds)
|
|
327
|
332
|
{
|
|
|
333
|
+ /* select() modifies the fd_set so we must set it every time, but we rely
|
|
|
334
|
+ * on it not touching other bits to avoid having to FD_ZERO it every time
|
|
|
335
|
+ */
|
|
|
336
|
+ FD_SET(fds->fd, &fds->selectfds);
|
|
328
|
337
|
int nfds = fds->fd+1;
|
|
329
|
338
|
return select(nfds, &fds->selectfds, NULL, NULL, NULL);
|
|
330
|
339
|
}
|
| ... |
... |
@@ -332,6 +341,10 @@ static int poll_no_timeout(fdset *fds) |
|
332
|
341
|
static int poll_with_timeout(fdset *fds, timeout *tv)
|
|
333
|
342
|
{
|
|
334
|
343
|
struct timeval tv_tmp = *tv; // copy since select may change this value.
|
|
|
344
|
+ /* select() modifies the fd_set so we must set it every time, but we rely
|
|
|
345
|
+ * on it not touching other bits to avoid having to FD_ZERO it every time
|
|
|
346
|
+ */
|
|
|
347
|
+ FD_SET(fds->fd, &fds->selectfds);
|
|
335
|
348
|
int nfds = fds->fd+1;
|
|
336
|
349
|
return select(nfds, &fds->selectfds, NULL, NULL, &tv_tmp);
|
|
337
|
350
|
}
|