--- libaitsched/src/hooks.c 2014/01/28 12:14:20 1.24.4.6 +++ libaitsched/src/hooks.c 2014/05/21 22:22:57 1.27.2.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: hooks.c,v 1.24.4.6 2014/01/28 12:14:20 misho Exp $ +* $Id: hooks.c,v 1.27.2.4 2014/05/21 22:22:57 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 +Copyright 2004 - 2014 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -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,7 +123,7 @@ 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 @@ -134,13 +140,13 @@ 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 @@ -158,7 +164,7 @@ 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 @@ -176,7 +182,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 +193,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 +202,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 +211,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 +223,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 +240,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 +262,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,10 +273,11 @@ 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) +#if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME) && defined(HAVE_TIMER_DELETE) case taskRTC: timer_delete((timer_t) TASK_FLAG(t)); schedCancel((sched_task_t*) TASK_RET(t)); @@ -280,7 +287,7 @@ 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); #endif return NULL; @@ -333,7 +340,7 @@ 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 }; #else @@ -342,11 +349,11 @@ sched_hook_read(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 -#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 @@ -379,7 +386,7 @@ 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 }; #else @@ -388,11 +395,11 @@ sched_hook_write(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 -#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 @@ -424,7 +431,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 +470,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 +509,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 +546,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 +602,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,11 +643,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, m, mtmp; + struct timespec *timeout; #else struct timeval *timeout, tv; + fd_set rfd, wfd, xfd; #endif register int i, flg; int en; @@ -733,29 +741,30 @@ 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; #else sched_timespec2val(&r->root_wait, &tv); timeout = &tv; -#endif /* KQ_DISABLE */ +#endif /* KQ_SUPPORT */ } else if (sched_timespecisinf(&r->root_poll)) timeout = NULL; else { -#ifndef KQ_DISABLE +#if SUP_ENABLE == KQ_SUPPORT timeout = &r->root_poll; #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) { #else - if ((en = select(r->root_kq, &r->root_fds[0], &r->root_fds[1], - &r->root_fds[0], timeout)) == -1) { -#endif /* KQ_DISABLE */ + rfd = xfd = r->root_fds[0]; + wfd = r->root_fds[1]; + if ((en = select(r->root_kq, &rfd, &wfd, &xfd, timeout)) == -1) { +#endif /* KQ_SUPPORT */ if (r->root_hooks.hook_exec.exception) { if (r->root_hooks.hook_exec.exception(r, NULL)) return NULL; @@ -767,7 +776,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; @@ -1140,15 +1149,15 @@ sched_hook_fetch(void *root, void *arg __unused) } #else /* end of kevent dispatcher */ for (i = 0; i < r->root_kq; i++) { - if (FD_ISSET(i, &r->root_fds[0])) { + if (FD_ISSET(i, &rfd) || FD_ISSET(i, &xfd)) { flg = 0; TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) { if (TASK_FD(task) != i) continue; else { flg++; - TASK_RET(task) ^= TASK_RET(task); - TASK_FLAG(task) ^= TASK_FLAG(task); + TASK_FLAG(task) = ioctl(TASK_FD(task), + FIONREAD, &TASK_RET(task)); } /* remove read handle */ #ifdef HAVE_LIBPTHREAD @@ -1194,15 +1203,15 @@ sched_hook_fetch(void *root, void *arg __unused) FD_CLR(i, &r->root_fds[0]); } - if (FD_ISSET(i, &r->root_fds[1])) { + if (FD_ISSET(i, &wfd)) { flg = 0; TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) { if (TASK_FD(task) != i) continue; else { flg++; - TASK_RET(task) ^= TASK_RET(task); - TASK_FLAG(task) ^= TASK_FLAG(task); + TASK_FLAG(task) = ioctl(TASK_FD(task), + FIONWRITE, &TASK_RET(task)); } /* remove write handle */ #ifdef HAVE_LIBPTHREAD @@ -1255,7 +1264,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 */ @@ -1385,7 +1394,7 @@ sched_hook_condition(void *root, void *arg) * @arg = unused * return: <0 errors and 0 ok */ -#if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME) +#if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME) && defined(HAVE_TIMER_DELETE) void * sched_hook_rtc(void *task, void *arg __unused) {