--- libaitsched/src/hooks.c 2014/04/27 16:20:37 1.27 +++ libaitsched/src/hooks.c 2014/05/21 23:05:43 1.27.2.5 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: hooks.c,v 1.27 2014/04/27 16:20:37 misho Exp $ +* $Id: hooks.c,v 1.27.2.5 2014/05/21 23:05:43 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -62,12 +62,18 @@ sched_hook_init(void *root, void *arg __unused) if (!r) return (void*) -1; -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT r->root_kq = kqueue(); if (r->root_kq == -1) { LOGERR; return (void*) -1; } +#elif SUP_ENABLE == EP_SUPPORT + r->root_kq = epoll_create(KQ_EVENTS); + if (r->root_kq == -1) { + LOGERR; + return (void*) -1; + } #else r->root_kq ^= r->root_kq; FD_ZERO(&r->root_fds[0]); @@ -92,7 +98,7 @@ sched_hook_fini(void *root, void *arg __unused) if (!r) return (void*) -1; -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT || SUP_ENABLE == EP_SUPPORT if (r->root_kq > 2) { close(r->root_kq); r->root_kq = 0; @@ -117,12 +123,12 @@ void * sched_hook_cancel(void *task, void *arg __unused) { sched_task_t *t = task; -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT struct kevent chg[1]; struct timespec timeout = { 0, 0 }; #else sched_root_task_t *r = NULL; - register int i; + register int i = -1; #endif #ifdef AIO_SUPPORT struct aiocb *acb; @@ -134,18 +140,20 @@ sched_hook_cancel(void *task, void *arg __unused) if (!t || !TASK_ROOT(t)) return (void*) -1; -#ifdef KQ_DISABLE +#if SUP_ENABLE != KQ_SUPPORT r = TASK_ROOT(t); #endif switch (TASK_TYPE(t)) { case taskREAD: -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t)); #else EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (void*) TASK_FD(t)); #endif +#elif SUP_ENABLE == EP_SUPPORT + i = TASK_FD(t); #else FD_CLR(TASK_FD(t), &r->root_fds[0]); @@ -158,12 +166,14 @@ sched_hook_cancel(void *task, void *arg __unused) #endif break; case taskWRITE: -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t)); #else EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (void*) TASK_FD(t)); #endif +#elif SUP_ENABLE == EP_SUPPORT + i = TASK_FD(t); #else FD_CLR(TASK_FD(t), &r->root_fds[1]); @@ -176,7 +186,7 @@ sched_hook_cancel(void *task, void *arg __unused) #endif break; case taskALARM: -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_DELETE, 0, 0, (intptr_t) TASK_DATA(t)); @@ -187,7 +197,7 @@ sched_hook_cancel(void *task, void *arg __unused) #endif break; case taskNODE: -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t)); #else @@ -196,7 +206,7 @@ sched_hook_cancel(void *task, void *arg __unused) #endif break; case taskPROC: -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t)); #else @@ -205,7 +215,7 @@ sched_hook_cancel(void *task, void *arg __unused) #endif break; case taskSIGNAL: -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t)); #else @@ -217,7 +227,7 @@ sched_hook_cancel(void *task, void *arg __unused) break; #ifdef AIO_SUPPORT case taskAIO: -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], TASK_VAL(t), EVFILT_AIO, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t)); #else @@ -234,7 +244,7 @@ sched_hook_cancel(void *task, void *arg __unused) break; #ifdef EVFILT_LIO case taskLIO: -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], TASK_VAL(t), EVFILT_LIO, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t)); #else @@ -256,7 +266,7 @@ sched_hook_cancel(void *task, void *arg __unused) #endif /* AIO_SUPPORT */ #ifdef EVFILT_USER case taskUSER: -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t)); #else @@ -267,7 +277,8 @@ sched_hook_cancel(void *task, void *arg __unused) #endif /* EVFILT_USER */ case taskTHREAD: #ifdef HAVE_LIBPTHREAD - pthread_cancel((pthread_t) TASK_VAL(t)); + if (TASK_VAL(t)) + pthread_cancel((pthread_t) TASK_VAL(t)); #endif return NULL; #if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME) && defined(HAVE_TIMER_DELETE) @@ -280,8 +291,11 @@ sched_hook_cancel(void *task, void *arg __unused) return NULL; } -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout); +#elif SUP_ENABLE == EP_SUPPORT + if (i != -1) + epoll_ctl(TASK_ROOT(t)->root_kq, EPOLL_CTL_DEL, i, NULL); #endif return NULL; } @@ -333,20 +347,19 @@ void * sched_hook_read(void *task, void *arg __unused) { sched_task_t *t = task; -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT struct kevent chg[1]; struct timespec timeout = { 0, 0 }; +#elif SUP_ENABLE == EP_SUPPORT + struct epoll_event ee = { .events = EPOLLIN | EPOLLPRI | EPOLLRDHUP, .data.fd = 0 }; #else sched_root_task_t *r = NULL; #endif if (!t || !TASK_ROOT(t)) return (void*) -1; -#ifdef KQ_DISABLE - r = TASK_ROOT(t); -#endif -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t)); #else @@ -359,7 +372,17 @@ sched_hook_read(void *task, void *arg __unused) LOGERR; return (void*) -1; } +#elif SUP_ENABLE == EP_SUPPORT + ee.data.fd = TASK_FD(t); + if (epoll_ctl(TASK_ROOT(t)->root_kq, EPOLL_CTL_ADD, TASK_FD(t), &ee) == -1) { + if (TASK_ROOT(t)->root_hooks.hook_exec.exception) + TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL); + else + LOGERR; + return (void*) -1; + } #else + r = TASK_ROOT(t); FD_SET(TASK_FD(t), &r->root_fds[0]); if (TASK_FD(t) >= r->root_kq) r->root_kq = TASK_FD(t) + 1; @@ -379,20 +402,19 @@ void * sched_hook_write(void *task, void *arg __unused) { sched_task_t *t = task; -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT struct kevent chg[1]; struct timespec timeout = { 0, 0 }; +#elif SUP_ENABLE == EP_SUPPORT + struct epoll_event ee = { .events = EPOLLOUT, .data.fd = 0 }; #else sched_root_task_t *r = NULL; #endif if (!t || !TASK_ROOT(t)) return (void*) -1; -#ifdef KQ_DISABLE - r = TASK_ROOT(t); -#endif -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT #ifdef __NetBSD__ EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t)); #else @@ -405,7 +427,17 @@ sched_hook_write(void *task, void *arg __unused) LOGERR; return (void*) -1; } +#elif SUP_ENABLE == EP_SUPPORT + ee.data.fd = TASK_FD(t); + if (epoll_ctl(TASK_ROOT(t)->root_kq, EPOLL_CTL_ADD, TASK_FD(t), &ee) == -1) { + if (TASK_ROOT(t)->root_hooks.hook_exec.exception) + TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL); + else + LOGERR; + return (void*) -1; + } #else + r = TASK_ROOT(t); FD_SET(TASK_FD(t), &r->root_fds[1]); if (TASK_FD(t) >= r->root_kq) r->root_kq = TASK_FD(t) + 1; @@ -424,7 +456,7 @@ sched_hook_write(void *task, void *arg __unused) void * sched_hook_alarm(void *task, void *arg __unused) { -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT sched_task_t *t = task; struct kevent chg[1]; struct timespec timeout = { 0, 0 }; @@ -463,7 +495,7 @@ sched_hook_alarm(void *task, void *arg __unused) void * sched_hook_node(void *task, void *arg __unused) { -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT sched_task_t *t = task; struct kevent chg[1]; struct timespec timeout = { 0, 0 }; @@ -502,7 +534,7 @@ sched_hook_node(void *task, void *arg __unused) void * sched_hook_proc(void *task, void *arg __unused) { -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT sched_task_t *t = task; struct kevent chg[1]; struct timespec timeout = { 0, 0 }; @@ -539,7 +571,7 @@ sched_hook_proc(void *task, void *arg __unused) void * sched_hook_signal(void *task, void *arg __unused) { -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT sched_task_t *t = task; struct kevent chg[1]; struct timespec timeout = { 0, 0 }; @@ -595,7 +627,7 @@ sched_hook_signal(void *task, void *arg __unused) void * sched_hook_user(void *task, void *arg __unused) { -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT sched_task_t *t = task; struct kevent chg[1]; struct timespec timeout = { 0, 0 }; @@ -636,9 +668,12 @@ sched_hook_fetch(void *root, void *arg __unused) sched_root_task_t *r = root; sched_task_t *task, *tmp; struct timespec now, m, mtmp; -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT struct kevent evt[1], res[KQ_EVENTS]; struct timespec *timeout; +#elif SUP_ENABLE == EP_SUPPORT + struct epoll_event res[KQ_EVENTS]; + u_long timeout = 0; #else struct timeval *timeout, tv; fd_set rfd, wfd, xfd; @@ -734,30 +769,40 @@ sched_hook_fetch(void *root, void *arg __unused) sched_timespecclear(&r->root_wait); if (r->root_wait.tv_sec != -1 && r->root_wait.tv_nsec != -1) { -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT timeout = &r->root_wait; +#elif SUP_ENABLE == EP_SUPPORT + timeout = r->root_wait.tv_sec * 1000 + r->root_wait.tv_nsec / 1000000; #else sched_timespec2val(&r->root_wait, &tv); timeout = &tv; -#endif /* KQ_DISABLE */ +#endif /* KQ_SUPPORT */ } else if (sched_timespecisinf(&r->root_poll)) +#if SUP_ENABLE == EP_SUPPORT + timeout = -1; +#else timeout = NULL; +#endif else { -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT timeout = &r->root_poll; +#elif SUP_ENABLE == EP_SUPPORT + timeout = r->root_poll.tv_sec * 1000 + r->root_poll.tv_nsec / 1000000; #else sched_timespec2val(&r->root_poll, &tv); timeout = &tv; -#endif /* KQ_DISABLE */ +#endif /* KQ_SUPPORT */ } -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT if ((en = kevent(r->root_kq, NULL, 0, res, KQ_EVENTS, timeout)) == -1) { +#elif SUP_ENABLE == EP_SUPPORT + if ((en = epoll_wait(r->root_kq, res, KQ_EVENTS, timeout)) == -1) { #else rfd = xfd = r->root_fds[0]; wfd = r->root_fds[1]; if ((en = select(r->root_kq, &rfd, &wfd, &xfd, timeout)) == -1) { -#endif /* KQ_DISABLE */ +#endif /* KQ_SUPPORT */ if (r->root_hooks.hook_exec.exception) { if (r->root_hooks.hook_exec.exception(r, NULL)) return NULL; @@ -769,7 +814,7 @@ sched_hook_fetch(void *root, void *arg __unused) /* kevent dispatcher */ now.tv_sec = now.tv_nsec = 0; /* Go and catch the cat into pipes ... */ -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT for (i = 0; i < en; i++) { memcpy(evt, &res[i], sizeof evt); evt->flags = EV_DELETE; @@ -1257,7 +1302,7 @@ sched_hook_fetch(void *root, void *arg __unused) break; if (i > 2) r->root_kq = i + 1; -#endif /* KQ_DISABLE */ +#endif /* KQ_SUPPORT */ skip_event: /* timer update & put in ready queue */