Пример #1
0
/* wait for any activity on the specified file descriptor using
   the specified deadline */
static int tio_wait(int fd, short events, int timeout,
                    struct timespec *deadline)
{
  int t;
  struct pollfd fds[1];
  int rv;
  while (1)
  {
    fds[0].fd = fd;
    fds[0].events = events;
    /* figure out the time we need to wait */
    if ((t = tio_time_remaining(deadline, timeout)) < 0)
    {
      errno = ETIME;
      return -1;
    }
    /* sanitiy check for moving clock */
    if (t > timeout)
      t = timeout;
    /* wait for activity */
    rv = poll(fds, 1, t);
    if (rv > 0)
      return 0; /* we have activity */
    else if (rv == 0)
    {
      /* no file descriptors were available within the specified time */
      errno = ETIME;
      return -1;
    }
    else if ((errno != EINTR) && (errno != EAGAIN))
      /* some error ocurred */
      return -1;
    /* we just try again on EINTR or EAGAIN */
  }
}
Пример #2
0
/* wait for any activity on the specified file descriptor using
   the specified deadline */
static int tio_wait(TFILE *fp,int readfd,const struct timeval *deadline)
{
  int timeout;
  struct pollfd fds[1];
  int rv;
  while (1)
  {
    /* figure out the time we need to wait */
    if ((timeout=tio_time_remaining(deadline))<0)
    {
      errno=ETIME;
      return -1;
    }
    /* wait for activity */
    if (readfd)
    {
      fds[0].fd=fp->fd;
      fds[0].events=POLLIN;
      /* santiy check for moving clock */
      if (timeout>fp->readtimeout)
        timeout=fp->readtimeout;
    }
    else
    {
      fds[0].fd=fp->fd;
      fds[0].events=POLLOUT;
      /* santiy check for moving clock */
      if (timeout>fp->writetimeout)
        timeout=fp->writetimeout;
    }
    rv=poll(fds,1,timeout);
    if (rv>0)
      return 0; /* we have activity */
    else if (rv==0)
    {
      /* no file descriptors were available within the specified time */
      errno=ETIME;
      return -1;
    }
    else if (errno!=EINTR)
      /* some error ocurred */
      return -1;
    /* we just try again on EINTR */
  }
}