File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / hooks.c
Revision 1.1.1.1.2.4: download - view: text, annotated - select for diffs - revision graph
Thu Aug 11 22:55:28 2011 UTC (13 years, 1 month ago) by misho
Branches: sched1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
fix initscheduler & hooks
+ register tasks

    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.1.1.1.2.4 2011/08/11 22:55:28 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
   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:  * @root = root task
   53:  * @data = optional data if !=NULL
   54:  * return: <0 errors and 0 ok
   55:  */
   56: void *
   57: sched_hook_init(void *root, void *data)
   58: {
   59: 	sched_root_task_t *r = root;
   60: 
   61: 	if (!r || r->root_data.iov_base || r->root_data.iov_len)
   62: 		return (void*) -1;
   63: 
   64: 	r->root_data.iov_base = malloc(sizeof(struct sched_IO));
   65: 	if (!r->root_data.iov_base) {
   66: 		LOGERR;
   67: 		return (void*) -1;
   68: 	} else {
   69: 		r->root_data.iov_len = sizeof(struct sched_IO);
   70: 		memset(r->root_data.iov_base, 0, r->root_data.iov_len);
   71: 	}
   72: 
   73: 	r->root_kq = kqueue();
   74: 	if (r->root_kq == -1) {
   75: 		LOGERR;
   76: 		return (void*) -1;
   77: 	}
   78: 
   79: 	return NULL;
   80: }
   81: 
   82: /*
   83:  * sched_hook_fini() - Default FINI hook
   84:  * @root = root task
   85:  * @arg = unused
   86:  * return: <0 errors and 0 ok
   87:  */
   88: void *
   89: sched_hook_fini(void *root, void *arg __unused)
   90: {
   91: 	sched_root_task_t *r = root;
   92: 
   93: 	if (!r)
   94: 		return (void*) -1;
   95: 
   96: 	if (r->root_kq > 2) {
   97: 		close(r->root_kq);
   98: 		r->root_kq = 0;
   99: 	}
  100: 
  101: 	if (r->root_data.iov_base && r->root_data.iov_len) {
  102: 		free(r->root_data.iov_base);
  103: 		r->root_data.iov_base = NULL;
  104: 		r->root_data.iov_len = 0;
  105: 	}
  106: 
  107: 	return NULL;
  108: }
  109: 
  110: /*
  111:  * sched_hook_cancel() - Default CANCEL hook
  112:  * @task = current task
  113:  * @arg = unused
  114:  * return: <0 errors and 0 ok
  115:  */
  116: void *
  117: sched_hook_cancel(void *task, void *arg __unused)
  118: {
  119: 	struct sched_IO *io;
  120: 	sched_task_t *t = task;
  121: 	struct kevent chg[2];
  122: 	struct timespec timeout = { 0, 0 };
  123: 
  124: 	if (!t || !t->task_root || !ROOT_DATA(t->task_root) || !ROOT_DATLEN(t->task_root))
  125: 		return (void*) -1;
  126: 	else
  127: 		io = ROOT_DATA(t->task_root);
  128: 
  129: 	switch (t->task_type) {
  130: 		case taskREAD:
  131: #ifdef __NetBSD__
  132: 			EV_SET(&chg[1], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (intptr_t) &TASK_FD(t));
  133: #else
  134: 			EV_SET(&chg[1], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, &TASK_FD(t));
  135: #endif
  136: 			if (FD_ISSET(TASK_FD(t), &io->wfd))
  137: #ifdef __NetBSD__
  138: 				EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD, 0, 0, (intptr_t) &TASK_FD(t));
  139: #else
  140: 				EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD, 0, 0, &TASK_FD(t));
  141: #endif
  142: 			else
  143: #ifdef __NetBSD__
  144: 				EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (intptr_t) &TASK_FD(t));
  145: #else
  146: 				EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, &TASK_FD(t));
  147: #endif
  148: 			kevent(t->task_root->root_kq, chg, 2, NULL, 0, &timeout);
  149: 
  150: 			FD_CLR(TASK_FD(t), &io->rfd);
  151: 			break;
  152: 		case taskWRITE:
  153: #ifdef __NetBSD__
  154: 			EV_SET(&chg[1], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (intptr_t) &TASK_FD(t));
  155: #else
  156: 			EV_SET(&chg[1], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, &TASK_FD(t));
  157: #endif
  158: 			if (FD_ISSET(TASK_FD(t), &io->rfd))
  159: #ifdef __NetBSD__
  160: 				EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD, 0, 0, (intptr_t) &TASK_FD(t));
  161: #else
  162: 				EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD, 0, 0, &TASK_FD(t));
  163: #endif
  164: 			else
  165: #ifdef __NetBSD__
  166: 				EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (intptr_t) &TASK_FD(t));
  167: #else
  168: 				EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, &TASK_FD(t));
  169: #endif
  170: 			kevent(t->task_root->root_kq, chg, 2, NULL, 0, &timeout);
  171: 
  172: 			FD_CLR(TASK_FD(t), &io->wfd);
  173: 			break;
  174: 		default:
  175: 			break;
  176: 	}
  177: 
  178: 	return NULL;
  179: }
  180: 
  181: /*
  182:  * sched_hook_read() - Default READ hook
  183:  * @task = current task
  184:  * @arg = unused
  185:  * return: <0 errors and 0 ok
  186:  */
  187: void *
  188: sched_hook_read(void *task, void *arg __unused)
  189: {
  190: 	struct sched_IO *io;
  191: 	sched_task_t *t = task;
  192: 	struct kevent chg[1];
  193: 	struct timespec timeout = { 0, 0 };
  194: 
  195: 	if (!t || !t->task_root || !ROOT_DATA(t->task_root) || !ROOT_DATLEN(t->task_root))
  196: 		return (void*) -1;
  197: 	else
  198: 		io = ROOT_DATA(t->task_root);
  199: 
  200: 	if (FD_ISSET(TASK_FD(t), &io->rfd))
  201: 		return NULL;
  202: 	else
  203: 		FD_SET(TASK_FD(t), &io->rfd);
  204: 
  205: #ifdef __NetBSD__
  206: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD, 0, 0, (intptr_t) &TASK_FD(t));
  207: #else
  208: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD, 0, 0, &TASK_FD(t));
  209: #endif
  210: 	if (kevent(t->task_root->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  211: 		LOGERR;
  212: 		return (void*) -1;
  213: 	}
  214: 
  215: 	return NULL;
  216: }
  217: 
  218: /*
  219:  * sched_hook_write() - Default WRITE hook
  220:  * @task = current task
  221:  * @arg = unused
  222:  * return: <0 errors and 0 ok
  223:  */
  224: void *
  225: sched_hook_write(void *task, void *arg __unused)
  226: {
  227: 	struct sched_IO *io;
  228: 	sched_task_t *t = task;
  229: 	struct kevent chg[1];
  230: 	struct timespec timeout = { 0, 0 };
  231: 
  232: 	if (!t || !t->task_root || !ROOT_DATA(t->task_root) || !ROOT_DATLEN(t->task_root))
  233: 		return (void*) -1;
  234: 	else
  235: 		io = ROOT_DATA(t->task_root);
  236: 
  237: 	if (FD_ISSET(TASK_FD(t), &io->wfd))
  238: 		return NULL;
  239: 	else
  240: 		FD_SET(TASK_FD(t), &io->wfd);
  241: 
  242: #ifdef __NetBSD__
  243: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD, 0, 0, (intptr_t) &TASK_FD(t));
  244: #else
  245: 	EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD, 0, 0, &TASK_FD(t));
  246: #endif
  247: 	if (kevent(t->task_root->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  248: 		LOGERR;
  249: 		return (void*) -1;
  250: 	}
  251: 
  252: 	return NULL;
  253: }
  254: 
  255: /*
  256:  * sched_hook_fetch() - Default FETCH hook
  257:  * @root = root task
  258:  * @arg = unused
  259:  * return: NULL error or !=NULL fetched task
  260:  */
  261: void *
  262: sched_hook_fetch(void *root, void *arg __unused)
  263: {
  264: 	struct sched_IO *io;
  265: 	sched_root_task_t *r = root;
  266: 	sched_task_t *task;
  267: 	struct timeval now, m, mtmp;
  268: 	struct timespec nw, *timeout;
  269: 	struct kevent evt[1], res[KQ_EVENTS];
  270: 	register int i;
  271: 	int en;
  272: 
  273: 	if (!r || !ROOT_DATA(r) || !ROOT_DATLEN(r))
  274: 		return NULL;
  275: 
  276: 	/* get new task by queue priority */
  277: retry:
  278: 	while ((task = TAILQ_FIRST(&r->root_event))) {
  279: 		TAILQ_REMOVE(&r->root_event, task, task_node);
  280: 		task->task_type = taskUNUSE;
  281: 		TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  282: 		return task;
  283: 	}
  284: 	while ((task = TAILQ_FIRST(&r->root_ready))) {
  285: 		TAILQ_REMOVE(&r->root_ready, task, task_node);
  286: 		task->task_type = taskUNUSE;
  287: 		TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  288: 		return task;
  289: 	}
  290: 
  291: #ifdef TIMER_WITHOUT_SORT
  292: 	clock_gettime(CLOCK_MONOTONIC, &nw);
  293: 	now.tv_sec = nw.tv_sec;
  294: 	now.tv_usec = nw.tv_nsec / 1000;
  295: 
  296: 	timerclear(&r->root_wait);
  297: 	TAILQ_FOREACH(task, &r->root_timer, task_node) {
  298: 		if (!timerisset(&r->root_wait))
  299: 			r->root_wait = TASK_TV(task);
  300: 		else if (timercmp(&TASK_TV(task), &r->root_wait, -) < 0)
  301: 			r->root_wait = TASK_TV(task);
  302: 	}
  303: 
  304: 	if (TAILQ_FIRST(&r->root_timer)) {
  305: 		m = r->root_wait;
  306: 		timersub(&m, &now, &mtmp);
  307: 		r->root_wait = mtmp;
  308: 	} else {
  309: 		/* set wait INFTIM */
  310: 		r->root_wait.tv_sec = r->root_wait.tv_usec = -1;
  311: 	}
  312: #else
  313: 	if (!TAILQ_FIRST(&r->root_eventlo) && (task = TAILQ_FIRST(&r->root_timer))) {
  314: 		clock_gettime(CLOCK_MONOTONIC, &nw);
  315: 		now.tv_sec = nw.tv_sec;
  316: 		now.tv_usec = nw.tv_nsec / 1000;
  317: 
  318: 		m = TASK_TV(task);
  319: 		timersub(&m, &now, &mtmp);
  320: 		r->root_wait = mtmp;
  321: 	} else {
  322: 		/* set wait INFTIM */
  323: 		r->root_wait.tv_sec = r->root_wait.tv_usec = -1;
  324: 	}
  325: #endif
  326: 	/* if present member of eventLo, set NOWAIT */
  327: 	if (TAILQ_FIRST(&r->root_eventlo))
  328: 		timerclear(&r->root_wait);
  329: 
  330: 	if (r->root_wait.tv_sec != -1 && r->root_wait.tv_usec != -1) {
  331: 		nw.tv_sec = r->root_wait.tv_sec;
  332: 		nw.tv_nsec = r->root_wait.tv_usec * 1000;
  333: 		timeout = &nw;
  334: 	} else	/* wait INFTIM */
  335: 		timeout = NULL;
  336: 	if ((en = kevent(r->root_kq, NULL, 0, res, KQ_EVENTS, timeout)) == -1) {
  337: 		LOGERR;
  338: 		goto retry;
  339: 	}
  340: 
  341: 	nw.tv_sec = nw.tv_nsec = 0;
  342: 	/* Go and catch the cat into pipes ... */
  343: 	for (i = 0; i < en; i++) {
  344: 		memcpy(evt, &res[i], sizeof evt);
  345: 		evt->flags = EV_DELETE;
  346: 		/* Put read/write task to ready queue */
  347: 		switch (res[i].filter) {
  348: 			case EVFILT_READ:
  349: 				TAILQ_FOREACH(task, &r->root_read, task_node) {
  350: 					if (TASK_FD(task) != *((int*) res[i].udata))
  351: 						continue;
  352: 					/* remove read handle */
  353: 					io = ROOT_DATA(task->task_root);
  354: 					FD_CLR(TASK_FD(task), &io->rfd);
  355: 
  356: 					TAILQ_REMOVE(&r->root_read, task, task_node);
  357: 					task->task_type = taskREADY;
  358: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  359: 					break;
  360: 				}
  361: 				break;
  362: 			case EVFILT_WRITE:
  363: 				TAILQ_FOREACH(task, &r->root_write, task_node) {
  364: 					if (TASK_FD(task) != *((int*) res[i].udata))
  365: 						continue;
  366: 					/* remove write handle */
  367: 					io = ROOT_DATA(task->task_root);
  368: 					FD_CLR(TASK_FD(task), &io->wfd);
  369: 
  370: 					TAILQ_REMOVE(&r->root_write, task, task_node);
  371: 					task->task_type = taskREADY;
  372: 					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  373: 					break;
  374: 				}
  375: 				break;
  376: 		}
  377: 
  378: 		if (kevent(r->root_kq, evt, 1, NULL, 0, &nw) == -1)
  379: 			LOGERR;
  380: 	}
  381: 
  382: 	/* timer update */
  383: 	clock_gettime(CLOCK_MONOTONIC, &nw);
  384: 	now.tv_sec = nw.tv_sec;
  385: 	now.tv_usec = nw.tv_nsec / 1000;
  386: 
  387: 	TAILQ_FOREACH(task, &r->root_timer, task_node)
  388: 		if (timercmp(&now, &TASK_TV(task), -) >= 0) {
  389: 			TAILQ_REMOVE(&r->root_timer, task, task_node);
  390: 			task->task_type = taskREADY;
  391: 			TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  392: 		}
  393: 
  394: 	/* put eventlo priority task to ready queue, if there is no ready task or 
  395: 	   	reach max missed fetch-rotate */
  396: 	if ((task = TAILQ_FIRST(&r->root_eventlo))) {
  397: 		if (!TAILQ_FIRST(&r->root_ready) || r->root_eventlo_miss > MAX_EVENTLO_MISS) {
  398: 			r->root_eventlo_miss = 0;
  399: 
  400: 			TAILQ_REMOVE(&r->root_eventlo, task, task_node);
  401: 			task->task_type = taskREADY;
  402: 			TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
  403: 		} else
  404: 			r->root_eventlo_miss++;
  405: 	} else
  406: 		r->root_eventlo_miss = 0;
  407: 
  408: 	/* OK, lets get ready task !!! */
  409: 	if (!(task = TAILQ_FIRST(&r->root_ready)))
  410: 		goto retry;
  411: 	TAILQ_REMOVE(&r->root_ready, task, task_node);
  412: 	task->task_type = taskUNUSE;
  413: 	TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
  414: 	return task;
  415: }
  416: 

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