[Tarantool-patches] [PATCH v6 1/4] coio: Export helpers and provide coio_read_fd_timeout

Cyrill Gorcunov gorcunov at gmail.com
Tue Dec 17 15:54:17 MSK 2019


There is no reason to hide functions. In particular
we will use coio_write_fd_timeout and coio_read_fd_timeout
for popen.

Part-of #4031

Signed-off-by: Cyrill Gorcunov <gorcunov at gmail.com>
---
 src/box/applier.cc   |  2 +-
 src/lib/core/coio.cc | 41 +++++++++++++++++++++++++++++++++++++++++
 src/lib/core/coio.h  | 31 ++++++++++++++++++++++++++-----
 3 files changed, 68 insertions(+), 6 deletions(-)

diff --git a/src/box/applier.cc b/src/box/applier.cc
index 42374f886..a44c6693f 100644
--- a/src/box/applier.cc
+++ b/src/box/applier.cc
@@ -935,7 +935,7 @@ applier_disconnect(struct applier *applier, enum applier_state state)
 		applier->writer = NULL;
 	}
 
-	coio_close(loop(), &applier->io);
+	coio_close_io(loop(), &applier->io);
 	/* Clear all unparsed input. */
 	ibuf_reinit(&applier->ibuf);
 	fiber_gc();
diff --git a/src/lib/core/coio.cc b/src/lib/core/coio.cc
index e88d724d5..b16497b7b 100644
--- a/src/lib/core/coio.cc
+++ b/src/lib/core/coio.cc
@@ -740,6 +740,47 @@ int coio_close(int fd)
 	return close(fd);
 }
 
+ssize_t
+coio_read_fd_timeout(int fd, void *data, size_t size, ev_tstamp timeout)
+{
+	ev_tstamp start, delay;
+	ev_loop *loop = loop();
+	ssize_t pos = 0;
+
+	evio_timeout_init(loop, &start, &delay, timeout);
+
+	while (size > 0) {
+		ssize_t rc = read(fd, data, size);
+		if (rc > 0) {
+			size -= (size_t)rc;
+			pos += rc;
+			data = (char *)data + rc;
+			continue;
+		} else if (rc == 0) {
+			/*
+			 * Socket peer could be closed
+			 * or shot down.
+			 */
+			break;
+		}
+
+		if (delay <= 0) {
+			diag_set(TimedOut);
+			return -1;
+		}
+
+		if (errno == EAGAIN || errno == EWOULDBLOCK) {
+			coio_wait(fd, COIO_READ, delay);
+			evio_timeout_update(loop, &start, &delay);
+		} else if (errno != EINTR) {
+			diag_set(SocketError, sio_socketname(fd), "read");
+			return -1;
+		}
+	}
+
+	return pos;
+}
+
 int
 coio_write_fd_timeout(int fd, const void *data, size_t size, ev_tstamp timeout)
 {
diff --git a/src/lib/core/coio.h b/src/lib/core/coio.h
index 6a2337689..e1b80ad68 100644
--- a/src/lib/core/coio.h
+++ b/src/lib/core/coio.h
@@ -32,9 +32,16 @@
  */
 #include "fiber.h"
 #include "trivia/util.h"
-#if defined(__cplusplus)
+
 #include "evio.h"
 
+#if defined(__cplusplus)
+extern "C" {
+#endif /* defined(__cplusplus) */
+
+struct sockaddr;
+struct uri;
+
 /**
  * Co-operative I/O
  * Yield the current fiber until IO is ready.
@@ -70,8 +77,12 @@ coio_accept(struct ev_io *coio, struct sockaddr *addr, socklen_t addrlen,
 void
 coio_create(struct ev_io *coio, int fd);
 
+/*
+ * Due to name conflict with coio_close in API_EXPORT
+ * we have to use coio_close_io() instead of plain coio_close().
+ */
 static inline void
-coio_close(ev_loop *loop, struct ev_io *coio)
+coio_close_io(ev_loop *loop, struct ev_io *coio)
 {
 	return evio_close(loop, coio);
 }
@@ -185,9 +196,6 @@ coio_stat_stat_timeout(ev_stat *stat, ev_tstamp delay);
 int
 coio_waitpid(pid_t pid);
 
-extern "C" {
-#endif /* defined(__cplusplus) */
-
 /** \cond public */
 
 enum {
@@ -232,6 +240,19 @@ coio_close(int fd);
 int
 coio_write_fd_timeout(int fd, const void *data, size_t size, ev_tstamp timeout);
 
+/**
+ * Read from a socket in at most @a timeout seconds.
+ * @param fd Socket descriptor.
+ * @param data Data to read.
+ * @param size Size of @a data.
+ * @param timeout Timeout on the operation.
+ *
+ * @retval >= 0 Read bytes (0 treated as EOF).
+ * @retval -1 Timeout or socket error.
+ */
+ssize_t
+coio_read_fd_timeout(int fd, void *data, size_t size, ev_tstamp timeout);
+
 #if defined(__cplusplus)
 } /* extern "C" */
 #endif /* defined(__cplusplus) */
-- 
2.20.1



More information about the Tarantool-patches mailing list