Annotation of libaitsched/src/aitsched.c, revision 1.1.1.1.2.7

1.1       misho       1: /*************************************************************************
                      2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
                      3: *  by Michael Pounov <misho@openbsd-bg.org>
                      4: *
                      5: * $Author: misho $
1.1.1.1.2.7! misho       6: * $Id: aitsched.c,v 1.1.1.1.2.6 2011/09/09 14:46:25 misho Exp $
1.1       misho       7: *
                      8: **************************************************************************
                      9: The ELWIX and AITNET software is distributed under the following
                     10: terms:
                     11: 
                     12: All of the documentation and software included in the ELWIX and AITNET
                     13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
                     14: 
                     15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
                     16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
                     17: 
                     18: Redistribution and use in source and binary forms, with or without
                     19: modification, are permitted provided that the following conditions
                     20: are met:
                     21: 1. Redistributions of source code must retain the above copyright
                     22:    notice, this list of conditions and the following disclaimer.
                     23: 2. Redistributions in binary form must reproduce the above copyright
                     24:    notice, this list of conditions and the following disclaimer in the
                     25:    documentation and/or other materials provided with the distribution.
                     26: 3. All advertising materials mentioning features or use of this software
                     27:    must display the following acknowledgement:
                     28: This product includes software developed by Michael Pounov <misho@elwix.org>
                     29: ELWIX - Embedded LightWeight unIX and its contributors.
                     30: 4. Neither the name of AITNET nor the names of its contributors
                     31:    may be used to endorse or promote products derived from this software
                     32:    without specific prior written permission.
                     33: 
                     34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
                     35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     44: SUCH DAMAGE.
                     45: */
                     46: #include "global.h"
                     47: #include "hooks.h"
                     48: 
                     49: 
                     50: #pragma GCC visibility push(hidden)
                     51: 
                     52: int sched_Errno;
                     53: char sched_Error[STRSIZ];
                     54: 
                     55: #pragma GCC visibility pop
                     56: 
                     57: 
                     58: // sched_GetErrno() Get error code of last operation
                     59: inline int
                     60: sched_GetErrno()
                     61: {
                     62:        return sched_Errno;
                     63: }
                     64: 
                     65: // sched_GetError() Get error text of last operation
                     66: inline const char *
                     67: sched_GetError()
                     68: {
                     69:        return sched_Error;
                     70: }
                     71: 
                     72: // sched_SetErr() Set error to variables for internal use!!!
                     73: inline void
                     74: sched_SetErr(int eno, char *estr, ...)
                     75: {
                     76:        va_list lst;
                     77: 
                     78:        sched_Errno = eno;
                     79:        memset(sched_Error, 0, sizeof sched_Error);
                     80:        va_start(lst, estr);
                     81:        vsnprintf(sched_Error, sizeof sched_Error, estr, lst);
                     82:        va_end(lst);
                     83: }
                     84: 
                     85: /* Init and prepare scheduler functions */
                     86: 
                     87: /*
1.1.1.1.2.1  misho      88:  * schedRegisterHooks() - Register IO handles and bind tasks to it
                     89:  * @root = root task
                     90:  * return: -1 error or 0 ok
                     91:  */
                     92: int
                     93: schedRegisterHooks(sched_root_task_t * __restrict root)
                     94: {
                     95:        if (!root || (root->root_data.iov_base && root->root_data.iov_len))
                     96:                return -1;
                     97: 
                     98:        if (root->root_hooks.hook_root.fini)
                     99:                root->root_hooks.hook_root.fini(root, NULL);
                    100:        memset(&root->root_hooks, 0, sizeof root->root_hooks);
                    101: 
                    102:        root->root_hooks.hook_add.read = sched_hook_read;
                    103:        root->root_hooks.hook_add.write = sched_hook_write;
                    104: 
                    105:        root->root_hooks.hook_exec.cancel = sched_hook_cancel;
                    106:        root->root_hooks.hook_exec.fetch = sched_hook_fetch;
                    107: 
                    108:        root->root_hooks.hook_root.init = sched_hook_init;
                    109:        root->root_hooks.hook_root.fini = sched_hook_fini;
                    110:        return 0;
                    111: }
                    112: 
                    113: /*
1.1       misho     114:  * schedInit() - Init scheduler
                    115:  * @data = optional data if !=NULL
                    116:  * @datlen = data len if data is set
                    117:  * return: allocated root task if ok or NULL error
                    118:  */
                    119: sched_root_task_t *
                    120: schedInit(void ** __restrict data, size_t datlen)
                    121: {
                    122:        sched_root_task_t *root = NULL;
                    123:        int (*func)(sched_root_task_t *);
                    124: 
                    125:        root = malloc(sizeof(sched_root_task_t));
1.1.1.1.2.1  misho     126:        if (!root) {
                    127:                LOGERR;
                    128:        } else {
1.1       misho     129:                memset(root, 0, sizeof(sched_root_task_t));
1.1.1.1.2.2  misho     130:                TAILQ_INIT(&root->root_read);
                    131:                TAILQ_INIT(&root->root_write);
                    132:                TAILQ_INIT(&root->root_timer);
                    133:                TAILQ_INIT(&root->root_event);
                    134:                TAILQ_INIT(&root->root_eventlo);
                    135:                TAILQ_INIT(&root->root_ready);
                    136:                TAILQ_INIT(&root->root_unuse);
1.1       misho     137: 
                    138:                if (data && *data) {
                    139:                        if (datlen) {
                    140:                                root->root_data.iov_base = *data;
                    141:                                root->root_data.iov_len = datlen;
                    142:                        } else {
1.1.1.1.2.2  misho     143:                                func = (int(*)(sched_root_task_t*)) data;
1.1       misho     144:                                func(root);
                    145:                        }
                    146:                }
1.1.1.1.2.1  misho     147: 
                    148:                if (root->root_hooks.hook_root.init)
                    149:                        root->root_hooks.hook_root.init(root, NULL);
1.1       misho     150:        }
                    151: 
                    152:        return root;
                    153: }
                    154: 
                    155: /*
                    156:  * schedEnd() - End scheduler & free all resources
                    157:  * @root = root task
                    158:  * return: -1 error or 0 ok
                    159:  */
                    160: int
1.1.1.1.2.5  misho     161: schedEnd(sched_root_task_t ** __restrict root)
1.1       misho     162: {
                    163:        sched_task_t *task;
                    164: 
1.1.1.1.2.5  misho     165:        if (!root || !*root)
1.1       misho     166:                return -1;
                    167: 
1.1.1.1.2.5  misho     168:        TAILQ_FOREACH(task, &(*root)->root_read, task_node) {
1.1       misho     169:                schedCancel(task);
                    170:        }
1.1.1.1.2.5  misho     171:        TAILQ_FOREACH(task, &(*root)->root_write, task_node) {
1.1       misho     172:                schedCancel(task);
                    173:        }
1.1.1.1.2.5  misho     174:        TAILQ_FOREACH(task, &(*root)->root_timer, task_node) {
1.1       misho     175:                schedCancel(task);
                    176:        }
1.1.1.1.2.5  misho     177:        TAILQ_FOREACH(task, &(*root)->root_event, task_node) {
1.1       misho     178:                schedCancel(task);
                    179:        }
1.1.1.1.2.5  misho     180:        TAILQ_FOREACH(task, &(*root)->root_ready, task_node) {
1.1       misho     181:                schedCancel(task);
                    182:        }
                    183: 
1.1.1.1.2.5  misho     184:        while ((task = TAILQ_FIRST(&(*root)->root_unuse))) {
                    185:                TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
1.1       misho     186:                free(task);
                    187:        }
                    188: 
1.1.1.1.2.5  misho     189:        if ((*root)->root_hooks.hook_root.fini)
                    190:                (*root)->root_hooks.hook_root.fini(*root, NULL);
1.1       misho     191: 
1.1.1.1.2.5  misho     192:        free(*root);
                    193:        *root = NULL;
1.1       misho     194:        return 0;
                    195: }
                    196: 
                    197: /*
                    198:  * schedCall() - Call task execution function
                    199:  * @task = current task
                    200:  * return: !=NULL error or =NULL ok
                    201:  */
                    202: inline void *
                    203: schedCall(sched_task_t * __restrict task)
                    204: {
                    205:        if (!task)
                    206:                return (void*) -1;
                    207: 
                    208:        task->task_id++;
                    209:        return task->task_func(task);
                    210: }
                    211: 
                    212: /*
                    213:  * schedFetch() - Fetch ready task
                    214:  * @root = root task
                    215:  * return: =NULL error or !=NULL ready task
                    216:  */
                    217: inline void *
                    218: schedFetch(sched_root_task_t * __restrict root)
                    219: {
                    220:        void *ptr;
                    221: 
                    222:        if (!root)
                    223:                return NULL;
                    224: 
                    225:        if (root->root_hooks.hook_exec.fetch)
                    226:                ptr = root->root_hooks.hook_exec.fetch(root, NULL);
                    227:        else
                    228:                ptr = NULL;
                    229: 
                    230:        return ptr;
                    231: }
                    232: 
                    233: /*
                    234:  * schedCancel() - Cancel task from scheduler
                    235:  * @task = task
                    236:  * return: -1 error or 0 ok
                    237:  */
                    238: int
                    239: schedCancel(sched_task_t * __restrict task)
                    240: {
                    241:        sched_queue_t *queue;
                    242: 
                    243:        if (!task || !task->task_root)
                    244:                return -1;
                    245: 
                    246:        if (task->task_root->root_hooks.hook_exec.cancel)
                    247:                if (task->task_root->root_hooks.hook_exec.cancel(task, NULL))
                    248:                        return -1;
                    249: 
                    250:        switch (task->task_type) {
                    251:                case taskREAD:
                    252:                        queue = &task->task_root->root_read;
                    253:                        break;
                    254:                case taskWRITE:
                    255:                        queue = &task->task_root->root_write;
                    256:                        break;
                    257:                case taskTIMER:
                    258:                        queue = &task->task_root->root_timer;
                    259:                        break;
                    260:                case taskEVENT:
                    261:                        queue = &task->task_root->root_event;
                    262:                        break;
                    263:                case taskREADY:
                    264:                        queue = &task->task_root->root_ready;
                    265:                        break;
                    266:                default:
                    267:                        queue = NULL;
                    268:        }
                    269:        if (queue)
                    270:                TAILQ_REMOVE(queue, task, task_node);
                    271:        if (task->task_type != taskUNUSE) {
                    272:                task->task_type = taskUNUSE;
                    273:                TAILQ_INSERT_TAIL(&task->task_root->root_unuse, task, task_node);
                    274:        }
                    275: 
                    276:        return 0;
                    277: }
                    278: 
                    279: /*
                    280:  * schedCancelby() - Cancel task from scheduler by criteria
                    281:  * @root = root task
                    282:  * @queue = cancel from queue, if =NULL cancel same task from all queues
                    283:  * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
                    284:  * @param = search parameter
                    285:  * @hook = custom cleanup hook function, may be NULL
                    286:  * return: -1 error or 0 ok
                    287:  */
                    288: int
                    289: schedCancelby(sched_root_task_t * __restrict root, sched_queue_t * __restrict queue, 
                    290:                u_char criteria, void *param, sched_hook_func_t hook)
                    291: {
                    292:        sched_task_t *task;
                    293:        int flg = 0;
                    294: 
                    295:        if (!root)
                    296:                return -1;
                    297:        if (!queue) {
                    298:                if (schedCancelby(root, &root->root_read, criteria, param, hook))
                    299:                        return -2;
                    300:                if (schedCancelby(root, &root->root_write, criteria, param, hook))
                    301:                        return -2;
                    302:                if (schedCancelby(root, &root->root_timer, criteria, param, hook))
                    303:                        return -2;
                    304:                if (schedCancelby(root, &root->root_event, criteria, param, hook))
                    305:                        return -2;
                    306:                if (schedCancelby(root, &root->root_ready, criteria, param, hook))
                    307:                        return -2;
                    308:                if (schedCancelby(root, &root->root_read, criteria, param, hook))
                    309:                        return -2;
                    310:                return 0;
                    311:        }
                    312: 
                    313:        TAILQ_FOREACH(task, queue, task_node)
                    314:                if (criteria == CRITERIA_CALL) {
                    315:                        if (task->task_func == (sched_task_func_t) param) {
                    316:                                flg++;
                    317:                                break;
                    318:                        }
                    319:                } else if (criteria == CRITERIA_ARG) {
                    320:                        if (task->task_arg == param) {
                    321:                                flg++;
                    322:                                break;
                    323:                        }
                    324:                } else if (criteria == CRITERIA_FD) {
1.1.1.1.2.7! misho     325:                        if (TASK_FD(task) == (int) param) {
1.1       misho     326:                                flg++;
                    327:                                break;
                    328:                        }
                    329:                } else if (criteria == CRITERIA_VAL) {
                    330:                        if (TASK_VAL(task) == (u_long) param) {
                    331:                                flg++;
                    332:                                break;
                    333:                        }
                    334:                } else if (criteria == CRITERIA_TV) {
                    335:                        if (&TASK_TV(task) == (struct timeval*) param) {
                    336:                                flg++;
                    337:                                break;
                    338:                        }
                    339:                } else {
                    340:                        sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
                    341:                        return -1;
                    342:                }
                    343:        if (!flg || !task)      /* task not found */
                    344:                return 0;
                    345: 
                    346:        if (task->task_root->root_hooks.hook_exec.cancel)
                    347:                if (task->task_root->root_hooks.hook_exec.cancel(task, NULL))
                    348:                        return -1;
                    349:        if (hook)
                    350:                if (hook(task, NULL))
                    351:                        return -3;
                    352: 
                    353:        TAILQ_REMOVE(queue, task, task_node);
                    354: 
                    355:        if (task->task_type != taskUNUSE) {
                    356:                task->task_type = taskUNUSE;
                    357:                TAILQ_INSERT_TAIL(&task->task_root->root_unuse, task, task_node);
                    358:        }
                    359:        return 0;
                    360: }
                    361: 
                    362: /*
                    363:  * schedRun() - Scheduler *run loop*
                    364:  * @root = root task
1.1.1.1.2.6  misho     365:  * @killState = kill condition variable, if !=0 stop scheduler loop
1.1       misho     366:  * return: -1 error or 0 ok
                    367:  */
                    368: int
1.1.1.1.2.6  misho     369: schedRun(sched_root_task_t * __restrict root, volatile intptr_t * __restrict killState)
1.1       misho     370: {
                    371:        sched_task_t *task;
                    372: 
                    373:        if (!root)
                    374:                return -1;
                    375: 
                    376:        if (root->root_hooks.hook_exec.run)
                    377:                if (root->root_hooks.hook_exec.run(root, NULL))
                    378:                        return -1;
1.1.1.1.2.6  misho     379:        if (root->root_hooks.hook_exec.fetch) {
                    380:                if (killState)
                    381:                        while (!*killState && (task = root->root_hooks.hook_exec.fetch(root, NULL)))
                    382:                                schedCall(task);
                    383:                else
                    384:                        while ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
                    385:                                schedCall(task);
                    386:        }
1.1       misho     387: 
                    388:        return 0;
                    389: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>