--- libaitsched/src/hooks.c 2011/10/04 12:34:33 1.2 +++ libaitsched/src/hooks.c 2011/10/04 23:03:32 1.2.2.4 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: hooks.c,v 1.2 2011/10/04 12:34:33 misho Exp $ +* $Id: hooks.c,v 1.2.2.4 2011/10/04 23:03:32 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -182,7 +182,10 @@ sched_hook_read(void *task, void *arg __unused) EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD, 0, 0, (void*) TASK_FD(t)); #endif if (kevent(t->task_root->root_kq, chg, 1, NULL, 0, &timeout) == -1) { - LOGERR; + if (t->task_root->root_hooks.hook_exec.exception) + t->task_root->root_hooks.hook_exec.exception(t->task_root, NULL); + else + LOGERR; return (void*) -1; } @@ -219,7 +222,10 @@ sched_hook_write(void *task, void *arg __unused) EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD, 0, 0, (void*) TASK_FD(t)); #endif if (kevent(t->task_root->root_kq, chg, 1, NULL, 0, &timeout) == -1) { - LOGERR; + if (t->task_root->root_hooks.hook_exec.exception) + t->task_root->root_hooks.hook_exec.exception(t->task_root, NULL); + else + LOGERR; return (void*) -1; } @@ -308,7 +314,11 @@ retry: } else /* wait INFTIM */ timeout = NULL; if ((en = kevent(r->root_kq, NULL, 0, res, KQ_EVENTS, timeout)) == -1) { - LOGERR; + if (r->root_hooks.hook_exec.exception) { + if (r->root_hooks.hook_exec.exception(r, NULL)) + return NULL; + } else + LOGERR; #ifdef NDEBUG /* kevent no exit by error, if non-debug version */ goto retry; @@ -327,45 +337,60 @@ retry: switch (res[i].filter) { case EVFILT_READ: TAILQ_FOREACH(task, &r->root_read, task_node) { - if (TASK_FD(task) != ((int) res[i].udata)) + if (TASK_FD(task) != ((intptr_t) res[i].udata)) continue; /* remove read handle */ io = ROOT_DATA(task->task_root); FD_CLR(TASK_FD(task), &io->rfd); TAILQ_REMOVE(&r->root_read, task, task_node); -/* if (res[i].flags & EV_EOF) { - task->task_type = taskUNUSE; - TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node); - } else { */ + if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) { + if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) { + task->task_type = taskUNUSE; + TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node); + } else { + task->task_type = taskREADY; + TAILQ_INSERT_TAIL(&r->root_ready, task, task_node); + } + } else { task->task_type = taskREADY; TAILQ_INSERT_TAIL(&r->root_ready, task, task_node); -/* } */ + } break; } break; case EVFILT_WRITE: TAILQ_FOREACH(task, &r->root_write, task_node) { - if (TASK_FD(task) != ((int) res[i].udata)) + if (TASK_FD(task) != ((intptr_t) res[i].udata)) continue; /* remove write handle */ io = ROOT_DATA(task->task_root); FD_CLR(TASK_FD(task), &io->wfd); TAILQ_REMOVE(&r->root_write, task, task_node); -/* if (res[i].flags & EV_EOF) { - task->task_type = taskUNUSE; - TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node); - } else { */ + if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) { + if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) { + task->task_type = taskUNUSE; + TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node); + } else { + task->task_type = taskREADY; + TAILQ_INSERT_TAIL(&r->root_ready, task, task_node); + } + } else { task->task_type = taskREADY; TAILQ_INSERT_TAIL(&r->root_ready, task, task_node); -/* } */ + } break; } break; } - if (kevent(r->root_kq, evt, 1, NULL, 0, &nw) == -1) - LOGERR; + if (kevent(r->root_kq, evt, 1, NULL, 0, &nw) == -1) { + if (r->root_hooks.hook_exec.exception) { + if (r->root_hooks.hook_exec.exception(r, NULL)) + return NULL; + } else + LOGERR; + } } /* timer update & put in ready queue */ @@ -401,4 +426,34 @@ retry: task->task_type = taskUNUSE; TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node); return task; +} + +/* + * sched_hook_exception() - Default EXCEPTION hook + * @root = root task + * @arg = custom handling: if arg == EV_EOF or other value; default: arg == NULL log errno + * return: <0 errors and 0 ok + */ +void * +sched_hook_exception(void *root, void *arg) +{ + sched_root_task_t *r = root; + + if (!r || !ROOT_DATA(r) || !ROOT_DATLEN(r)) + return NULL; + + /* custom exception handling ... */ + if (arg) { + if (arg == (void*) EV_EOF) + return NULL; + return (void*) -1; /* raise scheduler error!!! */ + } + + /* if error hook exists */ + if (r->root_hooks.hook_root.error) + return (r->root_hooks.hook_root.error(root, (void*) ((intptr_t) errno))); + + /* default case! */ + LOGERR; + return NULL; }