--- libaitsched/src/tasks.c 2012/07/24 14:06:11 1.10 +++ libaitsched/src/tasks.c 2012/08/01 13:43:14 1.10.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tasks.c,v 1.10 2012/07/24 14:06:11 misho Exp $ +* $Id: tasks.c,v 1.10.2.2 2012/08/01 13:43:14 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -480,6 +480,181 @@ schedAlarm(sched_root_task_t * __restrict root, sched_ task = _sched_unuseTask(task); return task; +} + +/* + * schedAIO() - Add AIO task to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @acb = AIO cb structure address + * @opt_data = Optional data + * @opt_dlen = Optional data length + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedAIO(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, + struct aiocb * __restrict acb, void *opt_data, size_t opt_dlen) +{ +#ifndef EVFILT_AIO + 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) = taskAIO; + TASK_ROOT(task) = root; + + TASK_ARG(task) = arg; + TASK_VAL(task) = (u_long) acb; + + TASK_DATA(task) = opt_data; + TASK_DATLEN(task) = opt_dlen; + + if (root->root_hooks.hook_add.aio) + ptr = root->root_hooks.hook_add.aio(task, NULL); + else + ptr = NULL; + + if (!ptr) { +#ifdef HAVE_LIBPTHREAD + pthread_mutex_lock(&root->root_mtx[taskAIO]); +#endif + TAILQ_INSERT_TAIL(&root->root_aio, TASK_ID(task), task_node); +#ifdef HAVE_LIBPTHREAD + pthread_mutex_unlock(&root->root_mtx[taskAIO]); +#endif + } else + task = _sched_unuseTask(task); + + return task; +#endif +} + +/* + * schedAIORead() - Add AIO read task to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @fd = file descriptor + * @buffer = Buffer + * @buflen = Buffer length + * 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) +{ +#ifndef EVFILT_AIO + sched_SetErr(ENOTSUP, "Not supported kevent() filter"); + return NULL; +#else + struct aiocb *acb; + off_t off = 0; + + if (!root || !func) + return NULL; + else + memset(buffer, 0, buflen); + + if (!(acb = malloc(sizeof(struct aiocb)))) { + LOGERR; + return NULL; + } else + memset(acb, 0, sizeof(struct aiocb)); + + 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_sigevent.sigev_notify = SIGEV_KEVENT; + acb->aio_sigevent.sigev_notify_kevent_flags = EV_CLEAR | EV_ONESHOT; + acb->aio_sigevent.sigev_notify_kqueue = root->root_kq; + acb->aio_sigevent.sigev_value.sival_ptr = (void*) O_RDONLY; + + if (aio_read(acb)) { + LOGERR; + free(acb); + return NULL; + } + + return schedAIO(root, func, arg, acb, buffer, buflen); +#endif +} + +/* + * schedAIOWrite() - Add AIO write task to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @fd = file descriptor + * @buffer = Buffer + * @buflen = Buffer length + * 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) +{ +#ifndef EVFILT_AIO + sched_SetErr(ENOTSUP, "Not supported kevent() filter"); + return NULL; +#else + struct aiocb *acb; + off_t off = 0; + + if (!root || !func) + return NULL; + else + memset(buffer, 0, buflen); + + if (!(acb = malloc(sizeof(struct aiocb)))) { + LOGERR; + return NULL; + } else + memset(acb, 0, sizeof(struct aiocb)); + + 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_sigevent.sigev_notify = SIGEV_KEVENT; + acb->aio_sigevent.sigev_notify_kevent_flags = EV_CLEAR | EV_ONESHOT; + acb->aio_sigevent.sigev_notify_kqueue = root->root_kq; + acb->aio_sigevent.sigev_value.sival_ptr = (void*) O_WRONLY; + + if (aio_write(acb)) { + LOGERR; + free(acb); + return NULL; + } + + return schedAIO(root, func, arg, acb, buffer, buflen); +#endif } /*