--- libaitsched/src/tasks.c 2015/07/02 22:43:30 1.25.2.2 +++ libaitsched/src/tasks.c 2023/02/25 15:55:01 1.31 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tasks.c,v 1.25.2.2 2015/07/02 22:43:30 misho Exp $ +* $Id: tasks.c,v 1.31 2023/02/25 15:55:01 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004 - 2015 +Copyright 2004 - 2023 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -67,7 +67,7 @@ sched_useTask(sched_root_task_t * __restrict root) SCHED_QUNLOCK(root, taskUNUSE); if (!task) { - task = malloc(sizeof(sched_task_t)); + task = e_malloc(sizeof(sched_task_t)); if (!task) { LOGERR; return NULL; @@ -133,6 +133,25 @@ 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) { + return schedReadExt(root, func, arg, fd, opt_data, opt_dlen, 0); +} + +/* + * schedReadExt() - Add READ I/O task to scheduler queue with custom event mask + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @fd = fd handle + * @opt_data = Optional data + * @opt_dlen = Optional data length + * @mask = Event mask + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedReadExt(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, + void *opt_data, size_t opt_dlen, u_long mask) +{ sched_task_t *task; void *ptr; @@ -153,8 +172,11 @@ schedRead(sched_root_task_t * __restrict root, sched_t TASK_DATA(task) = opt_data; TASK_DATLEN(task) = opt_dlen; + TASK_HARG(task) = mask; + if (root->root_hooks.hook_add.read) - ptr = root->root_hooks.hook_add.read(task, NULL); + ptr = root->root_hooks.hook_add.read(task, + (void*) task->task_harg); else ptr = NULL; @@ -181,6 +203,25 @@ 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) { + return schedWriteExt(root, func, arg, fd, opt_data, opt_dlen, 0); +} + +/* + * schedWriteExt() - Add WRITE I/O task to scheduler queue with custom event mask + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @fd = fd handle + * @opt_data = Optional data + * @opt_dlen = Optional data length + * @mask = Event mask + * return: NULL error or !=NULL new queued task + */ +sched_task_t * +schedWriteExt(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, + void *opt_data, size_t opt_dlen, u_long mask) +{ sched_task_t *task; void *ptr; @@ -201,8 +242,11 @@ schedWrite(sched_root_task_t * __restrict root, sched_ TASK_DATA(task) = opt_data; TASK_DATLEN(task) = opt_dlen; + TASK_HARG(task) = mask; + if (root->root_hooks.hook_add.write) - ptr = root->root_hooks.hook_add.write(task, NULL); + ptr = root->root_hooks.hook_add.write(task, + (void*) task->task_harg); else ptr = NULL; @@ -268,6 +312,64 @@ schedNode(sched_root_task_t * __restrict root, sched_t } /* + * schedNode2() - Add NODE task with all events 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 * +schedNode2(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd, + void *opt_data, size_t opt_dlen) +{ +#if SUP_ENABLE != KQ_SUPPORT + sched_SetErr(ENOTSUP, "disabled kqueue support"); + 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_FUNC(task) = 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) +#ifdef __FreeBSD__ + ptr = root->root_hooks.hook_add.node(task, + (void*) (NOTE_READ | NOTE_CLOSE_WRITE | NOTE_CLOSE | NOTE_OPEN)); +#else + ptr = root->root_hooks.hook_add.node(task, NULL); +#endif + else + ptr = NULL; + + if (!ptr) + insert_task_to(task, &root->root_node); + else + task = sched_unuseTask(task); + + return task; +#endif /* KQ_SUPPORT */ +} + +/* * schedProc() - Add PROC task to scheduler queue * * @root = root task @@ -393,10 +495,6 @@ 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) { -#if SUP_ENABLE != KQ_SUPPORT - sched_SetErr(ENOTSUP, "disabled kqueue support"); - return NULL; -#else sched_task_t *task; void *ptr; @@ -428,7 +526,6 @@ schedSignal(sched_root_task_t * __restrict root, sched task = sched_unuseTask(task); return task; -#endif /* KQ_SUPPORT */ } /* @@ -573,7 +670,7 @@ schedAIORead(sched_root_task_t * __restrict root, sche } else off = offset; - if (!(acb = malloc(sizeof(struct aiocb)))) { + if (!(acb = e_malloc(sizeof(struct aiocb)))) { LOGERR; return NULL; } else @@ -589,7 +686,7 @@ schedAIORead(sched_root_task_t * __restrict root, sche if (aio_read(acb)) { LOGERR; - free(acb); + e_free(acb); return NULL; } @@ -632,7 +729,7 @@ schedAIOWrite(sched_root_task_t * __restrict root, sch } else off = offset; - if (!(acb = malloc(sizeof(struct aiocb)))) { + if (!(acb = e_malloc(sizeof(struct aiocb)))) { LOGERR; return NULL; } else @@ -648,7 +745,7 @@ schedAIOWrite(sched_root_task_t * __restrict root, sch if (aio_write(acb)) { LOGERR; - free(acb); + e_free(acb); return NULL; } @@ -747,19 +844,19 @@ schedLIORead(sched_root_task_t * __restrict root, sche } else off = offset; - if (!(acb = calloc(sizeof(void*), nbufs))) { + if (!(acb = e_calloc(sizeof(void*), nbufs))) { LOGERR; return NULL; } else memset(acb, 0, sizeof(void*) * nbufs); for (i = 0; i < nbufs; off += bufs[i++].iov_len) { - acb[i] = malloc(sizeof(struct aiocb)); + acb[i] = e_malloc(sizeof(struct aiocb)); if (!acb[i]) { LOGERR; for (i = 0; i < nbufs; i++) if (acb[i]) - free(acb[i]); - free(acb); + e_free(acb[i]); + e_free(acb); return NULL; } else memset(acb[i], 0, sizeof(struct aiocb)); @@ -778,8 +875,8 @@ schedLIORead(sched_root_task_t * __restrict root, sche LOGERR; for (i = 0; i < nbufs; i++) if (acb[i]) - free(acb[i]); - free(acb); + e_free(acb[i]); + e_free(acb); return NULL; } @@ -824,19 +921,19 @@ schedLIOWrite(sched_root_task_t * __restrict root, sch } else off = offset; - if (!(acb = calloc(sizeof(void*), nbufs))) { + if (!(acb = e_calloc(sizeof(void*), nbufs))) { LOGERR; return NULL; } else memset(acb, 0, sizeof(void*) * nbufs); for (i = 0; i < nbufs; off += bufs[i++].iov_len) { - acb[i] = malloc(sizeof(struct aiocb)); + acb[i] = e_malloc(sizeof(struct aiocb)); if (!acb[i]) { LOGERR; for (i = 0; i < nbufs; i++) if (acb[i]) - free(acb[i]); - free(acb); + e_free(acb[i]); + e_free(acb); return NULL; } else memset(acb[i], 0, sizeof(struct aiocb)); @@ -855,8 +952,8 @@ schedLIOWrite(sched_root_task_t * __restrict root, sch LOGERR; for (i = 0; i < nbufs; i++) if (acb[i]) - free(acb[i]); - free(acb); + e_free(acb[i]); + e_free(acb); return NULL; }