File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / hooks.c
Revision 1.24.4.1: download - view: text, annotated - select for diffs - revision graph
Mon Jan 27 16:52:56 2014 UTC (10 years, 8 months ago) by misho
Branches: sched4_7
Diff to: branchpoint 1.24: preferred, unified
add feature for disable kqueue support

    1: /*************************************************************************
    2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: hooks.c,v 1.24.4.1 2014/01/27 16:52:56 misho Exp $
    7: *
    8: **************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
   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
   52:  *
   53:  * @root = root task
   54:  * @arg = unused
   55:  * return: <0 errors and 0 ok
   56:  */
   57: void *
   58: sched_hook_init(void *root, void *arg __unused)
   59: {
   60: 	sched_root_task_t *r = root;
   61: 
   62: 	if (!r)
   63: 		return (void*) -1;
   64: 
   65: #ifndef KQ_DISABLE
   66: 	r->root_kq = kqueue();
   67: 	if (r->root_kq == -1) {
   68: 		LOGERR;
   69: 		return (void*) -1;
   70: 	}
   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
   76: 
   77: 	return NULL;
   78: }
   79: 
   80: /*
   81:  * sched_hook_fini() - Default FINI hook
   82:  *
   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: 
   95: #ifndef KQ_DISABLE
   96: 	if (r->root_kq > 2) {
   97: 		close(r->root_kq);
   98: 		r->root_kq = 0;
   99: 	}
  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
  105: 
  106: 	return NULL;
  107: }
  108: 
  109: /*
  110:  * sched_hook_cancel() - Default CANCEL hook
  111:  *
  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;
  120: #ifndef KQ_DISABLE
  121: 	struct kevent chg[1];
  122: 	struct timespec timeout = { 0, 0 };
  123: #else
  124: 	sched_root_task_t *r = NULL;
  125: 	register int i;
  126: #endif
  127: #ifdef AIO_SUPPORT
  128: 	struct aiocb *acb;
  129: #ifdef EVFILT_LIO
  130: 	register int i = 0;
  131: 	struct aiocb **acbs;
  132: #endif	/* EVFILT_LIO */
  133: #endif	/* AIO_SUPPORT */
  134: 
  135: 	if (!t || !TASK_ROOT(t))
  136: 		return (void*) -1;
  137: #ifdef KQ_DISABLE
  138: 	r = TASK_ROOT(t);
  139: #endif
  140: 
  141: 	switch (TASK_TYPE(t)) {
  142: 		case taskREAD:
  143: #ifndef KQ_DISABLE
  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
  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
  159: 			break;
  160: 		case taskWRITE:
  161: #ifndef KQ_DISABLE
  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
  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
  177: 			break;
  178: 		case taskALARM:
  179: #ifndef KQ_DISABLE
  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
  187: #endif
  188: 			break;
  189: 		case taskNODE:
  190: #ifndef KQ_DISABLE
  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
  196: #endif
  197: 			break;
  198: 		case taskPROC:
  199: #ifndef KQ_DISABLE
  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
  205: #endif
  206: 			break;
  207: 		case taskSIGNAL:
  208: #ifndef KQ_DISABLE
  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
  214: 			/* restore signal */
  215: 			signal(TASK_VAL(t), SIG_DFL);
  216: #endif
  217: 			break;
  218: #ifdef AIO_SUPPORT
  219: 		case taskAIO:
  220: #ifndef KQ_DISABLE
  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: 			}
  233: #endif
  234: 			break;
  235: #ifdef EVFILT_LIO
  236: 		case taskLIO:
  237: #ifndef KQ_DISABLE
  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: 			}
  253: #endif
  254: 			break;
  255: #endif	/* EVFILT_LIO */
  256: #endif	/* AIO_SUPPORT */
  257: #ifdef EVFILT_USER
  258: 		case taskUSER:
  259: #ifndef KQ_DISABLE
  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
  265: #endif
  266: 			break;
  267: #endif	/* EVFILT_USER */
  268: 		case taskTHREAD:
  269: #ifdef HAVE_LIBPTHREAD
  270: 			pthread_cancel((pthread_t) TASK_VAL(t));
  271: #endif
  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 */
  279: 		default:
  280: 			return NULL;
  281: 	}
  282: 
  283: #ifndef KQ_DISABLE
  284: 	kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout);
  285: #endif
  286: 	return NULL;
  287: }
  288: 
  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;
  302: 	sigset_t s, o;
  303: 
  304: 	if (!t || !TASK_ROOT(t))
  305: 		return (void*) -1;
  306: 
  307: 	sigfillset(&s);
  308: 	pthread_sigmask(SIG_BLOCK, &s, &o);
  309: 	if ((errno = pthread_create(&tid, (pthread_attr_t*) arg, 
  310: 				(void *(*)(void*)) _sched_threadWrapper, t))) {
  311: 		LOGERR;
  312: 		pthread_sigmask(SIG_SETMASK, &o, NULL);
  313: 		return (void*) -1;
  314: 	} else
  315: 		TASK_VAL(t) = (u_long) tid;
  316: 
  317: 	if (!TASK_ISLOCKED(t))
  318: 		TASK_LOCK(t);
  319: 
  320: 	pthread_sigmask(SIG_SETMASK, &o, NULL);
  321: 	return NULL;
  322: }
  323: #endif
  324: 
  325: /*
  326:  * sched_hook_read() - Default READ hook
  327:  *
  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;
  336: #ifndef KQ_DISABLE
  337: 	struct kevent chg[1];
  338: 	struct timespec timeout = { 0, 0 };
  339: #else
  340: 	sched_root_task_t *r = NULL;
  341: #endif
  342: 
  343: 	if (!t || !TASK_ROOT(t))
  344: 		return (void*) -1;
  345: #ifdef KQ_DISABLE
  346: 	r = TASK_ROOT(t);
  347: #endif
  348: 
  349: #ifndef KQ_DISABLE
  350: #ifdef __NetBSD__
  351: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t));
  352: #else
  353: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t));
  354: #endif
  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);
  358: 		else
  359: 			LOGERR;
  360: 		return (void*) -1;
  361: 	}
  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
  367: 
  368: 	return NULL;
  369: }
  370: 
  371: /*
  372:  * sched_hook_write() - Default WRITE hook
  373:  *
  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;
  382: #ifndef KQ_DISABLE
  383: 	struct kevent chg[1];
  384: 	struct timespec timeout = { 0, 0 };
  385: #else
  386: 	sched_root_task_t *r = NULL;
  387: #endif
  388: 
  389: 	if (!t || !TASK_ROOT(t))
  390: 		return (void*) -1;
  391: #ifdef KQ_DISABLE
  392: 	r = TASK_ROOT(t);
  393: #endif
  394: 
  395: #ifndef KQ_DISABLE
  396: #ifdef __NetBSD__
  397: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t));
  398: #else
  399: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t));
  400: #endif
  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);
  404: 		else
  405: 			LOGERR;
  406: 		return (void*) -1;
  407: 	}
  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
  413: 
  414: 	return NULL;
  415: }
  416: 
  417: /*
  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: {
  427: #ifndef KQ_DISABLE
  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__
  436: 	EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_ADD | EV_CLEAR, 0, 
  437: 			t->task_val.ts.tv_sec * 1000 + t->task_val.ts.tv_nsec / 1000000, 
  438: 			(intptr_t) TASK_DATA(t));
  439: #else
  440: 	EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_ADD | EV_CLEAR, 0, 
  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: 
  452: #endif
  453: 	return NULL;
  454: }
  455: 
  456: /*
  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: {
  466: #ifndef KQ_DISABLE
  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: 
  491: #endif
  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: {
  505: #ifndef KQ_DISABLE
  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: 
  528: #endif
  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: {
  542: #ifndef KQ_DISABLE
  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: 
  550: 	/* ignore signal */
  551: 	signal(TASK_VAL(t), SIG_IGN);
  552: 
  553: #ifdef __NetBSD__
  554: 	EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_VAL(t));
  555: #else
  556: 	EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_VAL(t));
  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: 
  566: #endif
  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: {
  581: #ifndef KQ_DISABLE
  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: 
  604: #endif
  605: 	return NULL;
  606: }
  607: #endif
  608: 
  609: /*
  610:  * sched_hook_fetch() - Default FETCH hook
  611:  *
  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;
  620: 	sched_task_t *task, *tmp;
  621: 	struct timespec now;
  622: #ifndef KQ_DISABLE
  623: 	struct kevent evt[1], res[KQ_EVENTS];
  624: 	struct timespec *timeout, m, mtmp;
  625: #else
  626: 	struct timeval *timeout, m, mtmp, now2;
  627: #endif
  628: 	register int i, flg;
  629: 	int en;
  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 */
  641: 
  642: 	if (!r)
  643: 		return NULL;
  644: 
  645: 	/* get new task by queue priority */
  646: 	while ((task = TAILQ_FIRST(&r->root_event))) {
  647: #ifdef HAVE_LIBPTHREAD
  648: 		pthread_mutex_lock(&r->root_mtx[taskEVENT]);
  649: #endif
  650: 		TAILQ_REMOVE(&r->root_event, task, task_node);
  651: #ifdef HAVE_LIBPTHREAD
  652: 		pthread_mutex_unlock(&r->root_mtx[taskEVENT]);
  653: #endif
  654: 		task->task_type = taskUNUSE;
  655: #ifdef HAVE_LIBPTHREAD
  656: 		pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  657: #endif
  658: 		TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  659: #ifdef HAVE_LIBPTHREAD
  660: 		pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  661: #endif
  662: 		return task;
  663: 	}
  664: 	while ((task = TAILQ_FIRST(&r->root_ready))) {
  665: #ifdef HAVE_LIBPTHREAD
  666: 		pthread_mutex_lock(&r->root_mtx[taskREADY]);
  667: #endif
  668: 		TAILQ_REMOVE(&r->root_ready, task, task_node);
  669: #ifdef HAVE_LIBPTHREAD
  670: 		pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  671: #endif
  672: 		task->task_type = taskUNUSE;
  673: #ifdef HAVE_LIBPTHREAD
  674: 		pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  675: #endif
  676: 		TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  677: #ifdef HAVE_LIBPTHREAD
  678: 		pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  679: #endif
  680: 		return task;
  681: 	}
  682: 
  683: #ifdef TIMER_WITHOUT_SORT
  684: 	clock_gettime(CLOCK_MONOTONIC, &now);
  685: 
  686: #ifndef KQ_DISABLE
  687: 	sched_timespecclear(&r->root_wait);
  688: 	TAILQ_FOREACH(task, &r->root_timer, task_node) {
  689: 		if (!sched_timespecisset(&r->root_wait))
  690: 			r->root_wait = TASK_TS(task);
  691: 		else if (sched_timespeccmp(&TASK_TS(task), &r->root_wait, -) < 0)
  692: 			r->root_wait = TASK_TS(task);
  693: 	}
  694: #else
  695: 	sched_timevalclear(&r->root_wait);
  696: 	TAILQ_FOREACH(task, &r->root_timer, task_node) {
  697: 		if (!sched_timevalisset(&r->root_wait))
  698: 			TASK_TS2TV(task, &r->root_wait);
  699: 		else {
  700: 			TASK_TS2TV(task, &m);
  701: 			if (sched_timevalcmp(&m, &r->root_wait, -) < 0)
  702: 				TASK_TS2TV(task, &r->root_wait);
  703: 		}
  704: 	}
  705: #endif	/* KQ_DISABLE */
  706: 
  707: 	if (TAILQ_FIRST(&r->root_timer)) {
  708: 		m = r->root_wait;
  709: #ifndef KQ_DISABLE
  710: 		sched_timespecsub(&m, &now, &mtmp);
  711: #else
  712: 		sched_timespec2val(&now, &now2);
  713: 		sched_timevalsub(&m, &now2, &mtmp);
  714: #endif	/* KQ_DISABLE */
  715: 		r->root_wait = mtmp;
  716: 	} else {
  717: 		/* set wait INFTIM */
  718: #ifndef KQ_DISABLE
  719: 		sched_timespecinf(&r->root_wait);
  720: #else
  721: 		sched_timevalinf(&r->root_wait);
  722: #endif	/* KQ_DISABLE */
  723: 	}
  724: #else	/* ! TIMER_WITHOUT_SORT */
  725: 	if (!TAILQ_FIRST(&r->root_task) && (task = TAILQ_FIRST(&r->root_timer))) {
  726: 		clock_gettime(CLOCK_MONOTONIC, &now);
  727: 
  728: #ifndef KQ_DISABLE
  729: 		m = TASK_TS(task);
  730: 		sched_timespecsub(&m, &now, &mtmp);
  731: #else
  732: 		TASK_TS2TV(task, &m);
  733: 		sched_timespec2val(&now, &now2);
  734: 		sched_timevalsub(&m, &now2, &mtmp);
  735: #endif	/* KQ_DISABLE */
  736: 		r->root_wait = mtmp;
  737: 	} else {
  738: 		/* set wait INFTIM */
  739: #ifndef KQ_DISABLE
  740: 		sched_timespecinf(&r->root_wait);
  741: #else
  742: 		sched_timevalinf(&r->root_wait);
  743: #endif	/* KQ_DISABLE */
  744: 	}
  745: #endif	/* TIMER_WITHOUT_SORT */
  746: 	/* if present member of task, set NOWAIT */
  747: 	if (TAILQ_FIRST(&r->root_task))
  748: #ifndef KQ_DISABLE
  749: 		sched_timespecclear(&r->root_wait);
  750: #else
  751: 		sched_timevalclear(&r->root_wait);
  752: #endif	/* KQ_DISABLE */
  753: 
  754: #ifndef KQ_DISABLE
  755: 	if (r->root_wait.tv_sec != -1 && r->root_wait.tv_nsec != -1)
  756: #else
  757: 	if (r->root_wait.tv_sec != -1 && r->root_wait.tv_usec != -1)
  758: #endif	/* KQ_DISABLE */
  759: 		timeout = &r->root_wait;
  760: #ifndef KQ_DISABLE
  761: 	else if (sched_timespecisinf(&r->root_poll))
  762: #else
  763: 	else if (sched_timevalisinf(&r->root_poll))
  764: #endif	/* KQ_DISABLE */
  765: 		timeout = NULL;
  766: 	else
  767: 		timeout = &r->root_poll;
  768: 
  769: #ifndef KQ_DISABLE
  770: 	if ((en = kevent(r->root_kq, NULL, 0, res, KQ_EVENTS, timeout)) == -1) {
  771: #else
  772: 	if ((en = select(r->root_kq, &r->root_fds[0], &r->root_fds[1], 
  773: 					&r->root_fds[0], timeout)) == -1) {
  774: #endif	/* KQ_DISABLE */
  775: 		if (r->root_hooks.hook_exec.exception) {
  776: 			if (r->root_hooks.hook_exec.exception(r, NULL))
  777: 				return NULL;
  778: 		} else if (errno != EINTR)
  779: 			LOGERR;
  780: 		goto skip_event;
  781: 	}
  782: 
  783: 	/* kevent dispatcher */
  784: 	now.tv_sec = now.tv_nsec = 0;
  785: 	/* Go and catch the cat into pipes ... */
  786: 	for (i = 0; i < en; i++) {
  787: #ifndef KQ_DISABLE
  788: 		memcpy(evt, &res[i], sizeof evt);
  789: 		evt->flags = EV_DELETE;
  790: 		/* Put read/write task to ready queue */
  791: 		switch (res[i].filter) {
  792: 			case EVFILT_READ:
  793: 				flg = 0;
  794: 				TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) {
  795: 					if (TASK_FD(task) != ((intptr_t) res[i].udata))
  796: 						continue;
  797: 					else {
  798: 						flg++;
  799: 						TASK_RET(task) = res[i].data;
  800: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  801: 					}
  802: 					/* remove read handle */
  803: #ifdef HAVE_LIBPTHREAD
  804: 					pthread_mutex_lock(&r->root_mtx[taskREAD]);
  805: #endif
  806: 					TAILQ_REMOVE(&r->root_read, task, task_node);
  807: #ifdef HAVE_LIBPTHREAD
  808: 					pthread_mutex_unlock(&r->root_mtx[taskREAD]);
  809: #endif
  810: 					if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) {
  811:  						if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) {
  812: 							task->task_type = taskUNUSE;
  813: #ifdef HAVE_LIBPTHREAD
  814: 							pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  815: #endif
  816: 							TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  817: #ifdef HAVE_LIBPTHREAD
  818: 							pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  819: #endif
  820: 						} else {
  821: 							task->task_type = taskREADY;
  822: #ifdef HAVE_LIBPTHREAD
  823: 							pthread_mutex_lock(&r->root_mtx[taskREADY]);
  824: #endif
  825: 							TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  826: #ifdef HAVE_LIBPTHREAD
  827: 							pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  828: #endif
  829: 						}
  830: 					} else {
  831: 						task->task_type = taskREADY;
  832: #ifdef HAVE_LIBPTHREAD
  833: 						pthread_mutex_lock(&r->root_mtx[taskREADY]);
  834: #endif
  835: 						TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  836: #ifdef HAVE_LIBPTHREAD
  837: 						pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  838: #endif
  839: 					}
  840: 				}
  841: 				/* if match at least 2, don't remove resouce of event */
  842: 				if (flg > 1)
  843: 					evt->flags ^= evt->flags;
  844: 				break;
  845: 			case EVFILT_WRITE:
  846: 				flg = 0;
  847: 				TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) {
  848: 					if (TASK_FD(task) != ((intptr_t) res[i].udata))
  849: 						continue;
  850: 					else {
  851: 						flg++;
  852: 						TASK_RET(task) = res[i].data;
  853: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  854: 					}
  855: 					/* remove write handle */
  856: #ifdef HAVE_LIBPTHREAD
  857: 					pthread_mutex_lock(&r->root_mtx[taskWRITE]);
  858: #endif
  859: 					TAILQ_REMOVE(&r->root_write, task, task_node);
  860: #ifdef HAVE_LIBPTHREAD
  861: 					pthread_mutex_unlock(&r->root_mtx[taskWRITE]);
  862: #endif
  863: 					if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) {
  864:  						if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) {
  865: 							task->task_type = taskUNUSE;
  866: #ifdef HAVE_LIBPTHREAD
  867: 							pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  868: #endif
  869: 							TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  870: #ifdef HAVE_LIBPTHREAD
  871: 							pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  872: #endif
  873: 						} else {
  874: 							task->task_type = taskREADY;
  875: #ifdef HAVE_LIBPTHREAD
  876: 							pthread_mutex_lock(&r->root_mtx[taskREADY]);
  877: #endif
  878: 							TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  879: #ifdef HAVE_LIBPTHREAD
  880: 							pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  881: #endif
  882: 						}
  883: 					} else {
  884: 						task->task_type = taskREADY;
  885: #ifdef HAVE_LIBPTHREAD
  886: 						pthread_mutex_lock(&r->root_mtx[taskREADY]);
  887: #endif
  888: 						TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  889: #ifdef HAVE_LIBPTHREAD
  890: 						pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  891: #endif
  892: 					}
  893: 				}
  894: 				/* if match at least 2, don't remove resouce of event */
  895: 				if (flg > 1)
  896: 					evt->flags ^= evt->flags;
  897: 				break;
  898: 			case EVFILT_TIMER:
  899: 				flg = 0;
  900: 				TAILQ_FOREACH_SAFE(task, &r->root_alarm, task_node, tmp) {
  901: 					if ((uintptr_t) TASK_DATA(task) != ((uintptr_t) res[i].udata))
  902: 						continue;
  903: 					else {
  904: 						flg++;
  905: 						TASK_RET(task) = res[i].data;
  906: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  907: 					}
  908: 					/* remove alarm handle */
  909: #ifdef HAVE_LIBPTHREAD
  910: 					pthread_mutex_lock(&r->root_mtx[taskALARM]);
  911: #endif
  912: 					TAILQ_REMOVE(&r->root_alarm, task, task_node);
  913: #ifdef HAVE_LIBPTHREAD
  914: 					pthread_mutex_unlock(&r->root_mtx[taskALARM]);
  915: #endif
  916: 					task->task_type = taskREADY;
  917: #ifdef HAVE_LIBPTHREAD
  918: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
  919: #endif
  920: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  921: #ifdef HAVE_LIBPTHREAD
  922: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  923: #endif
  924: 				}
  925: 				/* if match at least 2, don't remove resouce of event */
  926: 				if (flg > 1)
  927: 					evt->flags ^= evt->flags;
  928: 				break;
  929: 			case EVFILT_VNODE:
  930: 				flg = 0;
  931: 				TAILQ_FOREACH_SAFE(task, &r->root_node, task_node, tmp) {
  932: 					if (TASK_FD(task) != ((intptr_t) res[i].udata))
  933: 						continue;
  934: 					else {
  935: 						flg++;
  936: 						TASK_RET(task) = res[i].data;
  937: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  938: 					}
  939: 					/* remove node handle */
  940: #ifdef HAVE_LIBPTHREAD
  941: 					pthread_mutex_lock(&r->root_mtx[taskNODE]);
  942: #endif
  943: 					TAILQ_REMOVE(&r->root_node, task, task_node);
  944: #ifdef HAVE_LIBPTHREAD
  945: 					pthread_mutex_unlock(&r->root_mtx[taskNODE]);
  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: 				}
  956: 				/* if match at least 2, don't remove resouce of event */
  957: 				if (flg > 1)
  958: 					evt->flags ^= evt->flags;
  959: 				break;
  960: 			case EVFILT_PROC:
  961: 				flg = 0;
  962: 				TAILQ_FOREACH_SAFE(task, &r->root_proc, task_node, tmp) {
  963: 					if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
  964: 						continue;
  965: 					else {
  966: 						flg++;
  967: 						TASK_RET(task) = res[i].data;
  968: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  969: 					}
  970: 					/* remove proc handle */
  971: #ifdef HAVE_LIBPTHREAD
  972: 					pthread_mutex_lock(&r->root_mtx[taskPROC]);
  973: #endif
  974: 					TAILQ_REMOVE(&r->root_proc, task, task_node);
  975: #ifdef HAVE_LIBPTHREAD
  976: 					pthread_mutex_unlock(&r->root_mtx[taskPROC]);
  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: 				}
  987: 				/* if match at least 2, don't remove resouce of event */
  988: 				if (flg > 1)
  989: 					evt->flags ^= evt->flags;
  990: 				break;
  991: 			case EVFILT_SIGNAL:
  992: 				flg = 0;
  993: 				TAILQ_FOREACH_SAFE(task, &r->root_signal, task_node, tmp) {
  994: 					if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
  995: 						continue;
  996: 					else {
  997: 						flg++;
  998: 						TASK_RET(task) = res[i].data;
  999: 						TASK_FLAG(task) = (u_long) res[i].fflags;
 1000: 					}
 1001: 					/* remove signal handle */
 1002: #ifdef HAVE_LIBPTHREAD
 1003: 					pthread_mutex_lock(&r->root_mtx[taskSIGNAL]);
 1004: #endif
 1005: 					TAILQ_REMOVE(&r->root_signal, task, task_node);
 1006: #ifdef HAVE_LIBPTHREAD
 1007: 					pthread_mutex_unlock(&r->root_mtx[taskSIGNAL]);
 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: 				}
 1018: 				/* if match at least 2, don't remove resouce of event */
 1019: 				if (flg > 1)
 1020: 					evt->flags ^= evt->flags;
 1021: 				break;
 1022: #ifdef AIO_SUPPORT
 1023: 			case EVFILT_AIO:
 1024: 				flg = 0;
 1025: 				TAILQ_FOREACH_SAFE(task, &r->root_aio, task_node, tmp) {
 1026: 					acb = (struct aiocb*) TASK_VAL(task);
 1027: 					if (acb != ((struct aiocb*) res[i].udata))
 1028: 						continue;
 1029: 					else {
 1030: 						flg++;
 1031: 						TASK_RET(task) = res[i].data;
 1032: 						TASK_FLAG(task) = (u_long) res[i].fflags;
 1033: 					}
 1034: 					/* remove user handle */
 1035: #ifdef HAVE_LIBPTHREAD
 1036: 					pthread_mutex_lock(&r->root_mtx[taskAIO]);
 1037: #endif
 1038: 					TAILQ_REMOVE(&r->root_aio, task, task_node);
 1039: #ifdef HAVE_LIBPTHREAD
 1040: 					pthread_mutex_unlock(&r->root_mtx[taskAIO]);
 1041: #endif
 1042: 					task->task_type = taskREADY;
 1043: #ifdef HAVE_LIBPTHREAD
 1044: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1045: #endif
 1046: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1047: #ifdef HAVE_LIBPTHREAD
 1048: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1049: #endif
 1050: 					fd = acb->aio_fildes;
 1051: 					if ((len = aio_return(acb)) != -1) {
 1052: 						if (lseek(fd, acb->aio_offset + len, SEEK_CUR) == -1)
 1053: 							LOGERR;
 1054: 					} else
 1055: 						LOGERR;
 1056: 					free(acb);
 1057: 					TASK_DATLEN(task) = (u_long) len;
 1058: 					TASK_FD(task) = fd;
 1059: 				}
 1060: 				/* if match at least 2, don't remove resouce of event */
 1061: 				if (flg > 1)
 1062: 					evt->flags ^= evt->flags;
 1063: 				break;
 1064: #ifdef EVFILT_LIO
 1065: 			case EVFILT_LIO:
 1066: 				flg = 0;
 1067: 				TAILQ_FOREACH_SAFE(task, &r->root_lio, task_node, tmp) {
 1068: 					acbs = (struct aiocb**) TASK_VAL(task);
 1069: 					if (acbs != ((struct aiocb**) res[i].udata))
 1070: 						continue;
 1071: 					else {
 1072: 						flg++;
 1073: 						TASK_RET(task) = res[i].data;
 1074: 						TASK_FLAG(task) = (u_long) res[i].fflags;
 1075: 					}
 1076: 					/* remove user handle */
 1077: #ifdef HAVE_LIBPTHREAD
 1078: 					pthread_mutex_lock(&r->root_mtx[taskLIO]);
 1079: #endif
 1080: 					TAILQ_REMOVE(&r->root_lio, task, task_node);
 1081: #ifdef HAVE_LIBPTHREAD
 1082: 					pthread_mutex_unlock(&r->root_mtx[taskLIO]);
 1083: #endif
 1084: 					task->task_type = taskREADY;
 1085: #ifdef HAVE_LIBPTHREAD
 1086: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1087: #endif
 1088: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1089: #ifdef HAVE_LIBPTHREAD
 1090: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1091: #endif
 1092: 					iv = (struct iovec*) TASK_DATA(task);
 1093: 					fd = acbs[0]->aio_fildes;
 1094: 					off = acbs[0]->aio_offset;
 1095: 					for (j = len = 0; i < TASK_DATLEN(task); len += l, i++) {
 1096: 						if ((iv[i].iov_len = aio_return(acbs[i])) == -1)
 1097: 							l = 0;
 1098: 						else
 1099: 							l = iv[i].iov_len;
 1100: 						free(acbs[i]);
 1101: 					}
 1102: 					free(acbs);
 1103: 					TASK_DATLEN(task) = (u_long) len;
 1104: 					TASK_FD(task) = fd;
 1105: 
 1106: 					if (lseek(fd, off + len, SEEK_CUR) == -1)
 1107: 						LOGERR;
 1108: 				}
 1109: 				/* if match at least 2, don't remove resouce of event */
 1110: 				if (flg > 1)
 1111: 					evt->flags ^= evt->flags;
 1112: 				break;
 1113: #endif	/* EVFILT_LIO */
 1114: #endif	/* AIO_SUPPORT */
 1115: #ifdef EVFILT_USER
 1116: 			case EVFILT_USER:
 1117: 				flg = 0;
 1118: 				TAILQ_FOREACH_SAFE(task, &r->root_user, task_node, tmp) {
 1119: 					if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
 1120: 						continue;
 1121: 					else {
 1122: 						flg++;
 1123: 						TASK_RET(task) = res[i].data;
 1124: 						TASK_FLAG(task) = (u_long) res[i].fflags;
 1125: 					}
 1126: 					/* remove user handle */
 1127: #ifdef HAVE_LIBPTHREAD
 1128: 					pthread_mutex_lock(&r->root_mtx[taskUSER]);
 1129: #endif
 1130: 					TAILQ_REMOVE(&r->root_user, task, task_node);
 1131: #ifdef HAVE_LIBPTHREAD
 1132: 					pthread_mutex_unlock(&r->root_mtx[taskUSER]);
 1133: #endif
 1134: 					task->task_type = taskREADY;
 1135: #ifdef HAVE_LIBPTHREAD
 1136: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1137: #endif
 1138: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1139: #ifdef HAVE_LIBPTHREAD
 1140: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1141: #endif
 1142: 				}
 1143: 				/* if match at least 2, don't remove resouce of event */
 1144: 				if (flg > 1)
 1145: 					evt->flags ^= evt->flags;
 1146: 				break;
 1147: #endif	/* EVFILT_USER */
 1148: 		}
 1149: 		if (kevent(r->root_kq, evt, 1, NULL, 0, &now) == -1) {
 1150: 			if (r->root_hooks.hook_exec.exception) {
 1151: 				if (r->root_hooks.hook_exec.exception(r, NULL))
 1152: 					return NULL;
 1153: 			} else
 1154: 				LOGERR;
 1155: 		}
 1156: #else	/* end of kevent dispatcher */
 1157: #endif	/* KQ_DISABLE */
 1158: 	}
 1159: 
 1160: skip_event:
 1161: 	/* timer update & put in ready queue */
 1162: 	clock_gettime(CLOCK_MONOTONIC, &now);
 1163: 
 1164: 	TAILQ_FOREACH_SAFE(task, &r->root_timer, task_node, tmp)
 1165: 		if (sched_timespeccmp(&now, &TASK_TS(task), -) >= 0) {
 1166: #ifdef HAVE_LIBPTHREAD
 1167: 			pthread_mutex_lock(&r->root_mtx[taskTIMER]);
 1168: #endif
 1169: 			TAILQ_REMOVE(&r->root_timer, task, task_node);
 1170: #ifdef HAVE_LIBPTHREAD
 1171: 			pthread_mutex_unlock(&r->root_mtx[taskTIMER]);
 1172: #endif
 1173: 			task->task_type = taskREADY;
 1174: #ifdef HAVE_LIBPTHREAD
 1175: 			pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1176: #endif
 1177: 			TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1178: #ifdef HAVE_LIBPTHREAD
 1179: 			pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1180: #endif
 1181: 		}
 1182: 
 1183: 	/* put regular task priority task to ready queue, 
 1184: 		if there is no ready task or reach max missing hit for regular task */
 1185: 	if ((task = TAILQ_FIRST(&r->root_task))) {
 1186: 		if (!TAILQ_FIRST(&r->root_ready) || r->root_miss >= TASK_VAL(task)) {
 1187: 			r->root_miss ^= r->root_miss;
 1188: 
 1189: #ifdef HAVE_LIBPTHREAD
 1190: 			pthread_mutex_lock(&r->root_mtx[taskTASK]);
 1191: #endif
 1192: 			TAILQ_REMOVE(&r->root_task, task, task_node);
 1193: #ifdef HAVE_LIBPTHREAD
 1194: 			pthread_mutex_unlock(&r->root_mtx[taskTASK]);
 1195: #endif
 1196: 			task->task_type = taskREADY;
 1197: #ifdef HAVE_LIBPTHREAD
 1198: 			pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1199: #endif
 1200: 			TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1201: #ifdef HAVE_LIBPTHREAD
 1202: 			pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1203: #endif
 1204: 		} else
 1205: 			r->root_miss++;
 1206: 	} else
 1207: 		r->root_miss ^= r->root_miss;
 1208: 
 1209: 	/* OK, lets get ready task !!! */
 1210: 	task = TAILQ_FIRST(&r->root_ready);
 1211: 	if (!(task))
 1212: 		return NULL;
 1213: 
 1214: #ifdef HAVE_LIBPTHREAD
 1215: 	pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1216: #endif
 1217: 	TAILQ_REMOVE(&r->root_ready, task, task_node);
 1218: #ifdef HAVE_LIBPTHREAD
 1219: 	pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1220: #endif
 1221: 	task->task_type = taskUNUSE;
 1222: #ifdef HAVE_LIBPTHREAD
 1223: 	pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
 1224: #endif
 1225: 	TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
 1226: #ifdef HAVE_LIBPTHREAD
 1227: 	pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
 1228: #endif
 1229: 	return task;
 1230: }
 1231: 
 1232: /*
 1233:  * sched_hook_exception() - Default EXCEPTION hook
 1234:  *
 1235:  * @root = root task
 1236:  * @arg = custom handling: if arg == EV_EOF or other value; default: arg == NULL log errno
 1237:  * return: <0 errors and 0 ok
 1238:  */
 1239: void *
 1240: sched_hook_exception(void *root, void *arg)
 1241: {
 1242: 	sched_root_task_t *r = root;
 1243: 
 1244: 	if (!r)
 1245: 		return NULL;
 1246: 
 1247: 	/* custom exception handling ... */
 1248: 	if (arg) {
 1249: 		if (arg == (void*) EV_EOF)
 1250: 			return NULL;
 1251: 		return (void*) -1;	/* raise scheduler error!!! */
 1252: 	}
 1253: 
 1254: 	/* if error hook exists */
 1255: 	if (r->root_hooks.hook_root.error)
 1256: 		return (r->root_hooks.hook_root.error(root, (void*) ((intptr_t) errno)));
 1257: 
 1258: 	/* default case! */
 1259: 	LOGERR;
 1260: 	return NULL;
 1261: }
 1262: 
 1263: /*
 1264:  * sched_hook_condition() - Default CONDITION hook
 1265:  *
 1266:  * @root = root task
 1267:  * @arg = killState from schedRun()
 1268:  * return: NULL kill scheduler loop or !=NULL ok
 1269:  */
 1270: void *
 1271: sched_hook_condition(void *root, void *arg)
 1272: {
 1273: 	sched_root_task_t *r = root;
 1274: 
 1275: 	if (!r)
 1276: 		return NULL;
 1277: 
 1278: 	return (void*) (r->root_cond - *(intptr_t*) arg);
 1279: }
 1280: 
 1281: /*
 1282:  * sched_hook_rtc() - Default RTC hook
 1283:  *
 1284:  * @task = current task
 1285:  * @arg = unused
 1286:  * return: <0 errors and 0 ok
 1287:  */
 1288: #if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME)
 1289: void *
 1290: sched_hook_rtc(void *task, void *arg __unused)
 1291: {
 1292: 	sched_task_t *sigt = NULL, *t = task;
 1293: 	struct itimerspec its;
 1294: 	struct sigevent evt;
 1295: 	timer_t tmr;
 1296: 
 1297: 	if (!t || !TASK_ROOT(t))
 1298: 		return (void*) -1;
 1299: 
 1300: 	memset(&evt, 0, sizeof evt);
 1301: 	evt.sigev_notify = SIGEV_SIGNAL;
 1302: 	evt.sigev_signo = (intptr_t) TASK_DATA(t) + SIGRTMIN;
 1303: 	evt.sigev_value.sival_ptr = TASK_DATA(t);
 1304: 
 1305: 	if (timer_create(CLOCK_MONOTONIC, &evt, &tmr) == -1) {
 1306: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
 1307: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
 1308: 		else
 1309: 			LOGERR;
 1310: 		return (void*) -1;
 1311: 	} else
 1312: 		TASK_FLAG(t) = (u_long) tmr;
 1313: 
 1314: 	if (!(sigt = schedSignal(TASK_ROOT(t), _sched_rtcWrapper, TASK_ARG(t), evt.sigev_signo, 
 1315: 				t, (size_t) tmr))) {
 1316: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
 1317: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
 1318: 		else
 1319: 			LOGERR;
 1320: 		timer_delete(tmr);
 1321: 		return (void*) -1;
 1322: 	} else
 1323: 		TASK_RET(t) = (uintptr_t) sigt;
 1324: 
 1325: 	memset(&its, 0, sizeof its);
 1326: 	its.it_value.tv_sec = t->task_val.ts.tv_sec;
 1327: 	its.it_value.tv_nsec = t->task_val.ts.tv_nsec;
 1328: 
 1329: 	if (timer_settime(tmr, TIMER_RELTIME, &its, NULL) == -1) {
 1330: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
 1331: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
 1332: 		else
 1333: 			LOGERR;
 1334: 		schedCancel(sigt);
 1335: 		timer_delete(tmr);
 1336: 		return (void*) -1;
 1337: 	}
 1338: 
 1339: 	return NULL;
 1340: }
 1341: #endif	/* HAVE_TIMER_CREATE */

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