From 8b2cf27e269441824a7d4afbfa1c7c670382d2d2 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Thu, 28 Mar 2024 11:34:19 +0800 Subject: [PATCH] Optimize IO timeout process in fiber. --- lib_fiber/c/src/event.c | 10 ++++++---- lib_fiber/c/src/event.h | 4 ++-- lib_fiber/c/src/fiber_io.c | 20 ++++++++++---------- lib_fiber/c/src/hook/epoll.c | 4 ++-- lib_fiber/c/src/hook/poll.c | 8 ++++---- 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib_fiber/c/src/event.c b/lib_fiber/c/src/event.c index 072057ca2..0032df544 100644 --- a/lib_fiber/c/src/event.c +++ b/lib_fiber/c/src/event.c @@ -346,14 +346,15 @@ int event_add_write(EVENT *ev, FILE_EVENT *fe, event_proc *proc) return 1; } -void event_del_read(EVENT *ev, FILE_EVENT *fe) +void event_del_read(EVENT *ev, FILE_EVENT *fe, int directly) { if (fe->oper & EVENT_ADD_READ) { fe->oper &=~EVENT_ADD_READ; } if (fe->mask & EVENT_READ) { - if (fe->mask & EVENT_DIRECT) { + if ((fe->mask & EVENT_DIRECT) || directly) { + ring_detach(&fe->me); (void) ev->del_read(ev, fe); } else if (fe->me.parent == &fe->me) { ring_prepend(&ev->events, &fe->me); @@ -365,14 +366,15 @@ void event_del_read(EVENT *ev, FILE_EVENT *fe) fe->r_proc = NULL; } -void event_del_write(EVENT *ev, FILE_EVENT *fe) +void event_del_write(EVENT *ev, FILE_EVENT *fe, int directly) { if (fe->oper & EVENT_ADD_WRITE) { fe->oper &= ~EVENT_ADD_WRITE; } if (fe->mask & EVENT_WRITE) { - if (fe->mask & EVENT_DIRECT) { + if ((fe->mask & EVENT_DIRECT) || directly) { + ring_detach(&fe->me); (void) ev->del_write(ev, fe); } else if (fe->me.parent == &fe->me) { ring_prepend(&ev->events, &fe->me); diff --git a/lib_fiber/c/src/event.h b/lib_fiber/c/src/event.h index 2ffb6e395..b6a0a7a21 100644 --- a/lib_fiber/c/src/event.h +++ b/lib_fiber/c/src/event.h @@ -441,8 +441,8 @@ int event_checkfd(EVENT *ev, FILE_EVENT *fe); int event_add_read(EVENT *ev, FILE_EVENT *fe, event_proc *proc); int event_add_write(EVENT *ev, FILE_EVENT *fe, event_proc *proc); -void event_del_read(EVENT *ev, FILE_EVENT *fe); -void event_del_write(EVENT *ev, FILE_EVENT *fe); +void event_del_read(EVENT *ev, FILE_EVENT *fe, int directly); +void event_del_write(EVENT *ev, FILE_EVENT *fe, int directly); int event_process(EVENT *ev, int left); #endif diff --git a/lib_fiber/c/src/fiber_io.c b/lib_fiber/c/src/fiber_io.c index 2c7bae5b1..ab10ab9c1 100644 --- a/lib_fiber/c/src/fiber_io.c +++ b/lib_fiber/c/src/fiber_io.c @@ -439,7 +439,7 @@ size_t acl_fiber_sleep(size_t seconds) static void read_callback(EVENT *ev, FILE_EVENT *fe) { CLR_READWAIT(fe); - event_del_read(ev, fe); + event_del_read(ev, fe, 0); /* If the reader fiber has been set in ready status when the * other fiber killed the reader fiber, the reader fiber should @@ -502,11 +502,11 @@ int fiber_wait_read(FILE_EVENT *fe) // If the IO has been canceled, we should try to remove the // IO read event, because the wakeup process wasn't from // read_callback normally. -#if 0 +#if 1 // Don't call event_del_read here, because it's a buffered // delete read operation; We'll real delete read operation on // the fd in event_close. - event_del_read(__thread_fiber->event, fe); + event_del_read(__thread_fiber->event, fe, 1); #endif acl_fiber_set_error(curr->errnum); return -1; @@ -514,11 +514,11 @@ int fiber_wait_read(FILE_EVENT *fe) // If the IO reading timeout set in setsockopt. // Clear FIBER_F_TIMER flag been set in wakeup_timers. curr->flag &= ~FIBER_F_TIMER; -#if 0 +#if 1 // Don't call event_del_read here, because it's a buffered // delete read operation; We'll real delete read operation on // the fd in event_close. - event_del_read(__thread_fiber->event, fe); + event_del_read(__thread_fiber->event, fe, 1); #endif acl_fiber_set_errno(curr, FIBER_EAGAIN); @@ -533,7 +533,7 @@ int fiber_wait_read(FILE_EVENT *fe) static void write_callback(EVENT *ev, FILE_EVENT *fe) { CLR_WRITEWAIT(fe); - event_del_write(ev, fe); + event_del_write(ev, fe, 0); /* If the writer fiber has been set in ready status when the * other fiber killed the writer fiber, the writer fiber should @@ -585,15 +585,15 @@ int fiber_wait_write(FILE_EVENT *fe) } if (acl_fiber_canceled(curr)) { -#if 0 - event_del_write(__thread_fiber->event, fe); +#if 1 + event_del_write(__thread_fiber->event, fe, 1); #endif acl_fiber_set_error(curr->errnum); return -1; } else if (curr->flag & FIBER_F_TIMER) { curr->flag &= ~FIBER_F_TIMER; -#if 0 - event_del_write(__thread_fiber->event, fe); +#if 1 + event_del_write(__thread_fiber->event, fe, 1); #endif acl_fiber_set_errno(curr, FIBER_EAGAIN); diff --git a/lib_fiber/c/src/hook/epoll.c b/lib_fiber/c/src/hook/epoll.c index b5f822d48..053771d0d 100644 --- a/lib_fiber/c/src/hook/epoll.c +++ b/lib_fiber/c/src/hook/epoll.c @@ -552,13 +552,13 @@ static void epoll_ctl_del(EVENT *ev, EPOLL_EVENT *ee, int fd) if (epx->mask & EVENT_READ) { assert(epx->fe); - event_del_read(ev, epx->fe); + event_del_read(ev, epx->fe, 0); CLR_READWAIT(epx->fe); } if (epx->mask & EVENT_WRITE) { assert(epx->fe); - event_del_write(ev, epx->fe); + event_del_write(ev, epx->fe, 0); CLR_WRITEWAIT(epx->fe); } diff --git a/lib_fiber/c/src/hook/poll.c b/lib_fiber/c/src/hook/poll.c index 587b2a738..f03dd3ff7 100644 --- a/lib_fiber/c/src/hook/poll.c +++ b/lib_fiber/c/src/hook/poll.c @@ -72,7 +72,7 @@ static void read_callback(EVENT *ev, FILE_EVENT *fe) POLLFD *pfd; RING *iter = fe->pfds.succ, *next = iter; - event_del_read(ev, fe); + event_del_read(ev, fe, 0); SET_READABLE(fe); // Walk througth the RING list, handle each poll event, and one RING @@ -129,7 +129,7 @@ static void write_callback(EVENT *ev, FILE_EVENT *fe) POLLFD *pfd; RING *iter = fe->pfds.succ, *next = iter; - event_del_write(ev, fe); + event_del_write(ev, fe, 0); SET_WRITABLE(fe); for (; iter != &fe->pfds; iter = next) { @@ -209,7 +209,7 @@ static void poll_event_clean(EVENT *ev, POLL_EVENT *pe) pfd->fe->mask &= ~EVENT_POLLIN; pfd->fe->r_timeout = -1; #endif - event_del_read(ev, pfd->fe); + event_del_read(ev, pfd->fe, 0); pfd->fe->fiber_r = NULL; } if (pfd->pfd->events & POLLOUT) { @@ -218,7 +218,7 @@ static void poll_event_clean(EVENT *ev, POLL_EVENT *pe) pfd->fe->mask &= ~EVENT_POLLOUT; pfd->fe->w_timeout = -1; #endif - event_del_write(ev, pfd->fe); + event_del_write(ev, pfd->fe, 0); pfd->fe->fiber_w = NULL; }