From dde1648e936c3aa3229bf918ff10e122af4f1672 Mon Sep 17 00:00:00 2001
From: Dmitry Simonenko <pmwkaa@gmail.com>
Date: Wed, 27 Mar 2013 17:41:44 +0400
Subject: [PATCH] coio-fiber-detach: move coio fiber update from lua socket to
 coio itself.

---
 include/coio.h       |  3 ---
 src/coio.m           | 12 ++++++++++--
 src/lua/lua_socket.m | 10 ----------
 3 files changed, 10 insertions(+), 15 deletions(-)

diff --git a/include/coio.h b/include/coio.h
index 11ad141763..da9814f2fe 100644
--- a/include/coio.h
+++ b/include/coio.h
@@ -65,9 +65,6 @@ coio_accept(struct ev_io *coio, struct sockaddr_in *addr, socklen_t addrlen,
 void
 coio_init(struct ev_io *coio);
 
-void
-coio_update_fiber(struct ev_io *coio);
-
 ssize_t
 coio_read_ahead_timeout(struct ev_io *coio, void *buf, size_t sz, size_t bufsiz,
 		        ev_tstamp timeout);
diff --git a/src/coio.m b/src/coio.m
index 461f70c15d..af950ed036 100644
--- a/src/coio.m
+++ b/src/coio.m
@@ -46,7 +46,7 @@ coio_init(struct ev_io *coio)
 	coio->fd = -1;
 }
 
-void
+static inline void
 coio_update_fiber(struct ev_io *coio)
 {
 	coio->data = fiber;
@@ -78,6 +78,7 @@ 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);
@@ -150,7 +151,6 @@ coio_accept(struct ev_io *coio, struct sockaddr_in *addr,
 {
 	ev_tstamp start, delay;
 	evio_timeout_init(&start, &delay, timeout);
-
 	while (true) {
 		/* Assume that there are waiting clients
 		 * available */
@@ -159,6 +159,8 @@ 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);
@@ -213,6 +215,8 @@ coio_read_ahead_timeout(struct ev_io *coio, void *buf, size_t sz,
 				errno = 0;
 				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);
@@ -305,6 +309,7 @@ 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);
@@ -376,6 +381,7 @@ 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);
@@ -414,6 +420,7 @@ 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);
@@ -462,6 +469,7 @@ 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);
diff --git a/src/lua/lua_socket.m b/src/lua/lua_socket.m
index 337f5506b5..e49e7b6397 100644
--- a/src/lua/lua_socket.m
+++ b/src/lua/lua_socket.m
@@ -257,7 +257,6 @@ lbox_socket_shutdown(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checkactivesocket(L, -1);
 	int how = luaL_checkint(L, 2);
-	coio_update_fiber(&s->coio);
 	bio_clearerr(s);
 	if (shutdown(s->coio.fd, how))
 		return bio_pushsockerror(L, s, errno);
@@ -298,7 +297,6 @@ lbox_socket_connect(struct lua_State *L)
 		timeout = luaL_checknumber(L, 4);
 	if (evio_is_active(&s->coio))
 		return bio_pushsockerror(L, s, EALREADY);
-	coio_update_fiber(&s->coio);
 	bio_clearerr(s);
 
 	/* try to resolve a hostname */
@@ -349,7 +347,6 @@ lbox_socket_send(struct lua_State *L)
 		timeout = luaL_checknumber(L, 3);
 	if (s->iob == NULL)
 		return bio_pushsenderror(L, s, 0, ENOTCONN);
-	coio_update_fiber(&s->coio);
 	bio_clearerr(s);
 	@try {
 		ssize_t nwr = coio_write_timeout(&s->coio, buf, buf_size,
@@ -387,7 +384,6 @@ lbox_socket_recv(struct lua_State *L)
 		timeout = luaL_checknumber(L, 3);
 	if (s->iob == NULL)
 		return bio_pushrecverror(L, s, ENOTCONN);
-	coio_update_fiber(&s->coio);
 	/* Clear possible old timeout status. */
 	bio_clearerr(s);
 	/*
@@ -569,7 +565,6 @@ lbox_socket_readline(struct lua_State *L)
 	struct bio_socket *s = bio_checkactivesocket(L, 1);
 	if (s->iob == NULL)
 		return bio_pushrecverror(L, s, ENOTCONN);
-	coio_update_fiber(&s->coio);
 	bio_clearerr(s);
 
 	unsigned int limit = UINT_MAX;
@@ -658,7 +653,6 @@ lbox_socket_bind(struct lua_State *L)
 		timeout = luaL_checknumber(L, 4);
 	if (evio_is_active(&s->coio))
 		return bio_pusherror(L, s, EALREADY);
-	coio_update_fiber(&s->coio);
 	bio_clearerr(s);
 	/* try to resolve a hostname */
 	struct addrinfo *ai = coeio_resolve(s->socktype, host, port, timeout);
@@ -690,7 +684,6 @@ static int
 lbox_socket_listen(struct lua_State *L)
 {
 	struct bio_socket *s = bio_checkactivesocket(L, 1);
-	coio_update_fiber(&s->coio);
 	bio_clearerr(s);
 	if (listen(s->coio.fd, sio_listen_backlog()))
 		return bio_pusherror(L, s, errno);
@@ -715,7 +708,6 @@ lbox_socket_accept(struct lua_State *L)
 	double timeout = TIMEOUT_INFINITY;
 	if (lua_gettop(L) == 2)
 		timeout = luaL_checknumber(L, 2);
-	coio_update_fiber(&s->coio);
 	bio_clearerr(s);
 
 	struct sockaddr_storage addr;
@@ -765,7 +757,6 @@ lbox_socket_sendto(struct lua_State *L)
 	double timeout = TIMEOUT_INFINITY;
 	if (lua_gettop(L) == 5)
 		timeout = luaL_checknumber(L, 5);
-	coio_update_fiber(&s->coio);
 	bio_clearerr(s);
 
 	/* try to resolve a hostname */
@@ -830,7 +821,6 @@ lbox_socket_recvfrom(struct lua_State *L)
 	double timeout = TIMEOUT_INFINITY;
 	if (lua_gettop(L) == 3)
 		timeout = luaL_checknumber(L, 3);
-	coio_update_fiber(&s->coio);
 	bio_clearerr(s);
 
 	/* Maybe initialize the buffer, can throw ER_MEMORY_ISSUE. */
-- 
GitLab