--- libaitsched/src/hooks.c 2022/10/17 22:45:06 1.36 +++ libaitsched/src/hooks.c 2022/10/19 01:45:08 1.37 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: hooks.c,v 1.36 2022/10/17 22:45:06 misho Exp $ +* $Id: hooks.c,v 1.37 2022/10/19 01:45:08 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -204,7 +204,7 @@ sched_hook_cancel(void *task, void *arg __unused) { sched_task_t *t = task, *tmp, *tt; sched_root_task_t *r = NULL; - int flg; + int flg = 0; #if SUP_ENABLE == KQ_SUPPORT struct kevent chg[1]; struct timespec timeout = { 0, 0 }; @@ -229,11 +229,8 @@ sched_hook_cancel(void *task, void *arg __unused) switch (TASK_TYPE(t)) { case taskREAD: /* check for multi subscribers */ - flg = 0; TAILQ_FOREACH_SAFE(tt, &r->root_read, task_node, tmp) - if (TASK_FD(tt) != TASK_FD(t)) - continue; - else + if (TASK_FD(tt) == TASK_FD(t)) flg++; #if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ @@ -258,21 +255,17 @@ sched_hook_cancel(void *task, void *arg __unused) FD_CLR(TASK_FD(t), &r->root_fds[0]); /* optimize select */ - for (i = r->root_kq - 1; i > 2; i--) + for (i = r->root_kq - 1; i >= 0; i--) if (FD_ISSET(i, &r->root_fds[0]) || FD_ISSET(i, &r->root_fds[1])) break; - if (i > 2) - r->root_kq = i + 1; + r->root_kq = i + 1; } #endif break; case taskWRITE: /* check for multi subscribers */ - flg = 0; TAILQ_FOREACH_SAFE(tt, &r->root_write, task_node, tmp) - if (TASK_FD(tt) != TASK_FD(t)) - continue; - else + if (TASK_FD(tt) == TASK_FD(t)) flg++; #if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ @@ -297,22 +290,18 @@ sched_hook_cancel(void *task, void *arg __unused) FD_CLR(TASK_FD(t), &r->root_fds[1]); /* optimize select */ - for (i = r->root_kq - 1; i > 2; i--) + for (i = r->root_kq - 1; i >= 0; i--) if (FD_ISSET(i, &r->root_fds[0]) || FD_ISSET(i, &r->root_fds[1])) break; - if (i > 2) - r->root_kq = i + 1; + r->root_kq = i + 1; } #endif break; case taskALARM: #if SUP_ENABLE == KQ_SUPPORT /* check for multi subscribers */ - flg = 0; TAILQ_FOREACH_SAFE(tt, &r->root_alarm, task_node, tmp) - if (TASK_DATA(tt) != TASK_DATA(t)) - continue; - else + if (TASK_DATA(tt) == TASK_DATA(t)) flg++; #ifdef __NetBSD__ EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, flg < 2 ? EV_DELETE : 0, @@ -326,11 +315,8 @@ sched_hook_cancel(void *task, void *arg __unused) case taskNODE: #if SUP_ENABLE == KQ_SUPPORT /* check for multi subscribers */ - flg = 0; TAILQ_FOREACH_SAFE(tt, &r->root_node, task_node, tmp) - if (TASK_FD(tt) != TASK_FD(t)) - continue; - else + if (TASK_FD(tt) == TASK_FD(t)) flg++; #ifdef __NetBSD__ EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, flg < 2 ? EV_DELETE : 0, @@ -344,11 +330,8 @@ sched_hook_cancel(void *task, void *arg __unused) case taskPROC: #if SUP_ENABLE == KQ_SUPPORT /* check for multi subscribers */ - flg = 0; TAILQ_FOREACH_SAFE(tt, &r->root_proc, task_node, tmp) - if (TASK_VAL(tt) != TASK_VAL(t)) - continue; - else + if (TASK_VAL(tt) == TASK_VAL(t)) flg++; #ifdef __NetBSD__ EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, flg < 2 ? EV_DELETE : 0, @@ -362,11 +345,8 @@ sched_hook_cancel(void *task, void *arg __unused) case taskSIGNAL: #if SUP_ENABLE == KQ_SUPPORT /* check for multi subscribers */ - flg = 0; TAILQ_FOREACH_SAFE(tt, &r->root_signal, task_node, tmp) - if (TASK_VAL(tt) != TASK_VAL(t)) - continue; - else + if (TASK_VAL(tt) == TASK_VAL(t)) flg++; #ifdef __NetBSD__ EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, flg < 2 ? EV_DELETE : 0, @@ -384,11 +364,8 @@ sched_hook_cancel(void *task, void *arg __unused) case taskAIO: #if SUP_ENABLE == KQ_SUPPORT /* check for multi subscribers */ - flg = 0; TAILQ_FOREACH_SAFE(tt, &r->root_aio, task_node, tmp) - if (TASK_VAL(tt) != TASK_VAL(t)) - continue; - else + if (TASK_VAL(tt) == TASK_VAL(t)) flg++; #ifdef __NetBSD__ EV_SET(&chg[0], TASK_VAL(t), EVFILT_AIO, flg < 2 ? EV_DELETE : 0, @@ -401,7 +378,7 @@ sched_hook_cancel(void *task, void *arg __unused) if (acb) { if (aio_cancel(acb->aio_fildes, acb) == AIO_CANCELED) aio_return(acb); - free(acb); + e_free(acb); TASK_VAL(t) = 0; } #endif @@ -410,11 +387,8 @@ sched_hook_cancel(void *task, void *arg __unused) case taskLIO: #if SUP_ENABLE == KQ_SUPPORT /* check for multi subscribers */ - flg = 0; TAILQ_FOREACH_SAFE(tt, &r->root_lio, task_node, tmp) - if (TASK_VAL(tt) != TASK_VAL(t)) - continue; - else + if (TASK_VAL(tt) == TASK_VAL(t)) flg++; #ifdef __NetBSD__ EV_SET(&chg[0], TASK_VAL(t), EVFILT_LIO, flg < 2 ? EV_DELETE : 0, @@ -428,9 +402,9 @@ sched_hook_cancel(void *task, void *arg __unused) for (i = 0; i < TASK_DATLEN(t); i++) { if (aio_cancel(acbs[i]->aio_fildes, acbs[i]) == AIO_CANCELED) aio_return(acbs[i]); - free(acbs[i]); + e_free(acbs[i]); } - free(acbs); + e_free(acbs); TASK_VAL(t) = 0; } #endif @@ -441,11 +415,8 @@ sched_hook_cancel(void *task, void *arg __unused) case taskUSER: #if SUP_ENABLE == KQ_SUPPORT /* check for multi subscribers */ - flg = 0; TAILQ_FOREACH_SAFE(tt, &r->root_user, task_node, tmp) - if (TASK_VAL(tt) != TASK_VAL(t)) - continue; - else + if (TASK_VAL(tt) == TASK_VAL(t)) flg++; #ifdef __NetBSD__ EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, flg < 2 ? EV_DELETE : 0, @@ -477,11 +448,8 @@ sched_hook_cancel(void *task, void *arg __unused) schedCancel((sched_task_t*) TASK_RET(t)); #else /* check for multi subscribers */ - flg = 0; TAILQ_FOREACH_SAFE(tt, &r->root_rtc, task_node, tmp) - if (TASK_DATA(tt) != TASK_DATA(t)) - continue; - else + if (TASK_DATA(tt) == TASK_DATA(t)) flg++; /* restore signal */ @@ -497,7 +465,9 @@ sched_hook_cancel(void *task, void *arg __unused) #if SUP_ENABLE == KQ_SUPPORT kevent(r->root_kq, chg, 1, NULL, 0, &timeout); #elif SUP_ENABLE == EP_SUPPORT - epoll_ctl(r->root_kq, ee.events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, ee.data.fd, &ee); + if (TASK_TYPE(t) == taskREAD || TASK_TYPE(t) == taskWRITE) { + epoll_ctl(r->root_kq, ee.events ? EPOLL_CTL_MOD : EPOLL_CTL_DEL, ee.data.fd, &ee); + } #endif return NULL; } @@ -650,8 +620,9 @@ sched_hook_write(void *task, void *arg __unused) flg |= 1; ee.events |= EPOLLIN | EPOLLPRI; } - if (FD_ISSET(TASK_FD(t), &r->root_fds[1])) + if (FD_ISSET(TASK_FD(t), &r->root_fds[1])) { flg |= 2; + } if (epoll_ctl(r->root_kq, flg ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, TASK_FD(t), &ee) == -1) { if (r->root_hooks.hook_exec.exception) @@ -1015,7 +986,7 @@ fetch_hook_kevent_proceed(int en, struct kevent *res, LOGERR; } else LOGERR; - free(acb); + e_free(acb); TASK_DATLEN(task) = (u_long) len; TASK_FD(task) = fd; } @@ -1043,9 +1014,9 @@ fetch_hook_kevent_proceed(int en, struct kevent *res, l = 0; else l = iv[i].iov_len; - free(acbs[i]); + e_free(acbs[i]); } - free(acbs); + e_free(acbs); TASK_DATLEN(task) = (u_long) len; TASK_FD(task) = fd; @@ -1093,26 +1064,27 @@ fetch_hook_kevent_proceed(int en, struct kevent *res, static inline void fetch_hook_epoll_proceed(int en, struct epoll_event *res, sched_root_task_t *r) { - register int i, flg; + register int i, rflg, wflg; int ops = EPOLL_CTL_DEL; sched_task_t *t, *tmp, *task; struct epoll_event evt[1]; for (i = 0; i < en; i++) { memcpy(evt, &res[i], sizeof evt); + evt->events ^= evt->events; + rflg = wflg = 0; - if (evt->events & (EPOLLIN | EPOLLPRI)) { - flg = 0; + if (res[i].events & (EPOLLIN | EPOLLPRI)) { task = NULL; TAILQ_FOREACH_SAFE(t, &r->root_read, task_node, tmp) { if (TASK_FD(t) == evt->data.fd) { - if (!flg) + if (!task) task = t; - flg++; + rflg++; } } - if (flg && task) { + if (task) { TASK_FLAG(task) = ioctl(TASK_FD(task), FIONREAD, &TASK_RET(task)); /* remove read handle */ remove_task_from(task, &r->root_read); @@ -1131,29 +1103,27 @@ fetch_hook_epoll_proceed(int en, struct epoll_event *r insert_task_to(task, &r->root_ready); } - evt->events ^= evt->events; - if (FD_ISSET(evt->data.fd, &r->root_fds[1])) { - ops = EPOLL_CTL_MOD; + if (!(res[i].events & EPOLLOUT) && FD_ISSET(evt->data.fd, &r->root_fds[1])) { evt->events |= EPOLLOUT; + wflg = 42; } - if (flg > 1) { - ops = EPOLL_CTL_MOD; + if (rflg > 1) evt->events |= EPOLLIN | EPOLLPRI; - } else + else FD_CLR(evt->data.fd, &r->root_fds[0]); } - } else if (evt->events & EPOLLOUT) { - flg = 0; + } + if (res[i].events & EPOLLOUT) { task = NULL; TAILQ_FOREACH_SAFE(t, &r->root_write, task_node, tmp) { if (TASK_FD(t) == evt->data.fd) { - if (!flg) + if (!task) task = t; - flg++; + wflg++; } } - if (flg && task) { + if (task) { TASK_FLAG(task) = ioctl(TASK_FD(task), FIONWRITE, &TASK_RET(task)); /* remove write handle */ remove_task_from(task, &r->root_write); @@ -1172,27 +1142,21 @@ fetch_hook_epoll_proceed(int en, struct epoll_event *r insert_task_to(task, &r->root_ready); } - evt->events ^= evt->events; - if (FD_ISSET(evt->data.fd, &r->root_fds[0])) { - ops = EPOLL_CTL_MOD; + if (!(res[i].events & (EPOLLIN | EPOLLPRI)) && FD_ISSET(evt->data.fd, &r->root_fds[0])) { evt->events |= EPOLLIN | EPOLLPRI; + rflg = 42; } - if (flg > 1) { - ops = EPOLL_CTL_MOD; + if (wflg > 1) evt->events |= EPOLLOUT; - } else + else FD_CLR(evt->data.fd, &r->root_fds[1]); } } + if (rflg > 1 || wflg > 1) + ops = EPOLL_CTL_MOD; + if (epoll_ctl(r->root_kq, ops, evt->data.fd, evt) == -1) { - if (errno == EBADF) { - epoll_ctl(r->root_kq, EPOLL_CTL_DEL, evt->data.fd, evt); - schedCancelby(r, taskREAD, CRITERIA_FD, - (void*) (uintptr_t) evt->data.fd, NULL); - schedCancelby(r, taskWRITE, CRITERIA_FD, - (void*) (uintptr_t) evt->data.fd, NULL); - } if (r->root_hooks.hook_exec.exception) { r->root_hooks.hook_exec.exception(r, NULL); } else @@ -1206,25 +1170,30 @@ fetch_hook_epoll_proceed(int en, struct epoll_event *r static inline void fetch_hook_select_proceed(int en, fd_set rfd, fd_set wfd, fd_set xfd, sched_root_task_t *r) { - register int i, flg; - sched_task_t *t, *tmp, *task = NULL; + register int i, rflg, wflg; + sched_task_t *t, *tmp, *task; /* skip select check if return value from select is zero */ if (!en) return; for (i = 0; i < r->root_kq; i++) { + if (!FD_ISSET(i, &r->root_fds[0]) && !FD_ISSET(i, &r->root_fds[1])) + continue; + + rflg = wflg = 0; + if (FD_ISSET(i, &rfd) || FD_ISSET(i, &xfd)) { - flg = 0; + task = NULL; TAILQ_FOREACH_SAFE(t, &r->root_read, task_node, tmp) { if (TASK_FD(t) == i) { - if (!flg) + if (!task) task = t; - flg++; + rflg++; } } - if (flg && task) { + if (task) { TASK_FLAG(task) = ioctl(TASK_FD(task), FIONREAD, &TASK_RET(task)); /* remove read handle */ @@ -1244,20 +1213,21 @@ fetch_hook_select_proceed(int en, fd_set rfd, fd_set w } /* remove resouce */ - if (flg == 1) + if (rflg == 1) FD_CLR(i, &r->root_fds[0]); } - } else if (FD_ISSET(i, &wfd)) { - flg = 0; + } + if (FD_ISSET(i, &wfd)) { + task = NULL; TAILQ_FOREACH_SAFE(t, &r->root_write, task_node, tmp) { if (TASK_FD(t) == i) { - if (!flg) + if (!task) task = t; - flg++; + wflg++; } } - if (flg && task) { + if (task) { TASK_FLAG(task) = ioctl(TASK_FD(task), FIONWRITE, &TASK_RET(task)); /* remove write handle */ @@ -1277,18 +1247,17 @@ fetch_hook_select_proceed(int en, fd_set rfd, fd_set w } /* remove resouce */ - if (flg == 1) + if (wflg == 1) FD_CLR(i, &r->root_fds[1]); } } } /* optimize select */ - for (i = r->root_kq - 1; i > 2; i--) + for (i = r->root_kq - 1; i >= 0; i--) if (FD_ISSET(i, &r->root_fds[0]) || FD_ISSET(i, &r->root_fds[1])) break; - if (i > 2) - r->root_kq = i + 1; + r->root_kq = i + 1; } #endif