--- libaitsched/src/aitsched.c 2012/01/08 03:28:26 1.4.2.3 +++ libaitsched/src/aitsched.c 2012/04/24 13:29:28 1.6.2.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsched.c,v 1.4.2.3 2012/01/08 03:28:26 misho Exp $ +* $Id: aitsched.c,v 1.6.2.1 2012/04/24 13:29:28 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 +Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -86,6 +86,7 @@ sched_SetErr(int eno, char *estr, ...) /* * schedRegisterHooks() - Register IO handles and bind tasks to it + * * @root = root task * return: -1 error or 0 ok */ @@ -113,6 +114,7 @@ schedRegisterHooks(sched_root_task_t * __restrict root /* * schedInit() - Init scheduler + * * @data = optional data if !=NULL * @datlen = data len if data is set * return: allocated root task if ok or NULL error @@ -132,6 +134,9 @@ schedInit(void ** __restrict data, size_t datlen) } else { memset(root, 0, sizeof(sched_root_task_t)); + /* INFINIT polling period by default */ + sched_timespecinf(&root->root_poll); + #ifdef HAVE_LIBPTHREAD for (i = 0; i < taskMAX; i++) if (pthread_mutex_init(&root->root_mtx[i], NULL)) { @@ -179,6 +184,7 @@ schedInit(void ** __restrict data, size_t datlen) /* * schedEnd() - End scheduler & free all resources + * * @root = root task * return: -1 error or 0 ok */ @@ -238,6 +244,7 @@ schedEnd(sched_root_task_t ** __restrict root) /* * schedCall() - Call task execution function + * * @task = current task * return: !=NULL error or =NULL ok */ @@ -261,6 +268,7 @@ schedCall(sched_task_t * __restrict task) /* * schedFetch() - Fetch ready task + * * @root = root task * return: =NULL error or !=NULL ready task */ @@ -282,6 +290,7 @@ schedFetch(sched_root_task_t * __restrict root) /* * schedCancel() - Cancel task from scheduler + * * @task = task * return: -1 error or 0 ok */ @@ -336,6 +345,7 @@ schedCancel(sched_task_t * __restrict task) /* * schedCancelby() - Cancel task from scheduler by criteria + * * @root = root task * @type = cancel from queue type, if =taskMAX cancel same task from all queues * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV] @@ -416,7 +426,7 @@ schedCancelby(sched_root_task_t * __restrict root, sch break; } } else if (criteria == CRITERIA_TV) { - if (!timercmp(&TASK_TV(task), (struct timeval*) param, -)) { + if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -)) { flg++; break; } @@ -455,6 +465,7 @@ 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 @@ -471,15 +482,66 @@ schedRun(sched_root_task_t * __restrict root, volatile if (root->root_hooks.hook_exec.run(root, NULL)) return -1; if (root->root_hooks.hook_exec.fetch) { - if (killState) - while (!*killState) { + if (killState) { + if (root->root_hooks.hook_exec.condition) + while (root->root_hooks.hook_exec.condition(root, (void*) killState)) { + if ((task = root->root_hooks.hook_exec.fetch(root, NULL))) + schedCall(task); + } + else + while (!*killState) { + if ((task = root->root_hooks.hook_exec.fetch(root, NULL))) + schedCall(task); + } + } else + while (42) if ((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; +} + +/* + * schedPolling() - Polling timeout period if no timer task is present + * + * @root = root task + * @ts = timeout polling period, if ==NULL INFINIT timeout + * @tsold = old timeout polling if !=NULL + * return: -1 error or 0 ok + */ +inline int +schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts, + struct timespec * __restrict tsold) +{ + if (!root) + return -1; + + if (tsold) + *tsold = root->root_poll; + + if (!ts) + sched_timespecinf(&root->root_poll); + else + root->root_poll = *ts; + + return 0; +} + +/* + * schedTermCondition() - Activate hook for scheduler condition kill + * + * @root = root task + * @condValue = condition value, kill schedRun() if condValue == killState + * return: -1 error ok 0 ok + */ +inline int +schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue) +{ + if (!root) + return -1; + + root->root_cond = condValue; + root->root_hooks.hook_exec.condition = sched_hook_condition; return 0; }