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

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

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