git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/8] xread: poll on non blocking fds

From
Stefan Beller <sbeller@google.com>
Date
Sep 28, 2015, 23:14 UTC
Message-ID
<1443482046-25569-3-git-send-email-sbeller@google.com>
In-Reply-To
<1443482046-25569-1-git-send-email-sbeller@google.com>
>From the man page:
EAGAIN The file descriptor fd refers to a file other than a socket
       and has been marked nonblocking (O_NONBLOCK), and the read
       would block.
EAGAIN or EWOULDBLOCK
       The file descriptor fd refers to a socket and has been marked
       nonblocking (O_NONBLOCK), and the read would block.  POSIX.1-2001
       allows either error to be returned for this case, and does not
       require these constants to have the same value, so a portable
       application should check for both possibilities.

If we get an EAGAIN or EWOULDBLOCK the fd must have set O_NONBLOCK. As the intent of xread is to read as much as possible either until the 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.

We should not care if the call to poll failed, as we're in an infinite loop and can only get out with the correct read().

Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 wrapper.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/wrapper.c b/wrapper.c
index ff49807..5517928 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -201,8 +201,17 @@ 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;
+				pfd.events = POLLIN;
+				pfd.fd = fd;
+				/* We deliberately ignore the return value */
+				poll(&pfd, 1, -1);
+			}
+		}
 		return nr;
 	}
 }
-- 
2.5.0.273.g6fa2560.dirty
Previous: Stefan BellerNext: Stefan Beller
Message 3 of 13 in “fetch submodules in parallel”
  1. 0/8 fetch submodules in parallelStefan Beller, Sep 28, 2015
  2. 1/8 submodule.c: write "Fetching submodule <foo>" to stderrStefan Beller, Sep 28, 2015
  3. 2/8 xread: poll on non blocking fdsStefan Beller, Sep 28, 2015
  4. 3/8 xread_nonblock: add functionality to read from fds without blockingStefan Beller, Sep 28, 2015
  5. 4/8 strbuf: add strbuf_read_once to read without blockingStefan Beller, Sep 28, 2015
  6. 5/8 sigchain: add command to pop all common signalsStefan Beller, Sep 28, 2015
  7. Junio C HamanoSep 30, 2015
  8. 6/8 run-command: add an asynchronous parallel child processorStefan Beller, Sep 28, 2015
  9. Junio C HamanoSep 30, 2015
  10. Stefan BellerSep 30, 2015
  11. Junio C HamanoSep 30, 2015
  12. 7/8 fetch_populated_submodules: use new parallel job processingStefan Beller, Sep 28, 2015
  13. 8/8 submodules: allow parallel fetching, add tests and documentationStefan Beller, Sep 28, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.