--- libaitsched/src/tasks.c 2012/08/02 09:19:31 1.10.2.6 +++ libaitsched/src/tasks.c 2012/08/02 13:53:22 1.10.2.12 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tasks.c,v 1.10.2.6 2012/08/02 09:19:31 misho Exp $ +* $Id: tasks.c,v 1.10.2.12 2012/08/02 13:53:22 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 @@ -649,11 +709,12 @@ schedAIOWrite(sched_root_task_t * __restrict root, sch * @fd = file descriptor * @bufs = Buffer's list * @nbufs = Number of Buffers + * @offset = Offset from start of file, if =-1 from current position * return: NULL error or !=NULL new queued task */ -inline sched_task_t * +sched_task_t * schedLIORead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, - struct iovec *bufs, size_t nbufs) + struct iovec *bufs, size_t nbufs, off_t offset) { struct sigevent sig; struct aiocb **acb; @@ -663,11 +724,14 @@ schedLIORead(sched_root_task_t * __restrict root, sche if (!root || !func || !bufs || !nbufs) return NULL; - off = lseek(fd, 0, SEEK_CUR); - if (off == -1) { - LOGERR; - return NULL; - } + if (offset == (off_t) -1) { + off = lseek(fd, 0, SEEK_CUR); + if (off == -1) { + LOGERR; + return NULL; + } + } else + off = offset; if (!(acb = calloc(sizeof(void*), nbufs))) { LOGERR; @@ -681,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)); @@ -698,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); } /* @@ -713,11 +781,12 @@ schedLIORead(sched_root_task_t * __restrict root, sche * @fd = file descriptor * @bufs = Buffer's list * @nbufs = Number of Buffers + * @offset = Offset from start of file, if =-1 from current position * return: NULL error or !=NULL new queued task */ inline sched_task_t * schedLIOWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, - struct iovec *bufs, size_t nbufs) + struct iovec *bufs, size_t nbufs, off_t offset) { struct sigevent sig; struct aiocb **acb; @@ -727,11 +796,14 @@ schedLIOWrite(sched_root_task_t * __restrict root, sch if (!root || !func || !bufs || !nbufs) return NULL; - off = lseek(fd, 0, SEEK_CUR); - if (off == -1) { - LOGERR; - return NULL; - } + if (offset == (off_t) -1) { + off = lseek(fd, 0, SEEK_CUR); + if (off == -1) { + LOGERR; + return NULL; + } + } else + off = offset; if (!(acb = calloc(sizeof(void*), nbufs))) { LOGERR; @@ -745,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)); @@ -762,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