--- libaitsched/src/tasks.c 2012/01/24 14:04:58 1.4.2.3 +++ libaitsched/src/tasks.c 2012/05/30 08:52:45 1.8 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tasks.c,v 1.4.2.3 2012/01/24 14:04:58 misho Exp $ +* $Id: tasks.c,v 1.8 2012/05/30 08:52:45 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 @@ -51,9 +51,9 @@ SUCH DAMAGE. inline sched_task_t * _sched_useTask(sched_root_task_t * __restrict root) { - sched_task_t *task; + sched_task_t *task, *tmp; - TAILQ_FOREACH(task, &root->root_unuse, task_node) { + TAILQ_FOREACH_SAFE(task, &root->root_unuse, task_node, tmp) { if (!TASK_ISLOCKED(task)) { #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&root->root_mtx[taskUNUSE]); @@ -99,6 +99,7 @@ _sched_unuseTask(sched_task_t * __restrict task) /* * schedRead() - Add READ I/O task to scheduler queue + * * @root = root task * @func = task execution function * @arg = 1st func argument @@ -155,6 +156,7 @@ schedRead(sched_root_task_t * __restrict root, sched_t /* * schedWrite() - Add WRITE I/O task to scheduler queue + * * @root = root task * @func = task execution function * @arg = 1st func argument @@ -210,7 +212,65 @@ schedWrite(sched_root_task_t * __restrict root, sched_ } /* + * schedAlarm() - Add ALARM task to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @ts = timeout argument structure, minimum alarm timer resolution is 1msec! + * @opt_data = Optional data + * @opt_dlen = Optional data length + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedAlarm(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, struct timespec ts, + void *opt_data, size_t opt_dlen) +{ + sched_task_t *task; + void *ptr; + + if (!root || !func) + return NULL; + + /* get new task */ + if (!(task = _sched_useTask(root))) + return NULL; + + memset(task, 0, sizeof(sched_task_t)); + task->task_id = 0; + task->task_lock = 0; + task->task_func = func; + TASK_TYPE(task) = taskALARM; + TASK_ROOT(task) = root; + + TASK_ARG(task) = arg; + TASK_TS(task) = ts; + + TASK_DATA(task) = opt_data; + TASK_DATLEN(task) = opt_dlen; + + if (root->root_hooks.hook_add.alarm) + ptr = root->root_hooks.hook_add.alarm(task, NULL); + else + ptr = NULL; + + if (!ptr) { +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&root->root_mtx[taskALARM]); +#endif + TAILQ_INSERT_TAIL(&root->root_alarm, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&root->root_mtx[taskALARM]); +#endif + } else + task = _sched_unuseTask(task); + + return task; +} + +/* * schedTimer() - Add TIMER task to scheduler queue + * * @root = root task * @func = task execution function * @arg = 1st func argument @@ -250,12 +310,12 @@ schedTimer(sched_root_task_t * __restrict root, sched_ clock_gettime(CLOCK_MONOTONIC, &now); now.tv_sec += ts.tv_sec; now.tv_nsec += ts.tv_nsec; - if (now.tv_nsec >= 1000000000) { + if (now.tv_nsec >= 1000000000L) { now.tv_sec++; - now.tv_nsec -= 1000000000; + now.tv_nsec -= 1000000000L; } else if (now.tv_nsec < 0) { now.tv_sec--; - now.tv_nsec += 1000000000; + now.tv_nsec += 1000000000L; } TASK_TS(task) = now; @@ -272,7 +332,7 @@ schedTimer(sched_root_task_t * __restrict root, sched_ TAILQ_INSERT_TAIL(&root->root_timer, task, task_node); #else TAILQ_FOREACH(t, &root->root_timer, task_node) - if (timespeccmp(&TASK_TS(task), &TASK_TS(t), -) < 1) + if (sched_timespeccmp(&TASK_TS(task), &TASK_TS(t), -) < 1) break; if (!t) TAILQ_INSERT_TAIL(&root->root_timer, task, task_node); @@ -290,6 +350,7 @@ schedTimer(sched_root_task_t * __restrict root, sched_ /* * schedEvent() - Add EVENT task to scheduler queue + * * @root = root task * @func = task execution function * @arg = 1st func argument @@ -347,6 +408,7 @@ schedEvent(sched_root_task_t * __restrict root, sched_ /* * schedEventLo() - Add EVENT_Lo task to scheduler queue + * * @root = root task * @func = task execution function * @arg = 1st func argument @@ -403,6 +465,7 @@ schedEventLo(sched_root_task_t * __restrict root, sche /* * schedCallOnce() - Call once from scheduler + * * @root = root task * @func = task execution function * @arg = 1st func argument