/************************************************************************* * (C) 2011 AITNET ltd - Sofia/Bulgaria - * by Michael Pounov * * $Author: misho $ * $Id: tasks.c,v 1.10 2012/07/24 14:06:11 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, 2012 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" #pragma GCC visibility push(hidden) inline sched_task_t * _sched_useTask(sched_root_task_t * __restrict root) { sched_task_t *task, *tmp; TAILQ_FOREACH_SAFE(task, &root->root_unuse, task_node, tmp) { if (!TASK_ISLOCKED(task)) { #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&root->root_mtx[taskUNUSE]); #endif TAILQ_REMOVE(&root->root_unuse, task, task_node); #ifdef HAVE_LIBPTHREAD pthread_mutex_unlock(&root->root_mtx[taskUNUSE]); #endif break; } } if (!task) { task = malloc(sizeof(sched_task_t)); if (!task) { LOGERR; return NULL; } } memset(task, 0, sizeof(sched_task_t)); task->task_id = (uintptr_t) task; return task; } inline sched_task_t * _sched_unuseTask(sched_task_t * __restrict task) { TASK_UNLOCK(task); TASK_TYPE(task) = taskUNUSE; #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[taskUNUSE]); #endif TAILQ_INSERT_TAIL(&TASK_ROOT(task)->root_unuse, TASK_ID(task), task_node); #ifdef HAVE_LIBPTHREAD pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[taskUNUSE]); #endif task = NULL; return task; } #pragma GCC visibility pop /* * schedRead() - Add READ I/O 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 * schedRead(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; task->task_func = func; TASK_TYPE(task) = taskREAD; 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.read) ptr = root->root_hooks.hook_add.read(task, NULL); else ptr = NULL; if (!ptr) { #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&root->root_mtx[taskREAD]); #endif TAILQ_INSERT_TAIL(&root->root_read, TASK_ID(task), task_node); #ifdef HAVE_LIBPTHREAD pthread_mutex_unlock(&root->root_mtx[taskREAD]); #endif } else task = _sched_unuseTask(task); return task; } /* * schedWrite() - Add WRITE I/O 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 * schedWrite(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; task->task_func = func; TASK_TYPE(task) = taskWRITE; 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.write) ptr = root->root_hooks.hook_add.write(task, NULL); else ptr = NULL; if (!ptr) { #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&root->root_mtx[taskWRITE]); #endif TAILQ_INSERT_TAIL(&root->root_write, TASK_ID(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; 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_ID(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; 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_ID(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) { #ifndef EVFILT_USER sched_SetErr(ENOTSUP, "Not supported kevent() filter"); return NULL; #else sched_task_t *task; void *ptr; if (!root || !func) return NULL; /* get new task */ if (!(task = _sched_useTask(root))) return NULL; 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_ID(task), task_node); #ifdef HAVE_LIBPTHREAD pthread_mutex_unlock(&root->root_mtx[taskUSER]); #endif } else task = _sched_unuseTask(task); return task; #endif } /* * 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; 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_ID(task), task_node); #ifdef HAVE_LIBPTHREAD pthread_mutex_unlock(&root->root_mtx[taskSIGNAL]); #endif } else task = _sched_unuseTask(task); return task; } /* * 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; 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_ID(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 * @ts = timeout argument structure * @opt_data = Optional data * @opt_dlen = Optional data length * return: NULL error or !=NULL new queued task */ sched_task_t * schedTimer(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, *tmp, *t = NULL; void *ptr; struct timespec now; if (!root || !func) return NULL; /* get new task */ if (!(task = _sched_useTask(root))) return NULL; task->task_func = func; TASK_TYPE(task) = taskTIMER; TASK_ROOT(task) = root; TASK_ARG(task) = arg; TASK_DATA(task) = opt_data; TASK_DATLEN(task) = opt_dlen; /* calculate timeval structure */ clock_gettime(CLOCK_MONOTONIC, &now); now.tv_sec += ts.tv_sec; now.tv_nsec += ts.tv_nsec; if (now.tv_nsec >= 1000000000L) { now.tv_sec++; now.tv_nsec -= 1000000000L; } else if (now.tv_nsec < 0) { now.tv_sec--; now.tv_nsec += 1000000000L; } TASK_TS(task) = now; if (root->root_hooks.hook_add.timer) ptr = root->root_hooks.hook_add.timer(task, NULL); else ptr = NULL; if (!ptr) { #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&root->root_mtx[taskTIMER]); #endif #ifdef TIMER_WITHOUT_SORT TAILQ_INSERT_TAIL(&root->root_timer, TASK_ID(task), task_node); #else TAILQ_FOREACH_SAFE(t, &root->root_timer, task_node, tmp) if (sched_timespeccmp(&TASK_TS(task), &TASK_TS(t), -) < 1) break; if (!t) TAILQ_INSERT_TAIL(&root->root_timer, TASK_ID(task), task_node); else TAILQ_INSERT_BEFORE(t, TASK_ID(task), task_node); #endif #ifdef HAVE_LIBPTHREAD pthread_mutex_unlock(&root->root_mtx[taskTIMER]); #endif } else task = _sched_unuseTask(task); return task; } /* * schedEvent() - Add EVENT task to scheduler queue * * @root = root task * @func = task execution function * @arg = 1st func argument * @val = additional func argument * @opt_data = Optional data * @opt_dlen = Optional data length * return: NULL error or !=NULL new queued task */ sched_task_t * schedEvent(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long val, 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; task->task_func = func; TASK_TYPE(task) = taskEVENT; TASK_ROOT(task) = root; TASK_ARG(task) = arg; TASK_VAL(task) = val; TASK_DATA(task) = opt_data; TASK_DATLEN(task) = opt_dlen; if (root->root_hooks.hook_add.event) ptr = root->root_hooks.hook_add.event(task, NULL); else ptr = NULL; if (!ptr) { #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&root->root_mtx[taskEVENT]); #endif TAILQ_INSERT_TAIL(&root->root_event, TASK_ID(task), task_node); #ifdef HAVE_LIBPTHREAD pthread_mutex_unlock(&root->root_mtx[taskEVENT]); #endif } else task = _sched_unuseTask(task); return task; } /* * schedEventLo() - Add EVENT_Lo task to scheduler queue * * @root = root task * @func = task execution function * @arg = 1st func argument * @val = additional func argument * @opt_data = Optional data * @opt_dlen = Optional data length * return: NULL error or !=NULL new queued task */ sched_task_t * schedEventLo(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long val, 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; task->task_func = func; TASK_TYPE(task) = taskEVENT; TASK_ROOT(task) = root; TASK_ARG(task) = arg; TASK_VAL(task) = val; TASK_DATA(task) = opt_data; TASK_DATLEN(task) = opt_dlen; if (root->root_hooks.hook_add.eventlo) ptr = root->root_hooks.hook_add.eventlo(task, NULL); else ptr = NULL; if (!ptr) { #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&root->root_mtx[taskEVENTLO]); #endif TAILQ_INSERT_TAIL(&root->root_eventlo, TASK_ID(task), task_node); #ifdef HAVE_LIBPTHREAD pthread_mutex_unlock(&root->root_mtx[taskEVENTLO]); #endif } else task = _sched_unuseTask(task); return task; } /* * schedSuspend() - Add Suspended 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 data length * return: NULL error or !=NULL new queued task */ sched_task_t * schedSuspend(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; task->task_func = func; TASK_TYPE(task) = taskSUSPEND; 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.suspend) ptr = root->root_hooks.hook_add.suspend(task, NULL); else ptr = NULL; if (!ptr) { #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&root->root_mtx[taskSUSPEND]); #endif TAILQ_INSERT_TAIL(&root->root_suspend, TASK_ID(task), task_node); #ifdef HAVE_LIBPTHREAD pthread_mutex_unlock(&root->root_mtx[taskSUSPEND]); #endif } else task = _sched_unuseTask(task); return task; } /* * schedCallOnce() - Call once from scheduler * * @root = root task * @func = task execution function * @arg = 1st func argument * @val = additional func argument * @opt_data = Optional data * @opt_dlen = Optional data length * return: return value from called func */ sched_task_t * schedCallOnce(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long val, void *opt_data, size_t opt_dlen) { sched_task_t *task; void *ret; if (!root || !func) return NULL; /* get new task */ if (!(task = _sched_useTask(root))) return NULL; task->task_func = func; TASK_TYPE(task) = taskEVENT; TASK_ROOT(task) = root; TASK_ARG(task) = arg; TASK_VAL(task) = val; TASK_DATA(task) = opt_data; TASK_DATLEN(task) = opt_dlen; ret = schedCall(task); _sched_unuseTask(task); return ret; }