--- libaitsched/src/tasks.c 2012/08/02 11:37:08 1.10.2.7 +++ libaitsched/src/tasks.c 2012/08/21 11:07:16 1.12.4.1 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tasks.c,v 1.10.2.7 2012/08/02 11:37:08 misho Exp $ +* $Id: tasks.c,v 1.12.4.1 2012/08/21 11:07:16 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -482,7 +482,7 @@ schedAlarm(sched_root_task_t * __restrict root, sched_ return task; } -#ifdef EVFILT_AIO +#ifdef AIO_SUPPORT /* * schedAIO() - Add AIO task to scheduler queue * @@ -546,20 +546,28 @@ schedAIO(sched_root_task_t * __restrict root, sched_ta * @fd = file descriptor * @buffer = Buffer * @buflen = Buffer length + * @offset = Offset from start of file, if =-1 from current position * return: NULL error or !=NULL new queued task */ inline sched_task_t * schedAIORead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, - void *buffer, size_t buflen) + void *buffer, size_t buflen, off_t offset) { struct aiocb *acb; - off_t off = 0; + off_t off; if (!root || !func || !buffer || !buflen) return NULL; - else - memset(buffer, 0, buflen); + if (offset == (off_t) -1) { + off = lseek(fd, 0, SEEK_CUR); + if (off == -1) { + LOGERR; + return NULL; + } + } else + off = offset; + if (!(acb = malloc(sizeof(struct aiocb)))) { LOGERR; return NULL; @@ -569,13 +577,7 @@ schedAIORead(sched_root_task_t * __restrict root, sche acb->aio_fildes = fd; acb->aio_nbytes = buflen; acb->aio_buf = buffer; - off = lseek(fd, 0, SEEK_CUR); - if (off == -1) { - LOGERR; - free(acb); - return NULL; - } else - acb->aio_offset = off; + acb->aio_offset = off; acb->aio_sigevent.sigev_notify = SIGEV_KEVENT; acb->aio_sigevent.sigev_notify_kqueue = root->root_kq; acb->aio_sigevent.sigev_value.sival_ptr = acb; @@ -598,18 +600,28 @@ schedAIORead(sched_root_task_t * __restrict root, sche * @fd = file descriptor * @buffer = Buffer * @buflen = Buffer length + * @offset = Offset from start of file, if =-1 from current position * return: NULL error or !=NULL new queued task */ inline sched_task_t * schedAIOWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, - void *buffer, size_t buflen) + void *buffer, size_t buflen, off_t offset) { struct aiocb *acb; - off_t off = 0; + off_t off; if (!root || !func || !buffer || !buflen) return NULL; + if (offset == (off_t) -1) { + off = lseek(fd, 0, SEEK_CUR); + if (off == -1) { + LOGERR; + return NULL; + } + } else + off = offset; + if (!(acb = malloc(sizeof(struct aiocb)))) { LOGERR; return NULL; @@ -619,13 +631,7 @@ schedAIOWrite(sched_root_task_t * __restrict root, sch acb->aio_fildes = fd; acb->aio_nbytes = buflen; acb->aio_buf = buffer; - off = lseek(fd, 0, SEEK_CUR); - if (off == -1) { - LOGERR; - free(acb); - return NULL; - } else - acb->aio_offset = off; + acb->aio_offset = off; acb->aio_sigevent.sigev_notify = SIGEV_KEVENT; acb->aio_sigevent.sigev_notify_kqueue = root->root_kq; acb->aio_sigevent.sigev_value.sival_ptr = acb; @@ -641,6 +647,60 @@ schedAIOWrite(sched_root_task_t * __restrict root, sch #ifdef EVFILT_LIO /* + * schedLIO() - Add AIO bulk tasks to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @acbs = AIO cb structure addresses + * @opt_data = Optional data + * @opt_dlen = Optional data length + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedLIO(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, + struct aiocb ** __restrict acbs, void *opt_data, size_t opt_dlen) +{ + sched_task_t *task; + void *ptr; + + if (!root || !func || !acbs || !opt_dlen) + return NULL; + + /* get new task */ + if (!(task = _sched_useTask(root))) + return NULL; + + task->task_func = func; + TASK_TYPE(task) = taskLIO; + TASK_ROOT(task) = root; + + TASK_ARG(task) = arg; + TASK_VAL(task) = (u_long) acbs; + + TASK_DATA(task) = opt_data; + TASK_DATLEN(task) = opt_dlen; + + if (root->root_hooks.hook_add.lio) + ptr = root->root_hooks.hook_add.lio(task, NULL); + else + ptr = NULL; + + if (!ptr) { +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&root->root_mtx[taskLIO]); +#endif + TAILQ_INSERT_TAIL(&root->root_lio, TASK_ID(task), task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&root->root_mtx[taskLIO]); +#endif + } else + task = _sched_unuseTask(task); + + return task; +} + +/* * schedLIORead() - Add list of AIO read tasks to scheduler queue * * @root = root task @@ -685,7 +745,7 @@ schedLIORead(sched_root_task_t * __restrict root, sche for (i = 0; i < nbufs; i++) if (acb[i]) free(acb[i]); - free(acb); + free(acb); return NULL; } else memset(acb[i], 0, sizeof(struct aiocb)); @@ -702,10 +762,14 @@ schedLIORead(sched_root_task_t * __restrict root, sche if (lio_listio(LIO_NOWAIT, acb, nbufs, &sig)) { LOGERR; + for (i = 0; i < nbufs; i++) + if (acb[i]) + free(acb[i]); + free(acb); return NULL; } - return schedAIO(root, func, arg, (void*) acb, bufs, nbufs); + return schedLIO(root, func, arg, (void*) acb, bufs, nbufs); } /* @@ -753,7 +817,7 @@ schedLIOWrite(sched_root_task_t * __restrict root, sch for (i = 0; i < nbufs; i++) if (acb[i]) free(acb[i]); - free(acb); + free(acb); return NULL; } else memset(acb[i], 0, sizeof(struct aiocb)); @@ -770,13 +834,17 @@ schedLIOWrite(sched_root_task_t * __restrict root, sch if (lio_listio(LIO_NOWAIT, acb, nbufs, &sig)) { LOGERR; + for (i = 0; i < nbufs; i++) + if (acb[i]) + free(acb[i]); + free(acb); return NULL; } - return schedAIO(root, func, arg, (void*) acb, bufs, nbufs); + return schedLIO(root, func, arg, (void*) acb, bufs, nbufs); } #endif /* EVFILT_LIO */ -#endif /* EVFILT_AIO */ +#endif /* AIO_SUPPORT */ /* * schedTimer() - Add TIMER task to scheduler queue @@ -911,21 +979,21 @@ schedEvent(sched_root_task_t * __restrict root, sched_ /* - * schedEventLo() - Add EVENT_Lo task to scheduler queue + * schedTask() - Add regular task to scheduler queue * * @root = root task * @func = task execution function * @arg = 1st func argument - * @val = additional func argument + * @prio = regular task priority, 0 is hi priority for regular tasks * @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, +schedTask(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, u_long prio, void *opt_data, size_t opt_dlen) { - sched_task_t *task; + sched_task_t *task, *tmp, *t = NULL; void *ptr; if (!root || !func) @@ -936,27 +1004,33 @@ schedEventLo(sched_root_task_t * __restrict root, sche return NULL; task->task_func = func; - TASK_TYPE(task) = taskEVENT; + TASK_TYPE(task) = taskTASK; TASK_ROOT(task) = root; TASK_ARG(task) = arg; - TASK_VAL(task) = val; + TASK_VAL(task) = prio; 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); + if (root->root_hooks.hook_add.task) + ptr = root->root_hooks.hook_add.task(task, NULL); else ptr = NULL; if (!ptr) { #ifdef HAVE_LIBPTHREAD - pthread_mutex_lock(&root->root_mtx[taskEVENTLO]); + pthread_mutex_lock(&root->root_mtx[taskTASK]); #endif - TAILQ_INSERT_TAIL(&root->root_eventlo, TASK_ID(task), task_node); + TAILQ_FOREACH_SAFE(t, &root->root_task, task_node, tmp) + if (TASK_VAL(task) < TASK_VAL(t)) + break; + if (!t) + TAILQ_INSERT_TAIL(&root->root_task, TASK_ID(task), task_node); + else + TAILQ_INSERT_BEFORE(t, TASK_ID(task), task_node); #ifdef HAVE_LIBPTHREAD - pthread_mutex_unlock(&root->root_mtx[taskEVENTLO]); + pthread_mutex_unlock(&root->root_mtx[taskTASK]); #endif } else task = _sched_unuseTask(task); @@ -1058,3 +1132,73 @@ schedCallOnce(sched_root_task_t * __restrict root, sch _sched_unuseTask(task); return ret; } + +/* + * schedThread() - Add thread task to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @detach = Detach thread from scheduler, if !=0 + * @opt_data = Optional data + * @opt_dlen = Optional data length + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedThread(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int detach, + void *opt_data, size_t opt_dlen) +{ +#ifndef HAVE_LIBPTHREAD + sched_SetErr(ENOTSUP, "Not supported thread tasks"); + return NULL; +#endif + sched_task_t *task; + void *ptr; + pthread_t tid; + pthread_attr_t attr; + + if (!root || !func) + return NULL; + + /* get new task */ + if (!(task = _sched_useTask(root))) + return NULL; + + task->task_func = func; + TASK_TYPE(task) = taskTHREAD; + TASK_ROOT(task) = root; + + TASK_ARG(task) = arg; + + TASK_DATA(task) = opt_data; + TASK_DATLEN(task) = opt_dlen; + + if (root->root_hooks.hook_add.thread) + ptr = root->root_hooks.hook_add.thread(task, NULL); + else + ptr = NULL; + + if (!ptr) { + pthread_mutex_lock(&root->root_mtx[taskTHREAD]); + TAILQ_INSERT_TAIL(&root->root_thread, TASK_ID(task), task_node); + pthread_mutex_unlock(&root->root_mtx[taskTHREAD]); + + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, detach ? + PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE); + if (pthread_create(&tid, &attr, + (void *(*)(void*)) task->task_func, task)) { + LOGERR; + pthread_mutex_lock(&root->root_mtx[taskTHREAD]); + TAILQ_REMOVE(&root->root_thread, TASK_ID(task), task_node); + pthread_mutex_unlock(&root->root_mtx[taskTHREAD]); + task = _sched_unuseTask(task); + } else + TASK_VAL(task) = (u_long) tid; + pthread_attr_destroy(&attr); + } else + task = _sched_unuseTask(task); + + return task; +} +