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

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.10    ! misho       6: * $Id: aitsched.c,v 1.9.2.4 2012/05/31 21:51:29 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: 
1.6       misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.1       misho      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
1.6       misho      89:  *
1.2       misho      90:  * @root = root task
                     91:  * return: -1 error or 0 ok
                     92:  */
                     93: int
                     94: schedRegisterHooks(sched_root_task_t * __restrict root)
                     95: {
1.7       misho      96:        assert(root);
1.2       misho      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;
1.9       misho     104:        root->root_hooks.hook_add.alarm = sched_hook_alarm;
1.10    ! misho     105:        root->root_hooks.hook_add.node = sched_hook_node;
        !           106:        root->root_hooks.hook_add.proc = sched_hook_proc;
        !           107:        root->root_hooks.hook_add.signal = sched_hook_signal;
        !           108: #ifdef EVFILT_USER
        !           109:        root->root_hooks.hook_add.user = sched_hook_user;
        !           110: #endif
1.2       misho     111: 
                    112:        root->root_hooks.hook_exec.cancel = sched_hook_cancel;
                    113:        root->root_hooks.hook_exec.fetch = sched_hook_fetch;
1.3       misho     114:        root->root_hooks.hook_exec.exception = sched_hook_exception;
1.2       misho     115: 
                    116:        root->root_hooks.hook_root.init = sched_hook_init;
                    117:        root->root_hooks.hook_root.fini = sched_hook_fini;
                    118:        return 0;
                    119: }
                    120: 
                    121: /*
1.1       misho     122:  * schedInit() - Init scheduler
1.6       misho     123:  *
1.1       misho     124:  * @data = optional data if !=NULL
                    125:  * @datlen = data len if data is set
                    126:  * return: allocated root task if ok or NULL error
                    127:  */
                    128: sched_root_task_t *
                    129: schedInit(void ** __restrict data, size_t datlen)
                    130: {
                    131:        sched_root_task_t *root = NULL;
                    132:        int (*func)(sched_root_task_t *);
1.5       misho     133: #ifdef HAVE_LIBPTHREAD
                    134:        register int i;
                    135: #endif
1.1       misho     136: 
                    137:        root = malloc(sizeof(sched_root_task_t));
1.2       misho     138:        if (!root) {
                    139:                LOGERR;
                    140:        } else {
1.1       misho     141:                memset(root, 0, sizeof(sched_root_task_t));
1.5       misho     142: 
                    143:                /* INFINIT polling period by default */
                    144:                sched_timespecinf(&root->root_poll);
                    145: 
                    146: #ifdef HAVE_LIBPTHREAD
                    147:                for (i = 0; i < taskMAX; i++)
                    148:                        if (pthread_mutex_init(&root->root_mtx[i], NULL)) {
                    149:                                LOGERR;
                    150:                                while (i)
                    151:                                        pthread_mutex_destroy(&root->root_mtx[--i]);
                    152:                                free(root);
                    153:                                return NULL;
                    154:                        }
                    155: 
                    156:                for (i = 0; i < taskMAX; i++)
                    157:                        pthread_mutex_lock(&root->root_mtx[i]);
                    158: #endif
                    159: 
1.2       misho     160:                TAILQ_INIT(&root->root_read);
                    161:                TAILQ_INIT(&root->root_write);
1.10    ! misho     162:                TAILQ_INIT(&root->root_timer);
1.9       misho     163:                TAILQ_INIT(&root->root_alarm);
1.10    ! misho     164:                TAILQ_INIT(&root->root_node);
        !           165:                TAILQ_INIT(&root->root_proc);
        !           166:                TAILQ_INIT(&root->root_user);
        !           167:                TAILQ_INIT(&root->root_signal);
1.2       misho     168:                TAILQ_INIT(&root->root_event);
                    169:                TAILQ_INIT(&root->root_eventlo);
                    170:                TAILQ_INIT(&root->root_ready);
                    171:                TAILQ_INIT(&root->root_unuse);
1.1       misho     172: 
1.5       misho     173: #ifdef HAVE_LIBPTHREAD
                    174:                for (i = 0; i < taskMAX; i++)
                    175:                        pthread_mutex_unlock(&root->root_mtx[i]);
                    176: #endif
                    177: 
1.1       misho     178:                if (data && *data) {
                    179:                        if (datlen) {
                    180:                                root->root_data.iov_base = *data;
                    181:                                root->root_data.iov_len = datlen;
1.3       misho     182:                        } else { /* if datlen == 0, switch to callbacks init mode */
                    183:                                 /* little hack :) for correct initialization of scheduler */
1.2       misho     184:                                func = (int(*)(sched_root_task_t*)) data;
1.1       misho     185:                                func(root);
                    186:                        }
                    187:                }
1.2       misho     188: 
                    189:                if (root->root_hooks.hook_root.init)
                    190:                        root->root_hooks.hook_root.init(root, NULL);
1.1       misho     191:        }
                    192: 
                    193:        return root;
                    194: }
                    195: 
                    196: /*
                    197:  * schedEnd() - End scheduler & free all resources
1.6       misho     198:  *
1.1       misho     199:  * @root = root task
                    200:  * return: -1 error or 0 ok
                    201:  */
                    202: int
1.2       misho     203: schedEnd(sched_root_task_t ** __restrict root)
1.1       misho     204: {
1.7       misho     205:        sched_task_t *task, *tmp;
1.5       misho     206: #ifdef HAVE_LIBPTHREAD
                    207:        register int i;
                    208: #endif
1.1       misho     209: 
1.2       misho     210:        if (!root || !*root)
1.1       misho     211:                return -1;
                    212: 
1.10    ! misho     213:        TAILQ_FOREACH_SAFE(task, &(*root)->root_read, task_node, tmp)
        !           214:                schedCancel(task);
        !           215:        TAILQ_FOREACH_SAFE(task, &(*root)->root_write, task_node, tmp)
1.1       misho     216:                schedCancel(task);
1.10    ! misho     217:        TAILQ_FOREACH_SAFE(task, &(*root)->root_timer, task_node, tmp)
        !           218:                schedCancel(task);
        !           219:        TAILQ_FOREACH_SAFE(task, &(*root)->root_alarm, task_node, tmp)
        !           220:                schedCancel(task);
        !           221:        TAILQ_FOREACH_SAFE(task, &(*root)->root_node, task_node, tmp)
        !           222:                schedCancel(task);
        !           223:        TAILQ_FOREACH_SAFE(task, &(*root)->root_proc, task_node, tmp)
1.1       misho     224:                schedCancel(task);
1.10    ! misho     225:        TAILQ_FOREACH_SAFE(task, &(*root)->root_user, task_node, tmp)
1.9       misho     226:                schedCancel(task);
1.10    ! misho     227:        TAILQ_FOREACH_SAFE(task, &(*root)->root_signal, task_node, tmp)
1.1       misho     228:                schedCancel(task);
1.10    ! misho     229:        TAILQ_FOREACH_SAFE(task, &(*root)->root_event, task_node, tmp)
1.1       misho     230:                schedCancel(task);
1.10    ! misho     231:        TAILQ_FOREACH_SAFE(task, &(*root)->root_eventlo, task_node, tmp)
1.5       misho     232:                schedCancel(task);
1.10    ! misho     233:        TAILQ_FOREACH_SAFE(task, &(*root)->root_ready, task_node, tmp)
1.1       misho     234:                schedCancel(task);
                    235: 
1.5       misho     236: #ifdef HAVE_LIBPTHREAD
                    237:        pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
                    238: #endif
1.10    ! misho     239:        TAILQ_FOREACH_SAFE(task, &(*root)->root_unuse, task_node, tmp) {
1.2       misho     240:                TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
1.1       misho     241:                free(task);
                    242:        }
1.5       misho     243: #ifdef HAVE_LIBPTHREAD
                    244:        pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
                    245: #endif
1.1       misho     246: 
1.2       misho     247:        if ((*root)->root_hooks.hook_root.fini)
                    248:                (*root)->root_hooks.hook_root.fini(*root, NULL);
1.1       misho     249: 
1.5       misho     250: #ifdef HAVE_LIBPTHREAD
                    251:        for (i = 0; i < taskMAX; i++)
                    252:                pthread_mutex_destroy(&(*root)->root_mtx[i]);
                    253: #endif
                    254: 
1.2       misho     255:        free(*root);
                    256:        *root = NULL;
1.1       misho     257:        return 0;
                    258: }
                    259: 
                    260: /*
                    261:  * schedCall() - Call task execution function
1.6       misho     262:  *
1.1       misho     263:  * @task = current task
                    264:  * return: !=NULL error or =NULL ok
                    265:  */
                    266: inline void *
                    267: schedCall(sched_task_t * __restrict task)
                    268: {
1.4       misho     269:        void *ptr = (void*) -1;
                    270: 
1.1       misho     271:        if (!task)
1.4       misho     272:                return ptr;
                    273: 
                    274:        if (!TASK_ISLOCKED(task))
                    275:                TASK_LOCK(task);
1.1       misho     276: 
1.4       misho     277:        ptr = task->task_func(task);
                    278: 
                    279:        TASK_UNLOCK(task);
                    280:        return ptr;
1.1       misho     281: }
                    282: 
                    283: /*
                    284:  * schedFetch() - Fetch ready task
1.6       misho     285:  *
1.1       misho     286:  * @root = root task
                    287:  * return: =NULL error or !=NULL ready task
                    288:  */
                    289: inline void *
                    290: schedFetch(sched_root_task_t * __restrict root)
                    291: {
                    292:        void *ptr;
                    293: 
                    294:        if (!root)
                    295:                return NULL;
                    296: 
                    297:        if (root->root_hooks.hook_exec.fetch)
                    298:                ptr = root->root_hooks.hook_exec.fetch(root, NULL);
                    299:        else
                    300:                ptr = NULL;
                    301: 
                    302:        return ptr;
                    303: }
                    304: 
                    305: /*
1.10    ! misho     306:  * schedTrigger() - Triggering USER task
        !           307:  *
        !           308:  * @task = task
        !           309:  * return: -1 error or 0 ok
        !           310:  */
        !           311: int
        !           312: schedTrigger(sched_task_t * __restrict task)
        !           313: {
        !           314: #ifndef EVFILT_USER
        !           315:        sched_SetErr(ENOTSUP, "Not supported kevent() filter");
        !           316:        return -1;
        !           317: #else
        !           318:        struct kevent chg[1];
        !           319:        struct timespec timeout = { 0, 0 };
        !           320: 
        !           321:        if (!task || !TASK_ROOT(task))
        !           322:                return -1;
        !           323: 
        !           324: #ifdef __NetBSD__
        !           325:        EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (intptr_t) TASK_VAL(task));
        !           326: #else
        !           327:        EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (void*) TASK_VAL(task));
        !           328: #endif
        !           329:        if (kevent(TASK_ROOT(task)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
        !           330:                LOGERR;
        !           331:                return -1;
        !           332:        }
        !           333: 
        !           334:        return 0;
        !           335: #endif
        !           336: }
        !           337: 
        !           338: /*
1.1       misho     339:  * schedCancel() - Cancel task from scheduler
1.6       misho     340:  *
1.1       misho     341:  * @task = task
                    342:  * return: -1 error or 0 ok
                    343:  */
                    344: int
                    345: schedCancel(sched_task_t * __restrict task)
                    346: {
                    347:        sched_queue_t *queue;
                    348: 
1.5       misho     349:        if (!task || !TASK_ROOT(task))
1.1       misho     350:                return -1;
                    351: 
1.5       misho     352:        if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
                    353:                if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1       misho     354:                        return -1;
                    355: 
1.5       misho     356:        switch (TASK_TYPE(task)) {
1.1       misho     357:                case taskREAD:
1.5       misho     358:                        queue = &TASK_ROOT(task)->root_read;
1.1       misho     359:                        break;
                    360:                case taskWRITE:
1.5       misho     361:                        queue = &TASK_ROOT(task)->root_write;
1.1       misho     362:                        break;
1.10    ! misho     363:                case taskTIMER:
        !           364:                        queue = &TASK_ROOT(task)->root_timer;
        !           365:                        break;
1.9       misho     366:                case taskALARM:
                    367:                        queue = &TASK_ROOT(task)->root_alarm;
                    368:                        break;
1.10    ! misho     369:                case taskNODE:
        !           370:                        queue = &TASK_ROOT(task)->root_node;
        !           371:                        break;
        !           372:                case taskPROC:
        !           373:                        queue = &TASK_ROOT(task)->root_proc;
        !           374:                        break;
        !           375:                case taskUSER:
        !           376:                        queue = &TASK_ROOT(task)->root_user;
        !           377:                        break;
        !           378:                case taskSIGNAL:
        !           379:                        queue = &TASK_ROOT(task)->root_signal;
1.1       misho     380:                        break;
                    381:                case taskEVENT:
1.5       misho     382:                        queue = &TASK_ROOT(task)->root_event;
                    383:                        break;
                    384:                case taskEVENTLO:
                    385:                        queue = &TASK_ROOT(task)->root_eventlo;
1.1       misho     386:                        break;
                    387:                case taskREADY:
1.5       misho     388:                        queue = &TASK_ROOT(task)->root_ready;
1.1       misho     389:                        break;
                    390:                default:
                    391:                        queue = NULL;
                    392:        }
1.5       misho     393:        if (queue) {
                    394: #ifdef HAVE_LIBPTHREAD
                    395:                pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
                    396: #endif
1.10    ! misho     397:                TAILQ_REMOVE(queue, TASK_ID(task), task_node);
1.5       misho     398: #ifdef HAVE_LIBPTHREAD
                    399:                pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
                    400: #endif
                    401:        }
                    402:        if (TASK_TYPE(task) != taskUNUSE)
1.4       misho     403:                _sched_unuseTask(task);
1.1       misho     404: 
                    405:        return 0;
                    406: }
                    407: 
                    408: /*
                    409:  * schedCancelby() - Cancel task from scheduler by criteria
1.6       misho     410:  *
1.1       misho     411:  * @root = root task
1.5       misho     412:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.10    ! misho     413:  * @criteria = find task by criteria 
        !           414:  *     [CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TS|CRITERIA_DATA]
1.1       misho     415:  * @param = search parameter
                    416:  * @hook = custom cleanup hook function, may be NULL
1.3       misho     417:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1       misho     418:  */
                    419: int
1.5       misho     420: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
1.1       misho     421:                u_char criteria, void *param, sched_hook_func_t hook)
                    422: {
1.8       misho     423:        sched_task_t *task, *tmp;
1.5       misho     424:        sched_queue_t *queue;
1.8       misho     425:        register int flg = 0;
1.1       misho     426: 
                    427:        if (!root)
                    428:                return -1;
1.10    ! misho     429:        /* if type == taskMAX check in all queues */
1.5       misho     430:        if (type == taskMAX) {
                    431:                if (schedCancelby(root, taskREAD, criteria, param, hook))
1.1       misho     432:                        return -2;
1.5       misho     433:                if (schedCancelby(root, taskWRITE, criteria, param, hook))
1.1       misho     434:                        return -2;
1.10    ! misho     435:                if (schedCancelby(root, taskTIMER, criteria, param, hook))
        !           436:                        return -2;
1.9       misho     437:                if (schedCancelby(root, taskALARM, criteria, param, hook))
                    438:                        return -2;
1.10    ! misho     439:                if (schedCancelby(root, taskNODE, criteria, param, hook))
        !           440:                        return -2;
        !           441:                if (schedCancelby(root, taskPROC, criteria, param, hook))
        !           442:                        return -2;
        !           443:                if (schedCancelby(root, taskUSER, criteria, param, hook))
        !           444:                        return -2;
        !           445:                if (schedCancelby(root, taskSIGNAL, criteria, param, hook))
1.1       misho     446:                        return -2;
1.5       misho     447:                if (schedCancelby(root, taskEVENT, criteria, param, hook))
1.1       misho     448:                        return -2;
1.5       misho     449:                if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
1.1       misho     450:                        return -2;
1.5       misho     451:                if (schedCancelby(root, taskREADY, criteria, param, hook))
1.1       misho     452:                        return -2;
                    453:                return 0;
                    454:        }
1.10    ! misho     455:        /* choosen queue */
1.5       misho     456:        switch (type) {
                    457:                case taskREAD:
                    458:                        queue = &root->root_read;
                    459:                        break;
                    460:                case taskWRITE:
                    461:                        queue = &root->root_write;
                    462:                        break;
1.10    ! misho     463:                case taskTIMER:
        !           464:                        queue = &root->root_timer;
        !           465:                        break;
1.9       misho     466:                case taskALARM:
                    467:                        queue = &root->root_alarm;
                    468:                        break;
1.10    ! misho     469:                case taskNODE:
        !           470:                        queue = &root->root_node;
        !           471:                        break;
        !           472:                case taskPROC:
        !           473:                        queue = &root->root_proc;
        !           474:                        break;
        !           475:                case taskUSER:
        !           476:                        queue = &root->root_user;
        !           477:                        break;
        !           478:                case taskSIGNAL:
        !           479:                        queue = &root->root_signal;
1.5       misho     480:                        break;
                    481:                case taskEVENT:
                    482:                        queue = &root->root_event;
                    483:                        break;
                    484:                case taskEVENTLO:
                    485:                        queue = &root->root_eventlo;
                    486:                        break;
                    487:                case taskREADY:
                    488:                        queue = &root->root_ready;
                    489:                        break;
                    490:                default:
                    491:                        return 0;
                    492:        }
1.1       misho     493: 
1.5       misho     494: #ifdef HAVE_LIBPTHREAD
                    495:        pthread_mutex_lock(&root->root_mtx[type]);
                    496: #endif
1.8       misho     497:        TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
                    498:                flg ^= flg;
                    499:                switch (criteria) {
1.10    ! misho     500:                        case CRITERIA_ANY:
        !           501:                                flg = 1;
        !           502:                                break;
1.8       misho     503:                        case CRITERIA_CALL:
                    504:                                if (TASK_FUNC(task) == (sched_task_func_t) param)
                    505:                                        flg = 1;
1.1       misho     506:                                break;
1.8       misho     507:                        case CRITERIA_ARG:
                    508:                                if (TASK_ARG(task) == param)
                    509:                                        flg = 1;
1.1       misho     510:                                break;
1.8       misho     511:                        case CRITERIA_FD:
                    512:                                if (TASK_FD(task) == (intptr_t) param)
                    513:                                        flg = 1;
1.1       misho     514:                                break;
1.8       misho     515:                        case CRITERIA_VAL:
                    516:                                if (TASK_VAL(task) == (u_long) param)
                    517:                                        flg = 1;
1.1       misho     518:                                break;
1.8       misho     519:                        case CRITERIA_TS:
                    520:                                if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
                    521:                                        flg = 1;
1.1       misho     522:                                break;
1.10    ! misho     523:                        case CRITERIA_DATA:
        !           524:                                if (TASK_DATA(task) == param)
        !           525:                                        flg = 1;
        !           526:                                break;
1.8       misho     527:                        default:
                    528:                                sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
                    529:                                flg = -1;
                    530:                }
1.10    ! misho     531:                if (flg < 0)            /* error */
1.8       misho     532:                        break;
                    533:                /* cancel choosen task */
                    534:                if (flg > 0) {
                    535:                        if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
                    536:                                if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
                    537:                                        flg = -1;
                    538:                                        break;
                    539:                                }
                    540:                        /* custom hook */
                    541:                        if (hook)
                    542:                                if (hook(task, NULL)) {
                    543:                                        flg = -3;
                    544:                                        break;
                    545:                                }
                    546: 
                    547:                        TAILQ_REMOVE(queue, task, task_node);
                    548:                        if (TASK_TYPE(task) != taskUNUSE)
                    549:                                _sched_unuseTask(task);
                    550: 
                    551:                        flg ^= flg;     /* ok */
1.1       misho     552:                }
1.8       misho     553:        }
1.5       misho     554: #ifdef HAVE_LIBPTHREAD
                    555:        pthread_mutex_unlock(&root->root_mtx[type]);
                    556: #endif
1.8       misho     557:        return flg;
1.1       misho     558: }
                    559: 
                    560: /*
                    561:  * schedRun() - Scheduler *run loop*
1.6       misho     562:  *
1.1       misho     563:  * @root = root task
1.2       misho     564:  * @killState = kill condition variable, if !=0 stop scheduler loop
1.1       misho     565:  * return: -1 error or 0 ok
                    566:  */
                    567: int
1.7       misho     568: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
1.1       misho     569: {
                    570:        sched_task_t *task;
                    571: 
                    572:        if (!root)
                    573:                return -1;
                    574: 
                    575:        if (root->root_hooks.hook_exec.run)
                    576:                if (root->root_hooks.hook_exec.run(root, NULL))
                    577:                        return -1;
1.7       misho     578: 
                    579:        if (killState) {
                    580:                if (root->root_hooks.hook_exec.condition)
                    581:                        /* condition scheduler loop */
                    582:                        while (root && root->root_hooks.hook_exec.fetch && 
                    583:                                        root->root_hooks.hook_exec.condition && 
                    584:                                        root->root_hooks.hook_exec.condition(root, (void*) killState)) {
                    585:                                if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
                    586:                                        schedCall(task);
                    587:                        }
                    588:                else
                    589:                        /* trigger scheduler loop */
                    590:                        while (!*killState && root && root->root_hooks.hook_exec.fetch) {
                    591:                                if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
                    592:                                        schedCall(task);
                    593:                        }
                    594:        } else
                    595:                /* infinite scheduler loop */
                    596:                while (root && root->root_hooks.hook_exec.fetch)
                    597:                        if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.2       misho     598:                                schedCall(task);
1.1       misho     599: 
                    600:        return 0;
                    601: }
1.5       misho     602: 
                    603: /*
                    604:  * schedPolling() - Polling timeout period if no timer task is present
1.6       misho     605:  *
1.5       misho     606:  * @root = root task
                    607:  * @ts = timeout polling period, if ==NULL INFINIT timeout
                    608:  * @tsold = old timeout polling if !=NULL
                    609:  * return: -1 error or 0 ok
                    610:  */
                    611: inline int
                    612: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts, 
                    613:                struct timespec * __restrict tsold)
                    614: {
                    615:        if (!root)
                    616:                return -1;
                    617: 
                    618:        if (tsold)
                    619:                *tsold = root->root_poll;
                    620: 
                    621:        if (!ts)
                    622:                sched_timespecinf(&root->root_poll);
                    623:        else
                    624:                root->root_poll = *ts;
                    625: 
                    626:        return 0;
                    627: }
1.6       misho     628: 
                    629: /*
                    630:  * schedTermCondition() - Activate hook for scheduler condition kill
                    631:  *
                    632:  * @root = root task
                    633:  * @condValue = condition value, kill schedRun() if condValue == killState
                    634:  * return: -1 error ok 0 ok
                    635:  */
                    636: inline int
                    637: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
                    638: {
                    639:        if (!root)
                    640:                return -1;
                    641: 
                    642:        root->root_cond = condValue;
                    643:        root->root_hooks.hook_exec.condition = sched_hook_condition;
                    644:        return 0;
                    645: }

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