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

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