--- libaitsched/src/tasks.c 2012/05/30 08:52:45 1.8 +++ libaitsched/src/tasks.c 2012/05/31 14:45:10 1.8.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tasks.c,v 1.8 2012/05/30 08:52:45 misho Exp $ +* $Id: tasks.c,v 1.8.2.2 2012/05/31 14:45:10 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -204,6 +204,234 @@ schedWrite(sched_root_task_t * __restrict root, sched_ TAILQ_INSERT_TAIL(&root->root_write, task, task_node); #ifdef HAVE_LIBPTHREAD pthread_mutex_unlock(&root->root_mtx[taskWRITE]); +#endif + } else + task = _sched_unuseTask(task); + + return task; +} + +/* + * schedNode() - Add NODE task to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @fd = fd handle + * @opt_data = Optional data + * @opt_dlen = Optional data length + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedNode(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, + 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) = taskNODE; + TASK_ROOT(task) = root; + + TASK_ARG(task) = arg; + TASK_FD(task) = fd; + + TASK_DATA(task) = opt_data; + TASK_DATLEN(task) = opt_dlen; + + if (root->root_hooks.hook_add.node) + ptr = root->root_hooks.hook_add.node(task, NULL); + else + ptr = NULL; + + if (!ptr) { +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&root->root_mtx[taskNODE]); +#endif + TAILQ_INSERT_TAIL(&root->root_node, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&root->root_mtx[taskNODE]); +#endif + } else + task = _sched_unuseTask(task); + + return task; +} + +/* + * schedProc() - Add PROC task to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @pid = PID + * @opt_data = Optional data + * @opt_dlen = Optional data length + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedProc(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long pid, + 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) = taskPROC; + TASK_ROOT(task) = root; + + TASK_ARG(task) = arg; + TASK_VAL(task) = pid; + + TASK_DATA(task) = opt_data; + TASK_DATLEN(task) = opt_dlen; + + if (root->root_hooks.hook_add.proc) + ptr = root->root_hooks.hook_add.proc(task, NULL); + else + ptr = NULL; + + if (!ptr) { +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&root->root_mtx[taskPROC]); +#endif + TAILQ_INSERT_TAIL(&root->root_proc, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&root->root_mtx[taskPROC]); +#endif + } else + task = _sched_unuseTask(task); + + return task; +} + +/* + * schedUser() - Add trigger USER task to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @id = Trigger ID + * @opt_data = Optional data + * @opt_dlen = Optional user's trigger flags + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedUser(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long id, + 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) = taskUSER; + TASK_ROOT(task) = root; + + TASK_ARG(task) = arg; + TASK_VAL(task) = id; + + TASK_DATA(task) = opt_data; + TASK_DATLEN(task) = opt_dlen; + + if (root->root_hooks.hook_add.user) + ptr = root->root_hooks.hook_add.user(task, NULL); + else + ptr = NULL; + + if (!ptr) { +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&root->root_mtx[taskUSER]); +#endif + TAILQ_INSERT_TAIL(&root->root_user, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&root->root_mtx[taskUSER]); +#endif + } else + task = _sched_unuseTask(task); + + return task; +} + +/* + * schedSignal() - Add SIGNAL task to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @sig = Signal + * @opt_data = Optional data + * @opt_dlen = Optional data length + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedSignal(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long sig, + 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) = taskSIGNAL; + TASK_ROOT(task) = root; + + TASK_ARG(task) = arg; + TASK_VAL(task) = sig; + + TASK_DATA(task) = opt_data; + TASK_DATLEN(task) = opt_dlen; + + if (root->root_hooks.hook_add.signal) + ptr = root->root_hooks.hook_add.signal(task, NULL); + else + ptr = NULL; + + if (!ptr) { +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&root->root_mtx[taskSIGNAL]); +#endif + TAILQ_INSERT_TAIL(&root->root_signal, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&root->root_mtx[taskSIGNAL]); #endif } else task = _sched_unuseTask(task);