--- libaitsched/src/aitsched.c 2011/08/05 15:52:00 1.1 +++ libaitsched/src/aitsched.c 2011/10/04 12:34:33 1.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsched.c,v 1.1 2011/08/05 15:52:00 misho Exp $ +* $Id: aitsched.c,v 1.2 2011/10/04 12:34:33 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -85,6 +85,32 @@ sched_SetErr(int eno, char *estr, ...) /* Init and prepare scheduler functions */ /* + * schedRegisterHooks() - Register IO handles and bind tasks to it + * @root = root task + * return: -1 error or 0 ok + */ +int +schedRegisterHooks(sched_root_task_t * __restrict root) +{ + if (!root || (root->root_data.iov_base && root->root_data.iov_len)) + return -1; + + if (root->root_hooks.hook_root.fini) + root->root_hooks.hook_root.fini(root, NULL); + memset(&root->root_hooks, 0, sizeof root->root_hooks); + + root->root_hooks.hook_add.read = sched_hook_read; + root->root_hooks.hook_add.write = sched_hook_write; + + root->root_hooks.hook_exec.cancel = sched_hook_cancel; + root->root_hooks.hook_exec.fetch = sched_hook_fetch; + + root->root_hooks.hook_root.init = sched_hook_init; + root->root_hooks.hook_root.fini = sched_hook_fini; + return 0; +} + +/* * schedInit() - Init scheduler * @data = optional data if !=NULL * @datlen = data len if data is set @@ -97,94 +123,74 @@ schedInit(void ** __restrict data, size_t datlen) int (*func)(sched_root_task_t *); root = malloc(sizeof(sched_root_task_t)); - if (!root) - LOGERR - else { + if (!root) { + LOGERR; + } else { memset(root, 0, sizeof(sched_root_task_t)); + TAILQ_INIT(&root->root_read); + TAILQ_INIT(&root->root_write); + TAILQ_INIT(&root->root_timer); + TAILQ_INIT(&root->root_event); + TAILQ_INIT(&root->root_eventlo); + TAILQ_INIT(&root->root_ready); + TAILQ_INIT(&root->root_unuse); if (data && *data) { if (datlen) { root->root_data.iov_base = *data; root->root_data.iov_len = datlen; } else { - func = *data; + func = (int(*)(sched_root_task_t*)) data; func(root); } } + + if (root->root_hooks.hook_root.init) + root->root_hooks.hook_root.init(root, NULL); } - if (root->root_hooks.hook_root.init) - root->root_hooks.hook_root.init(root, NULL); return root; } /* - * schedRegisterIO() - Register IO handles and bind tasks to it - * @root = root task - * return: -1 error or 0 ok - */ -int -schedRegisterIO(sched_root_task_t * __restrict root) -{ - if (!root || (root->root_data.iov_base && root->root_data.iov_len)) - return -1; - - if (root->root_hooks.hook_root.fini) - root->root_hooks.hook_root.fini(root, NULL); - memset(&root->root_hooks, 0, sizeof root->root_hooks); - - root->root_hooks.hook_add.read = sched_hook_read; - root->root_hooks.hook_add.write = sched_hook_write; - - root->root_hooks.hook_exec.cancel = sched_hook_cancel; - root->root_hooks.hook_exec.fetch = sched_hook_fetch; - - root->root_hooks.hook_root.init = sched_hook_init; - root->root_hooks.hook_root.fini = sched_hook_fini; - - if (root->root_hooks.hook_root.init) - root->root_hooks.hook_root.init(root, NULL); - return 0; -} - -/* * schedEnd() - End scheduler & free all resources * @root = root task * return: -1 error or 0 ok */ int -schedEnd(sched_root_task_t * __restrict root) +schedEnd(sched_root_task_t ** __restrict root) { sched_task_t *task; - if (!root) + if (!root || !*root) return -1; - TAILQ_FOREACH(task, &root->root_read, task_node) { + TAILQ_FOREACH(task, &(*root)->root_read, task_node) { schedCancel(task); } - TAILQ_FOREACH(task, &root->root_write, task_node) { + TAILQ_FOREACH(task, &(*root)->root_write, task_node) { schedCancel(task); } - TAILQ_FOREACH(task, &root->root_timer, task_node) { + TAILQ_FOREACH(task, &(*root)->root_timer, task_node) { schedCancel(task); } - TAILQ_FOREACH(task, &root->root_event, task_node) { + TAILQ_FOREACH(task, &(*root)->root_event, task_node) { schedCancel(task); } - TAILQ_FOREACH(task, &root->root_ready, task_node) { + TAILQ_FOREACH(task, &(*root)->root_ready, task_node) { schedCancel(task); } - while ((task = TAILQ_FIRST(&root->root_unuse))) { - TAILQ_REMOVE(&root->root_unuse, task, task_node); + while ((task = TAILQ_FIRST(&(*root)->root_unuse))) { + TAILQ_REMOVE(&(*root)->root_unuse, task, task_node); free(task); } - if (root->root_hooks.hook_root.fini) - root->root_hooks.hook_root.fini(root, NULL); + if ((*root)->root_hooks.hook_root.fini) + (*root)->root_hooks.hook_root.fini(*root, NULL); - free(root); + free(*root); + *root = NULL; return 0; } @@ -316,7 +322,7 @@ schedCancelby(sched_root_task_t * __restrict root, sch break; } } else if (criteria == CRITERIA_FD) { - if (TASK_FD(task) == (u_long) param) { + if (TASK_FD(task) == (int) param) { flg++; break; } @@ -356,10 +362,11 @@ schedCancelby(sched_root_task_t * __restrict root, sch /* * schedRun() - Scheduler *run loop* * @root = root task + * @killState = kill condition variable, if !=0 stop scheduler loop * return: -1 error or 0 ok */ int -schedRun(sched_root_task_t * __restrict root) +schedRun(sched_root_task_t * __restrict root, volatile intptr_t * __restrict killState) { sched_task_t *task; @@ -369,9 +376,14 @@ schedRun(sched_root_task_t * __restrict root) if (root->root_hooks.hook_exec.run) if (root->root_hooks.hook_exec.run(root, NULL)) return -1; - if (root->root_hooks.hook_exec.fetch) - while ((task = root->root_hooks.hook_exec.fetch(root, NULL))) - schedCall(task); + if (root->root_hooks.hook_exec.fetch) { + if (killState) + while (!*killState && (task = root->root_hooks.hook_exec.fetch(root, NULL))) + schedCall(task); + else + while ((task = root->root_hooks.hook_exec.fetch(root, NULL))) + schedCall(task); + } return 0; }