From 010160f988bdc8aed6257d4c39a27a458b7eac44 Mon Sep 17 00:00:00 2001
From: Konstantin Osipov <kostja@tarantool.org>
Date: Thu, 28 Mar 2013 11:34:35 +0400
Subject: [PATCH] coio-fiber-detach: review fixes.

---
 src/coio.m | 50 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 19 deletions(-)

diff --git a/src/coio.m b/src/coio.m
index af950ed036..799e4c68da 100644
--- a/src/coio.m
+++ b/src/coio.m
@@ -47,9 +47,24 @@ coio_init(struct ev_io *coio)
 }
 
 static inline void
-coio_update_fiber(struct ev_io *coio)
+coio_fiber_yield(struct ev_io *coio)
 {
 	coio->data = fiber;
+	fiber_yield();
+#ifdef DEBUG
+	coio->data = NULL;
+#endif
+}
+
+static inline bool
+coio_fiber_yield_timeout(struct ev_io *coio, ev_tstamp delay)
+{
+	coio->data = fiber;
+	bool is_timedout = fiber_yield_timeout(delay);
+#ifdef DEBUG
+	coio->data = NULL;
+#endif
+	return is_timedout;
 }
 
 /**
@@ -78,10 +93,9 @@ coio_connect_timeout(struct ev_io *coio, struct sockaddr_in *addr,
 	 * Wait until socket is ready for writing or
 	 * timed out.
 	 */
-	coio_update_fiber(coio);
 	ev_io_set(coio, coio->fd, EV_WRITE);
 	ev_io_start(coio);
-	bool is_timedout = fiber_yield_timeout(timeout);
+	bool is_timedout = coio_fiber_yield_timeout(coio, timeout);
 	ev_io_stop(coio);
 	fiber_testcancel();
 	if (is_timedout) {
@@ -159,16 +173,16 @@ coio_accept(struct ev_io *coio, struct sockaddr_in *addr,
 			evio_setsockopt_tcp(fd);
 			return fd;
 		}
-		/* update current coio fiber context */
-		coio_update_fiber(coio);
 		/* The socket is not ready, yield */
 		if (! ev_is_active(coio)) {
 			ev_io_set(coio, coio->fd, EV_READ);
 			ev_io_start(coio);
 		}
-		/* Yield control to other fibers until the timeout
-		 * is being reached. */
-		bool is_timedout = fiber_yield_timeout(delay);
+		/*
+		 * Yield control to other fibers until the
+		 * timeout is reached.
+		 */
+		bool is_timedout = coio_fiber_yield_timeout(coio, delay);
 		fiber_testcancel();
 		if (is_timedout) {
 			errno = ETIMEDOUT;
@@ -216,7 +230,6 @@ coio_read_ahead_timeout(struct ev_io *coio, void *buf, size_t sz,
 				return sz - to_read;
 			}
 
-			coio_update_fiber(coio);
 			/* The socket is not ready, yield */
 			if (! ev_is_active(coio)) {
 				ev_io_set(coio, coio->fd, EV_READ);
@@ -226,7 +239,8 @@ coio_read_ahead_timeout(struct ev_io *coio, void *buf, size_t sz,
 			 * Yield control to other fibers until the
 			 * timeout is being reached.
 			 */
-			bool is_timedout = fiber_yield_timeout(delay);
+			bool is_timedout = coio_fiber_yield_timeout(coio,
+								    delay);
 			fiber_testcancel();
 			if (is_timedout) {
 				errno = ETIMEDOUT;
@@ -309,20 +323,19 @@ coio_write_timeout(struct ev_io *coio, const void *buf, size_t sz,
 				towrite -= nwr;
 				buf += nwr;
 			}
-			coio_update_fiber(coio);
 			if (! ev_is_active(coio)) {
 				ev_io_set(coio, coio->fd, EV_WRITE);
 				ev_io_start(coio);
 			}
 			/* Yield control to other fibers. */
-			fiber_yield();
 			fiber_testcancel();
 			/*
 			 * Yield control to other fibers until the
 			 * timeout is reached or the socket is
 			 * ready.
 			 */
-			bool is_timedout = fiber_yield_timeout(delay);
+			bool is_timedout = coio_fiber_yield_timeout(coio,
+								    delay);
 			fiber_testcancel();
 
 			if (is_timedout) {
@@ -381,13 +394,12 @@ coio_writev(struct ev_io *coio, struct iovec *iov, int iovcnt,
 					break;
 				}
 			}
-			coio_update_fiber(coio);
 			if (! ev_is_active(coio)) {
 				ev_io_set(coio, coio->fd, EV_WRITE);
 				ev_io_start(coio);
 			}
 			/* Yield control to other fibers. */
-			fiber_yield();
+			coio_fiber_yield(coio);
 			fiber_testcancel();
 		}
 	} @finally {
@@ -420,7 +432,6 @@ coio_sendto_timeout(struct ev_io *coio, const void *buf, size_t sz, int flags,
 						 flags, dest_addr, addrlen);
 			if (nwr > 0)
 				return nwr;
-			coio_update_fiber(coio);
 			if (! ev_is_active(coio)) {
 				ev_io_set(coio, coio->fd, EV_WRITE);
 				ev_io_start(coio);
@@ -430,7 +441,8 @@ coio_sendto_timeout(struct ev_io *coio, const void *buf, size_t sz, int flags,
 			 * timeout is reached or the socket is
 			 * ready.
 			 */
-			bool is_timedout = fiber_yield_timeout(delay);
+			bool is_timedout = coio_fiber_yield_timeout(coio,
+								    delay);
 			fiber_testcancel();
 			if (is_timedout) {
 				errno = ETIMEDOUT;
@@ -469,7 +481,6 @@ coio_recvfrom_timeout(struct ev_io *coio, void *buf, size_t sz, int flags,
 			if (nrd >= 0)
 				return nrd;
 
-			coio_update_fiber(coio);
 			if (! ev_is_active(coio)) {
 				ev_io_set(coio, coio->fd, EV_WRITE);
 				ev_io_start(coio);
@@ -479,7 +490,8 @@ coio_recvfrom_timeout(struct ev_io *coio, void *buf, size_t sz, int flags,
 			 * timeout is reached or the socket is
 			 * ready.
 			 */
-			bool is_timedout = fiber_yield_timeout(delay);
+			bool is_timedout = coio_fiber_yield_timeout(coio,
+								    delay);
 			fiber_testcancel();
 			if (is_timedout) {
 				errno = ETIMEDOUT;
-- 
GitLab