File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / hooks.c
Revision 1.24.4.3: download - view: text, annotated - select for diffs - revision graph
Tue Jan 28 10:07:06 2014 UTC (10 years, 5 months ago) by misho
Branches: sched4_7
Diff to: branchpoint 1.24: preferred, unified
add select hook

    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.3 2014/01/28 10:07:06 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, m, mtmp;
  622: #ifndef KQ_DISABLE
  623: 	struct kevent evt[1], res[KQ_EVENTS];
  624: 	struct timespec *timeout, m, mtmp;
  625: #else
  626: 	struct timeval *timeout, tv;
  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: 	sched_timespecclear(&r->root_wait);
  687: 	TAILQ_FOREACH(task, &r->root_timer, task_node) {
  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);
  692: 	}
  693: 
  694: 	if (TAILQ_FIRST(&r->root_timer)) {
  695: 		m = r->root_wait;
  696: 		sched_timespecsub(&m, &now, &mtmp);
  697: 		r->root_wait = mtmp;
  698: 	} else {
  699: 		/* set wait INFTIM */
  700: 		sched_timespecinf(&r->root_wait);
  701: 	}
  702: #else	/* ! TIMER_WITHOUT_SORT */
  703: 	if (!TAILQ_FIRST(&r->root_task) && (task = TAILQ_FIRST(&r->root_timer))) {
  704: 		clock_gettime(CLOCK_MONOTONIC, &now);
  705: 
  706: 		m = TASK_TS(task);
  707: 		sched_timespecsub(&m, &now, &mtmp);
  708: 		r->root_wait = mtmp;
  709: 	} else {
  710: 		/* set wait INFTIM */
  711: 		sched_timespecinf(&r->root_wait);
  712: 	}
  713: #endif	/* TIMER_WITHOUT_SORT */
  714: 	/* if present member of task, set NOWAIT */
  715: 	if (TAILQ_FIRST(&r->root_task))
  716: 		sched_timespecclear(&r->root_wait);
  717: 
  718: 	if (r->root_wait.tv_sec != -1 && r->root_wait.tv_nsec != -1) {
  719: #ifndef KQ_DISABLE
  720: 		timeout = &r->root_wait;
  721: #else
  722: 		sched_timespec2val(&r->root_wait, &tv);
  723: 		timeout = &tv;
  724: #endif	/* KQ_DISABLE */
  725: 	} else if (sched_timespecisinf(&r->root_poll))
  726: 		timeout = NULL;
  727: 	else {
  728: #ifndef KQ_DISABLE
  729: 		timeout = &r->root_poll;
  730: #else
  731: 		sched_timespec2val(&r->root_poll, &tv);
  732: 		timeout = &tv;
  733: #endif	/* KQ_DISABLE */
  734: 	}
  735: 
  736: #ifndef KQ_DISABLE
  737: 	if ((en = kevent(r->root_kq, NULL, 0, res, KQ_EVENTS, timeout)) == -1) {
  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 */
  742: 		if (r->root_hooks.hook_exec.exception) {
  743: 			if (r->root_hooks.hook_exec.exception(r, NULL))
  744: 				return NULL;
  745: 		} else if (errno != EINTR)
  746: 			LOGERR;
  747: 		goto skip_event;
  748: 	}
  749: 
  750: 	/* kevent dispatcher */
  751: 	now.tv_sec = now.tv_nsec = 0;
  752: 	/* Go and catch the cat into pipes ... */
  753: #ifndef KQ_DISABLE
  754: 	for (i = 0; i < en; i++) {
  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:
  760: 				flg = 0;
  761: 				TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) {
  762: 					if (TASK_FD(task) != ((intptr_t) res[i].udata))
  763: 						continue;
  764: 					else {
  765: 						flg++;
  766: 						TASK_RET(task) = res[i].data;
  767: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  768: 					}
  769: 					/* remove read handle */
  770: #ifdef HAVE_LIBPTHREAD
  771: 					pthread_mutex_lock(&r->root_mtx[taskREAD]);
  772: #endif
  773: 					TAILQ_REMOVE(&r->root_read, task, task_node);
  774: #ifdef HAVE_LIBPTHREAD
  775: 					pthread_mutex_unlock(&r->root_mtx[taskREAD]);
  776: #endif
  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;
  780: #ifdef HAVE_LIBPTHREAD
  781: 							pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  782: #endif
  783: 							TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  784: #ifdef HAVE_LIBPTHREAD
  785: 							pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  786: #endif
  787: 						} else {
  788: 							task->task_type = taskREADY;
  789: #ifdef HAVE_LIBPTHREAD
  790: 							pthread_mutex_lock(&r->root_mtx[taskREADY]);
  791: #endif
  792: 							TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  793: #ifdef HAVE_LIBPTHREAD
  794: 							pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  795: #endif
  796: 						}
  797: 					} else {
  798: 						task->task_type = taskREADY;
  799: #ifdef HAVE_LIBPTHREAD
  800: 						pthread_mutex_lock(&r->root_mtx[taskREADY]);
  801: #endif
  802: 						TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  803: #ifdef HAVE_LIBPTHREAD
  804: 						pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  805: #endif
  806: 					}
  807: 				}
  808: 				/* if match at least 2, don't remove resouce of event */
  809: 				if (flg > 1)
  810: 					evt->flags ^= evt->flags;
  811: 				break;
  812: 			case EVFILT_WRITE:
  813: 				flg = 0;
  814: 				TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) {
  815: 					if (TASK_FD(task) != ((intptr_t) res[i].udata))
  816: 						continue;
  817: 					else {
  818: 						flg++;
  819: 						TASK_RET(task) = res[i].data;
  820: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  821: 					}
  822: 					/* remove write handle */
  823: #ifdef HAVE_LIBPTHREAD
  824: 					pthread_mutex_lock(&r->root_mtx[taskWRITE]);
  825: #endif
  826: 					TAILQ_REMOVE(&r->root_write, task, task_node);
  827: #ifdef HAVE_LIBPTHREAD
  828: 					pthread_mutex_unlock(&r->root_mtx[taskWRITE]);
  829: #endif
  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;
  833: #ifdef HAVE_LIBPTHREAD
  834: 							pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  835: #endif
  836: 							TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  837: #ifdef HAVE_LIBPTHREAD
  838: 							pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  839: #endif
  840: 						} else {
  841: 							task->task_type = taskREADY;
  842: #ifdef HAVE_LIBPTHREAD
  843: 							pthread_mutex_lock(&r->root_mtx[taskREADY]);
  844: #endif
  845: 							TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  846: #ifdef HAVE_LIBPTHREAD
  847: 							pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  848: #endif
  849: 						}
  850: 					} else {
  851: 						task->task_type = taskREADY;
  852: #ifdef HAVE_LIBPTHREAD
  853: 						pthread_mutex_lock(&r->root_mtx[taskREADY]);
  854: #endif
  855: 						TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  856: #ifdef HAVE_LIBPTHREAD
  857: 						pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  858: #endif
  859: 					}
  860: 				}
  861: 				/* if match at least 2, don't remove resouce of event */
  862: 				if (flg > 1)
  863: 					evt->flags ^= evt->flags;
  864: 				break;
  865: 			case EVFILT_TIMER:
  866: 				flg = 0;
  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;
  870: 					else {
  871: 						flg++;
  872: 						TASK_RET(task) = res[i].data;
  873: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  874: 					}
  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: 				}
  892: 				/* if match at least 2, don't remove resouce of event */
  893: 				if (flg > 1)
  894: 					evt->flags ^= evt->flags;
  895: 				break;
  896: 			case EVFILT_VNODE:
  897: 				flg = 0;
  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 {
  902: 						flg++;
  903: 						TASK_RET(task) = res[i].data;
  904: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  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: 				}
  923: 				/* if match at least 2, don't remove resouce of event */
  924: 				if (flg > 1)
  925: 					evt->flags ^= evt->flags;
  926: 				break;
  927: 			case EVFILT_PROC:
  928: 				flg = 0;
  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 {
  933: 						flg++;
  934: 						TASK_RET(task) = res[i].data;
  935: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  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: 				}
  954: 				/* if match at least 2, don't remove resouce of event */
  955: 				if (flg > 1)
  956: 					evt->flags ^= evt->flags;
  957: 				break;
  958: 			case EVFILT_SIGNAL:
  959: 				flg = 0;
  960: 				TAILQ_FOREACH_SAFE(task, &r->root_signal, task_node, tmp) {
  961: 					if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
  962: 						continue;
  963: 					else {
  964: 						flg++;
  965: 						TASK_RET(task) = res[i].data;
  966: 						TASK_FLAG(task) = (u_long) res[i].fflags;
  967: 					}
  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: 				}
  985: 				/* if match at least 2, don't remove resouce of event */
  986: 				if (flg > 1)
  987: 					evt->flags ^= evt->flags;
  988: 				break;
  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;
  996: 					else {
  997: 						flg++;
  998: 						TASK_RET(task) = res[i].data;
  999: 						TASK_FLAG(task) = (u_long) res[i].fflags;
 1000: 					}
 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;
 1038: 					else {
 1039: 						flg++;
 1040: 						TASK_RET(task) = res[i].data;
 1041: 						TASK_FLAG(task) = (u_long) res[i].fflags;
 1042: 					}
 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 */
 1082: #ifdef EVFILT_USER
 1083: 			case EVFILT_USER:
 1084: 				flg = 0;
 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 {
 1089: 						flg++;
 1090: 						TASK_RET(task) = res[i].data;
 1091: 						TASK_FLAG(task) = (u_long) res[i].fflags;
 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: 				}
 1110: 				/* if match at least 2, don't remove resouce of event */
 1111: 				if (flg > 1)
 1112: 					evt->flags ^= evt->flags;
 1113: 				break;
 1114: #endif	/* EVFILT_USER */
 1115: 		}
 1116: 		if (kevent(r->root_kq, evt, 1, NULL, 0, &now) == -1) {
 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: 		}
 1123: 	}
 1124: #else	/* end of kevent dispatcher */
 1125: 	for (i = 0; i < r->root_kq; i++) {
 1126: 		if (FD_ISSET(i, &r->root_fds[0])) {
 1127: 			flg = 0;
 1128: 			TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) {
 1129: 				if (TASK_FD(task) != i)
 1130: 					continue;
 1131: 				else {
 1132: 					flg++;
 1133: 					TASK_RET(task) ^= TASK_RET(task);
 1134: 					TASK_FLAG(task) ^= TASK_FLAG(task);
 1135: 				}
 1136: 				/* remove read handle */
 1137: #ifdef HAVE_LIBPTHREAD
 1138: 				pthread_mutex_lock(&r->root_mtx[taskREAD]);
 1139: #endif
 1140: 				TAILQ_REMOVE(&r->root_read, task, task_node);
 1141: #ifdef HAVE_LIBPTHREAD
 1142: 				pthread_mutex_unlock(&r->root_mtx[taskREAD]);
 1143: #endif
 1144: 				if (r->root_hooks.hook_exec.exception) {
 1145:  					if (r->root_hooks.hook_exec.exception(r, NULL)) {
 1146: 						task->task_type = taskUNUSE;
 1147: #ifdef HAVE_LIBPTHREAD
 1148: 						pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
 1149: #endif
 1150: 						TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
 1151: #ifdef HAVE_LIBPTHREAD
 1152: 						pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
 1153: #endif
 1154: 					} else {
 1155: 						task->task_type = taskREADY;
 1156: #ifdef HAVE_LIBPTHREAD
 1157: 						pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1158: #endif
 1159: 						TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1160: #ifdef HAVE_LIBPTHREAD
 1161: 						pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1162: #endif
 1163: 					}
 1164: 				} else {
 1165: 					task->task_type = taskREADY;
 1166: #ifdef HAVE_LIBPTHREAD
 1167: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1168: #endif
 1169: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1170: #ifdef HAVE_LIBPTHREAD
 1171: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1172: #endif
 1173: 				}
 1174: 			}
 1175: 			/* if match equal to 1, remove resouce */
 1176: 			if (flg == 1)
 1177: 				FD_CLR(i, &r->root_fds[0]);
 1178: 		}
 1179: 
 1180: 		if (FD_ISSET(i, &r->root_fds[1])) {
 1181: 			flg = 0;
 1182: 			TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) {
 1183: 				if (TASK_FD(task) != i)
 1184: 					continue;
 1185: 				else {
 1186: 					flg++;
 1187: 					TASK_RET(task) ^= TASK_RET(task);
 1188: 					TASK_FLAG(task) ^= TASK_FLAG(task);
 1189: 				}
 1190: 				/* remove write handle */
 1191: #ifdef HAVE_LIBPTHREAD
 1192: 				pthread_mutex_lock(&r->root_mtx[taskWRITE]);
 1193: #endif
 1194: 				TAILQ_REMOVE(&r->root_write, task, task_node);
 1195: #ifdef HAVE_LIBPTHREAD
 1196: 				pthread_mutex_unlock(&r->root_mtx[taskWRITE]);
 1197: #endif
 1198: 				if (r->root_hooks.hook_exec.exception) {
 1199:  					if (r->root_hooks.hook_exec.exception(r, NULL)) {
 1200: 						task->task_type = taskUNUSE;
 1201: #ifdef HAVE_LIBPTHREAD
 1202: 						pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
 1203: #endif
 1204: 						TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
 1205: #ifdef HAVE_LIBPTHREAD
 1206: 						pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
 1207: #endif
 1208: 					} else {
 1209: 						task->task_type = taskREADY;
 1210: #ifdef HAVE_LIBPTHREAD
 1211: 						pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1212: #endif
 1213: 						TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1214: #ifdef HAVE_LIBPTHREAD
 1215: 						pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1216: #endif
 1217: 					}
 1218: 				} else {
 1219: 					task->task_type = taskREADY;
 1220: #ifdef HAVE_LIBPTHREAD
 1221: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1222: #endif
 1223: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1224: #ifdef HAVE_LIBPTHREAD
 1225: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1226: #endif
 1227: 				}
 1228: 			}
 1229: 			/* if match equal to 1, remove resouce */
 1230: 			if (flg == 1)
 1231: 				FD_CLR(i, &r->root_fds[1]);
 1232: 		}
 1233: 	}
 1234: #endif	/* KQ_DISABLE */
 1235: 
 1236: skip_event:
 1237: 	/* timer update & put in ready queue */
 1238: 	clock_gettime(CLOCK_MONOTONIC, &now);
 1239: 
 1240: 	TAILQ_FOREACH_SAFE(task, &r->root_timer, task_node, tmp)
 1241: 		if (sched_timespeccmp(&now, &TASK_TS(task), -) >= 0) {
 1242: #ifdef HAVE_LIBPTHREAD
 1243: 			pthread_mutex_lock(&r->root_mtx[taskTIMER]);
 1244: #endif
 1245: 			TAILQ_REMOVE(&r->root_timer, task, task_node);
 1246: #ifdef HAVE_LIBPTHREAD
 1247: 			pthread_mutex_unlock(&r->root_mtx[taskTIMER]);
 1248: #endif
 1249: 			task->task_type = taskREADY;
 1250: #ifdef HAVE_LIBPTHREAD
 1251: 			pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1252: #endif
 1253: 			TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1254: #ifdef HAVE_LIBPTHREAD
 1255: 			pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1256: #endif
 1257: 		}
 1258: 
 1259: 	/* put regular task priority task to ready queue, 
 1260: 		if there is no ready task or reach max missing hit for regular task */
 1261: 	if ((task = TAILQ_FIRST(&r->root_task))) {
 1262: 		if (!TAILQ_FIRST(&r->root_ready) || r->root_miss >= TASK_VAL(task)) {
 1263: 			r->root_miss ^= r->root_miss;
 1264: 
 1265: #ifdef HAVE_LIBPTHREAD
 1266: 			pthread_mutex_lock(&r->root_mtx[taskTASK]);
 1267: #endif
 1268: 			TAILQ_REMOVE(&r->root_task, task, task_node);
 1269: #ifdef HAVE_LIBPTHREAD
 1270: 			pthread_mutex_unlock(&r->root_mtx[taskTASK]);
 1271: #endif
 1272: 			task->task_type = taskREADY;
 1273: #ifdef HAVE_LIBPTHREAD
 1274: 			pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1275: #endif
 1276: 			TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
 1277: #ifdef HAVE_LIBPTHREAD
 1278: 			pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1279: #endif
 1280: 		} else
 1281: 			r->root_miss++;
 1282: 	} else
 1283: 		r->root_miss ^= r->root_miss;
 1284: 
 1285: 	/* OK, lets get ready task !!! */
 1286: 	task = TAILQ_FIRST(&r->root_ready);
 1287: 	if (!(task))
 1288: 		return NULL;
 1289: 
 1290: #ifdef HAVE_LIBPTHREAD
 1291: 	pthread_mutex_lock(&r->root_mtx[taskREADY]);
 1292: #endif
 1293: 	TAILQ_REMOVE(&r->root_ready, task, task_node);
 1294: #ifdef HAVE_LIBPTHREAD
 1295: 	pthread_mutex_unlock(&r->root_mtx[taskREADY]);
 1296: #endif
 1297: 	task->task_type = taskUNUSE;
 1298: #ifdef HAVE_LIBPTHREAD
 1299: 	pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
 1300: #endif
 1301: 	TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
 1302: #ifdef HAVE_LIBPTHREAD
 1303: 	pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
 1304: #endif
 1305: 	return task;
 1306: }
 1307: 
 1308: /*
 1309:  * sched_hook_exception() - Default EXCEPTION hook
 1310:  *
 1311:  * @root = root task
 1312:  * @arg = custom handling: if arg == EV_EOF or other value; default: arg == NULL log errno
 1313:  * return: <0 errors and 0 ok
 1314:  */
 1315: void *
 1316: sched_hook_exception(void *root, void *arg)
 1317: {
 1318: 	sched_root_task_t *r = root;
 1319: 
 1320: 	if (!r)
 1321: 		return NULL;
 1322: 
 1323: 	/* custom exception handling ... */
 1324: 	if (arg) {
 1325: 		if (arg == (void*) EV_EOF)
 1326: 			return NULL;
 1327: 		return (void*) -1;	/* raise scheduler error!!! */
 1328: 	}
 1329: 
 1330: 	/* if error hook exists */
 1331: 	if (r->root_hooks.hook_root.error)
 1332: 		return (r->root_hooks.hook_root.error(root, (void*) ((intptr_t) errno)));
 1333: 
 1334: 	/* default case! */
 1335: 	LOGERR;
 1336: 	return NULL;
 1337: }
 1338: 
 1339: /*
 1340:  * sched_hook_condition() - Default CONDITION hook
 1341:  *
 1342:  * @root = root task
 1343:  * @arg = killState from schedRun()
 1344:  * return: NULL kill scheduler loop or !=NULL ok
 1345:  */
 1346: void *
 1347: sched_hook_condition(void *root, void *arg)
 1348: {
 1349: 	sched_root_task_t *r = root;
 1350: 
 1351: 	if (!r)
 1352: 		return NULL;
 1353: 
 1354: 	return (void*) (r->root_cond - *(intptr_t*) arg);
 1355: }
 1356: 
 1357: /*
 1358:  * sched_hook_rtc() - Default RTC hook
 1359:  *
 1360:  * @task = current task
 1361:  * @arg = unused
 1362:  * return: <0 errors and 0 ok
 1363:  */
 1364: #if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME)
 1365: void *
 1366: sched_hook_rtc(void *task, void *arg __unused)
 1367: {
 1368: 	sched_task_t *sigt = NULL, *t = task;
 1369: 	struct itimerspec its;
 1370: 	struct sigevent evt;
 1371: 	timer_t tmr;
 1372: 
 1373: 	if (!t || !TASK_ROOT(t))
 1374: 		return (void*) -1;
 1375: 
 1376: 	memset(&evt, 0, sizeof evt);
 1377: 	evt.sigev_notify = SIGEV_SIGNAL;
 1378: 	evt.sigev_signo = (intptr_t) TASK_DATA(t) + SIGRTMIN;
 1379: 	evt.sigev_value.sival_ptr = TASK_DATA(t);
 1380: 
 1381: 	if (timer_create(CLOCK_MONOTONIC, &evt, &tmr) == -1) {
 1382: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
 1383: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
 1384: 		else
 1385: 			LOGERR;
 1386: 		return (void*) -1;
 1387: 	} else
 1388: 		TASK_FLAG(t) = (u_long) tmr;
 1389: 
 1390: 	if (!(sigt = schedSignal(TASK_ROOT(t), _sched_rtcWrapper, TASK_ARG(t), evt.sigev_signo, 
 1391: 				t, (size_t) tmr))) {
 1392: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
 1393: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
 1394: 		else
 1395: 			LOGERR;
 1396: 		timer_delete(tmr);
 1397: 		return (void*) -1;
 1398: 	} else
 1399: 		TASK_RET(t) = (uintptr_t) sigt;
 1400: 
 1401: 	memset(&its, 0, sizeof its);
 1402: 	its.it_value.tv_sec = t->task_val.ts.tv_sec;
 1403: 	its.it_value.tv_nsec = t->task_val.ts.tv_nsec;
 1404: 
 1405: 	if (timer_settime(tmr, TIMER_RELTIME, &its, NULL) == -1) {
 1406: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
 1407: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
 1408: 		else
 1409: 			LOGERR;
 1410: 		schedCancel(sigt);
 1411: 		timer_delete(tmr);
 1412: 		return (void*) -1;
 1413: 	}
 1414: 
 1415: 	return NULL;
 1416: }
 1417: #endif	/* HAVE_TIMER_CREATE */

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