--- libaitsched/src/hooks.c 2012/05/30 08:52:45 1.7 +++ libaitsched/src/hooks.c 2012/05/31 22:31:48 1.8 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: hooks.c,v 1.7 2012/05/30 08:52:45 misho Exp $ +* $Id: hooks.c,v 1.8 2012/05/31 22:31:48 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -118,7 +118,6 @@ sched_hook_cancel(void *task, void *arg __unused) #else EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (void*) TASK_FD(t)); #endif - kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout); break; case taskWRITE: #ifdef __NetBSD__ @@ -126,7 +125,6 @@ sched_hook_cancel(void *task, void *arg __unused) #else EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (void*) TASK_FD(t)); #endif - kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout); break; case taskALARM: #ifdef __NetBSD__ @@ -136,12 +134,42 @@ sched_hook_cancel(void *task, void *arg __unused) EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_DELETE, 0, 0, (void*) TASK_DATA(t)); #endif - kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout); break; - default: + case taskNODE: +#ifdef __NetBSD__ + EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t)); +#else + EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (void*) TASK_FD(t)); +#endif break; + case taskPROC: +#ifdef __NetBSD__ + EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t)); +#else + EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (void*) TASK_VAL(t)); +#endif + break; + case taskSIGNAL: +#ifdef __NetBSD__ + EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t)); +#else + EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (void*) TASK_VAL(t)); +#endif + break; +#ifdef EVFILT_USER + case taskUSER: +#ifdef __NetBSD__ + EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t)); +#else + EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (void*) TASK_VAL(t)); +#endif +#endif + break; + default: + return NULL; } + kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout); return NULL; } @@ -163,9 +191,9 @@ sched_hook_read(void *task, void *arg __unused) return (void*) -1; #ifdef __NetBSD__ - EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD, 0, 0, (intptr_t) TASK_FD(t)); + EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t)); #else - EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD, 0, 0, (void*) TASK_FD(t)); + EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t)); #endif if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) { if (TASK_ROOT(t)->root_hooks.hook_exec.exception) @@ -196,9 +224,9 @@ sched_hook_write(void *task, void *arg __unused) return (void*) -1; #ifdef __NetBSD__ - EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD, 0, 0, (intptr_t) TASK_FD(t)); + EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t)); #else - EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD, 0, 0, (void*) TASK_FD(t)); + EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t)); #endif if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) { if (TASK_ROOT(t)->root_hooks.hook_exec.exception) @@ -249,6 +277,148 @@ sched_hook_alarm(void *task, void *arg __unused) } /* + * sched_hook_node() - Default NODE hook + * + * @task = current task + * @arg = unused + * return: <0 errors and 0 ok + */ +void * +sched_hook_node(void *task, void *arg __unused) +{ + sched_task_t *t = task; + struct kevent chg[1]; + struct timespec timeout = { 0, 0 }; + + if (!t || !TASK_ROOT(t)) + return (void*) -1; + +#ifdef __NetBSD__ + EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_ADD | EV_CLEAR, + NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | + NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, (intptr_t) TASK_FD(t)); +#else + EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_ADD | EV_CLEAR, + NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | + NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, (void*) TASK_FD(t)); +#endif + if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -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; + } + + return NULL; +} + +/* + * sched_hook_proc() - Default PROC hook + * + * @task = current task + * @arg = unused + * return: <0 errors and 0 ok + */ +void * +sched_hook_proc(void *task, void *arg __unused) +{ + sched_task_t *t = task; + struct kevent chg[1]; + struct timespec timeout = { 0, 0 }; + + if (!t || !TASK_ROOT(t)) + return (void*) -1; + +#ifdef __NetBSD__ + EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_ADD | EV_CLEAR, + NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, (intptr_t) TASK_VAL(t)); +#else + EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_ADD | EV_CLEAR, + NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, (void*) TASK_VAL(t)); +#endif + if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -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; + } + + return NULL; +} + +/* + * sched_hook_signal() - Default SIGNAL hook + * + * @task = current task + * @arg = unused + * return: <0 errors and 0 ok + */ +void * +sched_hook_signal(void *task, void *arg __unused) +{ + sched_task_t *t = task; + struct kevent chg[1]; + struct timespec timeout = { 0, 0 }; + + if (!t || !TASK_ROOT(t)) + return (void*) -1; + +#ifdef __NetBSD__ + EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD, 0, 0, (intptr_t) TASK_VAL(t)); +#else + EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD, 0, 0, (void*) TASK_VAL(t)); +#endif + if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -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; + } + + return NULL; +} + +/* + * sched_hook_user() - Default USER hook + * + * @task = current task + * @arg = unused + * return: <0 errors and 0 ok + */ +#ifdef EVFILT_USER +void * +sched_hook_user(void *task, void *arg __unused) +{ + sched_task_t *t = task; + struct kevent chg[1]; + struct timespec timeout = { 0, 0 }; + + if (!t || !TASK_ROOT(t)) + return (void*) -1; + +#ifdef __NetBSD__ + EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_ADD | EV_CLEAR, TASK_DATLEN(t), + 0, (intptr_t) TASK_VAL(t)); +#else + EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_ADD | EV_CLEAR, TASK_DATLEN(t), + 0, (void*) TASK_VAL(t)); +#endif + if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -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; + } + + return NULL; +} +#endif + +/* * sched_hook_fetch() - Default FETCH hook * * @root = root task @@ -477,6 +647,112 @@ sched_hook_fetch(void *root, void *arg __unused) break; } break; + case EVFILT_VNODE: + TAILQ_FOREACH_SAFE(task, &r->root_node, task_node, tmp) { + if (TASK_FD(task) != ((intptr_t) res[i].udata)) + continue; + else { + TASK_DATA(task) = (void*) (uintptr_t) res[i].data; + TASK_DATLEN(task) = res[i].fflags; + } + /* remove node handle */ +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskNODE]); +#endif + TAILQ_REMOVE(&r->root_node, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskNODE]); +#endif + task->task_type = taskREADY; +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskREADY]); +#endif + TAILQ_INSERT_TAIL(&r->root_ready, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskREADY]); +#endif + break; + } + break; + case EVFILT_PROC: + TAILQ_FOREACH_SAFE(task, &r->root_proc, task_node, tmp) { + if (TASK_VAL(task) != ((uintptr_t) res[i].udata)) + continue; + else { + TASK_DATA(task) = (void*) (uintptr_t) res[i].data; + TASK_DATLEN(task) = res[i].fflags; + } + /* remove proc handle */ +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskPROC]); +#endif + TAILQ_REMOVE(&r->root_proc, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskPROC]); +#endif + task->task_type = taskREADY; +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskREADY]); +#endif + TAILQ_INSERT_TAIL(&r->root_ready, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskREADY]); +#endif + break; + } + break; + case EVFILT_SIGNAL: + TAILQ_FOREACH_SAFE(task, &r->root_signal, task_node, tmp) { + if (TASK_VAL(task) != ((uintptr_t) res[i].udata)) + continue; + /* remove signal handle */ +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskSIGNAL]); +#endif + TAILQ_REMOVE(&r->root_signal, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskSIGNAL]); +#endif + task->task_type = taskREADY; +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskREADY]); +#endif + TAILQ_INSERT_TAIL(&r->root_ready, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskREADY]); +#endif + break; + } + break; +#ifdef EVFILT_USER + case EVFILT_USER: + TAILQ_FOREACH_SAFE(task, &r->root_user, task_node, tmp) { + if (TASK_VAL(task) != ((uintptr_t) res[i].udata)) + continue; + else { + TASK_DATA(task) = (void*) res[i].data; + TASK_DATLEN(task) = res[i].fflags; + } + /* remove user handle */ +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskUSER]); +#endif + TAILQ_REMOVE(&r->root_user, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskUSER]); +#endif + task->task_type = taskREADY; +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskREADY]); +#endif + TAILQ_INSERT_TAIL(&r->root_ready, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskREADY]); +#endif + break; + } + break; +#endif } if (kevent(r->root_kq, evt, 1, NULL, 0, &now) == -1) { if (r->root_hooks.hook_exec.exception) {