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

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