File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / hooks.c
Revision 1.10.2.1: download - view: text, annotated - select for diffs - revision graph
Wed Aug 1 12:49:26 2012 UTC (12 years, 2 months ago) by misho
Branches: sched2_6
Diff to: branchpoint 1.10: preferred, unified
initial AIO kevent scheduler support

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

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