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

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.4     ! misho       6: * $Id: aitsched.c,v 1.3.2.1 2011/12/08 09:17:34 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.2       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;
1.3       misho     107:        root->root_hooks.hook_exec.exception = sched_hook_exception;
1.2       misho     108: 
                    109:        root->root_hooks.hook_root.init = sched_hook_init;
                    110:        root->root_hooks.hook_root.fini = sched_hook_fini;
                    111:        return 0;
                    112: }
                    113: 
                    114: /*
1.1       misho     115:  * schedInit() - Init scheduler
                    116:  * @data = optional data if !=NULL
                    117:  * @datlen = data len if data is set
                    118:  * return: allocated root task if ok or NULL error
                    119:  */
                    120: sched_root_task_t *
                    121: schedInit(void ** __restrict data, size_t datlen)
                    122: {
                    123:        sched_root_task_t *root = NULL;
                    124:        int (*func)(sched_root_task_t *);
                    125: 
                    126:        root = malloc(sizeof(sched_root_task_t));
1.2       misho     127:        if (!root) {
                    128:                LOGERR;
                    129:        } else {
1.1       misho     130:                memset(root, 0, sizeof(sched_root_task_t));
1.2       misho     131:                TAILQ_INIT(&root->root_read);
                    132:                TAILQ_INIT(&root->root_write);
                    133:                TAILQ_INIT(&root->root_timer);
                    134:                TAILQ_INIT(&root->root_event);
                    135:                TAILQ_INIT(&root->root_eventlo);
                    136:                TAILQ_INIT(&root->root_ready);
                    137:                TAILQ_INIT(&root->root_unuse);
1.1       misho     138: 
                    139:                if (data && *data) {
                    140:                        if (datlen) {
                    141:                                root->root_data.iov_base = *data;
                    142:                                root->root_data.iov_len = datlen;
1.3       misho     143:                        } else { /* if datlen == 0, switch to callbacks init mode */
                    144:                                 /* little hack :) for correct initialization of scheduler */
1.2       misho     145:                                func = (int(*)(sched_root_task_t*)) data;
1.1       misho     146:                                func(root);
                    147:                        }
                    148:                }
1.2       misho     149: 
                    150:                if (root->root_hooks.hook_root.init)
                    151:                        root->root_hooks.hook_root.init(root, NULL);
1.1       misho     152:        }
                    153: 
                    154:        return root;
                    155: }
                    156: 
                    157: /*
                    158:  * schedEnd() - End scheduler & free all resources
                    159:  * @root = root task
                    160:  * return: -1 error or 0 ok
                    161:  */
                    162: int
1.2       misho     163: schedEnd(sched_root_task_t ** __restrict root)
1.1       misho     164: {
                    165:        sched_task_t *task;
                    166: 
1.2       misho     167:        if (!root || !*root)
1.1       misho     168:                return -1;
                    169: 
1.2       misho     170:        TAILQ_FOREACH(task, &(*root)->root_read, task_node) {
1.1       misho     171:                schedCancel(task);
                    172:        }
1.2       misho     173:        TAILQ_FOREACH(task, &(*root)->root_write, task_node) {
1.1       misho     174:                schedCancel(task);
                    175:        }
1.2       misho     176:        TAILQ_FOREACH(task, &(*root)->root_timer, task_node) {
1.1       misho     177:                schedCancel(task);
                    178:        }
1.2       misho     179:        TAILQ_FOREACH(task, &(*root)->root_event, task_node) {
1.1       misho     180:                schedCancel(task);
                    181:        }
1.2       misho     182:        TAILQ_FOREACH(task, &(*root)->root_ready, task_node) {
1.1       misho     183:                schedCancel(task);
                    184:        }
                    185: 
1.2       misho     186:        while ((task = TAILQ_FIRST(&(*root)->root_unuse))) {
                    187:                TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
1.1       misho     188:                free(task);
                    189:        }
                    190: 
1.2       misho     191:        if ((*root)->root_hooks.hook_root.fini)
                    192:                (*root)->root_hooks.hook_root.fini(*root, NULL);
1.1       misho     193: 
1.2       misho     194:        free(*root);
                    195:        *root = NULL;
1.1       misho     196:        return 0;
                    197: }
                    198: 
                    199: /*
                    200:  * schedCall() - Call task execution function
                    201:  * @task = current task
                    202:  * return: !=NULL error or =NULL ok
                    203:  */
                    204: inline void *
                    205: schedCall(sched_task_t * __restrict task)
                    206: {
1.4     ! misho     207:        void *ptr = (void*) -1;
        !           208: 
1.1       misho     209:        if (!task)
1.4     ! misho     210:                return ptr;
        !           211: 
        !           212:        if (!TASK_ISLOCKED(task))
        !           213:                TASK_LOCK(task);
1.1       misho     214: 
                    215:        task->task_id++;
1.4     ! misho     216:        ptr = task->task_func(task);
        !           217: 
        !           218:        TASK_UNLOCK(task);
        !           219:        return ptr;
1.1       misho     220: }
                    221: 
                    222: /*
                    223:  * schedFetch() - Fetch ready task
                    224:  * @root = root task
                    225:  * return: =NULL error or !=NULL ready task
                    226:  */
                    227: inline void *
                    228: schedFetch(sched_root_task_t * __restrict root)
                    229: {
                    230:        void *ptr;
                    231: 
                    232:        if (!root)
                    233:                return NULL;
                    234: 
                    235:        if (root->root_hooks.hook_exec.fetch)
                    236:                ptr = root->root_hooks.hook_exec.fetch(root, NULL);
                    237:        else
                    238:                ptr = NULL;
                    239: 
                    240:        return ptr;
                    241: }
                    242: 
                    243: /*
                    244:  * schedCancel() - Cancel task from scheduler
                    245:  * @task = task
                    246:  * return: -1 error or 0 ok
                    247:  */
                    248: int
                    249: schedCancel(sched_task_t * __restrict task)
                    250: {
                    251:        sched_queue_t *queue;
                    252: 
                    253:        if (!task || !task->task_root)
                    254:                return -1;
                    255: 
                    256:        if (task->task_root->root_hooks.hook_exec.cancel)
                    257:                if (task->task_root->root_hooks.hook_exec.cancel(task, NULL))
                    258:                        return -1;
                    259: 
                    260:        switch (task->task_type) {
                    261:                case taskREAD:
                    262:                        queue = &task->task_root->root_read;
                    263:                        break;
                    264:                case taskWRITE:
                    265:                        queue = &task->task_root->root_write;
                    266:                        break;
                    267:                case taskTIMER:
                    268:                        queue = &task->task_root->root_timer;
                    269:                        break;
                    270:                case taskEVENT:
                    271:                        queue = &task->task_root->root_event;
                    272:                        break;
                    273:                case taskREADY:
                    274:                        queue = &task->task_root->root_ready;
                    275:                        break;
                    276:                default:
                    277:                        queue = NULL;
                    278:        }
                    279:        if (queue)
                    280:                TAILQ_REMOVE(queue, task, task_node);
1.4     ! misho     281:        if (task->task_type != taskUNUSE)
        !           282:                _sched_unuseTask(task);
1.1       misho     283: 
                    284:        return 0;
                    285: }
                    286: 
                    287: /*
                    288:  * schedCancelby() - Cancel task from scheduler by criteria
                    289:  * @root = root task
                    290:  * @queue = cancel from queue, if =NULL cancel same task from all queues
                    291:  * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
                    292:  * @param = search parameter
                    293:  * @hook = custom cleanup hook function, may be NULL
1.3       misho     294:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1       misho     295:  */
                    296: int
                    297: schedCancelby(sched_root_task_t * __restrict root, sched_queue_t * __restrict queue, 
                    298:                u_char criteria, void *param, sched_hook_func_t hook)
                    299: {
                    300:        sched_task_t *task;
                    301:        int flg = 0;
                    302: 
                    303:        if (!root)
                    304:                return -1;
                    305:        if (!queue) {
                    306:                if (schedCancelby(root, &root->root_read, criteria, param, hook))
                    307:                        return -2;
                    308:                if (schedCancelby(root, &root->root_write, criteria, param, hook))
                    309:                        return -2;
                    310:                if (schedCancelby(root, &root->root_timer, criteria, param, hook))
                    311:                        return -2;
                    312:                if (schedCancelby(root, &root->root_event, criteria, param, hook))
                    313:                        return -2;
                    314:                if (schedCancelby(root, &root->root_ready, criteria, param, hook))
                    315:                        return -2;
                    316:                if (schedCancelby(root, &root->root_read, criteria, param, hook))
                    317:                        return -2;
                    318:                return 0;
                    319:        }
                    320: 
                    321:        TAILQ_FOREACH(task, queue, task_node)
                    322:                if (criteria == CRITERIA_CALL) {
                    323:                        if (task->task_func == (sched_task_func_t) param) {
                    324:                                flg++;
                    325:                                break;
                    326:                        }
                    327:                } else if (criteria == CRITERIA_ARG) {
                    328:                        if (task->task_arg == param) {
                    329:                                flg++;
                    330:                                break;
                    331:                        }
                    332:                } else if (criteria == CRITERIA_FD) {
1.3       misho     333:                        if (TASK_FD(task) == (intptr_t) param) {
1.1       misho     334:                                flg++;
                    335:                                break;
                    336:                        }
                    337:                } else if (criteria == CRITERIA_VAL) {
                    338:                        if (TASK_VAL(task) == (u_long) param) {
                    339:                                flg++;
                    340:                                break;
                    341:                        }
                    342:                } else if (criteria == CRITERIA_TV) {
1.3       misho     343:                        if (!timercmp(&TASK_TV(task), (struct timeval*) param, -)) {
1.1       misho     344:                                flg++;
                    345:                                break;
                    346:                        }
                    347:                } else {
                    348:                        sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
                    349:                        return -1;
                    350:                }
                    351:        if (!flg || !task)      /* task not found */
                    352:                return 0;
                    353: 
                    354:        if (task->task_root->root_hooks.hook_exec.cancel)
                    355:                if (task->task_root->root_hooks.hook_exec.cancel(task, NULL))
                    356:                        return -1;
                    357:        if (hook)
                    358:                if (hook(task, NULL))
                    359:                        return -3;
                    360: 
                    361:        TAILQ_REMOVE(queue, task, task_node);
                    362: 
1.4     ! misho     363:        if (task->task_type != taskUNUSE)
        !           364:                _sched_unuseTask(task);
1.1       misho     365:        return 0;
                    366: }
                    367: 
                    368: /*
                    369:  * schedRun() - Scheduler *run loop*
                    370:  * @root = root task
1.2       misho     371:  * @killState = kill condition variable, if !=0 stop scheduler loop
1.1       misho     372:  * return: -1 error or 0 ok
                    373:  */
                    374: int
1.2       misho     375: schedRun(sched_root_task_t * __restrict root, volatile intptr_t * __restrict killState)
1.1       misho     376: {
                    377:        sched_task_t *task;
                    378: 
                    379:        if (!root)
                    380:                return -1;
                    381: 
                    382:        if (root->root_hooks.hook_exec.run)
                    383:                if (root->root_hooks.hook_exec.run(root, NULL))
                    384:                        return -1;
1.2       misho     385:        if (root->root_hooks.hook_exec.fetch) {
                    386:                if (killState)
1.3       misho     387:                        while (!*killState) {
                    388:                                if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
                    389:                                        schedCall(task);
                    390:                        }
1.2       misho     391:                else
                    392:                        while ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
                    393:                                schedCall(task);
                    394:        }
1.1       misho     395: 
                    396:        return 0;
                    397: }

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