Annotation of libaitsched/src/hooks.c, revision 1.27.2.5

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.27.2.5! misho       6: * $Id: hooks.c,v 1.27.2.4 2014/05/21 22:22:57 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.25      misho      15: Copyright 2004 - 2014
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: /*
                     51:  * sched_hook_init() - Default INIT hook
1.5       misho      52:  *
1.1       misho      53:  * @root = root task
1.6       misho      54:  * @arg = unused
1.1       misho      55:  * return: <0 errors and 0 ok
                     56:  */
                     57: void *
1.6       misho      58: sched_hook_init(void *root, void *arg __unused)
1.1       misho      59: {
                     60:        sched_root_task_t *r = root;
                     61: 
1.6       misho      62:        if (!r)
1.1       misho      63:                return (void*) -1;
                     64: 
1.27.2.4  misho      65: #if SUP_ENABLE == KQ_SUPPORT
1.1       misho      66:        r->root_kq = kqueue();
                     67:        if (r->root_kq == -1) {
                     68:                LOGERR;
                     69:                return (void*) -1;
                     70:        }
1.27.2.4  misho      71: #elif SUP_ENABLE == EP_SUPPORT
                     72:        r->root_kq = epoll_create(KQ_EVENTS);
                     73:        if (r->root_kq == -1) {
                     74:                LOGERR;
                     75:                return (void*) -1;
                     76:        }
1.25      misho      77: #else
                     78:        r->root_kq ^= r->root_kq;
                     79:        FD_ZERO(&r->root_fds[0]);
                     80:        FD_ZERO(&r->root_fds[1]);
                     81: #endif
1.1       misho      82: 
                     83:        return NULL;
                     84: }
                     85: 
                     86: /*
                     87:  * sched_hook_fini() - Default FINI hook
1.5       misho      88:  *
1.1       misho      89:  * @root = root task
                     90:  * @arg = unused
                     91:  * return: <0 errors and 0 ok
                     92:  */
                     93: void *
                     94: sched_hook_fini(void *root, void *arg __unused)
                     95: {
                     96:        sched_root_task_t *r = root;
                     97: 
                     98:        if (!r)
                     99:                return (void*) -1;
                    100: 
1.27.2.4  misho     101: #if SUP_ENABLE == KQ_SUPPORT || SUP_ENABLE == EP_SUPPORT
1.1       misho     102:        if (r->root_kq > 2) {
                    103:                close(r->root_kq);
                    104:                r->root_kq = 0;
                    105:        }
1.25      misho     106: #else
                    107:        FD_ZERO(&r->root_fds[1]);
                    108:        FD_ZERO(&r->root_fds[0]);
                    109:        r->root_kq ^= r->root_kq;
                    110: #endif
1.1       misho     111: 
                    112:        return NULL;
                    113: }
                    114: 
                    115: /*
                    116:  * sched_hook_cancel() - Default CANCEL hook
1.5       misho     117:  *
1.1       misho     118:  * @task = current task
                    119:  * @arg = unused
                    120:  * return: <0 errors and 0 ok
                    121:  */
                    122: void *
                    123: sched_hook_cancel(void *task, void *arg __unused)
                    124: {
                    125:        sched_task_t *t = task;
1.27.2.4  misho     126: #if SUP_ENABLE == KQ_SUPPORT
1.1       misho     127:        struct kevent chg[1];
1.2       misho     128:        struct timespec timeout = { 0, 0 };
1.25      misho     129: #else
                    130:        sched_root_task_t *r = NULL;
1.27.2.5! misho     131:        register int i = -1;
1.25      misho     132: #endif
1.11      misho     133: #ifdef AIO_SUPPORT
                    134:        struct aiocb *acb;
                    135: #ifdef EVFILT_LIO
1.15      misho     136:        register int i = 0;
1.11      misho     137:        struct aiocb **acbs;
                    138: #endif /* EVFILT_LIO */
                    139: #endif /* AIO_SUPPORT */
1.1       misho     140: 
1.6       misho     141:        if (!t || !TASK_ROOT(t))
1.1       misho     142:                return (void*) -1;
1.27.2.4  misho     143: #if SUP_ENABLE != KQ_SUPPORT
1.25      misho     144:        r = TASK_ROOT(t);
                    145: #endif
1.1       misho     146: 
1.4       misho     147:        switch (TASK_TYPE(t)) {
1.1       misho     148:                case taskREAD:
1.27.2.4  misho     149: #if SUP_ENABLE == KQ_SUPPORT
1.2       misho     150: #ifdef __NetBSD__
                    151:                        EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
                    152: #else
                    153:                        EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (void*) TASK_FD(t));
                    154: #endif
1.27.2.5! misho     155: #elif SUP_ENABLE == EP_SUPPORT
        !           156:                        i = TASK_FD(t);
1.25      misho     157: #else
                    158:                        FD_CLR(TASK_FD(t), &r->root_fds[0]);
                    159: 
                    160:                        /* optimize select */
                    161:                        for (i = r->root_kq - 1; i > 2; i--)
                    162:                                if (FD_ISSET(i, &r->root_fds[0]) || FD_ISSET(i, &r->root_fds[1]))
                    163:                                        break;
                    164:                        if (i > 2)
                    165:                                r->root_kq = i + 1;
                    166: #endif
1.1       misho     167:                        break;
                    168:                case taskWRITE:
1.27.2.4  misho     169: #if SUP_ENABLE == KQ_SUPPORT
1.2       misho     170: #ifdef __NetBSD__
                    171:                        EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
                    172: #else
                    173:                        EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (void*) TASK_FD(t));
                    174: #endif
1.27.2.5! misho     175: #elif SUP_ENABLE == EP_SUPPORT
        !           176:                        i = TASK_FD(t);
1.25      misho     177: #else
                    178:                        FD_CLR(TASK_FD(t), &r->root_fds[1]);
                    179: 
                    180:                        /* optimize select */
                    181:                        for (i = r->root_kq - 1; i > 2; i--)
                    182:                                if (FD_ISSET(i, &r->root_fds[0]) || FD_ISSET(i, &r->root_fds[1]))
                    183:                                        break;
                    184:                        if (i > 2)
                    185:                                r->root_kq = i + 1;
                    186: #endif
1.1       misho     187:                        break;
1.7       misho     188:                case taskALARM:
1.27.2.4  misho     189: #if SUP_ENABLE == KQ_SUPPORT
1.7       misho     190: #ifdef __NetBSD__
                    191:                        EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_DELETE, 
                    192:                                        0, 0, (intptr_t) TASK_DATA(t));
                    193: #else
                    194:                        EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_DELETE, 
                    195:                                        0, 0, (void*) TASK_DATA(t));
                    196: #endif
1.25      misho     197: #endif
1.8       misho     198:                        break;
                    199:                case taskNODE:
1.27.2.4  misho     200: #if SUP_ENABLE == KQ_SUPPORT
1.8       misho     201: #ifdef __NetBSD__
                    202:                        EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
                    203: #else
                    204:                        EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (void*) TASK_FD(t));
                    205: #endif
1.25      misho     206: #endif
1.8       misho     207:                        break;
                    208:                case taskPROC:
1.27.2.4  misho     209: #if SUP_ENABLE == KQ_SUPPORT
1.8       misho     210: #ifdef __NetBSD__
                    211:                        EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
                    212: #else
                    213:                        EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
                    214: #endif
1.25      misho     215: #endif
1.8       misho     216:                        break;
                    217:                case taskSIGNAL:
1.27.2.4  misho     218: #if SUP_ENABLE == KQ_SUPPORT
1.8       misho     219: #ifdef __NetBSD__
                    220:                        EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
                    221: #else
                    222:                        EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
                    223: #endif
1.18      misho     224:                        /* restore signal */
                    225:                        signal(TASK_VAL(t), SIG_DFL);
1.25      misho     226: #endif
1.8       misho     227:                        break;
1.11      misho     228: #ifdef AIO_SUPPORT
                    229:                case taskAIO:
1.27.2.4  misho     230: #if SUP_ENABLE == KQ_SUPPORT
1.11      misho     231: #ifdef __NetBSD__
                    232:                        EV_SET(&chg[0], TASK_VAL(t), EVFILT_AIO, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
                    233: #else
                    234:                        EV_SET(&chg[0], TASK_VAL(t), EVFILT_AIO, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
                    235: #endif
                    236:                        acb = (struct aiocb*) TASK_VAL(t);
                    237:                        if (acb) {
                    238:                                if (aio_cancel(acb->aio_fildes, acb) == AIO_CANCELED)
                    239:                                        aio_return(acb);
                    240:                                free(acb);
                    241:                                TASK_VAL(t) = 0;
                    242:                        }
1.25      misho     243: #endif
1.11      misho     244:                        break;
                    245: #ifdef EVFILT_LIO
                    246:                case taskLIO:
1.27.2.4  misho     247: #if SUP_ENABLE == KQ_SUPPORT
1.11      misho     248: #ifdef __NetBSD__
                    249:                        EV_SET(&chg[0], TASK_VAL(t), EVFILT_LIO, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
                    250: #else
                    251:                        EV_SET(&chg[0], TASK_VAL(t), EVFILT_LIO, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
                    252: #endif
                    253:                        acbs = (struct aiocb**) TASK_VAL(t);
                    254:                        if (acbs) {
                    255:                                for (i = 0; i < TASK_DATLEN(t); i++) {
                    256:                                        if (aio_cancel(acbs[i]->aio_fildes, acbs[i]) == AIO_CANCELED)
                    257:                                                aio_return(acbs[i]);
                    258:                                        free(acbs[i]);
                    259:                                }
                    260:                                free(acbs);
                    261:                                TASK_VAL(t) = 0;
                    262:                        }
1.25      misho     263: #endif
1.11      misho     264:                        break;
                    265: #endif /* EVFILT_LIO */
                    266: #endif /* AIO_SUPPORT */
1.8       misho     267: #ifdef EVFILT_USER
                    268:                case taskUSER:
1.27.2.4  misho     269: #if SUP_ENABLE == KQ_SUPPORT
1.8       misho     270: #ifdef __NetBSD__
                    271:                        EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
                    272: #else
                    273:                        EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
                    274: #endif
1.25      misho     275: #endif
1.10      misho     276:                        break;
1.19      misho     277: #endif /* EVFILT_USER */
1.14      misho     278:                case taskTHREAD:
                    279: #ifdef HAVE_LIBPTHREAD
1.27.2.2  misho     280:                        if (TASK_VAL(t))
                    281:                                pthread_cancel((pthread_t) TASK_VAL(t));
1.14      misho     282: #endif
1.19      misho     283:                        return NULL;
1.27      misho     284: #if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME) && defined(HAVE_TIMER_DELETE)
1.19      misho     285:                case taskRTC:
                    286:                        timer_delete((timer_t) TASK_FLAG(t));
                    287:                        schedCancel((sched_task_t*) TASK_RET(t));
                    288:                        return NULL;
                    289: #endif /* HAVE_TIMER_CREATE */
1.1       misho     290:                default:
1.8       misho     291:                        return NULL;
1.1       misho     292:        }
                    293: 
1.27.2.4  misho     294: #if SUP_ENABLE == KQ_SUPPORT
1.8       misho     295:        kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout);
1.27.2.5! misho     296: #elif SUP_ENABLE == EP_SUPPORT
        !           297:        if (i != -1)
        !           298:                epoll_ctl(TASK_ROOT(t)->root_kq, EPOLL_CTL_DEL, i, NULL);
1.25      misho     299: #endif
1.1       misho     300:        return NULL;
                    301: }
                    302: 
1.14      misho     303: #ifdef HAVE_LIBPTHREAD
                    304: /*
                    305:  * sched_hook_thread() - Default THREAD hook
                    306:  *
                    307:  * @task = current task
                    308:  * @arg = pthread attributes
                    309:  * return: <0 errors and 0 ok
                    310:  */
                    311: void *
                    312: sched_hook_thread(void *task, void *arg)
                    313: {
                    314:        sched_task_t *t = task;
                    315:        pthread_t tid;
1.15      misho     316:        sigset_t s, o;
1.14      misho     317: 
                    318:        if (!t || !TASK_ROOT(t))
                    319:                return (void*) -1;
                    320: 
1.15      misho     321:        sigfillset(&s);
                    322:        pthread_sigmask(SIG_BLOCK, &s, &o);
1.16      misho     323:        if ((errno = pthread_create(&tid, (pthread_attr_t*) arg, 
                    324:                                (void *(*)(void*)) _sched_threadWrapper, t))) {
1.14      misho     325:                LOGERR;
1.15      misho     326:                pthread_sigmask(SIG_SETMASK, &o, NULL);
1.14      misho     327:                return (void*) -1;
1.15      misho     328:        } else
                    329:                TASK_VAL(t) = (u_long) tid;
1.14      misho     330: 
                    331:        if (!TASK_ISLOCKED(t))
                    332:                TASK_LOCK(t);
                    333: 
1.15      misho     334:        pthread_sigmask(SIG_SETMASK, &o, NULL);
1.14      misho     335:        return NULL;
                    336: }
                    337: #endif
                    338: 
1.1       misho     339: /*
                    340:  * sched_hook_read() - Default READ hook
1.5       misho     341:  *
1.1       misho     342:  * @task = current task
                    343:  * @arg = unused
                    344:  * return: <0 errors and 0 ok
                    345:  */
                    346: void *
                    347: sched_hook_read(void *task, void *arg __unused)
                    348: {
                    349:        sched_task_t *t = task;
1.27.2.4  misho     350: #if SUP_ENABLE == KQ_SUPPORT
1.1       misho     351:        struct kevent chg[1];
1.2       misho     352:        struct timespec timeout = { 0, 0 };
1.27.2.5! misho     353: #elif SUP_ENABLE == EP_SUPPORT
        !           354:        struct epoll_event ee = { .events = EPOLLIN | EPOLLPRI | EPOLLRDHUP, .data.fd = 0 };
1.25      misho     355: #else
                    356:        sched_root_task_t *r = NULL;
                    357: #endif
1.1       misho     358: 
1.6       misho     359:        if (!t || !TASK_ROOT(t))
1.1       misho     360:                return (void*) -1;
                    361: 
1.27.2.4  misho     362: #if SUP_ENABLE == KQ_SUPPORT
1.2       misho     363: #ifdef __NetBSD__
1.8       misho     364:        EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t));
1.2       misho     365: #else
1.8       misho     366:        EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t));
1.2       misho     367: #endif
1.4       misho     368:        if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
                    369:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                    370:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
1.3       misho     371:                else
                    372:                        LOGERR;
1.1       misho     373:                return (void*) -1;
                    374:        }
1.27.2.5! misho     375: #elif SUP_ENABLE == EP_SUPPORT
        !           376:        ee.data.fd = TASK_FD(t);
        !           377:        if (epoll_ctl(TASK_ROOT(t)->root_kq, EPOLL_CTL_ADD, TASK_FD(t), &ee) == -1) {
        !           378:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
        !           379:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
        !           380:                else
        !           381:                        LOGERR;
        !           382:                return (void*) -1;
        !           383:        }
1.25      misho     384: #else
1.27.2.5! misho     385:        r = TASK_ROOT(t);
1.25      misho     386:        FD_SET(TASK_FD(t), &r->root_fds[0]);
                    387:        if (TASK_FD(t) >= r->root_kq)
                    388:                r->root_kq = TASK_FD(t) + 1;
                    389: #endif
1.1       misho     390: 
                    391:        return NULL;
                    392: }
                    393: 
                    394: /*
                    395:  * sched_hook_write() - Default WRITE hook
1.5       misho     396:  *
1.1       misho     397:  * @task = current task
                    398:  * @arg = unused
                    399:  * return: <0 errors and 0 ok
                    400:  */
                    401: void *
                    402: sched_hook_write(void *task, void *arg __unused)
                    403: {
                    404:        sched_task_t *t = task;
1.27.2.4  misho     405: #if SUP_ENABLE == KQ_SUPPORT
1.1       misho     406:        struct kevent chg[1];
1.2       misho     407:        struct timespec timeout = { 0, 0 };
1.27.2.5! misho     408: #elif SUP_ENABLE == EP_SUPPORT
        !           409:        struct epoll_event ee = { .events = EPOLLOUT, .data.fd = 0 };
1.25      misho     410: #else
                    411:        sched_root_task_t *r = NULL;
                    412: #endif
1.1       misho     413: 
1.6       misho     414:        if (!t || !TASK_ROOT(t))
1.1       misho     415:                return (void*) -1;
                    416: 
1.27.2.4  misho     417: #if SUP_ENABLE == KQ_SUPPORT
1.2       misho     418: #ifdef __NetBSD__
1.8       misho     419:        EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t));
1.2       misho     420: #else
1.8       misho     421:        EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t));
1.2       misho     422: #endif
1.4       misho     423:        if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
                    424:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                    425:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
1.3       misho     426:                else
                    427:                        LOGERR;
1.1       misho     428:                return (void*) -1;
                    429:        }
1.27.2.5! misho     430: #elif SUP_ENABLE == EP_SUPPORT
        !           431:        ee.data.fd = TASK_FD(t);
        !           432:        if (epoll_ctl(TASK_ROOT(t)->root_kq, EPOLL_CTL_ADD, TASK_FD(t), &ee) == -1) {
        !           433:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
        !           434:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
        !           435:                else
        !           436:                        LOGERR;
        !           437:                return (void*) -1;
        !           438:        }
1.25      misho     439: #else
1.27.2.5! misho     440:        r = TASK_ROOT(t);
1.25      misho     441:        FD_SET(TASK_FD(t), &r->root_fds[1]);
                    442:        if (TASK_FD(t) >= r->root_kq)
                    443:                r->root_kq = TASK_FD(t) + 1;
                    444: #endif
1.1       misho     445: 
                    446:        return NULL;
                    447: }
                    448: 
                    449: /*
1.7       misho     450:  * sched_hook_alarm() - Default ALARM hook
                    451:  *
                    452:  * @task = current task
                    453:  * @arg = unused
                    454:  * return: <0 errors and 0 ok
                    455:  */
                    456: void *
                    457: sched_hook_alarm(void *task, void *arg __unused)
                    458: {
1.27.2.4  misho     459: #if SUP_ENABLE == KQ_SUPPORT
1.7       misho     460:        sched_task_t *t = task;
                    461:        struct kevent chg[1];
                    462:        struct timespec timeout = { 0, 0 };
                    463: 
                    464:        if (!t || !TASK_ROOT(t))
                    465:                return (void*) -1;
                    466: 
                    467: #ifdef __NetBSD__
1.19      misho     468:        EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_ADD | EV_CLEAR, 0, 
1.7       misho     469:                        t->task_val.ts.tv_sec * 1000 + t->task_val.ts.tv_nsec / 1000000, 
                    470:                        (intptr_t) TASK_DATA(t));
                    471: #else
1.19      misho     472:        EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_ADD | EV_CLEAR, 0, 
1.7       misho     473:                        t->task_val.ts.tv_sec * 1000 + t->task_val.ts.tv_nsec / 1000000, 
                    474:                        (void*) TASK_DATA(t));
                    475: #endif
                    476:        if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
                    477:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                    478:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
                    479:                else
                    480:                        LOGERR;
                    481:                return (void*) -1;
                    482:        }
                    483: 
1.25      misho     484: #endif
1.7       misho     485:        return NULL;
                    486: }
                    487: 
                    488: /*
1.8       misho     489:  * sched_hook_node() - Default NODE hook
                    490:  *
                    491:  * @task = current task
                    492:  * @arg = unused
                    493:  * return: <0 errors and 0 ok
                    494:  */
                    495: void *
                    496: sched_hook_node(void *task, void *arg __unused)
                    497: {
1.27.2.4  misho     498: #if SUP_ENABLE == KQ_SUPPORT
1.8       misho     499:        sched_task_t *t = task;
                    500:        struct kevent chg[1];
                    501:        struct timespec timeout = { 0, 0 };
                    502: 
                    503:        if (!t || !TASK_ROOT(t))
                    504:                return (void*) -1;
                    505: 
                    506: #ifdef __NetBSD__
                    507:        EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_ADD | EV_CLEAR, 
                    508:                        NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | 
                    509:                        NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, (intptr_t) TASK_FD(t));
                    510: #else
                    511:        EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_ADD | EV_CLEAR, 
                    512:                        NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | 
                    513:                        NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, (void*) TASK_FD(t));
                    514: #endif
                    515:        if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
                    516:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                    517:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
                    518:                else
                    519:                        LOGERR;
                    520:                return (void*) -1;
                    521:        }
                    522: 
1.25      misho     523: #endif
1.8       misho     524:        return NULL;
                    525: }
                    526: 
                    527: /*
                    528:  * sched_hook_proc() - Default PROC hook
                    529:  *
                    530:  * @task = current task
                    531:  * @arg = unused
                    532:  * return: <0 errors and 0 ok
                    533:  */
                    534: void *
                    535: sched_hook_proc(void *task, void *arg __unused)
                    536: {
1.27.2.4  misho     537: #if SUP_ENABLE == KQ_SUPPORT
1.8       misho     538:        sched_task_t *t = task;
                    539:        struct kevent chg[1];
                    540:        struct timespec timeout = { 0, 0 };
                    541: 
                    542:        if (!t || !TASK_ROOT(t))
                    543:                return (void*) -1;
                    544: 
                    545: #ifdef __NetBSD__
                    546:        EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_ADD | EV_CLEAR, 
                    547:                        NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, (intptr_t) TASK_VAL(t));
                    548: #else
                    549:        EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_ADD | EV_CLEAR, 
                    550:                        NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, (void*) TASK_VAL(t));
                    551: #endif
                    552:        if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
                    553:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                    554:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
                    555:                else
                    556:                        LOGERR;
                    557:                return (void*) -1;
                    558:        }
                    559: 
1.25      misho     560: #endif
1.8       misho     561:        return NULL;
                    562: }
                    563: 
                    564: /*
                    565:  * sched_hook_signal() - Default SIGNAL hook
                    566:  *
                    567:  * @task = current task
                    568:  * @arg = unused
                    569:  * return: <0 errors and 0 ok
                    570:  */
                    571: void *
                    572: sched_hook_signal(void *task, void *arg __unused)
                    573: {
1.27.2.4  misho     574: #if SUP_ENABLE == KQ_SUPPORT
1.8       misho     575:        sched_task_t *t = task;
                    576:        struct kevent chg[1];
                    577:        struct timespec timeout = { 0, 0 };
                    578: 
                    579:        if (!t || !TASK_ROOT(t))
                    580:                return (void*) -1;
                    581: 
1.18      misho     582:        /* ignore signal */
                    583:        signal(TASK_VAL(t), SIG_IGN);
                    584: 
1.8       misho     585: #ifdef __NetBSD__
1.19      misho     586:        EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_VAL(t));
1.8       misho     587: #else
1.19      misho     588:        EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_VAL(t));
1.8       misho     589: #endif
                    590:        if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
                    591:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                    592:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
                    593:                else
                    594:                        LOGERR;
                    595:                return (void*) -1;
                    596:        }
1.25      misho     597: #else
                    598: #if 0
                    599:        sched_task_t *t = task;
                    600:        struct sigaction sa;
                    601: 
                    602:        memset(&sa, 0, sizeof sa);
                    603:        sigemptyset(&sa.sa_mask);
                    604:        sa.sa_handler = _sched_sigHandler;
                    605:        sa.sa_flags = SA_RESETHAND | SA_RESTART;
1.8       misho     606: 
1.25      misho     607:        if (sigaction(TASK_VAL(t), &sa, NULL) == -1) {
                    608:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                    609:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
                    610:                else
                    611:                        LOGERR;
                    612:                return (void*) -1;
                    613:        }
                    614: #endif /* 0 */
                    615: #endif
1.8       misho     616:        return NULL;
                    617: }
                    618: 
                    619: /*
                    620:  * sched_hook_user() - Default USER hook
                    621:  *
                    622:  * @task = current task
                    623:  * @arg = unused
                    624:  * return: <0 errors and 0 ok
                    625:  */
                    626: #ifdef EVFILT_USER
                    627: void *
                    628: sched_hook_user(void *task, void *arg __unused)
                    629: {
1.27.2.4  misho     630: #if SUP_ENABLE == KQ_SUPPORT
1.8       misho     631:        sched_task_t *t = task;
                    632:        struct kevent chg[1];
                    633:        struct timespec timeout = { 0, 0 };
                    634: 
                    635:        if (!t || !TASK_ROOT(t))
                    636:                return (void*) -1;
                    637: 
                    638: #ifdef __NetBSD__
                    639:        EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_ADD | EV_CLEAR, TASK_DATLEN(t), 
                    640:                        0, (intptr_t) TASK_VAL(t));
                    641: #else
                    642:        EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_ADD | EV_CLEAR, TASK_DATLEN(t), 
                    643:                        0, (void*) TASK_VAL(t));
                    644: #endif
                    645:        if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
                    646:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                    647:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
                    648:                else
                    649:                        LOGERR;
                    650:                return (void*) -1;
                    651:        }
                    652: 
1.25      misho     653: #endif
1.8       misho     654:        return NULL;
                    655: }
                    656: #endif
                    657: 
                    658: /*
1.1       misho     659:  * sched_hook_fetch() - Default FETCH hook
1.5       misho     660:  *
1.1       misho     661:  * @root = root task
                    662:  * @arg = unused
                    663:  * return: NULL error or !=NULL fetched task
                    664:  */
                    665: void *
                    666: sched_hook_fetch(void *root, void *arg __unused)
                    667: {
                    668:        sched_root_task_t *r = root;
1.6       misho     669:        sched_task_t *task, *tmp;
1.4       misho     670:        struct timespec now, m, mtmp;
1.27.2.4  misho     671: #if SUP_ENABLE == KQ_SUPPORT
1.25      misho     672:        struct kevent evt[1], res[KQ_EVENTS];
1.4       misho     673:        struct timespec *timeout;
1.27.2.5! misho     674: #elif SUP_ENABLE == EP_SUPPORT
        !           675:        struct epoll_event res[KQ_EVENTS];
        !           676:        u_long timeout = 0;
1.25      misho     677: #else
                    678:        struct timeval *timeout, tv;
                    679:        fd_set rfd, wfd, xfd;
                    680: #endif
1.9       misho     681:        register int i, flg;
1.1       misho     682:        int en;
1.11      misho     683: #ifdef AIO_SUPPORT
                    684:        int len, fd;
                    685:        struct aiocb *acb;
                    686: #ifdef EVFILT_LIO
                    687:        int l;
                    688:        register int j;
                    689:        off_t off;
                    690:        struct aiocb **acbs;
                    691:        struct iovec *iv;
                    692: #endif /* EVFILT_LIO */
                    693: #endif /* AIO_SUPPORT */
1.1       misho     694: 
1.6       misho     695:        if (!r)
1.1       misho     696:                return NULL;
                    697: 
                    698:        /* get new task by queue priority */
                    699:        while ((task = TAILQ_FIRST(&r->root_event))) {
1.4       misho     700: #ifdef HAVE_LIBPTHREAD
                    701:                pthread_mutex_lock(&r->root_mtx[taskEVENT]);
                    702: #endif
1.1       misho     703:                TAILQ_REMOVE(&r->root_event, task, task_node);
1.4       misho     704: #ifdef HAVE_LIBPTHREAD
                    705:                pthread_mutex_unlock(&r->root_mtx[taskEVENT]);
                    706: #endif
1.1       misho     707:                task->task_type = taskUNUSE;
1.4       misho     708: #ifdef HAVE_LIBPTHREAD
                    709:                pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
                    710: #endif
1.1       misho     711:                TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
1.4       misho     712: #ifdef HAVE_LIBPTHREAD
                    713:                pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
                    714: #endif
1.1       misho     715:                return task;
                    716:        }
                    717:        while ((task = TAILQ_FIRST(&r->root_ready))) {
1.4       misho     718: #ifdef HAVE_LIBPTHREAD
                    719:                pthread_mutex_lock(&r->root_mtx[taskREADY]);
                    720: #endif
1.1       misho     721:                TAILQ_REMOVE(&r->root_ready, task, task_node);
1.4       misho     722: #ifdef HAVE_LIBPTHREAD
                    723:                pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                    724: #endif
1.1       misho     725:                task->task_type = taskUNUSE;
1.4       misho     726: #ifdef HAVE_LIBPTHREAD
                    727:                pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
                    728: #endif
1.1       misho     729:                TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
1.4       misho     730: #ifdef HAVE_LIBPTHREAD
                    731:                pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
                    732: #endif
1.1       misho     733:                return task;
                    734:        }
                    735: 
                    736: #ifdef TIMER_WITHOUT_SORT
1.4       misho     737:        clock_gettime(CLOCK_MONOTONIC, &now);
1.1       misho     738: 
1.4       misho     739:        sched_timespecclear(&r->root_wait);
1.1       misho     740:        TAILQ_FOREACH(task, &r->root_timer, task_node) {
1.4       misho     741:                if (!sched_timespecisset(&r->root_wait))
                    742:                        r->root_wait = TASK_TS(task);
                    743:                else if (sched_timespeccmp(&TASK_TS(task), &r->root_wait, -) < 0)
                    744:                        r->root_wait = TASK_TS(task);
1.1       misho     745:        }
                    746: 
                    747:        if (TAILQ_FIRST(&r->root_timer)) {
                    748:                m = r->root_wait;
1.4       misho     749:                sched_timespecsub(&m, &now, &mtmp);
1.1       misho     750:                r->root_wait = mtmp;
                    751:        } else {
                    752:                /* set wait INFTIM */
1.4       misho     753:                sched_timespecinf(&r->root_wait);
1.1       misho     754:        }
1.25      misho     755: #else  /* ! TIMER_WITHOUT_SORT */
1.12      misho     756:        if (!TAILQ_FIRST(&r->root_task) && (task = TAILQ_FIRST(&r->root_timer))) {
1.4       misho     757:                clock_gettime(CLOCK_MONOTONIC, &now);
1.1       misho     758: 
1.4       misho     759:                m = TASK_TS(task);
                    760:                sched_timespecsub(&m, &now, &mtmp);
1.1       misho     761:                r->root_wait = mtmp;
                    762:        } else {
                    763:                /* set wait INFTIM */
1.4       misho     764:                sched_timespecinf(&r->root_wait);
1.1       misho     765:        }
1.25      misho     766: #endif /* TIMER_WITHOUT_SORT */
1.12      misho     767:        /* if present member of task, set NOWAIT */
                    768:        if (TAILQ_FIRST(&r->root_task))
1.4       misho     769:                sched_timespecclear(&r->root_wait);
1.1       misho     770: 
1.25      misho     771:        if (r->root_wait.tv_sec != -1 && r->root_wait.tv_nsec != -1) {
1.27.2.4  misho     772: #if SUP_ENABLE == KQ_SUPPORT
1.4       misho     773:                timeout = &r->root_wait;
1.27.2.5! misho     774: #elif SUP_ENABLE == EP_SUPPORT
        !           775:                timeout = r->root_wait.tv_sec * 1000 + r->root_wait.tv_nsec / 1000000;
1.25      misho     776: #else
                    777:                sched_timespec2val(&r->root_wait, &tv);
                    778:                timeout = &tv;
1.27.2.3  misho     779: #endif /* KQ_SUPPORT */
1.25      misho     780:        } else if (sched_timespecisinf(&r->root_poll))
1.27.2.5! misho     781: #if SUP_ENABLE == EP_SUPPORT
        !           782:                timeout = -1;
        !           783: #else
1.1       misho     784:                timeout = NULL;
1.27.2.5! misho     785: #endif
1.25      misho     786:        else {
1.27.2.4  misho     787: #if SUP_ENABLE == KQ_SUPPORT
1.4       misho     788:                timeout = &r->root_poll;
1.27.2.5! misho     789: #elif SUP_ENABLE == EP_SUPPORT
        !           790:                timeout = r->root_poll.tv_sec * 1000 + r->root_poll.tv_nsec / 1000000;
1.25      misho     791: #else
                    792:                sched_timespec2val(&r->root_poll, &tv);
                    793:                timeout = &tv;
1.27.2.3  misho     794: #endif /* KQ_SUPPORT */
1.25      misho     795:        }
                    796: 
1.27.2.4  misho     797: #if SUP_ENABLE == KQ_SUPPORT
1.1       misho     798:        if ((en = kevent(r->root_kq, NULL, 0, res, KQ_EVENTS, timeout)) == -1) {
1.27.2.5! misho     799: #elif SUP_ENABLE == EP_SUPPORT
        !           800:        if ((en = epoll_wait(r->root_kq, res, KQ_EVENTS, timeout)) == -1) {
1.25      misho     801: #else
                    802:        rfd = xfd = r->root_fds[0];
                    803:        wfd = r->root_fds[1];
                    804:        if ((en = select(r->root_kq, &rfd, &wfd, &xfd, timeout)) == -1) {
1.27.2.3  misho     805: #endif /* KQ_SUPPORT */
1.3       misho     806:                if (r->root_hooks.hook_exec.exception) {
                    807:                        if (r->root_hooks.hook_exec.exception(r, NULL))
                    808:                                return NULL;
1.6       misho     809:                } else if (errno != EINTR)
1.3       misho     810:                        LOGERR;
1.24      misho     811:                goto skip_event;
1.1       misho     812:        }
                    813: 
1.24      misho     814:        /* kevent dispatcher */
1.4       misho     815:        now.tv_sec = now.tv_nsec = 0;
1.1       misho     816:        /* Go and catch the cat into pipes ... */
1.27.2.4  misho     817: #if SUP_ENABLE == KQ_SUPPORT
1.1       misho     818:        for (i = 0; i < en; i++) {
                    819:                memcpy(evt, &res[i], sizeof evt);
                    820:                evt->flags = EV_DELETE;
                    821:                /* Put read/write task to ready queue */
                    822:                switch (res[i].filter) {
                    823:                        case EVFILT_READ:
1.9       misho     824:                                flg = 0;
1.6       misho     825:                                TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) {
1.3       misho     826:                                        if (TASK_FD(task) != ((intptr_t) res[i].udata))
1.1       misho     827:                                                continue;
1.14      misho     828:                                        else {
1.9       misho     829:                                                flg++;
1.14      misho     830:                                                TASK_RET(task) = res[i].data;
1.19      misho     831:                                                TASK_FLAG(task) = (u_long) res[i].fflags;
1.14      misho     832:                                        }
1.1       misho     833:                                        /* remove read handle */
1.4       misho     834: #ifdef HAVE_LIBPTHREAD
                    835:                                        pthread_mutex_lock(&r->root_mtx[taskREAD]);
                    836: #endif
1.1       misho     837:                                        TAILQ_REMOVE(&r->root_read, task, task_node);
1.4       misho     838: #ifdef HAVE_LIBPTHREAD
                    839:                                        pthread_mutex_unlock(&r->root_mtx[taskREAD]);
                    840: #endif
1.3       misho     841:                                        if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) {
                    842:                                                if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) {
                    843:                                                        task->task_type = taskUNUSE;
1.4       misho     844: #ifdef HAVE_LIBPTHREAD
                    845:                                                        pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
                    846: #endif
1.3       misho     847:                                                        TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
1.4       misho     848: #ifdef HAVE_LIBPTHREAD
                    849:                                                        pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
                    850: #endif
1.3       misho     851:                                                } else {
                    852:                                                        task->task_type = taskREADY;
1.4       misho     853: #ifdef HAVE_LIBPTHREAD
                    854:                                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                    855: #endif
1.3       misho     856:                                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
1.4       misho     857: #ifdef HAVE_LIBPTHREAD
                    858:                                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                    859: #endif
1.3       misho     860:                                                }
                    861:                                        } else {
1.2       misho     862:                                                task->task_type = taskREADY;
1.4       misho     863: #ifdef HAVE_LIBPTHREAD
                    864:                                                pthread_mutex_lock(&r->root_mtx[taskREADY]);
                    865: #endif
1.2       misho     866:                                                TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
1.4       misho     867: #ifdef HAVE_LIBPTHREAD
                    868:                                                pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                    869: #endif
1.3       misho     870:                                        }
1.1       misho     871:                                }
1.9       misho     872:                                /* if match at least 2, don't remove resouce of event */
                    873:                                if (flg > 1)
                    874:                                        evt->flags ^= evt->flags;
1.1       misho     875:                                break;
                    876:                        case EVFILT_WRITE:
1.9       misho     877:                                flg = 0;
1.6       misho     878:                                TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) {
1.3       misho     879:                                        if (TASK_FD(task) != ((intptr_t) res[i].udata))
1.1       misho     880:                                                continue;
1.14      misho     881:                                        else {
1.9       misho     882:                                                flg++;
1.14      misho     883:                                                TASK_RET(task) = res[i].data;
1.19      misho     884:                                                TASK_FLAG(task) = (u_long) res[i].fflags;
1.14      misho     885:                                        }
1.1       misho     886:                                        /* remove write handle */
1.4       misho     887: #ifdef HAVE_LIBPTHREAD
                    888:                                        pthread_mutex_lock(&r->root_mtx[taskWRITE]);
                    889: #endif
1.1       misho     890:                                        TAILQ_REMOVE(&r->root_write, task, task_node);
1.4       misho     891: #ifdef HAVE_LIBPTHREAD
                    892:                                        pthread_mutex_unlock(&r->root_mtx[taskWRITE]);
                    893: #endif
1.3       misho     894:                                        if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) {
                    895:                                                if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) {
                    896:                                                        task->task_type = taskUNUSE;
1.4       misho     897: #ifdef HAVE_LIBPTHREAD
                    898:                                                        pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
                    899: #endif
1.3       misho     900:                                                        TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
1.4       misho     901: #ifdef HAVE_LIBPTHREAD
                    902:                                                        pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
                    903: #endif
1.3       misho     904:                                                } else {
                    905:                                                        task->task_type = taskREADY;
1.4       misho     906: #ifdef HAVE_LIBPTHREAD
                    907:                                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                    908: #endif
1.3       misho     909:                                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
1.4       misho     910: #ifdef HAVE_LIBPTHREAD
                    911:                                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                    912: #endif
1.3       misho     913:                                                }
                    914:                                        } else {
1.2       misho     915:                                                task->task_type = taskREADY;
1.4       misho     916: #ifdef HAVE_LIBPTHREAD
                    917:                                                pthread_mutex_lock(&r->root_mtx[taskREADY]);
                    918: #endif
1.2       misho     919:                                                TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
1.4       misho     920: #ifdef HAVE_LIBPTHREAD
                    921:                                                pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                    922: #endif
1.3       misho     923:                                        }
1.1       misho     924:                                }
1.9       misho     925:                                /* if match at least 2, don't remove resouce of event */
                    926:                                if (flg > 1)
                    927:                                        evt->flags ^= evt->flags;
1.1       misho     928:                                break;
1.7       misho     929:                        case EVFILT_TIMER:
1.9       misho     930:                                flg = 0;
1.7       misho     931:                                TAILQ_FOREACH_SAFE(task, &r->root_alarm, task_node, tmp) {
                    932:                                        if ((uintptr_t) TASK_DATA(task) != ((uintptr_t) res[i].udata))
                    933:                                                continue;
1.14      misho     934:                                        else {
1.9       misho     935:                                                flg++;
1.14      misho     936:                                                TASK_RET(task) = res[i].data;
1.19      misho     937:                                                TASK_FLAG(task) = (u_long) res[i].fflags;
1.14      misho     938:                                        }
1.7       misho     939:                                        /* remove alarm handle */
                    940: #ifdef HAVE_LIBPTHREAD
                    941:                                        pthread_mutex_lock(&r->root_mtx[taskALARM]);
                    942: #endif
                    943:                                        TAILQ_REMOVE(&r->root_alarm, task, task_node);
                    944: #ifdef HAVE_LIBPTHREAD
                    945:                                        pthread_mutex_unlock(&r->root_mtx[taskALARM]);
                    946: #endif
                    947:                                        task->task_type = taskREADY;
                    948: #ifdef HAVE_LIBPTHREAD
                    949:                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                    950: #endif
                    951:                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                    952: #ifdef HAVE_LIBPTHREAD
                    953:                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                    954: #endif
                    955:                                }
1.9       misho     956:                                /* if match at least 2, don't remove resouce of event */
                    957:                                if (flg > 1)
                    958:                                        evt->flags ^= evt->flags;
1.7       misho     959:                                break;
1.8       misho     960:                        case EVFILT_VNODE:
1.9       misho     961:                                flg = 0;
1.8       misho     962:                                TAILQ_FOREACH_SAFE(task, &r->root_node, task_node, tmp) {
                    963:                                        if (TASK_FD(task) != ((intptr_t) res[i].udata))
                    964:                                                continue;
                    965:                                        else {
1.9       misho     966:                                                flg++;
1.14      misho     967:                                                TASK_RET(task) = res[i].data;
1.19      misho     968:                                                TASK_FLAG(task) = (u_long) res[i].fflags;
1.8       misho     969:                                        }
                    970:                                        /* remove node handle */
                    971: #ifdef HAVE_LIBPTHREAD
                    972:                                        pthread_mutex_lock(&r->root_mtx[taskNODE]);
                    973: #endif
                    974:                                        TAILQ_REMOVE(&r->root_node, task, task_node);
                    975: #ifdef HAVE_LIBPTHREAD
                    976:                                        pthread_mutex_unlock(&r->root_mtx[taskNODE]);
                    977: #endif
                    978:                                        task->task_type = taskREADY;
                    979: #ifdef HAVE_LIBPTHREAD
                    980:                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                    981: #endif
                    982:                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                    983: #ifdef HAVE_LIBPTHREAD
                    984:                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                    985: #endif
                    986:                                }
1.9       misho     987:                                /* if match at least 2, don't remove resouce of event */
                    988:                                if (flg > 1)
                    989:                                        evt->flags ^= evt->flags;
1.8       misho     990:                                break;
                    991:                        case EVFILT_PROC:
1.9       misho     992:                                flg = 0;
1.8       misho     993:                                TAILQ_FOREACH_SAFE(task, &r->root_proc, task_node, tmp) {
                    994:                                        if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
                    995:                                                continue;
                    996:                                        else {
1.9       misho     997:                                                flg++;
1.14      misho     998:                                                TASK_RET(task) = res[i].data;
1.19      misho     999:                                                TASK_FLAG(task) = (u_long) res[i].fflags;
1.8       misho    1000:                                        }
                   1001:                                        /* remove proc handle */
                   1002: #ifdef HAVE_LIBPTHREAD
                   1003:                                        pthread_mutex_lock(&r->root_mtx[taskPROC]);
                   1004: #endif
                   1005:                                        TAILQ_REMOVE(&r->root_proc, task, task_node);
                   1006: #ifdef HAVE_LIBPTHREAD
                   1007:                                        pthread_mutex_unlock(&r->root_mtx[taskPROC]);
                   1008: #endif
                   1009:                                        task->task_type = taskREADY;
                   1010: #ifdef HAVE_LIBPTHREAD
                   1011:                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1012: #endif
                   1013:                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                   1014: #ifdef HAVE_LIBPTHREAD
                   1015:                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1016: #endif
                   1017:                                }
1.9       misho    1018:                                /* if match at least 2, don't remove resouce of event */
                   1019:                                if (flg > 1)
                   1020:                                        evt->flags ^= evt->flags;
1.8       misho    1021:                                break;
                   1022:                        case EVFILT_SIGNAL:
1.9       misho    1023:                                flg = 0;
1.8       misho    1024:                                TAILQ_FOREACH_SAFE(task, &r->root_signal, task_node, tmp) {
                   1025:                                        if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
                   1026:                                                continue;
1.14      misho    1027:                                        else {
1.9       misho    1028:                                                flg++;
1.14      misho    1029:                                                TASK_RET(task) = res[i].data;
1.19      misho    1030:                                                TASK_FLAG(task) = (u_long) res[i].fflags;
1.14      misho    1031:                                        }
1.8       misho    1032:                                        /* remove signal handle */
                   1033: #ifdef HAVE_LIBPTHREAD
                   1034:                                        pthread_mutex_lock(&r->root_mtx[taskSIGNAL]);
                   1035: #endif
                   1036:                                        TAILQ_REMOVE(&r->root_signal, task, task_node);
                   1037: #ifdef HAVE_LIBPTHREAD
                   1038:                                        pthread_mutex_unlock(&r->root_mtx[taskSIGNAL]);
                   1039: #endif
                   1040:                                        task->task_type = taskREADY;
                   1041: #ifdef HAVE_LIBPTHREAD
                   1042:                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1043: #endif
                   1044:                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                   1045: #ifdef HAVE_LIBPTHREAD
                   1046:                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1047: #endif
                   1048:                                }
1.9       misho    1049:                                /* if match at least 2, don't remove resouce of event */
                   1050:                                if (flg > 1)
                   1051:                                        evt->flags ^= evt->flags;
1.8       misho    1052:                                break;
1.11      misho    1053: #ifdef AIO_SUPPORT
                   1054:                        case EVFILT_AIO:
                   1055:                                flg = 0;
                   1056:                                TAILQ_FOREACH_SAFE(task, &r->root_aio, task_node, tmp) {
                   1057:                                        acb = (struct aiocb*) TASK_VAL(task);
                   1058:                                        if (acb != ((struct aiocb*) res[i].udata))
                   1059:                                                continue;
1.14      misho    1060:                                        else {
1.11      misho    1061:                                                flg++;
1.14      misho    1062:                                                TASK_RET(task) = res[i].data;
1.19      misho    1063:                                                TASK_FLAG(task) = (u_long) res[i].fflags;
1.14      misho    1064:                                        }
1.11      misho    1065:                                        /* remove user handle */
                   1066: #ifdef HAVE_LIBPTHREAD
                   1067:                                        pthread_mutex_lock(&r->root_mtx[taskAIO]);
                   1068: #endif
                   1069:                                        TAILQ_REMOVE(&r->root_aio, task, task_node);
                   1070: #ifdef HAVE_LIBPTHREAD
                   1071:                                        pthread_mutex_unlock(&r->root_mtx[taskAIO]);
                   1072: #endif
                   1073:                                        task->task_type = taskREADY;
                   1074: #ifdef HAVE_LIBPTHREAD
                   1075:                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1076: #endif
                   1077:                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                   1078: #ifdef HAVE_LIBPTHREAD
                   1079:                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1080: #endif
                   1081:                                        fd = acb->aio_fildes;
                   1082:                                        if ((len = aio_return(acb)) != -1) {
                   1083:                                                if (lseek(fd, acb->aio_offset + len, SEEK_CUR) == -1)
                   1084:                                                        LOGERR;
                   1085:                                        } else
                   1086:                                                LOGERR;
                   1087:                                        free(acb);
                   1088:                                        TASK_DATLEN(task) = (u_long) len;
                   1089:                                        TASK_FD(task) = fd;
                   1090:                                }
                   1091:                                /* if match at least 2, don't remove resouce of event */
                   1092:                                if (flg > 1)
                   1093:                                        evt->flags ^= evt->flags;
                   1094:                                break;
                   1095: #ifdef EVFILT_LIO
                   1096:                        case EVFILT_LIO:
                   1097:                                flg = 0;
                   1098:                                TAILQ_FOREACH_SAFE(task, &r->root_lio, task_node, tmp) {
                   1099:                                        acbs = (struct aiocb**) TASK_VAL(task);
                   1100:                                        if (acbs != ((struct aiocb**) res[i].udata))
                   1101:                                                continue;
1.14      misho    1102:                                        else {
1.11      misho    1103:                                                flg++;
1.14      misho    1104:                                                TASK_RET(task) = res[i].data;
1.19      misho    1105:                                                TASK_FLAG(task) = (u_long) res[i].fflags;
1.14      misho    1106:                                        }
1.11      misho    1107:                                        /* remove user handle */
                   1108: #ifdef HAVE_LIBPTHREAD
                   1109:                                        pthread_mutex_lock(&r->root_mtx[taskLIO]);
                   1110: #endif
                   1111:                                        TAILQ_REMOVE(&r->root_lio, task, task_node);
                   1112: #ifdef HAVE_LIBPTHREAD
                   1113:                                        pthread_mutex_unlock(&r->root_mtx[taskLIO]);
                   1114: #endif
                   1115:                                        task->task_type = taskREADY;
                   1116: #ifdef HAVE_LIBPTHREAD
                   1117:                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1118: #endif
                   1119:                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                   1120: #ifdef HAVE_LIBPTHREAD
                   1121:                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1122: #endif
                   1123:                                        iv = (struct iovec*) TASK_DATA(task);
                   1124:                                        fd = acbs[0]->aio_fildes;
                   1125:                                        off = acbs[0]->aio_offset;
                   1126:                                        for (j = len = 0; i < TASK_DATLEN(task); len += l, i++) {
                   1127:                                                if ((iv[i].iov_len = aio_return(acbs[i])) == -1)
                   1128:                                                        l = 0;
                   1129:                                                else
                   1130:                                                        l = iv[i].iov_len;
                   1131:                                                free(acbs[i]);
                   1132:                                        }
                   1133:                                        free(acbs);
                   1134:                                        TASK_DATLEN(task) = (u_long) len;
                   1135:                                        TASK_FD(task) = fd;
                   1136: 
                   1137:                                        if (lseek(fd, off + len, SEEK_CUR) == -1)
                   1138:                                                LOGERR;
                   1139:                                }
                   1140:                                /* if match at least 2, don't remove resouce of event */
                   1141:                                if (flg > 1)
                   1142:                                        evt->flags ^= evt->flags;
                   1143:                                break;
                   1144: #endif /* EVFILT_LIO */
                   1145: #endif /* AIO_SUPPORT */
1.8       misho    1146: #ifdef EVFILT_USER
                   1147:                        case EVFILT_USER:
1.9       misho    1148:                                flg = 0;
1.8       misho    1149:                                TAILQ_FOREACH_SAFE(task, &r->root_user, task_node, tmp) {
                   1150:                                        if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
                   1151:                                                continue;
                   1152:                                        else {
1.9       misho    1153:                                                flg++;
1.14      misho    1154:                                                TASK_RET(task) = res[i].data;
1.19      misho    1155:                                                TASK_FLAG(task) = (u_long) res[i].fflags;
1.8       misho    1156:                                        }
                   1157:                                        /* remove user handle */
                   1158: #ifdef HAVE_LIBPTHREAD
                   1159:                                        pthread_mutex_lock(&r->root_mtx[taskUSER]);
                   1160: #endif
                   1161:                                        TAILQ_REMOVE(&r->root_user, task, task_node);
                   1162: #ifdef HAVE_LIBPTHREAD
                   1163:                                        pthread_mutex_unlock(&r->root_mtx[taskUSER]);
                   1164: #endif
                   1165:                                        task->task_type = taskREADY;
                   1166: #ifdef HAVE_LIBPTHREAD
                   1167:                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1168: #endif
                   1169:                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                   1170: #ifdef HAVE_LIBPTHREAD
                   1171:                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1172: #endif
                   1173:                                }
1.9       misho    1174:                                /* if match at least 2, don't remove resouce of event */
                   1175:                                if (flg > 1)
                   1176:                                        evt->flags ^= evt->flags;
1.8       misho    1177:                                break;
1.11      misho    1178: #endif /* EVFILT_USER */
1.1       misho    1179:                }
1.4       misho    1180:                if (kevent(r->root_kq, evt, 1, NULL, 0, &now) == -1) {
1.3       misho    1181:                        if (r->root_hooks.hook_exec.exception) {
                   1182:                                if (r->root_hooks.hook_exec.exception(r, NULL))
                   1183:                                        return NULL;
                   1184:                        } else
                   1185:                                LOGERR;
                   1186:                }
1.25      misho    1187:        }
                   1188: #else  /* end of kevent dispatcher */
                   1189:        for (i = 0; i < r->root_kq; i++) {
                   1190:                if (FD_ISSET(i, &rfd) || FD_ISSET(i, &xfd)) {
                   1191:                        flg = 0;
                   1192:                        TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) {
                   1193:                                if (TASK_FD(task) != i)
                   1194:                                        continue;
                   1195:                                else {
                   1196:                                        flg++;
1.26      misho    1197:                                        TASK_FLAG(task) = ioctl(TASK_FD(task), 
                   1198:                                                        FIONREAD, &TASK_RET(task));
1.25      misho    1199:                                }
                   1200:                                /* remove read handle */
                   1201: #ifdef HAVE_LIBPTHREAD
                   1202:                                pthread_mutex_lock(&r->root_mtx[taskREAD]);
                   1203: #endif
                   1204:                                TAILQ_REMOVE(&r->root_read, task, task_node);
                   1205: #ifdef HAVE_LIBPTHREAD
                   1206:                                pthread_mutex_unlock(&r->root_mtx[taskREAD]);
                   1207: #endif
                   1208:                                if (r->root_hooks.hook_exec.exception) {
                   1209:                                        if (r->root_hooks.hook_exec.exception(r, NULL)) {
                   1210:                                                task->task_type = taskUNUSE;
                   1211: #ifdef HAVE_LIBPTHREAD
                   1212:                                                pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
                   1213: #endif
                   1214:                                                TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
                   1215: #ifdef HAVE_LIBPTHREAD
                   1216:                                                pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
                   1217: #endif
                   1218:                                        } else {
                   1219:                                                task->task_type = taskREADY;
                   1220: #ifdef HAVE_LIBPTHREAD
                   1221:                                                pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1222: #endif
                   1223:                                                TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                   1224: #ifdef HAVE_LIBPTHREAD
                   1225:                                                pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1226: #endif
                   1227:                                        }
                   1228:                                } else {
                   1229:                                        task->task_type = taskREADY;
                   1230: #ifdef HAVE_LIBPTHREAD
                   1231:                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1232: #endif
                   1233:                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                   1234: #ifdef HAVE_LIBPTHREAD
                   1235:                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1236: #endif
                   1237:                                }
                   1238:                        }
                   1239:                        /* if match equal to 1, remove resouce */
                   1240:                        if (flg == 1)
                   1241:                                FD_CLR(i, &r->root_fds[0]);
                   1242:                }
                   1243: 
                   1244:                if (FD_ISSET(i, &wfd)) {
                   1245:                        flg = 0;
                   1246:                        TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) {
                   1247:                                if (TASK_FD(task) != i)
                   1248:                                        continue;
                   1249:                                else {
                   1250:                                        flg++;
1.26      misho    1251:                                        TASK_FLAG(task) = ioctl(TASK_FD(task), 
                   1252:                                                        FIONWRITE, &TASK_RET(task));
1.25      misho    1253:                                }
                   1254:                                /* remove write handle */
                   1255: #ifdef HAVE_LIBPTHREAD
                   1256:                                pthread_mutex_lock(&r->root_mtx[taskWRITE]);
                   1257: #endif
                   1258:                                TAILQ_REMOVE(&r->root_write, task, task_node);
                   1259: #ifdef HAVE_LIBPTHREAD
                   1260:                                pthread_mutex_unlock(&r->root_mtx[taskWRITE]);
                   1261: #endif
                   1262:                                if (r->root_hooks.hook_exec.exception) {
                   1263:                                        if (r->root_hooks.hook_exec.exception(r, NULL)) {
                   1264:                                                task->task_type = taskUNUSE;
                   1265: #ifdef HAVE_LIBPTHREAD
                   1266:                                                pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
                   1267: #endif
                   1268:                                                TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
                   1269: #ifdef HAVE_LIBPTHREAD
                   1270:                                                pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
                   1271: #endif
                   1272:                                        } else {
                   1273:                                                task->task_type = taskREADY;
                   1274: #ifdef HAVE_LIBPTHREAD
                   1275:                                                pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1276: #endif
                   1277:                                                TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                   1278: #ifdef HAVE_LIBPTHREAD
                   1279:                                                pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1280: #endif
                   1281:                                        }
                   1282:                                } else {
                   1283:                                        task->task_type = taskREADY;
                   1284: #ifdef HAVE_LIBPTHREAD
                   1285:                                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1286: #endif
                   1287:                                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
                   1288: #ifdef HAVE_LIBPTHREAD
                   1289:                                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1290: #endif
                   1291:                                }
                   1292:                        }
                   1293:                        /* if match equal to 1, remove resouce */
                   1294:                        if (flg == 1)
                   1295:                                FD_CLR(i, &r->root_fds[1]);
                   1296:                }
                   1297:        }
                   1298: 
                   1299:        /* optimize select */
                   1300:        for (i = r->root_kq - 1; i > 2; i--)
                   1301:                if (FD_ISSET(i, &r->root_fds[0]) || FD_ISSET(i, &r->root_fds[1]))
                   1302:                        break;
                   1303:        if (i > 2)
                   1304:                r->root_kq = i + 1;
1.27.2.3  misho    1305: #endif /* KQ_SUPPORT */
1.1       misho    1306: 
1.24      misho    1307: skip_event:
1.2       misho    1308:        /* timer update & put in ready queue */
1.4       misho    1309:        clock_gettime(CLOCK_MONOTONIC, &now);
1.1       misho    1310: 
1.6       misho    1311:        TAILQ_FOREACH_SAFE(task, &r->root_timer, task_node, tmp)
1.4       misho    1312:                if (sched_timespeccmp(&now, &TASK_TS(task), -) >= 0) {
                   1313: #ifdef HAVE_LIBPTHREAD
                   1314:                        pthread_mutex_lock(&r->root_mtx[taskTIMER]);
                   1315: #endif
1.1       misho    1316:                        TAILQ_REMOVE(&r->root_timer, task, task_node);
1.4       misho    1317: #ifdef HAVE_LIBPTHREAD
                   1318:                        pthread_mutex_unlock(&r->root_mtx[taskTIMER]);
                   1319: #endif
1.1       misho    1320:                        task->task_type = taskREADY;
1.4       misho    1321: #ifdef HAVE_LIBPTHREAD
                   1322:                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1323: #endif
1.1       misho    1324:                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
1.4       misho    1325: #ifdef HAVE_LIBPTHREAD
                   1326:                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1327: #endif
1.1       misho    1328:                }
                   1329: 
1.12      misho    1330:        /* put regular task priority task to ready queue, 
1.13      misho    1331:                if there is no ready task or reach max missing hit for regular task */
1.12      misho    1332:        if ((task = TAILQ_FIRST(&r->root_task))) {
1.13      misho    1333:                if (!TAILQ_FIRST(&r->root_ready) || r->root_miss >= TASK_VAL(task)) {
                   1334:                        r->root_miss ^= r->root_miss;
1.1       misho    1335: 
1.4       misho    1336: #ifdef HAVE_LIBPTHREAD
1.12      misho    1337:                        pthread_mutex_lock(&r->root_mtx[taskTASK]);
1.4       misho    1338: #endif
1.12      misho    1339:                        TAILQ_REMOVE(&r->root_task, task, task_node);
1.4       misho    1340: #ifdef HAVE_LIBPTHREAD
1.12      misho    1341:                        pthread_mutex_unlock(&r->root_mtx[taskTASK]);
1.4       misho    1342: #endif
1.1       misho    1343:                        task->task_type = taskREADY;
1.4       misho    1344: #ifdef HAVE_LIBPTHREAD
                   1345:                        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1346: #endif
1.1       misho    1347:                        TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
1.4       misho    1348: #ifdef HAVE_LIBPTHREAD
                   1349:                        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1350: #endif
1.1       misho    1351:                } else
1.13      misho    1352:                        r->root_miss++;
1.1       misho    1353:        } else
1.13      misho    1354:                r->root_miss ^= r->root_miss;
1.1       misho    1355: 
                   1356:        /* OK, lets get ready task !!! */
1.6       misho    1357:        task = TAILQ_FIRST(&r->root_ready);
                   1358:        if (!(task))
                   1359:                return NULL;
                   1360: 
1.4       misho    1361: #ifdef HAVE_LIBPTHREAD
                   1362:        pthread_mutex_lock(&r->root_mtx[taskREADY]);
                   1363: #endif
1.1       misho    1364:        TAILQ_REMOVE(&r->root_ready, task, task_node);
1.4       misho    1365: #ifdef HAVE_LIBPTHREAD
                   1366:        pthread_mutex_unlock(&r->root_mtx[taskREADY]);
                   1367: #endif
1.1       misho    1368:        task->task_type = taskUNUSE;
1.4       misho    1369: #ifdef HAVE_LIBPTHREAD
                   1370:        pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
                   1371: #endif
1.1       misho    1372:        TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
1.4       misho    1373: #ifdef HAVE_LIBPTHREAD
                   1374:        pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
                   1375: #endif
1.1       misho    1376:        return task;
                   1377: }
1.3       misho    1378: 
                   1379: /*
                   1380:  * sched_hook_exception() - Default EXCEPTION hook
1.5       misho    1381:  *
1.3       misho    1382:  * @root = root task
                   1383:  * @arg = custom handling: if arg == EV_EOF or other value; default: arg == NULL log errno
                   1384:  * return: <0 errors and 0 ok
                   1385:  */
                   1386: void *
                   1387: sched_hook_exception(void *root, void *arg)
                   1388: {
                   1389:        sched_root_task_t *r = root;
                   1390: 
1.6       misho    1391:        if (!r)
1.3       misho    1392:                return NULL;
                   1393: 
                   1394:        /* custom exception handling ... */
                   1395:        if (arg) {
                   1396:                if (arg == (void*) EV_EOF)
                   1397:                        return NULL;
                   1398:                return (void*) -1;      /* raise scheduler error!!! */
                   1399:        }
                   1400: 
                   1401:        /* if error hook exists */
                   1402:        if (r->root_hooks.hook_root.error)
                   1403:                return (r->root_hooks.hook_root.error(root, (void*) ((intptr_t) errno)));
                   1404: 
                   1405:        /* default case! */
                   1406:        LOGERR;
                   1407:        return NULL;
                   1408: }
1.5       misho    1409: 
                   1410: /*
                   1411:  * sched_hook_condition() - Default CONDITION hook
                   1412:  *
                   1413:  * @root = root task
                   1414:  * @arg = killState from schedRun()
                   1415:  * return: NULL kill scheduler loop or !=NULL ok
                   1416:  */
                   1417: void *
                   1418: sched_hook_condition(void *root, void *arg)
                   1419: {
                   1420:        sched_root_task_t *r = root;
                   1421: 
1.6       misho    1422:        if (!r)
1.5       misho    1423:                return NULL;
                   1424: 
                   1425:        return (void*) (r->root_cond - *(intptr_t*) arg);
                   1426: }
1.19      misho    1427: 
                   1428: /*
                   1429:  * sched_hook_rtc() - Default RTC hook
                   1430:  *
                   1431:  * @task = current task
                   1432:  * @arg = unused
                   1433:  * return: <0 errors and 0 ok
                   1434:  */
1.27      misho    1435: #if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME) && defined(HAVE_TIMER_DELETE)
1.19      misho    1436: void *
                   1437: sched_hook_rtc(void *task, void *arg __unused)
                   1438: {
                   1439:        sched_task_t *sigt = NULL, *t = task;
                   1440:        struct itimerspec its;
                   1441:        struct sigevent evt;
                   1442:        timer_t tmr;
                   1443: 
                   1444:        if (!t || !TASK_ROOT(t))
                   1445:                return (void*) -1;
                   1446: 
                   1447:        memset(&evt, 0, sizeof evt);
                   1448:        evt.sigev_notify = SIGEV_SIGNAL;
1.20      misho    1449:        evt.sigev_signo = (intptr_t) TASK_DATA(t) + SIGRTMIN;
1.19      misho    1450:        evt.sigev_value.sival_ptr = TASK_DATA(t);
                   1451: 
                   1452:        if (timer_create(CLOCK_MONOTONIC, &evt, &tmr) == -1) {
                   1453:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                   1454:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
                   1455:                else
                   1456:                        LOGERR;
                   1457:                return (void*) -1;
                   1458:        } else
                   1459:                TASK_FLAG(t) = (u_long) tmr;
                   1460: 
1.21      misho    1461:        if (!(sigt = schedSignal(TASK_ROOT(t), _sched_rtcWrapper, TASK_ARG(t), evt.sigev_signo, 
                   1462:                                t, (size_t) tmr))) {
1.19      misho    1463:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                   1464:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
                   1465:                else
                   1466:                        LOGERR;
                   1467:                timer_delete(tmr);
                   1468:                return (void*) -1;
                   1469:        } else
                   1470:                TASK_RET(t) = (uintptr_t) sigt;
                   1471: 
                   1472:        memset(&its, 0, sizeof its);
                   1473:        its.it_value.tv_sec = t->task_val.ts.tv_sec;
                   1474:        its.it_value.tv_nsec = t->task_val.ts.tv_nsec;
                   1475: 
                   1476:        if (timer_settime(tmr, TIMER_RELTIME, &its, NULL) == -1) {
                   1477:                if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
                   1478:                        TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
                   1479:                else
                   1480:                        LOGERR;
                   1481:                schedCancel(sigt);
                   1482:                timer_delete(tmr);
                   1483:                return (void*) -1;
                   1484:        }
                   1485: 
                   1486:        return NULL;
                   1487: }
                   1488: #endif /* HAVE_TIMER_CREATE */

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