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

[PATCHv2 2/7] xread: poll on non blocking fds

From
Stefan Beller <sbeller@google.com>
Date
Dec 16, 2015, 00:04 UTC
Message-ID
<1450224252-16833-3-git-send-email-sbeller@google.com>
In-Reply-To
<1450224252-16833-1-git-send-email-sbeller@google.com>
The man page of read(2) says:
  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>
Acked-by: Johannes Sixt <j6t@kdbg.org>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 wrapper.c | 20 ++++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/wrapper.c b/wrapper.c
index 6fcaa4d..1770efa 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -236,8 +236,24 @@ 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;
+				/*
+				 * it is OK if this poll() failed; we
+				 * want to leave this infinite loop
+				 * only when read() returns with
+				 * success, or an expected failure,
+				 * which would be checked by the next
+				 * call to read(2).
+				 */
+				poll(&pfd, 1, -1);
+			}
+		}
 		return nr;
 	}
 }
-- 
2.6.4.443.ge094245.dirty
Previous: Stefan BellerNext: Torsten Bögershausen
Message 3 of 19 in “[PATCHv2 0/7] Rerolling sb/submodule-parallel-fetch for the time after 2.7”
  1. Stefan BellerDec 16, 2015
  2. 1/7 submodule.c: write "Fetching submodule <foo>" to stderrStefan Beller, Dec 16, 2015
  3. 2/7 xread: poll on non blocking fdsStefan Beller, Dec 16, 2015
  4. Torsten BögershausenDec 17, 2015
  5. Stefan BellerDec 17, 2015
  6. Torsten BögershausenDec 17, 2015
  7. Stefan BellerDec 17, 2015
  8. Jeff KingDec 18, 2015
  9. Jeff KingDec 18, 2015
  10. Torsten BögershausenDec 18, 2015
  11. Jeff KingDec 18, 2015
  12. Jeff KingDec 18, 2015
  13. 3/7 strbuf: add strbuf_read_once to read without blockingStefan Beller, Dec 16, 2015
  14. 4/7 sigchain: add command to pop all common signalsStefan Beller, Dec 16, 2015
  15. 5/7 run-command: add an asynchronous parallel child processorStefan Beller, Dec 16, 2015
  16. 6/7 fetch_populated_submodules: use new parallel job processingStefan Beller, Dec 16, 2015
  17. 7/7 submodules: allow parallel fetching, add tests and documentationStefan Beller, Dec 16, 2015
  18. Stefan BellerDec 16, 2015
  19. Junio C HamanoDec 16, 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.