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

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

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