/************************************************************************* * (C) 2011 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: aitsched.c,v 1.2.2.2 2011/10/04 14:04:35 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following 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 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed by Michael Pounov ELWIX - Embedded LightWeight unIX and its contributors. 4. Neither the name of AITNET nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "global.h" #include "hooks.h" #pragma GCC visibility push(hidden) int sched_Errno; char sched_Error[STRSIZ]; #pragma GCC visibility pop // sched_GetErrno() Get error code of last operation inline int sched_GetErrno() { return sched_Errno; } // sched_GetError() Get error text of last operation inline const char * sched_GetError() { return sched_Error; } // sched_SetErr() Set error to variables for internal use!!! inline void sched_SetErr(int eno, char *estr, ...) { va_list lst; sched_Errno = eno; memset(sched_Error, 0, sizeof sched_Error); va_start(lst, estr); vsnprintf(sched_Error, sizeof sched_Error, estr, lst); va_end(lst); } /* 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_exec.exception = sched_hook_exception; 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 * return: allocated root task if ok or NULL error */ sched_root_task_t * schedInit(void ** __restrict data, size_t datlen) { sched_root_task_t *root = NULL; int (*func)(sched_root_task_t *); root = malloc(sizeof(sched_root_task_t)); 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 = (int(*)(sched_root_task_t*)) data; func(root); } } if (root->root_hooks.hook_root.init) root->root_hooks.hook_root.init(root, NULL); } return root; } /* * schedEnd() - End scheduler & free all resources * @root = root task * return: -1 error or 0 ok */ int schedEnd(sched_root_task_t ** __restrict root) { sched_task_t *task; if (!root || !*root) return -1; TAILQ_FOREACH(task, &(*root)->root_read, task_node) { schedCancel(task); } TAILQ_FOREACH(task, &(*root)->root_write, task_node) { schedCancel(task); } TAILQ_FOREACH(task, &(*root)->root_timer, task_node) { schedCancel(task); } TAILQ_FOREACH(task, &(*root)->root_event, task_node) { schedCancel(task); } 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); free(task); } if ((*root)->root_hooks.hook_root.fini) (*root)->root_hooks.hook_root.fini(*root, NULL); free(*root); *root = NULL; return 0; } /* * schedCall() - Call task execution function * @task = current task * return: !=NULL error or =NULL ok */ inline void * schedCall(sched_task_t * __restrict task) { if (!task) return (void*) -1; task->task_id++; return task->task_func(task); } /* * schedFetch() - Fetch ready task * @root = root task * return: =NULL error or !=NULL ready task */ inline void * schedFetch(sched_root_task_t * __restrict root) { void *ptr; if (!root) return NULL; if (root->root_hooks.hook_exec.fetch) ptr = root->root_hooks.hook_exec.fetch(root, NULL); else ptr = NULL; return ptr; } /* * schedCancel() - Cancel task from scheduler * @task = task * return: -1 error or 0 ok */ int schedCancel(sched_task_t * __restrict task) { sched_queue_t *queue; if (!task || !task->task_root) return -1; if (task->task_root->root_hooks.hook_exec.cancel) if (task->task_root->root_hooks.hook_exec.cancel(task, NULL)) return -1; switch (task->task_type) { case taskREAD: queue = &task->task_root->root_read; break; case taskWRITE: queue = &task->task_root->root_write; break; case taskTIMER: queue = &task->task_root->root_timer; break; case taskEVENT: queue = &task->task_root->root_event; break; case taskREADY: queue = &task->task_root->root_ready; break; default: queue = NULL; } if (queue) TAILQ_REMOVE(queue, task, task_node); if (task->task_type != taskUNUSE) { task->task_type = taskUNUSE; TAILQ_INSERT_TAIL(&task->task_root->root_unuse, task, task_node); } return 0; } /* * schedCancelby() - Cancel task from scheduler by criteria * @root = root task * @queue = cancel from queue, if =NULL cancel same task from all queues * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV] * @param = search parameter * @hook = custom cleanup hook function, may be NULL * return: -1 error or 0 ok */ int schedCancelby(sched_root_task_t * __restrict root, sched_queue_t * __restrict queue, u_char criteria, void *param, sched_hook_func_t hook) { sched_task_t *task; int flg = 0; if (!root) return -1; if (!queue) { if (schedCancelby(root, &root->root_read, criteria, param, hook)) return -2; if (schedCancelby(root, &root->root_write, criteria, param, hook)) return -2; if (schedCancelby(root, &root->root_timer, criteria, param, hook)) return -2; if (schedCancelby(root, &root->root_event, criteria, param, hook)) return -2; if (schedCancelby(root, &root->root_ready, criteria, param, hook)) return -2; if (schedCancelby(root, &root->root_read, criteria, param, hook)) return -2; return 0; } TAILQ_FOREACH(task, queue, task_node) if (criteria == CRITERIA_CALL) { if (task->task_func == (sched_task_func_t) param) { flg++; break; } } else if (criteria == CRITERIA_ARG) { if (task->task_arg == param) { flg++; break; } } else if (criteria == CRITERIA_FD) { if (TASK_FD(task) == (int) param) { flg++; break; } } else if (criteria == CRITERIA_VAL) { if (TASK_VAL(task) == (u_long) param) { flg++; break; } } else if (criteria == CRITERIA_TV) { if (&TASK_TV(task) == (struct timeval*) param) { flg++; break; } } else { sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria); return -1; } if (!flg || !task) /* task not found */ return 0; if (task->task_root->root_hooks.hook_exec.cancel) if (task->task_root->root_hooks.hook_exec.cancel(task, NULL)) return -1; if (hook) if (hook(task, NULL)) return -3; TAILQ_REMOVE(queue, task, task_node); if (task->task_type != taskUNUSE) { task->task_type = taskUNUSE; TAILQ_INSERT_TAIL(&task->task_root->root_unuse, task, task_node); } return 0; } /* * 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, volatile intptr_t * __restrict killState) { sched_task_t *task; if (!root) return -1; 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) { if (killState) while (!*killState) 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; }