File: iopoll.c

package info (click to toggle)
cvm 0.11-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 492 kB
  • ctags: 215
  • sloc: ansic: 1,777; makefile: 48; sh: 12
file content (44 lines) | stat: -rw-r--r-- 982 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include "iopoll.h"
#include "select.h"

#ifdef IOPOLL_SELECT

int iopoll(iopoll_fd* fds, unsigned nfds, unsigned long timeout)
{
  struct timeval tv;
  fd_set rfds;
  fd_set wfds;
  int fdmax;
  iopoll_fd* io;
  int fd;
  unsigned i;
  int r;
  
  FD_ZERO(&rfds);
  FD_ZERO(&wfds);

  for (fdmax = 0, i = 0, io = fds; i < nfds; ++i, ++io) {
    fd = io->fd;
    if (fd < 0) continue;
    if (fd > fdmax) fdmax = fd + 1;
    if (io->events & IOPOLL_READ) FD_SET(fd, &rfds);
    if (io->events & IOPOLL_WRITE) FD_SET(fd, &wfds);
  }

  tv.tv_sec = timeout / 1000;
  tv.tv_usec = (timeout % 1000) * 1000;

  if ((r = select(fdmax, &rfds, &wfds, (fd_set*)0, &tv)) <= 0) return r;

  for (i = 0, io = fds; i < nfds; ++i, ++io) {
    fd = io->fd;
    if (fd < 0) continue;
    if ((io->events & IOPOLL_READ) && FD_ISSET(fd, &rfds))
      io->revents |= IOPOLL_READ;
    if ((io->events & IOPOLL_WRITE) && FD_ISSET(fd, &wfds))
      io->revents |= IOPOLL_WRITE;
  }
  return r;
}

#endif