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

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.6.1! misho       6: * $Id: aitsched.c,v 1.10 2012/05/31 22:31:48 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);
1.10.6.1! misho     170:                TAILQ_INIT(&root->root_suspend);
1.2       misho     171:                TAILQ_INIT(&root->root_ready);
                    172:                TAILQ_INIT(&root->root_unuse);
1.1       misho     173: 
1.5       misho     174: #ifdef HAVE_LIBPTHREAD
                    175:                for (i = 0; i < taskMAX; i++)
                    176:                        pthread_mutex_unlock(&root->root_mtx[i]);
                    177: #endif
                    178: 
1.1       misho     179:                if (data && *data) {
                    180:                        if (datlen) {
                    181:                                root->root_data.iov_base = *data;
                    182:                                root->root_data.iov_len = datlen;
1.3       misho     183:                        } else { /* if datlen == 0, switch to callbacks init mode */
                    184:                                 /* little hack :) for correct initialization of scheduler */
1.2       misho     185:                                func = (int(*)(sched_root_task_t*)) data;
1.1       misho     186:                                func(root);
                    187:                        }
                    188:                }
1.2       misho     189: 
                    190:                if (root->root_hooks.hook_root.init)
                    191:                        root->root_hooks.hook_root.init(root, NULL);
1.1       misho     192:        }
                    193: 
                    194:        return root;
                    195: }
                    196: 
                    197: /*
                    198:  * schedEnd() - End scheduler & free all resources
1.6       misho     199:  *
1.1       misho     200:  * @root = root task
                    201:  * return: -1 error or 0 ok
                    202:  */
                    203: int
1.2       misho     204: schedEnd(sched_root_task_t ** __restrict root)
1.1       misho     205: {
1.7       misho     206:        sched_task_t *task, *tmp;
1.5       misho     207: #ifdef HAVE_LIBPTHREAD
                    208:        register int i;
                    209: #endif
1.1       misho     210: 
1.2       misho     211:        if (!root || !*root)
1.1       misho     212:                return -1;
                    213: 
1.10      misho     214:        TAILQ_FOREACH_SAFE(task, &(*root)->root_read, task_node, tmp)
                    215:                schedCancel(task);
                    216:        TAILQ_FOREACH_SAFE(task, &(*root)->root_write, task_node, tmp)
1.1       misho     217:                schedCancel(task);
1.10      misho     218:        TAILQ_FOREACH_SAFE(task, &(*root)->root_timer, task_node, tmp)
                    219:                schedCancel(task);
                    220:        TAILQ_FOREACH_SAFE(task, &(*root)->root_alarm, task_node, tmp)
                    221:                schedCancel(task);
                    222:        TAILQ_FOREACH_SAFE(task, &(*root)->root_node, task_node, tmp)
                    223:                schedCancel(task);
                    224:        TAILQ_FOREACH_SAFE(task, &(*root)->root_proc, task_node, tmp)
1.1       misho     225:                schedCancel(task);
1.10      misho     226:        TAILQ_FOREACH_SAFE(task, &(*root)->root_user, task_node, tmp)
1.9       misho     227:                schedCancel(task);
1.10      misho     228:        TAILQ_FOREACH_SAFE(task, &(*root)->root_signal, task_node, tmp)
1.1       misho     229:                schedCancel(task);
1.10      misho     230:        TAILQ_FOREACH_SAFE(task, &(*root)->root_event, task_node, tmp)
1.1       misho     231:                schedCancel(task);
1.10      misho     232:        TAILQ_FOREACH_SAFE(task, &(*root)->root_eventlo, task_node, tmp)
1.5       misho     233:                schedCancel(task);
1.10.6.1! misho     234:        TAILQ_FOREACH_SAFE(task, &(*root)->root_suspend, task_node, tmp)
        !           235:                schedCancel(task);
1.10      misho     236:        TAILQ_FOREACH_SAFE(task, &(*root)->root_ready, task_node, tmp)
1.1       misho     237:                schedCancel(task);
                    238: 
1.5       misho     239: #ifdef HAVE_LIBPTHREAD
                    240:        pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
                    241: #endif
1.10      misho     242:        TAILQ_FOREACH_SAFE(task, &(*root)->root_unuse, task_node, tmp) {
1.2       misho     243:                TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
1.1       misho     244:                free(task);
                    245:        }
1.5       misho     246: #ifdef HAVE_LIBPTHREAD
                    247:        pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
                    248: #endif
1.1       misho     249: 
1.2       misho     250:        if ((*root)->root_hooks.hook_root.fini)
                    251:                (*root)->root_hooks.hook_root.fini(*root, NULL);
1.1       misho     252: 
1.5       misho     253: #ifdef HAVE_LIBPTHREAD
                    254:        for (i = 0; i < taskMAX; i++)
                    255:                pthread_mutex_destroy(&(*root)->root_mtx[i]);
                    256: #endif
                    257: 
1.2       misho     258:        free(*root);
                    259:        *root = NULL;
1.1       misho     260:        return 0;
                    261: }
                    262: 
                    263: /*
                    264:  * schedCall() - Call task execution function
1.6       misho     265:  *
1.1       misho     266:  * @task = current task
                    267:  * return: !=NULL error or =NULL ok
                    268:  */
                    269: inline void *
                    270: schedCall(sched_task_t * __restrict task)
                    271: {
1.4       misho     272:        void *ptr = (void*) -1;
                    273: 
1.1       misho     274:        if (!task)
1.4       misho     275:                return ptr;
                    276: 
                    277:        if (!TASK_ISLOCKED(task))
                    278:                TASK_LOCK(task);
1.1       misho     279: 
1.4       misho     280:        ptr = task->task_func(task);
                    281: 
                    282:        TASK_UNLOCK(task);
                    283:        return ptr;
1.1       misho     284: }
                    285: 
                    286: /*
                    287:  * schedFetch() - Fetch ready task
1.6       misho     288:  *
1.1       misho     289:  * @root = root task
                    290:  * return: =NULL error or !=NULL ready task
                    291:  */
                    292: inline void *
                    293: schedFetch(sched_root_task_t * __restrict root)
                    294: {
                    295:        void *ptr;
                    296: 
                    297:        if (!root)
                    298:                return NULL;
                    299: 
                    300:        if (root->root_hooks.hook_exec.fetch)
                    301:                ptr = root->root_hooks.hook_exec.fetch(root, NULL);
                    302:        else
                    303:                ptr = NULL;
                    304: 
                    305:        return ptr;
                    306: }
                    307: 
                    308: /*
1.10      misho     309:  * schedTrigger() - Triggering USER task
                    310:  *
                    311:  * @task = task
                    312:  * return: -1 error or 0 ok
                    313:  */
                    314: int
                    315: schedTrigger(sched_task_t * __restrict task)
                    316: {
                    317: #ifndef EVFILT_USER
                    318:        sched_SetErr(ENOTSUP, "Not supported kevent() filter");
                    319:        return -1;
                    320: #else
                    321:        struct kevent chg[1];
                    322:        struct timespec timeout = { 0, 0 };
                    323: 
                    324:        if (!task || !TASK_ROOT(task))
                    325:                return -1;
                    326: 
                    327: #ifdef __NetBSD__
                    328:        EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (intptr_t) TASK_VAL(task));
                    329: #else
                    330:        EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (void*) TASK_VAL(task));
                    331: #endif
                    332:        if (kevent(TASK_ROOT(task)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
                    333:                LOGERR;
                    334:                return -1;
                    335:        }
                    336: 
                    337:        return 0;
                    338: #endif
                    339: }
                    340: 
                    341: /*
1.1       misho     342:  * schedCancel() - Cancel task from scheduler
1.6       misho     343:  *
1.1       misho     344:  * @task = task
                    345:  * return: -1 error or 0 ok
                    346:  */
                    347: int
                    348: schedCancel(sched_task_t * __restrict task)
                    349: {
                    350:        sched_queue_t *queue;
                    351: 
1.5       misho     352:        if (!task || !TASK_ROOT(task))
1.1       misho     353:                return -1;
                    354: 
1.5       misho     355:        if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
                    356:                if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1       misho     357:                        return -1;
                    358: 
1.5       misho     359:        switch (TASK_TYPE(task)) {
1.1       misho     360:                case taskREAD:
1.5       misho     361:                        queue = &TASK_ROOT(task)->root_read;
1.1       misho     362:                        break;
                    363:                case taskWRITE:
1.5       misho     364:                        queue = &TASK_ROOT(task)->root_write;
1.1       misho     365:                        break;
1.10      misho     366:                case taskTIMER:
                    367:                        queue = &TASK_ROOT(task)->root_timer;
                    368:                        break;
1.9       misho     369:                case taskALARM:
                    370:                        queue = &TASK_ROOT(task)->root_alarm;
                    371:                        break;
1.10      misho     372:                case taskNODE:
                    373:                        queue = &TASK_ROOT(task)->root_node;
                    374:                        break;
                    375:                case taskPROC:
                    376:                        queue = &TASK_ROOT(task)->root_proc;
                    377:                        break;
                    378:                case taskUSER:
                    379:                        queue = &TASK_ROOT(task)->root_user;
                    380:                        break;
                    381:                case taskSIGNAL:
                    382:                        queue = &TASK_ROOT(task)->root_signal;
1.1       misho     383:                        break;
                    384:                case taskEVENT:
1.5       misho     385:                        queue = &TASK_ROOT(task)->root_event;
                    386:                        break;
                    387:                case taskEVENTLO:
                    388:                        queue = &TASK_ROOT(task)->root_eventlo;
1.1       misho     389:                        break;
1.10.6.1! misho     390:                case taskSUSPEND:
        !           391:                        queue = &TASK_ROOT(task)->root_suspend;
        !           392:                        break;
1.1       misho     393:                case taskREADY:
1.5       misho     394:                        queue = &TASK_ROOT(task)->root_ready;
1.1       misho     395:                        break;
                    396:                default:
                    397:                        queue = NULL;
                    398:        }
1.5       misho     399:        if (queue) {
                    400: #ifdef HAVE_LIBPTHREAD
                    401:                pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
                    402: #endif
1.10      misho     403:                TAILQ_REMOVE(queue, TASK_ID(task), task_node);
1.5       misho     404: #ifdef HAVE_LIBPTHREAD
                    405:                pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
                    406: #endif
                    407:        }
                    408:        if (TASK_TYPE(task) != taskUNUSE)
1.4       misho     409:                _sched_unuseTask(task);
1.1       misho     410: 
                    411:        return 0;
                    412: }
                    413: 
                    414: /*
                    415:  * schedCancelby() - Cancel task from scheduler by criteria
1.6       misho     416:  *
1.1       misho     417:  * @root = root task
1.5       misho     418:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.10      misho     419:  * @criteria = find task by criteria 
1.10.6.1! misho     420:  *     [CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA]
1.1       misho     421:  * @param = search parameter
                    422:  * @hook = custom cleanup hook function, may be NULL
1.3       misho     423:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1       misho     424:  */
                    425: int
1.5       misho     426: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
1.1       misho     427:                u_char criteria, void *param, sched_hook_func_t hook)
                    428: {
1.8       misho     429:        sched_task_t *task, *tmp;
1.5       misho     430:        sched_queue_t *queue;
1.8       misho     431:        register int flg = 0;
1.1       misho     432: 
                    433:        if (!root)
                    434:                return -1;
1.10      misho     435:        /* if type == taskMAX check in all queues */
1.5       misho     436:        if (type == taskMAX) {
                    437:                if (schedCancelby(root, taskREAD, criteria, param, hook))
1.1       misho     438:                        return -2;
1.5       misho     439:                if (schedCancelby(root, taskWRITE, criteria, param, hook))
1.1       misho     440:                        return -2;
1.10      misho     441:                if (schedCancelby(root, taskTIMER, criteria, param, hook))
                    442:                        return -2;
1.9       misho     443:                if (schedCancelby(root, taskALARM, criteria, param, hook))
                    444:                        return -2;
1.10      misho     445:                if (schedCancelby(root, taskNODE, criteria, param, hook))
                    446:                        return -2;
                    447:                if (schedCancelby(root, taskPROC, criteria, param, hook))
                    448:                        return -2;
                    449:                if (schedCancelby(root, taskUSER, criteria, param, hook))
                    450:                        return -2;
                    451:                if (schedCancelby(root, taskSIGNAL, criteria, param, hook))
1.1       misho     452:                        return -2;
1.5       misho     453:                if (schedCancelby(root, taskEVENT, criteria, param, hook))
1.1       misho     454:                        return -2;
1.5       misho     455:                if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
1.1       misho     456:                        return -2;
1.10.6.1! misho     457:                if (schedCancelby(root, taskSUSPEND, criteria, param, hook))
        !           458:                        return -2;
1.5       misho     459:                if (schedCancelby(root, taskREADY, criteria, param, hook))
1.1       misho     460:                        return -2;
                    461:                return 0;
                    462:        }
1.10      misho     463:        /* choosen queue */
1.5       misho     464:        switch (type) {
                    465:                case taskREAD:
                    466:                        queue = &root->root_read;
                    467:                        break;
                    468:                case taskWRITE:
                    469:                        queue = &root->root_write;
                    470:                        break;
1.10      misho     471:                case taskTIMER:
                    472:                        queue = &root->root_timer;
                    473:                        break;
1.9       misho     474:                case taskALARM:
                    475:                        queue = &root->root_alarm;
                    476:                        break;
1.10      misho     477:                case taskNODE:
                    478:                        queue = &root->root_node;
                    479:                        break;
                    480:                case taskPROC:
                    481:                        queue = &root->root_proc;
                    482:                        break;
                    483:                case taskUSER:
                    484:                        queue = &root->root_user;
                    485:                        break;
                    486:                case taskSIGNAL:
                    487:                        queue = &root->root_signal;
1.5       misho     488:                        break;
                    489:                case taskEVENT:
                    490:                        queue = &root->root_event;
                    491:                        break;
                    492:                case taskEVENTLO:
                    493:                        queue = &root->root_eventlo;
                    494:                        break;
1.10.6.1! misho     495:                case taskSUSPEND:
        !           496:                        queue = &root->root_suspend;
        !           497:                        break;
1.5       misho     498:                case taskREADY:
                    499:                        queue = &root->root_ready;
                    500:                        break;
                    501:                default:
                    502:                        return 0;
                    503:        }
1.1       misho     504: 
1.5       misho     505: #ifdef HAVE_LIBPTHREAD
                    506:        pthread_mutex_lock(&root->root_mtx[type]);
                    507: #endif
1.8       misho     508:        TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
                    509:                flg ^= flg;
                    510:                switch (criteria) {
1.10      misho     511:                        case CRITERIA_ANY:
                    512:                                flg = 1;
                    513:                                break;
1.8       misho     514:                        case CRITERIA_CALL:
                    515:                                if (TASK_FUNC(task) == (sched_task_func_t) param)
                    516:                                        flg = 1;
1.1       misho     517:                                break;
1.8       misho     518:                        case CRITERIA_ARG:
                    519:                                if (TASK_ARG(task) == param)
                    520:                                        flg = 1;
1.1       misho     521:                                break;
1.8       misho     522:                        case CRITERIA_FD:
                    523:                                if (TASK_FD(task) == (intptr_t) param)
                    524:                                        flg = 1;
1.1       misho     525:                                break;
1.10.6.1! misho     526:                        case CRITERIA_ID:
1.8       misho     527:                        case CRITERIA_VAL:
                    528:                                if (TASK_VAL(task) == (u_long) param)
                    529:                                        flg = 1;
1.1       misho     530:                                break;
1.8       misho     531:                        case CRITERIA_TS:
                    532:                                if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
                    533:                                        flg = 1;
1.1       misho     534:                                break;
1.10      misho     535:                        case CRITERIA_DATA:
                    536:                                if (TASK_DATA(task) == param)
                    537:                                        flg = 1;
                    538:                                break;
1.8       misho     539:                        default:
                    540:                                sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
                    541:                                flg = -1;
                    542:                }
1.10      misho     543:                if (flg < 0)            /* error */
1.8       misho     544:                        break;
                    545:                /* cancel choosen task */
                    546:                if (flg > 0) {
                    547:                        if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
                    548:                                if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
                    549:                                        flg = -1;
                    550:                                        break;
                    551:                                }
                    552:                        /* custom hook */
                    553:                        if (hook)
                    554:                                if (hook(task, NULL)) {
                    555:                                        flg = -3;
                    556:                                        break;
                    557:                                }
                    558: 
                    559:                        TAILQ_REMOVE(queue, task, task_node);
                    560:                        if (TASK_TYPE(task) != taskUNUSE)
                    561:                                _sched_unuseTask(task);
                    562: 
                    563:                        flg ^= flg;     /* ok */
1.1       misho     564:                }
1.8       misho     565:        }
1.5       misho     566: #ifdef HAVE_LIBPTHREAD
                    567:        pthread_mutex_unlock(&root->root_mtx[type]);
                    568: #endif
1.8       misho     569:        return flg;
1.1       misho     570: }
                    571: 
                    572: /*
                    573:  * schedRun() - Scheduler *run loop*
1.6       misho     574:  *
1.1       misho     575:  * @root = root task
1.2       misho     576:  * @killState = kill condition variable, if !=0 stop scheduler loop
1.1       misho     577:  * return: -1 error or 0 ok
                    578:  */
                    579: int
1.7       misho     580: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
1.1       misho     581: {
                    582:        sched_task_t *task;
                    583: 
                    584:        if (!root)
                    585:                return -1;
                    586: 
                    587:        if (root->root_hooks.hook_exec.run)
                    588:                if (root->root_hooks.hook_exec.run(root, NULL))
                    589:                        return -1;
1.7       misho     590: 
                    591:        if (killState) {
                    592:                if (root->root_hooks.hook_exec.condition)
                    593:                        /* condition scheduler loop */
                    594:                        while (root && root->root_hooks.hook_exec.fetch && 
                    595:                                        root->root_hooks.hook_exec.condition && 
                    596:                                        root->root_hooks.hook_exec.condition(root, (void*) killState)) {
                    597:                                if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
                    598:                                        schedCall(task);
                    599:                        }
                    600:                else
                    601:                        /* trigger scheduler loop */
                    602:                        while (!*killState && root && root->root_hooks.hook_exec.fetch) {
                    603:                                if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
                    604:                                        schedCall(task);
                    605:                        }
                    606:        } else
                    607:                /* infinite scheduler loop */
                    608:                while (root && root->root_hooks.hook_exec.fetch)
                    609:                        if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.2       misho     610:                                schedCall(task);
1.1       misho     611: 
                    612:        return 0;
                    613: }
1.5       misho     614: 
                    615: /*
                    616:  * schedPolling() - Polling timeout period if no timer task is present
1.6       misho     617:  *
1.5       misho     618:  * @root = root task
                    619:  * @ts = timeout polling period, if ==NULL INFINIT timeout
                    620:  * @tsold = old timeout polling if !=NULL
                    621:  * return: -1 error or 0 ok
                    622:  */
                    623: inline int
                    624: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts, 
                    625:                struct timespec * __restrict tsold)
                    626: {
                    627:        if (!root)
                    628:                return -1;
                    629: 
                    630:        if (tsold)
                    631:                *tsold = root->root_poll;
                    632: 
                    633:        if (!ts)
                    634:                sched_timespecinf(&root->root_poll);
                    635:        else
                    636:                root->root_poll = *ts;
                    637: 
                    638:        return 0;
                    639: }
1.6       misho     640: 
                    641: /*
                    642:  * schedTermCondition() - Activate hook for scheduler condition kill
                    643:  *
                    644:  * @root = root task
                    645:  * @condValue = condition value, kill schedRun() if condValue == killState
                    646:  * return: -1 error ok 0 ok
                    647:  */
                    648: inline int
                    649: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
                    650: {
                    651:        if (!root)
                    652:                return -1;
                    653: 
                    654:        root->root_cond = condValue;
                    655:        root->root_hooks.hook_exec.condition = sched_hook_condition;
                    656:        return 0;
                    657: }
1.10.6.1! misho     658: 
        !           659: /*
        !           660:  * schedResumeby() - Resume suspended task
        !           661:  *
        !           662:  * @root = root task
        !           663:  * @criteria = find task by criteria 
        !           664:  *     [CRITERIA_ANY|CRITERIA_ID|CRITERIA_DATA]
        !           665:  * @param = search parameter (sched_task_t *task| u_long id)
        !           666:  * return: -1 error or 0 resumed ok
        !           667:  */
        !           668: int
        !           669: schedResumeby(sched_root_task_t * __restrict root, u_char criteria, void *param)
        !           670: {
        !           671:        sched_task_t *task, *tmp;
        !           672:        register int flg = 0;
        !           673: 
        !           674:        if (!root)
        !           675:                return -1;
        !           676: 
        !           677: #ifdef HAVE_LIBPTHREAD
        !           678:        pthread_mutex_lock(&root->root_mtx[taskSUSPEND]);
        !           679: #endif
        !           680:        TAILQ_FOREACH_SAFE(task, &root->root_suspend, task_node, tmp) {
        !           681:                flg ^= flg;
        !           682:                switch (criteria) {
        !           683:                        case CRITERIA_ANY:
        !           684:                                flg = 1;
        !           685:                                break;
        !           686:                        case CRITERIA_ID:
        !           687:                                if (TASK_VAL(task) == (u_long) param)
        !           688:                                        flg = 1;
        !           689:                                break;
        !           690:                        case CRITERIA_DATA:
        !           691:                                if (TASK_ID(task) == (sched_task_t*) param)
        !           692:                                        flg = 1;
        !           693:                                break;
        !           694:                        default:
        !           695:                                sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
        !           696:                                flg = -1;
        !           697:                }
        !           698:                if (flg < 0)
        !           699:                        break;
        !           700:                /* resume choosen task */
        !           701:                if (flg > 0) {
        !           702:                        if (root->root_hooks.hook_exec.resume)
        !           703:                                if (root->root_hooks.hook_exec.resume(task, NULL)) {
        !           704:                                        flg = -1;
        !           705:                                        break;
        !           706:                                }
        !           707: 
        !           708:                        TAILQ_REMOVE(&root->root_suspend, task, task_node);
        !           709: 
        !           710:                        task->task_type = taskREADY;
        !           711: #ifdef HAVE_LIBPTHREAD
        !           712:                        pthread_mutex_lock(&root->root_mtx[taskREADY]);
        !           713: #endif
        !           714:                        TAILQ_INSERT_TAIL(&root->root_ready, task, task_node);
        !           715: #ifdef HAVE_LIBPTHREAD
        !           716:                        pthread_mutex_unlock(&root->root_mtx[taskREADY]);
        !           717: #endif
        !           718: 
        !           719:                        flg ^= flg;     /* ok */
        !           720:                }
        !           721:        }
        !           722: #ifdef HAVE_LIBPTHREAD
        !           723:        pthread_mutex_unlock(&root->root_mtx[taskSUSPEND]);
        !           724: #endif
        !           725: 
        !           726:        return flg;
        !           727: }

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