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

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