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

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.4.2.8 ! misho       6: * $Id: aitsched.c,v 1.4.2.7 2012/01/24 15:29:09 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: 
                     15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
                     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
                     89:  * @root = root task
                     90:  * return: -1 error or 0 ok
                     91:  */
                     92: int
                     93: schedRegisterHooks(sched_root_task_t * __restrict root)
                     94: {
                     95:        if (!root || (root->root_data.iov_base && root->root_data.iov_len))
                     96:                return -1;
                     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;
                    104: 
                    105:        root->root_hooks.hook_exec.cancel = sched_hook_cancel;
                    106:        root->root_hooks.hook_exec.fetch = sched_hook_fetch;
1.3       misho     107:        root->root_hooks.hook_exec.exception = sched_hook_exception;
1.2       misho     108: 
                    109:        root->root_hooks.hook_root.init = sched_hook_init;
                    110:        root->root_hooks.hook_root.fini = sched_hook_fini;
                    111:        return 0;
                    112: }
                    113: 
                    114: /*
1.1       misho     115:  * schedInit() - Init scheduler
                    116:  * @data = optional data if !=NULL
                    117:  * @datlen = data len if data is set
                    118:  * return: allocated root task if ok or NULL error
                    119:  */
                    120: sched_root_task_t *
                    121: schedInit(void ** __restrict data, size_t datlen)
                    122: {
                    123:        sched_root_task_t *root = NULL;
                    124:        int (*func)(sched_root_task_t *);
1.4.2.1   misho     125: #ifdef HAVE_LIBPTHREAD
                    126:        register int i;
                    127: #endif
1.1       misho     128: 
                    129:        root = malloc(sizeof(sched_root_task_t));
1.2       misho     130:        if (!root) {
                    131:                LOGERR;
                    132:        } else {
1.1       misho     133:                memset(root, 0, sizeof(sched_root_task_t));
1.4.2.1   misho     134: 
1.4.2.6   misho     135:                /* INFINIT polling period by default */
1.4.2.8 ! misho     136:                sched_timespecinf(&root->root_poll);
1.4.2.6   misho     137: 
1.4.2.1   misho     138: #ifdef HAVE_LIBPTHREAD
                    139:                for (i = 0; i < taskMAX; i++)
                    140:                        if (pthread_mutex_init(&root->root_mtx[i], NULL)) {
                    141:                                LOGERR;
                    142:                                while (i)
                    143:                                        pthread_mutex_destroy(&root->root_mtx[--i]);
                    144:                                free(root);
                    145:                                return NULL;
                    146:                        }
                    147: 
                    148:                for (i = 0; i < taskMAX; i++)
1.4.2.2   misho     149:                        pthread_mutex_lock(&root->root_mtx[i]);
1.4.2.1   misho     150: #endif
                    151: 
1.2       misho     152:                TAILQ_INIT(&root->root_read);
                    153:                TAILQ_INIT(&root->root_write);
                    154:                TAILQ_INIT(&root->root_timer);
                    155:                TAILQ_INIT(&root->root_event);
                    156:                TAILQ_INIT(&root->root_eventlo);
                    157:                TAILQ_INIT(&root->root_ready);
                    158:                TAILQ_INIT(&root->root_unuse);
1.1       misho     159: 
1.4.2.1   misho     160: #ifdef HAVE_LIBPTHREAD
                    161:                for (i = 0; i < taskMAX; i++)
1.4.2.2   misho     162:                        pthread_mutex_unlock(&root->root_mtx[i]);
1.4.2.1   misho     163: #endif
                    164: 
1.1       misho     165:                if (data && *data) {
                    166:                        if (datlen) {
                    167:                                root->root_data.iov_base = *data;
                    168:                                root->root_data.iov_len = datlen;
1.3       misho     169:                        } else { /* if datlen == 0, switch to callbacks init mode */
                    170:                                 /* little hack :) for correct initialization of scheduler */
1.2       misho     171:                                func = (int(*)(sched_root_task_t*)) data;
1.1       misho     172:                                func(root);
                    173:                        }
                    174:                }
1.2       misho     175: 
                    176:                if (root->root_hooks.hook_root.init)
                    177:                        root->root_hooks.hook_root.init(root, NULL);
1.1       misho     178:        }
                    179: 
                    180:        return root;
                    181: }
                    182: 
                    183: /*
                    184:  * schedEnd() - End scheduler & free all resources
                    185:  * @root = root task
                    186:  * return: -1 error or 0 ok
                    187:  */
                    188: int
1.2       misho     189: schedEnd(sched_root_task_t ** __restrict root)
1.1       misho     190: {
                    191:        sched_task_t *task;
1.4.2.1   misho     192: #ifdef HAVE_LIBPTHREAD
                    193:        register int i;
                    194: #endif
1.1       misho     195: 
1.2       misho     196:        if (!root || !*root)
1.1       misho     197:                return -1;
                    198: 
1.2       misho     199:        TAILQ_FOREACH(task, &(*root)->root_read, task_node) {
1.1       misho     200:                schedCancel(task);
                    201:        }
1.2       misho     202:        TAILQ_FOREACH(task, &(*root)->root_write, task_node) {
1.1       misho     203:                schedCancel(task);
                    204:        }
1.2       misho     205:        TAILQ_FOREACH(task, &(*root)->root_timer, task_node) {
1.1       misho     206:                schedCancel(task);
                    207:        }
1.2       misho     208:        TAILQ_FOREACH(task, &(*root)->root_event, task_node) {
1.1       misho     209:                schedCancel(task);
                    210:        }
1.4.2.2   misho     211:        TAILQ_FOREACH(task, &(*root)->root_eventlo, task_node) {
                    212:                schedCancel(task);
                    213:        }
1.2       misho     214:        TAILQ_FOREACH(task, &(*root)->root_ready, task_node) {
1.1       misho     215:                schedCancel(task);
                    216:        }
                    217: 
1.4.2.3   misho     218: #ifdef HAVE_LIBPTHREAD
                    219:        pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
                    220: #endif
1.2       misho     221:        while ((task = TAILQ_FIRST(&(*root)->root_unuse))) {
                    222:                TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
1.1       misho     223:                free(task);
                    224:        }
1.4.2.1   misho     225: #ifdef HAVE_LIBPTHREAD
1.4.2.3   misho     226:        pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
1.4.2.1   misho     227: #endif
1.1       misho     228: 
1.2       misho     229:        if ((*root)->root_hooks.hook_root.fini)
                    230:                (*root)->root_hooks.hook_root.fini(*root, NULL);
1.1       misho     231: 
1.4.2.1   misho     232: #ifdef HAVE_LIBPTHREAD
                    233:        for (i = 0; i < taskMAX; i++)
                    234:                pthread_mutex_destroy(&(*root)->root_mtx[i]);
                    235: #endif
                    236: 
1.2       misho     237:        free(*root);
                    238:        *root = NULL;
1.1       misho     239:        return 0;
                    240: }
                    241: 
                    242: /*
                    243:  * schedCall() - Call task execution function
                    244:  * @task = current task
                    245:  * return: !=NULL error or =NULL ok
                    246:  */
                    247: inline void *
                    248: schedCall(sched_task_t * __restrict task)
                    249: {
1.4       misho     250:        void *ptr = (void*) -1;
                    251: 
1.1       misho     252:        if (!task)
1.4       misho     253:                return ptr;
                    254: 
                    255:        if (!TASK_ISLOCKED(task))
                    256:                TASK_LOCK(task);
1.1       misho     257: 
                    258:        task->task_id++;
1.4       misho     259:        ptr = task->task_func(task);
                    260: 
                    261:        TASK_UNLOCK(task);
                    262:        return ptr;
1.1       misho     263: }
                    264: 
                    265: /*
                    266:  * schedFetch() - Fetch ready task
                    267:  * @root = root task
                    268:  * return: =NULL error or !=NULL ready task
                    269:  */
                    270: inline void *
                    271: schedFetch(sched_root_task_t * __restrict root)
                    272: {
                    273:        void *ptr;
                    274: 
                    275:        if (!root)
                    276:                return NULL;
                    277: 
                    278:        if (root->root_hooks.hook_exec.fetch)
                    279:                ptr = root->root_hooks.hook_exec.fetch(root, NULL);
                    280:        else
                    281:                ptr = NULL;
                    282: 
                    283:        return ptr;
                    284: }
                    285: 
                    286: /*
                    287:  * schedCancel() - Cancel task from scheduler
                    288:  * @task = task
                    289:  * return: -1 error or 0 ok
                    290:  */
                    291: int
                    292: schedCancel(sched_task_t * __restrict task)
                    293: {
                    294:        sched_queue_t *queue;
                    295: 
1.4.2.2   misho     296:        if (!task || !TASK_ROOT(task))
1.1       misho     297:                return -1;
                    298: 
1.4.2.2   misho     299:        if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
                    300:                if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1       misho     301:                        return -1;
                    302: 
1.4.2.2   misho     303:        switch (TASK_TYPE(task)) {
1.1       misho     304:                case taskREAD:
1.4.2.2   misho     305:                        queue = &TASK_ROOT(task)->root_read;
1.1       misho     306:                        break;
                    307:                case taskWRITE:
1.4.2.2   misho     308:                        queue = &TASK_ROOT(task)->root_write;
1.1       misho     309:                        break;
                    310:                case taskTIMER:
1.4.2.2   misho     311:                        queue = &TASK_ROOT(task)->root_timer;
1.1       misho     312:                        break;
                    313:                case taskEVENT:
1.4.2.2   misho     314:                        queue = &TASK_ROOT(task)->root_event;
                    315:                        break;
                    316:                case taskEVENTLO:
                    317:                        queue = &TASK_ROOT(task)->root_eventlo;
1.1       misho     318:                        break;
                    319:                case taskREADY:
1.4.2.2   misho     320:                        queue = &TASK_ROOT(task)->root_ready;
1.1       misho     321:                        break;
                    322:                default:
                    323:                        queue = NULL;
                    324:        }
1.4.2.2   misho     325:        if (queue) {
                    326: #ifdef HAVE_LIBPTHREAD
                    327:                pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
                    328: #endif
1.1       misho     329:                TAILQ_REMOVE(queue, task, task_node);
1.4.2.2   misho     330: #ifdef HAVE_LIBPTHREAD
                    331:                pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
                    332: #endif
                    333:        }
                    334:        if (TASK_TYPE(task) != taskUNUSE)
1.4       misho     335:                _sched_unuseTask(task);
1.1       misho     336: 
                    337:        return 0;
                    338: }
                    339: 
                    340: /*
                    341:  * schedCancelby() - Cancel task from scheduler by criteria
                    342:  * @root = root task
1.4.2.2   misho     343:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.1       misho     344:  * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
                    345:  * @param = search parameter
                    346:  * @hook = custom cleanup hook function, may be NULL
1.3       misho     347:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1       misho     348:  */
                    349: int
1.4.2.2   misho     350: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
1.1       misho     351:                u_char criteria, void *param, sched_hook_func_t hook)
                    352: {
                    353:        sched_task_t *task;
1.4.2.2   misho     354:        sched_queue_t *queue;
1.1       misho     355:        int flg = 0;
                    356: 
                    357:        if (!root)
                    358:                return -1;
1.4.2.2   misho     359:        if (type == taskMAX) {
                    360:                if (schedCancelby(root, taskREAD, criteria, param, hook))
1.1       misho     361:                        return -2;
1.4.2.2   misho     362:                if (schedCancelby(root, taskWRITE, criteria, param, hook))
1.1       misho     363:                        return -2;
1.4.2.2   misho     364:                if (schedCancelby(root, taskTIMER, criteria, param, hook))
1.1       misho     365:                        return -2;
1.4.2.2   misho     366:                if (schedCancelby(root, taskEVENT, criteria, param, hook))
1.1       misho     367:                        return -2;
1.4.2.2   misho     368:                if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
1.1       misho     369:                        return -2;
1.4.2.2   misho     370:                if (schedCancelby(root, taskREADY, criteria, param, hook))
1.1       misho     371:                        return -2;
                    372:                return 0;
                    373:        }
1.4.2.2   misho     374:        switch (type) {
                    375:                case taskREAD:
                    376:                        queue = &root->root_read;
                    377:                        break;
                    378:                case taskWRITE:
                    379:                        queue = &root->root_write;
                    380:                        break;
                    381:                case taskTIMER:
                    382:                        queue = &root->root_timer;
                    383:                        break;
                    384:                case taskEVENT:
                    385:                        queue = &root->root_event;
                    386:                        break;
                    387:                case taskEVENTLO:
                    388:                        queue = &root->root_eventlo;
                    389:                        break;
                    390:                case taskREADY:
                    391:                        queue = &root->root_ready;
                    392:                        break;
                    393:                default:
                    394:                        return 0;
                    395:        }
1.1       misho     396: 
1.4.2.2   misho     397: #ifdef HAVE_LIBPTHREAD
                    398:        pthread_mutex_lock(&root->root_mtx[type]);
                    399: #endif
1.1       misho     400:        TAILQ_FOREACH(task, queue, task_node)
                    401:                if (criteria == CRITERIA_CALL) {
                    402:                        if (task->task_func == (sched_task_func_t) param) {
                    403:                                flg++;
                    404:                                break;
                    405:                        }
                    406:                } else if (criteria == CRITERIA_ARG) {
                    407:                        if (task->task_arg == param) {
                    408:                                flg++;
                    409:                                break;
                    410:                        }
                    411:                } else if (criteria == CRITERIA_FD) {
1.3       misho     412:                        if (TASK_FD(task) == (intptr_t) param) {
1.1       misho     413:                                flg++;
                    414:                                break;
                    415:                        }
                    416:                } else if (criteria == CRITERIA_VAL) {
                    417:                        if (TASK_VAL(task) == (u_long) param) {
                    418:                                flg++;
                    419:                                break;
                    420:                        }
                    421:                } else if (criteria == CRITERIA_TV) {
1.4.2.7   misho     422:                        if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -)) {
1.1       misho     423:                                flg++;
                    424:                                break;
                    425:                        }
                    426:                } else {
1.4.2.2   misho     427: #ifdef HAVE_LIBPTHREAD
                    428:                        pthread_mutex_unlock(&root->root_mtx[type]);
                    429: #endif
1.1       misho     430:                        sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
                    431:                        return -1;
                    432:                }
1.4.2.2   misho     433: #ifdef HAVE_LIBPTHREAD
                    434:        pthread_mutex_unlock(&root->root_mtx[type]);
                    435: #endif
1.1       misho     436:        if (!flg || !task)      /* task not found */
                    437:                return 0;
                    438: 
1.4.2.2   misho     439:        if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
                    440:                if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1       misho     441:                        return -1;
                    442:        if (hook)
                    443:                if (hook(task, NULL))
                    444:                        return -3;
                    445: 
1.4.2.2   misho     446: #ifdef HAVE_LIBPTHREAD
                    447:        pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[type]);
                    448: #endif
1.1       misho     449:        TAILQ_REMOVE(queue, task, task_node);
1.4.2.2   misho     450: #ifdef HAVE_LIBPTHREAD
                    451:        pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[type]);
                    452: #endif
1.1       misho     453: 
1.4.2.2   misho     454:        if (TASK_TYPE(task) != taskUNUSE)
1.4       misho     455:                _sched_unuseTask(task);
1.1       misho     456:        return 0;
                    457: }
                    458: 
                    459: /*
                    460:  * schedRun() - Scheduler *run loop*
                    461:  * @root = root task
1.2       misho     462:  * @killState = kill condition variable, if !=0 stop scheduler loop
1.1       misho     463:  * return: -1 error or 0 ok
                    464:  */
                    465: int
1.2       misho     466: schedRun(sched_root_task_t * __restrict root, volatile intptr_t * __restrict killState)
1.1       misho     467: {
                    468:        sched_task_t *task;
                    469: 
                    470:        if (!root)
                    471:                return -1;
                    472: 
                    473:        if (root->root_hooks.hook_exec.run)
                    474:                if (root->root_hooks.hook_exec.run(root, NULL))
                    475:                        return -1;
1.2       misho     476:        if (root->root_hooks.hook_exec.fetch) {
                    477:                if (killState)
1.3       misho     478:                        while (!*killState) {
                    479:                                if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
                    480:                                        schedCall(task);
                    481:                        }
1.2       misho     482:                else
                    483:                        while ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
                    484:                                schedCall(task);
                    485:        }
1.1       misho     486: 
                    487:        return 0;
                    488: }
1.4.2.5   misho     489: 
                    490: /*
                    491:  * schedPolling() - Polling timeout period if no timer task is present
                    492:  * @root = root task
                    493:  * @ts = timeout polling period, if ==NULL INFINIT timeout
                    494:  * @tsold = old timeout polling if !=NULL
                    495:  * return: -1 error or 0 ok
                    496:  */
                    497: inline int
                    498: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts, 
                    499:                struct timespec * __restrict tsold)
                    500: {
                    501:        if (!root)
                    502:                return -1;
                    503: 
                    504:        if (tsold)
                    505:                *tsold = root->root_poll;
                    506: 
                    507:        if (!ts)
1.4.2.8 ! misho     508:                sched_timespecinf(&root->root_poll);
1.4.2.5   misho     509:        else
                    510:                root->root_poll = *ts;
                    511: 
                    512:        return 0;
                    513: }

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