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

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

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