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

[PATCH 3/8] xread_nonblock: add functionality to read from fds without blocking

From
Stefan Beller <sbeller@google.com>
Date
Dec 14, 2015, 19:37 UTC
Message-ID
<1450121838-7069-4-git-send-email-sbeller@google.com>
In-Reply-To
<1450121838-7069-1-git-send-email-sbeller@google.com>

Provide a wrapper to read(), similar to xread(), that restarts on EINTR but not EAGAIN (or EWOULDBLOCK). This enables the caller to handle polling itself, possibly polling multiple sockets or performing some other action.

Helped-by: Jacob Keller <jacob.keller@gmail.com>
Helped-by: Jeff King <peff@peff.net>,
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Stefan Beller <sbeller@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 git-compat-util.h |  1 +
 wrapper.c         | 22 ++++++++++++++++++++++
 2 files changed, 23 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h
index 8e39867..87456a3 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -723,6 +723,7 @@ extern void *xmmap(void *start, size_t length, int prot, int flags, int fd, off_
 extern void *xmmap_gently(void *start, size_t length, int prot, int flags, int fd, off_t offset);
 extern int xopen(const char *path, int flags, ...);
 extern ssize_t xread(int fd, void *buf, size_t len);
+extern ssize_t xread_nonblock(int fd, void *buf, size_t len);
 extern ssize_t xwrite(int fd, const void *buf, size_t len);
 extern ssize_t xpread(int fd, void *buf, size_t len, off_t offset);
 extern int xdup(int fd);
diff --git a/wrapper.c b/wrapper.c
index 4f720fe..f71237c 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -252,6 +252,28 @@ ssize_t xread(int fd, void *buf, size_t len)
 }
 
 /*
+ * xread_nonblock() is the same a read(), but it automatically restarts read()
+ * interrupted operations (EINTR). xread_nonblock() DOES NOT GUARANTEE that
+ * "len" bytes is read. EWOULDBLOCK is turned into EAGAIN.
+ */
+ssize_t xread_nonblock(int fd, void *buf, size_t len)
+{
+	ssize_t nr;
+	if (len > MAX_IO_SIZE)
+		len = MAX_IO_SIZE;
+	while (1) {
+		nr = read(fd, buf, len);
+		if (nr < 0) {
+			if (errno == EINTR)
+				continue;
+			if (errno == EWOULDBLOCK)
+				errno = EAGAIN;
+		}
+		return nr;
+	}
+}
+
+/*
  * xwrite() is the same a write(), but it automatically restarts write()
  * operations with a recoverable error (EAGAIN and EINTR). xwrite() DOES NOT
  * GUARANTEE that "len" bytes is written even if the operation is successful.
-- 
2.6.4.443.ge094245.dirty
Previous: Stefan BellerNext: Junio C Hamano
Message 8 of 30 in “Rerolling sb/submodule-parallel-fetch for the time after 2.7”
  1. 0/8 Rerolling sb/submodule-parallel-fetch for the time after 2.7Stefan Beller, Dec 14, 2015
  2. 1/8 submodule.c: write "Fetching submodule <foo>" to stderrStefan Beller, Dec 14, 2015
  3. 2/8 xread: poll on non blocking fdsStefan Beller, Dec 14, 2015
  4. Eric SunshineDec 14, 2015
  5. Stefan BellerDec 14, 2015
  6. Junio C HamanoDec 14, 2015
  7. Stefan BellerDec 14, 2015
  8. 3/8 xread_nonblock: add functionality to read from fds without blockingStefan Beller, Dec 14, 2015
  9. Junio C HamanoDec 14, 2015
  10. Eric SunshineDec 14, 2015
  11. Eric SunshineDec 14, 2015
  12. Junio C HamanoDec 14, 2015
  13. Jeff KingDec 14, 2015
  14. Stefan BellerDec 15, 2015
  15. Jeff KingDec 15, 2015
  16. Stefan BellerDec 15, 2015
  17. Jeff KingDec 15, 2015
  18. Johannes SixtDec 15, 2015
  19. Junio C HamanoDec 15, 2015
  20. 4/8 strbuf: add strbuf_read_once to read without blockingStefan Beller, Dec 14, 2015
  21. Eric SunshineDec 14, 2015
  22. Stefan BellerDec 14, 2015
  23. 5/8 sigchain: add command to pop all common signalsStefan Beller, Dec 14, 2015
  24. 6/8 run-command: add an asynchronous parallel child processorStefan Beller, Dec 14, 2015
  25. Johannes SixtDec 14, 2015
  26. Stefan BellerDec 14, 2015
  27. 7/8 fetch_populated_submodules: use new parallel job processingStefan Beller, Dec 14, 2015
  28. 8/8 submodules: allow parallel fetching, add tests and documentationStefan Beller, Dec 14, 2015
  29. Johannes SixtDec 14, 2015
  30. Junio C HamanoDec 14, 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.