File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / hooks.c
Revision 1.10.2.6: download - view: text, annotated - select for diffs - revision graph
Wed Aug 1 16:47:06 2012 UTC (12 years, 2 months ago) by misho
Branches: sched2_6
Diff to: branchpoint 1.10: preferred, unified
polishing

    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.10.2.6 2012/08/01 16:47: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
   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: 	r->root_kq = kqueue();
   66: 	if (r->root_kq == -1) {
   67: 		LOGERR;
   68: 		return (void*) -1;
   69: 	}
   70: 
   71: 	return NULL;
   72: }
   73: 
   74: /*
   75:  * sched_hook_fini() - Default FINI hook
   76:  *
   77:  * @root = root task
   78:  * @arg = unused
   79:  * return: <0 errors and 0 ok
   80:  */
   81: void *
   82: sched_hook_fini(void *root, void *arg __unused)
   83: {
   84: 	sched_root_task_t *r = root;
   85: 
   86: 	if (!r)
   87: 		return (void*) -1;
   88: 
   89: 	if (r->root_kq > 2) {
   90: 		close(r->root_kq);
   91: 		r->root_kq = 0;
   92: 	}
   93: 
   94: 	return NULL;
   95: }
   96: 
   97: /*
   98:  * sched_hook_cancel() - Default CANCEL hook
   99:  *
  100:  * @task = current task
  101:  * @arg = unused
  102:  * return: <0 errors and 0 ok
  103:  */
  104: void *
  105: sched_hook_cancel(void *task, void *arg __unused)
  106: {
  107: 	sched_task_t *t = task;
  108: 	struct kevent chg[1];
  109: 	struct timespec timeout = { 0, 0 };
  110: #ifdef EVFILT_AIO
  111: 	struct aiocb *acb;
  112: #endif
  113: 
  114: 	if (!t || !TASK_ROOT(t))
  115: 		return (void*) -1;
  116: 
  117: 	switch (TASK_TYPE(t)) {
  118: 		case taskREAD:
  119: #ifdef __NetBSD__
  120: 			EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
  121: #else
  122: 			EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (void*) TASK_FD(t));
  123: #endif
  124: 			break;
  125: 		case taskWRITE:
  126: #ifdef __NetBSD__
  127: 			EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
  128: #else
  129: 			EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (void*) TASK_FD(t));
  130: #endif
  131: 			break;
  132: 		case taskALARM:
  133: #ifdef __NetBSD__
  134: 			EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_DELETE, 
  135: 					0, 0, (intptr_t) TASK_DATA(t));
  136: #else
  137: 			EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_DELETE, 
  138: 					0, 0, (void*) TASK_DATA(t));
  139: #endif
  140: 			break;
  141: 		case taskNODE:
  142: #ifdef __NetBSD__
  143: 			EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
  144: #else
  145: 			EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (void*) TASK_FD(t));
  146: #endif
  147: 			break;
  148: 		case taskPROC:
  149: #ifdef __NetBSD__
  150: 			EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
  151: #else
  152: 			EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
  153: #endif
  154: 			break;
  155: 		case taskSIGNAL:
  156: #ifdef __NetBSD__
  157: 			EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
  158: #else
  159: 			EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
  160: #endif
  161: 			break;
  162: #ifdef EVFILT_AIO
  163: 		case taskAIO:
  164: #ifdef __NetBSD__
  165: 			EV_SET(&chg[0], TASK_VAL(t), EVFILT_AIO, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
  166: #else
  167: 			EV_SET(&chg[0], TASK_VAL(t), EVFILT_AIO, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
  168: #endif
  169: 			acb = (struct aiocb*) TASK_VAL(t);
  170: 			if (acb) {
  171: 				if (aio_cancel(acb->aio_fildes, acb) == AIO_CANCELED)
  172: 					aio_return(acb);
  173: 				free(acb);
  174: 				TASK_VAL(t) = 0;
  175: 			}
  176: 			break;
  177: #endif
  178: #ifdef EVFILT_USER
  179: 		case taskUSER:
  180: #ifdef __NetBSD__
  181: 			EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
  182: #else
  183: 			EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
  184: #endif
  185: 			break;
  186: #endif
  187: 		default:
  188: 			return NULL;
  189: 	}
  190: 
  191: 	kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout);
  192: 	return NULL;
  193: }
  194: 
  195: /*
  196:  * sched_hook_read() - Default READ hook
  197:  *
  198:  * @task = current task
  199:  * @arg = unused
  200:  * return: <0 errors and 0 ok
  201:  */
  202: void *
  203: sched_hook_read(void *task, void *arg __unused)
  204: {
  205: 	sched_task_t *t = task;
  206: 	struct kevent chg[1];
  207: 	struct timespec timeout = { 0, 0 };
  208: 
  209: 	if (!t || !TASK_ROOT(t))
  210: 		return (void*) -1;
  211: 
  212: #ifdef __NetBSD__
  213: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t));
  214: #else
  215: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t));
  216: #endif
  217: 	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  218: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
  219: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
  220: 		else
  221: 			LOGERR;
  222: 		return (void*) -1;
  223: 	}
  224: 
  225: 	return NULL;
  226: }
  227: 
  228: /*
  229:  * sched_hook_write() - Default WRITE hook
  230:  *
  231:  * @task = current task
  232:  * @arg = unused
  233:  * return: <0 errors and 0 ok
  234:  */
  235: void *
  236: sched_hook_write(void *task, void *arg __unused)
  237: {
  238: 	sched_task_t *t = task;
  239: 	struct kevent chg[1];
  240: 	struct timespec timeout = { 0, 0 };
  241: 
  242: 	if (!t || !TASK_ROOT(t))
  243: 		return (void*) -1;
  244: 
  245: #ifdef __NetBSD__
  246: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t));
  247: #else
  248: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t));
  249: #endif
  250: 	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  251: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
  252: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
  253: 		else
  254: 			LOGERR;
  255: 		return (void*) -1;
  256: 	}
  257: 
  258: 	return NULL;
  259: }
  260: 
  261: /*
  262:  * sched_hook_alarm() - Default ALARM hook
  263:  *
  264:  * @task = current task
  265:  * @arg = unused
  266:  * return: <0 errors and 0 ok
  267:  */
  268: void *
  269: sched_hook_alarm(void *task, void *arg __unused)
  270: {
  271: 	sched_task_t *t = task;
  272: 	struct kevent chg[1];
  273: 	struct timespec timeout = { 0, 0 };
  274: 
  275: 	if (!t || !TASK_ROOT(t))
  276: 		return (void*) -1;
  277: 
  278: #ifdef __NetBSD__
  279: 	EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 
  280: 			t->task_val.ts.tv_sec * 1000 + t->task_val.ts.tv_nsec / 1000000, 
  281: 			(intptr_t) TASK_DATA(t));
  282: #else
  283: 	EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 
  284: 			t->task_val.ts.tv_sec * 1000 + t->task_val.ts.tv_nsec / 1000000, 
  285: 			(void*) TASK_DATA(t));
  286: #endif
  287: 	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  288: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
  289: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
  290: 		else
  291: 			LOGERR;
  292: 		return (void*) -1;
  293: 	}
  294: 
  295: 	return NULL;
  296: }
  297: 
  298: /*
  299:  * sched_hook_node() - Default NODE hook
  300:  *
  301:  * @task = current task
  302:  * @arg = unused
  303:  * return: <0 errors and 0 ok
  304:  */
  305: void *
  306: sched_hook_node(void *task, void *arg __unused)
  307: {
  308: 	sched_task_t *t = task;
  309: 	struct kevent chg[1];
  310: 	struct timespec timeout = { 0, 0 };
  311: 
  312: 	if (!t || !TASK_ROOT(t))
  313: 		return (void*) -1;
  314: 
  315: #ifdef __NetBSD__
  316: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_ADD | EV_CLEAR, 
  317: 			NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | 
  318: 			NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, (intptr_t) TASK_FD(t));
  319: #else
  320: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_ADD | EV_CLEAR, 
  321: 			NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | 
  322: 			NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, (void*) TASK_FD(t));
  323: #endif
  324: 	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  325: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
  326: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
  327: 		else
  328: 			LOGERR;
  329: 		return (void*) -1;
  330: 	}
  331: 
  332: 	return NULL;
  333: }
  334: 
  335: /*
  336:  * sched_hook_proc() - Default PROC hook
  337:  *
  338:  * @task = current task
  339:  * @arg = unused
  340:  * return: <0 errors and 0 ok
  341:  */
  342: void *
  343: sched_hook_proc(void *task, void *arg __unused)
  344: {
  345: 	sched_task_t *t = task;
  346: 	struct kevent chg[1];
  347: 	struct timespec timeout = { 0, 0 };
  348: 
  349: 	if (!t || !TASK_ROOT(t))
  350: 		return (void*) -1;
  351: 
  352: #ifdef __NetBSD__
  353: 	EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_ADD | EV_CLEAR, 
  354: 			NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, (intptr_t) TASK_VAL(t));
  355: #else
  356: 	EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_ADD | EV_CLEAR, 
  357: 			NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, (void*) TASK_VAL(t));
  358: #endif
  359: 	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  360: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
  361: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
  362: 		else
  363: 			LOGERR;
  364: 		return (void*) -1;
  365: 	}
  366: 
  367: 	return NULL;
  368: }
  369: 
  370: /*
  371:  * sched_hook_signal() - Default SIGNAL hook
  372:  *
  373:  * @task = current task
  374:  * @arg = unused
  375:  * return: <0 errors and 0 ok
  376:  */
  377: void *
  378: sched_hook_signal(void *task, void *arg __unused)
  379: {
  380: 	sched_task_t *t = task;
  381: 	struct kevent chg[1];
  382: 	struct timespec timeout = { 0, 0 };
  383: 
  384: 	if (!t || !TASK_ROOT(t))
  385: 		return (void*) -1;
  386: 
  387: #ifdef __NetBSD__
  388: 	EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD, 0, 0, (intptr_t) TASK_VAL(t));
  389: #else
  390: 	EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD, 0, 0, (void*) TASK_VAL(t));
  391: #endif
  392: 	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  393: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
  394: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
  395: 		else
  396: 			LOGERR;
  397: 		return (void*) -1;
  398: 	}
  399: 
  400: 	return NULL;
  401: }
  402: 
  403: /*
  404:  * sched_hook_user() - Default USER hook
  405:  *
  406:  * @task = current task
  407:  * @arg = unused
  408:  * return: <0 errors and 0 ok
  409:  */
  410: #ifdef EVFILT_USER
  411: void *
  412: sched_hook_user(void *task, void *arg __unused)
  413: {
  414: 	sched_task_t *t = task;
  415: 	struct kevent chg[1];
  416: 	struct timespec timeout = { 0, 0 };
  417: 
  418: 	if (!t || !TASK_ROOT(t))
  419: 		return (void*) -1;
  420: 
  421: #ifdef __NetBSD__
  422: 	EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_ADD | EV_CLEAR, TASK_DATLEN(t), 
  423: 			0, (intptr_t) TASK_VAL(t));
  424: #else
  425: 	EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_ADD | EV_CLEAR, TASK_DATLEN(t), 
  426: 			0, (void*) TASK_VAL(t));
  427: #endif
  428: 	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  429: 		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
  430: 			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
  431: 		else
  432: 			LOGERR;
  433: 		return (void*) -1;
  434: 	}
  435: 
  436: 	return NULL;
  437: }
  438: #endif
  439: 
  440: /*
  441:  * sched_hook_fetch() - Default FETCH hook
  442:  *
  443:  * @root = root task
  444:  * @arg = unused
  445:  * return: NULL error or !=NULL fetched task
  446:  */
  447: void *
  448: sched_hook_fetch(void *root, void *arg __unused)
  449: {
  450: 	sched_root_task_t *r = root;
  451: 	sched_task_t *task, *tmp;
  452: 	struct timespec now, m, mtmp;
  453: 	struct timespec *timeout;
  454: 	struct kevent evt[1], res[KQ_EVENTS];
  455: 	register int i, flg;
  456: 	int en;
  457: #ifdef EVFILT_AIO
  458: 	int len, fd;
  459: 	struct aiocb *acb;
  460: #endif
  461: 
  462: 	if (!r)
  463: 		return NULL;
  464: 
  465: 	/* get new task by queue priority */
  466: 	while ((task = TAILQ_FIRST(&r->root_event))) {
  467: #ifdef HAVE_LIBPTHREAD
  468: 		pthread_mutex_lock(&r->root_mtx[taskEVENT]);
  469: #endif
  470: 		TAILQ_REMOVE(&r->root_event, task, task_node);
  471: #ifdef HAVE_LIBPTHREAD
  472: 		pthread_mutex_unlock(&r->root_mtx[taskEVENT]);
  473: #endif
  474: 		task->task_type = taskUNUSE;
  475: #ifdef HAVE_LIBPTHREAD
  476: 		pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  477: #endif
  478: 		TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  479: #ifdef HAVE_LIBPTHREAD
  480: 		pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  481: #endif
  482: 		return task;
  483: 	}
  484: 	while ((task = TAILQ_FIRST(&r->root_ready))) {
  485: #ifdef HAVE_LIBPTHREAD
  486: 		pthread_mutex_lock(&r->root_mtx[taskREADY]);
  487: #endif
  488: 		TAILQ_REMOVE(&r->root_ready, task, task_node);
  489: #ifdef HAVE_LIBPTHREAD
  490: 		pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  491: #endif
  492: 		task->task_type = taskUNUSE;
  493: #ifdef HAVE_LIBPTHREAD
  494: 		pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  495: #endif
  496: 		TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  497: #ifdef HAVE_LIBPTHREAD
  498: 		pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  499: #endif
  500: 		return task;
  501: 	}
  502: 
  503: #ifdef TIMER_WITHOUT_SORT
  504: 	clock_gettime(CLOCK_MONOTONIC, &now);
  505: 
  506: 	sched_timespecclear(&r->root_wait);
  507: 	TAILQ_FOREACH(task, &r->root_timer, task_node) {
  508: 		if (!sched_timespecisset(&r->root_wait))
  509: 			r->root_wait = TASK_TS(task);
  510: 		else if (sched_timespeccmp(&TASK_TS(task), &r->root_wait, -) < 0)
  511: 			r->root_wait = TASK_TS(task);
  512: 	}
  513: 
  514: 	if (TAILQ_FIRST(&r->root_timer)) {
  515: 		m = r->root_wait;
  516: 		sched_timespecsub(&m, &now, &mtmp);
  517: 		r->root_wait = mtmp;
  518: 	} else {
  519: 		/* set wait INFTIM */
  520: 		sched_timespecinf(&r->root_wait);
  521: 	}
  522: #else
  523: 	if (!TAILQ_FIRST(&r->root_eventlo) && (task = TAILQ_FIRST(&r->root_timer))) {
  524: 		clock_gettime(CLOCK_MONOTONIC, &now);
  525: 
  526: 		m = TASK_TS(task);
  527: 		sched_timespecsub(&m, &now, &mtmp);
  528: 		r->root_wait = mtmp;
  529: 	} else {
  530: 		/* set wait INFTIM */
  531: 		sched_timespecinf(&r->root_wait);
  532: 	}
  533: #endif
  534: 	/* if present member of eventLo, set NOWAIT */
  535: 	if (TAILQ_FIRST(&r->root_eventlo))
  536: 		sched_timespecclear(&r->root_wait);
  537: 
  538: 	if (r->root_wait.tv_sec != -1 && r->root_wait.tv_nsec != -1)
  539: 		timeout = &r->root_wait;
  540: 	else if (sched_timespecisinf(&r->root_poll))
  541: 		timeout = NULL;
  542: 	else
  543: 		timeout = &r->root_poll;
  544: 	if ((en = kevent(r->root_kq, NULL, 0, res, KQ_EVENTS, timeout)) == -1) {
  545: 		if (r->root_hooks.hook_exec.exception) {
  546: 			if (r->root_hooks.hook_exec.exception(r, NULL))
  547: 				return NULL;
  548: 		} else if (errno != EINTR)
  549: 			LOGERR;
  550: 		return NULL;
  551: 	}
  552: 
  553: 	now.tv_sec = now.tv_nsec = 0;
  554: 	/* Go and catch the cat into pipes ... */
  555: 	for (i = 0; i < en; i++) {
  556: 		memcpy(evt, &res[i], sizeof evt);
  557: 		evt->flags = EV_DELETE;
  558: 		/* Put read/write task to ready queue */
  559: 		switch (res[i].filter) {
  560: 			case EVFILT_READ:
  561: 				flg = 0;
  562: 				TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) {
  563: 					if (TASK_FD(task) != ((intptr_t) res[i].udata))
  564: 						continue;
  565: 					else
  566: 						flg++;
  567: 					/* remove read handle */
  568: #ifdef HAVE_LIBPTHREAD
  569: 					pthread_mutex_lock(&r->root_mtx[taskREAD]);
  570: #endif
  571: 					TAILQ_REMOVE(&r->root_read, task, task_node);
  572: #ifdef HAVE_LIBPTHREAD
  573: 					pthread_mutex_unlock(&r->root_mtx[taskREAD]);
  574: #endif
  575: 					if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) {
  576:  						if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) {
  577: 							task->task_type = taskUNUSE;
  578: #ifdef HAVE_LIBPTHREAD
  579: 							pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  580: #endif
  581: 							TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  582: #ifdef HAVE_LIBPTHREAD
  583: 							pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  584: #endif
  585: 						} else {
  586: 							task->task_type = taskREADY;
  587: #ifdef HAVE_LIBPTHREAD
  588: 							pthread_mutex_lock(&r->root_mtx[taskREADY]);
  589: #endif
  590: 							TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  591: #ifdef HAVE_LIBPTHREAD
  592: 							pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  593: #endif
  594: 						}
  595: 					} else {
  596: 						task->task_type = taskREADY;
  597: #ifdef HAVE_LIBPTHREAD
  598: 						pthread_mutex_lock(&r->root_mtx[taskREADY]);
  599: #endif
  600: 						TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  601: #ifdef HAVE_LIBPTHREAD
  602: 						pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  603: #endif
  604: 					}
  605: 				}
  606: 				/* if match at least 2, don't remove resouce of event */
  607: 				if (flg > 1)
  608: 					evt->flags ^= evt->flags;
  609: 				break;
  610: 			case EVFILT_WRITE:
  611: 				flg = 0;
  612: 				TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) {
  613: 					if (TASK_FD(task) != ((intptr_t) res[i].udata))
  614: 						continue;
  615: 					else
  616: 						flg++;
  617: 					/* remove write handle */
  618: #ifdef HAVE_LIBPTHREAD
  619: 					pthread_mutex_lock(&r->root_mtx[taskWRITE]);
  620: #endif
  621: 					TAILQ_REMOVE(&r->root_write, task, task_node);
  622: #ifdef HAVE_LIBPTHREAD
  623: 					pthread_mutex_unlock(&r->root_mtx[taskWRITE]);
  624: #endif
  625: 					if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) {
  626:  						if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) {
  627: 							task->task_type = taskUNUSE;
  628: #ifdef HAVE_LIBPTHREAD
  629: 							pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  630: #endif
  631: 							TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  632: #ifdef HAVE_LIBPTHREAD
  633: 							pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  634: #endif
  635: 						} else {
  636: 							task->task_type = taskREADY;
  637: #ifdef HAVE_LIBPTHREAD
  638: 							pthread_mutex_lock(&r->root_mtx[taskREADY]);
  639: #endif
  640: 							TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  641: #ifdef HAVE_LIBPTHREAD
  642: 							pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  643: #endif
  644: 						}
  645: 					} else {
  646: 						task->task_type = taskREADY;
  647: #ifdef HAVE_LIBPTHREAD
  648: 						pthread_mutex_lock(&r->root_mtx[taskREADY]);
  649: #endif
  650: 						TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  651: #ifdef HAVE_LIBPTHREAD
  652: 						pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  653: #endif
  654: 					}
  655: 				}
  656: 				/* if match at least 2, don't remove resouce of event */
  657: 				if (flg > 1)
  658: 					evt->flags ^= evt->flags;
  659: 				break;
  660: 			case EVFILT_TIMER:
  661: 				flg = 0;
  662: 				TAILQ_FOREACH_SAFE(task, &r->root_alarm, task_node, tmp) {
  663: 					if ((uintptr_t) TASK_DATA(task) != ((uintptr_t) res[i].udata))
  664: 						continue;
  665: 					else
  666: 						flg++;
  667: 					/* remove alarm handle */
  668: #ifdef HAVE_LIBPTHREAD
  669: 					pthread_mutex_lock(&r->root_mtx[taskALARM]);
  670: #endif
  671: 					TAILQ_REMOVE(&r->root_alarm, task, task_node);
  672: #ifdef HAVE_LIBPTHREAD
  673: 					pthread_mutex_unlock(&r->root_mtx[taskALARM]);
  674: #endif
  675: 					task->task_type = taskREADY;
  676: #ifdef HAVE_LIBPTHREAD
  677: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
  678: #endif
  679: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  680: #ifdef HAVE_LIBPTHREAD
  681: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  682: #endif
  683: 				}
  684: 				/* if match at least 2, don't remove resouce of event */
  685: 				if (flg > 1)
  686: 					evt->flags ^= evt->flags;
  687: 				break;
  688: 			case EVFILT_VNODE:
  689: 				flg = 0;
  690: 				TAILQ_FOREACH_SAFE(task, &r->root_node, task_node, tmp) {
  691: 					if (TASK_FD(task) != ((intptr_t) res[i].udata))
  692: 						continue;
  693: 					else {
  694: 						flg++;
  695: 						TASK_DATA(task) = (void*) (uintptr_t) res[i].data;
  696: 						TASK_DATLEN(task) = res[i].fflags;
  697: 					}
  698: 					/* remove node handle */
  699: #ifdef HAVE_LIBPTHREAD
  700: 					pthread_mutex_lock(&r->root_mtx[taskNODE]);
  701: #endif
  702: 					TAILQ_REMOVE(&r->root_node, task, task_node);
  703: #ifdef HAVE_LIBPTHREAD
  704: 					pthread_mutex_unlock(&r->root_mtx[taskNODE]);
  705: #endif
  706: 					task->task_type = taskREADY;
  707: #ifdef HAVE_LIBPTHREAD
  708: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
  709: #endif
  710: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  711: #ifdef HAVE_LIBPTHREAD
  712: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  713: #endif
  714: 				}
  715: 				/* if match at least 2, don't remove resouce of event */
  716: 				if (flg > 1)
  717: 					evt->flags ^= evt->flags;
  718: 				break;
  719: 			case EVFILT_PROC:
  720: 				flg = 0;
  721: 				TAILQ_FOREACH_SAFE(task, &r->root_proc, task_node, tmp) {
  722: 					if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
  723: 						continue;
  724: 					else {
  725: 						flg++;
  726: 						TASK_DATA(task) = (void*) (uintptr_t) res[i].data;
  727: 						TASK_DATLEN(task) = res[i].fflags;
  728: 					}
  729: 					/* remove proc handle */
  730: #ifdef HAVE_LIBPTHREAD
  731: 					pthread_mutex_lock(&r->root_mtx[taskPROC]);
  732: #endif
  733: 					TAILQ_REMOVE(&r->root_proc, task, task_node);
  734: #ifdef HAVE_LIBPTHREAD
  735: 					pthread_mutex_unlock(&r->root_mtx[taskPROC]);
  736: #endif
  737: 					task->task_type = taskREADY;
  738: #ifdef HAVE_LIBPTHREAD
  739: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
  740: #endif
  741: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  742: #ifdef HAVE_LIBPTHREAD
  743: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  744: #endif
  745: 				}
  746: 				/* if match at least 2, don't remove resouce of event */
  747: 				if (flg > 1)
  748: 					evt->flags ^= evt->flags;
  749: 				break;
  750: 			case EVFILT_SIGNAL:
  751: 				flg = 0;
  752: 				TAILQ_FOREACH_SAFE(task, &r->root_signal, task_node, tmp) {
  753: 					if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
  754: 						continue;
  755: 					else
  756: 						flg++;
  757: 					/* remove signal handle */
  758: #ifdef HAVE_LIBPTHREAD
  759: 					pthread_mutex_lock(&r->root_mtx[taskSIGNAL]);
  760: #endif
  761: 					TAILQ_REMOVE(&r->root_signal, task, task_node);
  762: #ifdef HAVE_LIBPTHREAD
  763: 					pthread_mutex_unlock(&r->root_mtx[taskSIGNAL]);
  764: #endif
  765: 					task->task_type = taskREADY;
  766: #ifdef HAVE_LIBPTHREAD
  767: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
  768: #endif
  769: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  770: #ifdef HAVE_LIBPTHREAD
  771: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  772: #endif
  773: 				}
  774: 				/* if match at least 2, don't remove resouce of event */
  775: 				if (flg > 1)
  776: 					evt->flags ^= evt->flags;
  777: 				break;
  778: #ifdef EVFILT_AIO
  779: 			case EVFILT_AIO:
  780: 				flg = 0;
  781: 				TAILQ_FOREACH_SAFE(task, &r->root_aio, task_node, tmp) {
  782: 					acb = (struct aiocb*) TASK_VAL(task);
  783: 					if (acb != ((struct aiocb*) res[i].udata))
  784: 						continue;
  785: 					else
  786: 						flg++;
  787: 					/* remove user handle */
  788: #ifdef HAVE_LIBPTHREAD
  789: 					pthread_mutex_lock(&r->root_mtx[taskAIO]);
  790: #endif
  791: 					TAILQ_REMOVE(&r->root_aio, task, task_node);
  792: #ifdef HAVE_LIBPTHREAD
  793: 					pthread_mutex_unlock(&r->root_mtx[taskAIO]);
  794: #endif
  795: 					task->task_type = taskREADY;
  796: #ifdef HAVE_LIBPTHREAD
  797: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
  798: #endif
  799: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  800: #ifdef HAVE_LIBPTHREAD
  801: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  802: #endif
  803: 					fd = acb->aio_fildes;
  804: 					if ((len = aio_return(acb)) != -1) {
  805: 						if (lseek(fd, acb->aio_offset + len, SEEK_CUR) == -1)
  806: 							LOGERR;
  807: 					} else
  808: 						LOGERR;
  809: 
  810: 					free(acb);
  811: 					TASK_FD(task) = fd;
  812: 					TASK_DATLEN(task) = (u_long) len;
  813: 				}
  814: 				/* if match at least 2, don't remove resouce of event */
  815: 				if (flg > 1)
  816: 					evt->flags ^= evt->flags;
  817: 				break;
  818: #endif	/* EVFILT_AIO */
  819: #ifdef EVFILT_USER
  820: 			case EVFILT_USER:
  821: 				flg = 0;
  822: 				TAILQ_FOREACH_SAFE(task, &r->root_user, task_node, tmp) {
  823: 					if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
  824: 						continue;
  825: 					else {
  826: 						flg++;
  827: 						TASK_DATA(task) = (void*) res[i].data;
  828: 						TASK_DATLEN(task) = res[i].fflags;
  829: 					}
  830: 					/* remove user handle */
  831: #ifdef HAVE_LIBPTHREAD
  832: 					pthread_mutex_lock(&r->root_mtx[taskUSER]);
  833: #endif
  834: 					TAILQ_REMOVE(&r->root_user, task, task_node);
  835: #ifdef HAVE_LIBPTHREAD
  836: 					pthread_mutex_unlock(&r->root_mtx[taskUSER]);
  837: #endif
  838: 					task->task_type = taskREADY;
  839: #ifdef HAVE_LIBPTHREAD
  840: 					pthread_mutex_lock(&r->root_mtx[taskREADY]);
  841: #endif
  842: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  843: #ifdef HAVE_LIBPTHREAD
  844: 					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  845: #endif
  846: 				}
  847: 				/* if match at least 2, don't remove resouce of event */
  848: 				if (flg > 1)
  849: 					evt->flags ^= evt->flags;
  850: 				break;
  851: #endif	/* EVFILT_USER */
  852: 		}
  853: 		if (kevent(r->root_kq, evt, 1, NULL, 0, &now) == -1) {
  854: 			if (r->root_hooks.hook_exec.exception) {
  855: 				if (r->root_hooks.hook_exec.exception(r, NULL))
  856: 					return NULL;
  857: 			} else
  858: 				LOGERR;
  859: 		}
  860: 	}
  861: 
  862: 	/* timer update & put in ready queue */
  863: 	clock_gettime(CLOCK_MONOTONIC, &now);
  864: 
  865: 	TAILQ_FOREACH_SAFE(task, &r->root_timer, task_node, tmp)
  866: 		if (sched_timespeccmp(&now, &TASK_TS(task), -) >= 0) {
  867: #ifdef HAVE_LIBPTHREAD
  868: 			pthread_mutex_lock(&r->root_mtx[taskTIMER]);
  869: #endif
  870: 			TAILQ_REMOVE(&r->root_timer, task, task_node);
  871: #ifdef HAVE_LIBPTHREAD
  872: 			pthread_mutex_unlock(&r->root_mtx[taskTIMER]);
  873: #endif
  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: 
  884: 	/* put eventlo priority task to ready queue, if there is no ready task or 
  885: 	   	reach max missed fetch-rotate */
  886: 	if ((task = TAILQ_FIRST(&r->root_eventlo))) {
  887: 		if (!TAILQ_FIRST(&r->root_ready) || r->root_eventlo_miss > MAX_EVENTLO_MISS) {
  888: 			r->root_eventlo_miss = 0;
  889: 
  890: #ifdef HAVE_LIBPTHREAD
  891: 			pthread_mutex_lock(&r->root_mtx[taskEVENTLO]);
  892: #endif
  893: 			TAILQ_REMOVE(&r->root_eventlo, task, task_node);
  894: #ifdef HAVE_LIBPTHREAD
  895: 			pthread_mutex_unlock(&r->root_mtx[taskEVENTLO]);
  896: #endif
  897: 			task->task_type = taskREADY;
  898: #ifdef HAVE_LIBPTHREAD
  899: 			pthread_mutex_lock(&r->root_mtx[taskREADY]);
  900: #endif
  901: 			TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  902: #ifdef HAVE_LIBPTHREAD
  903: 			pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  904: #endif
  905: 		} else
  906: 			r->root_eventlo_miss++;
  907: 	} else
  908: 		r->root_eventlo_miss = 0;
  909: 
  910: 	/* OK, lets get ready task !!! */
  911: 	task = TAILQ_FIRST(&r->root_ready);
  912: 	if (!(task))
  913: 		return NULL;
  914: 
  915: #ifdef HAVE_LIBPTHREAD
  916: 	pthread_mutex_lock(&r->root_mtx[taskREADY]);
  917: #endif
  918: 	TAILQ_REMOVE(&r->root_ready, task, task_node);
  919: #ifdef HAVE_LIBPTHREAD
  920: 	pthread_mutex_unlock(&r->root_mtx[taskREADY]);
  921: #endif
  922: 	task->task_type = taskUNUSE;
  923: #ifdef HAVE_LIBPTHREAD
  924: 	pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
  925: #endif
  926: 	TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  927: #ifdef HAVE_LIBPTHREAD
  928: 	pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
  929: #endif
  930: 	return task;
  931: }
  932: 
  933: /*
  934:  * sched_hook_exception() - Default EXCEPTION hook
  935:  *
  936:  * @root = root task
  937:  * @arg = custom handling: if arg == EV_EOF or other value; default: arg == NULL log errno
  938:  * return: <0 errors and 0 ok
  939:  */
  940: void *
  941: sched_hook_exception(void *root, void *arg)
  942: {
  943: 	sched_root_task_t *r = root;
  944: 
  945: 	if (!r)
  946: 		return NULL;
  947: 
  948: 	/* custom exception handling ... */
  949: 	if (arg) {
  950: 		if (arg == (void*) EV_EOF)
  951: 			return NULL;
  952: 		return (void*) -1;	/* raise scheduler error!!! */
  953: 	}
  954: 
  955: 	/* if error hook exists */
  956: 	if (r->root_hooks.hook_root.error)
  957: 		return (r->root_hooks.hook_root.error(root, (void*) ((intptr_t) errno)));
  958: 
  959: 	/* default case! */
  960: 	LOGERR;
  961: 	return NULL;
  962: }
  963: 
  964: /*
  965:  * sched_hook_condition() - Default CONDITION hook
  966:  *
  967:  * @root = root task
  968:  * @arg = killState from schedRun()
  969:  * return: NULL kill scheduler loop or !=NULL ok
  970:  */
  971: void *
  972: sched_hook_condition(void *root, void *arg)
  973: {
  974: 	sched_root_task_t *r = root;
  975: 
  976: 	if (!r)
  977: 		return NULL;
  978: 
  979: 	return (void*) (r->root_cond - *(intptr_t*) arg);
  980: }

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