--- libaitsched/src/tasks.c 2013/05/30 09:13:52 1.16 +++ libaitsched/src/tasks.c 2013/08/26 13:36:45 1.20 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tasks.c,v 1.16 2013/05/30 09:13:52 misho Exp $ +* $Id: tasks.c,v 1.20 2013/08/26 13:36:45 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -129,17 +129,36 @@ _sched_threadWrapper(sched_task_t *t) { void *ret = NULL; sem_t *s = NULL; + sched_root_task_t *r; if (!t || !TASK_ROOT(t) || !TASK_RET(t)) pthread_exit(ret); - else + else { s = (sem_t*) TASK_RET(t); + r = TASK_ROOT(t); + } pthread_cleanup_push((void (*)(void*)) _sched_threadCleanup, t); pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskTHREAD]); +#endif + TAILQ_REMOVE(&r->root_thread, t, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskTHREAD]); +#endif + t->task_type = taskUNUSE; +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskUNUSE]); +#endif + TAILQ_INSERT_TAIL(&r->root_unuse, t, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskUNUSE]); +#endif + /* notify parent, thread is ready for execution */ sem_post(s); pthread_testcancel(); @@ -152,6 +171,46 @@ _sched_threadWrapper(sched_task_t *t) } #endif +#if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME) +void * +_sched_rtcWrapper(sched_task_t *t) +{ + void *ret = NULL; + sched_task_func_t func; + sched_task_t *task; + sched_root_task_t *r; + + if (!t || !TASK_ROOT(t) || !TASK_DATA(t)) + return NULL; + else { + r = TASK_ROOT(t); + task = (sched_task_t*) TASK_DATA(t); + func = TASK_FUNC(task); + } + +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskRTC]); +#endif + TAILQ_REMOVE(&r->root_rtc, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskRTC]); +#endif + task->task_type = taskUNUSE; +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&r->root_mtx[taskUNUSE]); +#endif + TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&r->root_mtx[taskUNUSE]); +#endif + + ret = func(task); + + timer_delete((timer_t) TASK_DATLEN(t)); + return ret; +} +#endif + #pragma GCC visibility pop /* @@ -511,7 +570,7 @@ schedSignal(sched_root_task_t * __restrict root, sched * @func = task execution function * @arg = 1st func argument * @ts = timeout argument structure, minimum alarm timer resolution is 1msec! - * @opt_data = Optional data + * @opt_data = Alarm timer ID * @opt_dlen = Optional data length * return: NULL error or !=NULL new queued task */ @@ -1322,3 +1381,61 @@ schedThread(sched_root_task_t * __restrict root, sched return task; } +/* + * schedRTC() - Add RTC 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 RTC ID + * @opt_dlen = Optional data length + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedRTC(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, struct timespec ts, + void *opt_data, size_t opt_dlen) +{ +#if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME) + 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) = taskRTC; + 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.rtc) + ptr = root->root_hooks.hook_add.rtc(task, NULL); + else + ptr = NULL; + + if (!ptr) { +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&root->root_mtx[taskRTC]); +#endif + TAILQ_INSERT_TAIL(&root->root_rtc, TASK_ID(task), task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&root->root_mtx[taskRTC]); +#endif + } else + task = sched_unuseTask(task); + + return task; +#else + sched_SetErr(ENOTSUP, "Not supported realtime clock extensions"); + return NULL; +#endif +}