From e3a50c370da2c7ae248ee4177483fbcd02ff0c44 Mon Sep 17 00:00:00 2001 From: zsxxsz Date: Tue, 16 Jan 2018 23:46:42 +0800 Subject: [PATCH] fiber io: incorrect used SET_READABLE --- lib_fiber/c/src/hook/io.c | 10 +++---- lib_fiber/c/src/hook/socket.c | 51 ++++++++++++++++++++++------------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/lib_fiber/c/src/hook/io.c b/lib_fiber/c/src/hook/io.c index 714a81a66..c23678a05 100644 --- a/lib_fiber/c/src/hook/io.c +++ b/lib_fiber/c/src/hook/io.c @@ -201,7 +201,7 @@ ssize_t acl_fiber_read(socket_t fd, void *buf, size_t count) ssize_t ret; int err; - if (SET_READABLE(fe)) { + if (IS_READABLE(fe)) { CLR_READABLE(fe); } else { fiber_wait_read(fe); @@ -250,7 +250,7 @@ ssize_t acl_fiber_readv(socket_t fd, const struct iovec *iov, int iovcnt) ssize_t ret; int err; - if (SET_READABLE(fe)) { + if (IS_READABLE(fe)) { CLR_READABLE(fe); } else { fiber_wait_read(fe); @@ -304,7 +304,7 @@ ssize_t __stdcall acl_fiber_recv(socket_t sockfd, void *buf, size_t len, int fla ssize_t ret; int err; - if (SET_READABLE(fe)) { + if (IS_READABLE(fe)) { CLR_READABLE(fe); } else { fiber_wait_read(fe); @@ -360,7 +360,7 @@ ssize_t __stdcall acl_fiber_recvfrom(socket_t sockfd, void *buf, size_t len, ssize_t ret; int err; - if (SET_READABLE(fe)) { + if (IS_READABLE(fe)) { CLR_READABLE(fe); } else { fiber_wait_read(fe); @@ -412,7 +412,7 @@ ssize_t acl_fiber_recvmsg(socket_t sockfd, struct msghdr *msg, int flags) ssize_t ret; int err; - if (SET_READABLE(fe)) { + if (IS_READABLE(fe)) { CLR_READABLE(fe); } else { fiber_wait_read(fe); diff --git a/lib_fiber/c/src/hook/socket.c b/lib_fiber/c/src/hook/socket.c index 15bb104b8..117e402a9 100644 --- a/lib_fiber/c/src/hook/socket.c +++ b/lib_fiber/c/src/hook/socket.c @@ -100,6 +100,12 @@ int acl_fiber_listen(socket_t sockfd, int backlog) return -1; } +#if FIBER_EAGAIN == FIBER_EWOULDBLOCK +# define error_again(x) ((x) == FIBER_EAGAIN) +#else +# define error_again(x) ((x) == FIBER_EAGAIN || (x) == FIBER_EWOULDBLOCK) +#endif + //#define FAST_ACCEPT socket_t __stdcall acl_fiber_accept(socket_t sockfd, struct sockaddr *addr, @@ -107,9 +113,7 @@ socket_t __stdcall acl_fiber_accept(socket_t sockfd, struct sockaddr *addr, { FILE_EVENT *fe; socket_t clifd; -#ifdef FAST_ACCEPT int err; -#endif if (sockfd == INVALID_SOCKET) { msg_error("%s: invalid sockfd %d", __FUNCTION__, sockfd); @@ -167,24 +171,35 @@ socket_t __stdcall acl_fiber_accept(socket_t sockfd, struct sockaddr *addr, return clifd; #else fe = fiber_file_open(sockfd); - fiber_wait_read(fe); - if (acl_fiber_killed(fe->fiber)) { - msg_info("%s(%d), %s: fiber-%u was killed", __FILE__, - __LINE__, __FUNCTION__, acl_fiber_id(fe->fiber)); - return -1; + while(1) { + if (IS_READABLE(fe)) { + CLR_READABLE(fe); + } else { + fiber_wait_read(fe); + } + + clifd = __sys_accept(sockfd, addr, addrlen); + + if (clifd != INVALID_SOCKET) { + non_blocking(clifd, NON_BLOCKING); + tcp_nodelay(clifd, 1); + return clifd; + } + + err = acl_fiber_last_error(); + fiber_save_errno(err); + + if (acl_fiber_killed(fe->fiber)) { + msg_info("%s(%d), %s: fiber-%u was killed", __FILE__, + __LINE__, __FUNCTION__, acl_fiber_id(fe->fiber)); + return -1; + } + + if (!error_again(err)) { + return clifd; + } } - - clifd = __sys_accept(sockfd, addr, addrlen); - - if (clifd != INVALID_SOCKET) { - non_blocking(clifd, NON_BLOCKING); - tcp_nodelay(clifd, 1); - return clifd; - } - - fiber_save_errno(acl_fiber_last_error()); - return clifd; #endif }