Re: [PATCHv3 02/13] xread: poll on non blocking fds
- From
Eric Sunshine <sunshine@sunshineco.com>
- Date
- Sep 21, 2015, 23:56 UTC
- Message-ID
- <CAPig+cQKOEYYR3j-uEeFzF3-qAfqq4SdQrH8LPmSP0VmAOCtzw@mail.gmail.com>
- In-Reply-To
- <1442875159-13027-3-git-send-email-sbeller@google.com>
On Mon, Sep 21, 2015 at 6:39 PM, Stefan Beller <sbeller@google.com> wrote:
> From the man page: > [...] > So if we get an EAGAIN or EWOULDBLOCK error the fd must be nonblocking.
s/So/&,/ s/error/&,/
> As the intend of xread is to read as much as possible either until the
s/intend/intent/
Show 23 quoted lines
> fd is EOF or an actual error occurs, we can ease the feeder of the fd
> by not spinning the whole time, but rather wait for it politely by not
> busy waiting.
>
> Signed-off-by: Stefan Beller <sbeller@google.com>
> ---
> --- a/wrapper.c
> +++ b/wrapper.c
> @@ -201,8 +201,23 @@ ssize_t xread(int fd, void *buf, size_t len)
> len = MAX_IO_SIZE;
> while (1) {
> nr = read(fd, buf, len);
> - if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
> - continue;
> + if (nr < 0) {
> + if (errno == EINTR)
> + continue;
> + if (errno == EAGAIN || errno == EWOULDBLOCK) {
> + struct pollfd pfd;
> + int i;
> + pfd.events = POLLIN;
> + pfd.fd = fd;
> + i = poll(&pfd, 1, 100);Why is this poll() using a timeout? Isn't that still a busy wait of sorts (even if less aggressive)?
Show 13 quoted lines
> + if (i < 0) {
> + if (errno == EINTR || errno == ENOMEM)
> + continue;
> + else
> + die_errno("poll");
> + }
> + }
> + }
> return nr;
> }
> }
> --
> 2.5.0.275.ge015d2a