File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.6.2.4: download - view: text, annotated - select for diffs - revision graph
Thu May 3 15:05:09 2012 UTC (12 years, 1 month ago) by misho
Branches: sched1_5
Diff to: branchpoint 1.6: preferred, unified
optimize scheduler ... remove unused structure into hooks

    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: aitsched.c,v 1.6.2.4 2012/05/03 15:05:09 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: #pragma GCC visibility push(hidden)
   51: 
   52: int sched_Errno;
   53: char sched_Error[STRSIZ];
   54: 
   55: #pragma GCC visibility pop
   56: 
   57: 
   58: // sched_GetErrno() Get error code of last operation
   59: inline int
   60: sched_GetErrno()
   61: {
   62: 	return sched_Errno;
   63: }
   64: 
   65: // sched_GetError() Get error text of last operation
   66: inline const char *
   67: sched_GetError()
   68: {
   69: 	return sched_Error;
   70: }
   71: 
   72: // sched_SetErr() Set error to variables for internal use!!!
   73: inline void
   74: sched_SetErr(int eno, char *estr, ...)
   75: {
   76: 	va_list lst;
   77: 
   78: 	sched_Errno = eno;
   79: 	memset(sched_Error, 0, sizeof sched_Error);
   80: 	va_start(lst, estr);
   81: 	vsnprintf(sched_Error, sizeof sched_Error, estr, lst);
   82: 	va_end(lst);
   83: }
   84: 
   85: /* Init and prepare scheduler functions */
   86: 
   87: /*
   88:  * schedRegisterHooks() - Register IO handles and bind tasks to it
   89:  *
   90:  * @root = root task
   91:  * return: -1 error or 0 ok
   92:  */
   93: int
   94: schedRegisterHooks(sched_root_task_t * __restrict root)
   95: {
   96: 	assert(root);
   97: 
   98: 	if (root->root_hooks.hook_root.fini)
   99: 		root->root_hooks.hook_root.fini(root, NULL);
  100: 	memset(&root->root_hooks, 0, sizeof root->root_hooks);
  101: 
  102: 	root->root_hooks.hook_add.read = sched_hook_read;
  103: 	root->root_hooks.hook_add.write = sched_hook_write;
  104: 
  105: 	root->root_hooks.hook_exec.cancel = sched_hook_cancel;
  106: 	root->root_hooks.hook_exec.fetch = sched_hook_fetch;
  107: 	root->root_hooks.hook_exec.exception = sched_hook_exception;
  108: 
  109: 	root->root_hooks.hook_root.init = sched_hook_init;
  110: 	root->root_hooks.hook_root.fini = sched_hook_fini;
  111: 	return 0;
  112: }
  113: 
  114: /*
  115:  * schedInit() - Init scheduler
  116:  *
  117:  * @data = optional data if !=NULL
  118:  * @datlen = data len if data is set
  119:  * return: allocated root task if ok or NULL error
  120:  */
  121: sched_root_task_t *
  122: schedInit(void ** __restrict data, size_t datlen)
  123: {
  124: 	sched_root_task_t *root = NULL;
  125: 	int (*func)(sched_root_task_t *);
  126: #ifdef HAVE_LIBPTHREAD
  127: 	register int i;
  128: #endif
  129: 
  130: 	root = malloc(sizeof(sched_root_task_t));
  131: 	if (!root) {
  132: 		LOGERR;
  133: 	} else {
  134: 		memset(root, 0, sizeof(sched_root_task_t));
  135: 
  136: 		/* INFINIT polling period by default */
  137: 		sched_timespecinf(&root->root_poll);
  138: 
  139: #ifdef HAVE_LIBPTHREAD
  140: 		for (i = 0; i < taskMAX; i++)
  141: 			if (pthread_mutex_init(&root->root_mtx[i], NULL)) {
  142: 				LOGERR;
  143: 				while (i)
  144: 					pthread_mutex_destroy(&root->root_mtx[--i]);
  145: 				free(root);
  146: 				return NULL;
  147: 			}
  148: 
  149: 		for (i = 0; i < taskMAX; i++)
  150: 			pthread_mutex_lock(&root->root_mtx[i]);
  151: #endif
  152: 
  153: 		TAILQ_INIT(&root->root_read);
  154: 		TAILQ_INIT(&root->root_write);
  155: 		TAILQ_INIT(&root->root_timer);
  156: 		TAILQ_INIT(&root->root_event);
  157: 		TAILQ_INIT(&root->root_eventlo);
  158: 		TAILQ_INIT(&root->root_ready);
  159: 		TAILQ_INIT(&root->root_unuse);
  160: 
  161: #ifdef HAVE_LIBPTHREAD
  162: 		for (i = 0; i < taskMAX; i++)
  163: 			pthread_mutex_unlock(&root->root_mtx[i]);
  164: #endif
  165: 
  166: 		if (data && *data) {
  167: 			if (datlen) {
  168: 				root->root_data.iov_base = *data;
  169: 				root->root_data.iov_len = datlen;
  170: 			} else { /* if datlen == 0, switch to callbacks init mode */
  171: 				 /* little hack :) for correct initialization of scheduler */
  172: 				func = (int(*)(sched_root_task_t*)) data;
  173: 				func(root);
  174: 			}
  175: 		}
  176: 
  177: 		if (root->root_hooks.hook_root.init)
  178: 			root->root_hooks.hook_root.init(root, NULL);
  179: 	}
  180: 
  181: 	return root;
  182: }
  183: 
  184: /*
  185:  * schedEnd() - End scheduler & free all resources
  186:  *
  187:  * @root = root task
  188:  * return: -1 error or 0 ok
  189:  */
  190: int
  191: schedEnd(sched_root_task_t ** __restrict root)
  192: {
  193: 	sched_task_t *task;
  194: #ifdef HAVE_LIBPTHREAD
  195: 	register int i;
  196: #endif
  197: 
  198: 	if (!root || !*root)
  199: 		return -1;
  200: 
  201: 	TAILQ_FOREACH(task, &(*root)->root_read, task_node) {
  202: 		schedCancel(task);
  203: 	}
  204: 	TAILQ_FOREACH(task, &(*root)->root_write, task_node) {
  205: 		schedCancel(task);
  206: 	}
  207: 	TAILQ_FOREACH(task, &(*root)->root_timer, task_node) {
  208: 		schedCancel(task);
  209: 	}
  210: 	TAILQ_FOREACH(task, &(*root)->root_event, task_node) {
  211: 		schedCancel(task);
  212: 	}
  213: 	TAILQ_FOREACH(task, &(*root)->root_eventlo, task_node) {
  214: 		schedCancel(task);
  215: 	}
  216: 	TAILQ_FOREACH(task, &(*root)->root_ready, task_node) {
  217: 		schedCancel(task);
  218: 	}
  219: 
  220: #ifdef HAVE_LIBPTHREAD
  221: 	pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
  222: #endif
  223: 	while ((task = TAILQ_FIRST(&(*root)->root_unuse))) {
  224: 		TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
  225: 		free(task);
  226: 	}
  227: #ifdef HAVE_LIBPTHREAD
  228: 	pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
  229: #endif
  230: 
  231: 	if ((*root)->root_hooks.hook_root.fini)
  232: 		(*root)->root_hooks.hook_root.fini(*root, NULL);
  233: 
  234: #ifdef HAVE_LIBPTHREAD
  235: 	for (i = 0; i < taskMAX; i++)
  236: 		pthread_mutex_destroy(&(*root)->root_mtx[i]);
  237: #endif
  238: 
  239: 	free(*root);
  240: 	*root = NULL;
  241: 	return 0;
  242: }
  243: 
  244: /*
  245:  * schedCall() - Call task execution function
  246:  *
  247:  * @task = current task
  248:  * return: !=NULL error or =NULL ok
  249:  */
  250: inline void *
  251: schedCall(sched_task_t * __restrict task)
  252: {
  253: 	void *ptr = (void*) -1;
  254: 
  255: 	if (!task)
  256: 		return ptr;
  257: 
  258: 	if (!TASK_ISLOCKED(task))
  259: 		TASK_LOCK(task);
  260: 
  261: 	task->task_id++;
  262: 	ptr = task->task_func(task);
  263: 
  264: 	TASK_UNLOCK(task);
  265: 	return ptr;
  266: }
  267: 
  268: /*
  269:  * schedFetch() - Fetch ready task
  270:  *
  271:  * @root = root task
  272:  * return: =NULL error or !=NULL ready task
  273:  */
  274: inline void *
  275: schedFetch(sched_root_task_t * __restrict root)
  276: {
  277: 	void *ptr;
  278: 
  279: 	if (!root)
  280: 		return NULL;
  281: 
  282: 	if (root->root_hooks.hook_exec.fetch)
  283: 		ptr = root->root_hooks.hook_exec.fetch(root, NULL);
  284: 	else
  285: 		ptr = NULL;
  286: 
  287: 	return ptr;
  288: }
  289: 
  290: /*
  291:  * schedCancel() - Cancel task from scheduler
  292:  *
  293:  * @task = task
  294:  * return: -1 error or 0 ok
  295:  */
  296: int
  297: schedCancel(sched_task_t * __restrict task)
  298: {
  299: 	sched_queue_t *queue;
  300: 
  301: 	if (!task || !TASK_ROOT(task))
  302: 		return -1;
  303: 
  304: 	if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  305: 		if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
  306: 			return -1;
  307: 
  308: 	switch (TASK_TYPE(task)) {
  309: 		case taskREAD:
  310: 			queue = &TASK_ROOT(task)->root_read;
  311: 			break;
  312: 		case taskWRITE:
  313: 			queue = &TASK_ROOT(task)->root_write;
  314: 			break;
  315: 		case taskTIMER:
  316: 			queue = &TASK_ROOT(task)->root_timer;
  317: 			break;
  318: 		case taskEVENT:
  319: 			queue = &TASK_ROOT(task)->root_event;
  320: 			break;
  321: 		case taskEVENTLO:
  322: 			queue = &TASK_ROOT(task)->root_eventlo;
  323: 			break;
  324: 		case taskREADY:
  325: 			queue = &TASK_ROOT(task)->root_ready;
  326: 			break;
  327: 		default:
  328: 			queue = NULL;
  329: 	}
  330: 	if (queue) {
  331: #ifdef HAVE_LIBPTHREAD
  332: 		pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  333: #endif
  334: 		TAILQ_REMOVE(queue, task, task_node);
  335: #ifdef HAVE_LIBPTHREAD
  336: 		pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  337: #endif
  338: 	}
  339: 	if (TASK_TYPE(task) != taskUNUSE)
  340: 		_sched_unuseTask(task);
  341: 
  342: 	return 0;
  343: }
  344: 
  345: /*
  346:  * schedCancelby() - Cancel task from scheduler by criteria
  347:  *
  348:  * @root = root task
  349:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
  350:  * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
  351:  * @param = search parameter
  352:  * @hook = custom cleanup hook function, may be NULL
  353:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
  354:  */
  355: int
  356: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
  357: 		u_char criteria, void *param, sched_hook_func_t hook)
  358: {
  359: 	sched_task_t *task;
  360: 	sched_queue_t *queue;
  361: 	int flg = 0;
  362: 
  363: 	if (!root)
  364: 		return -1;
  365: 	if (type == taskMAX) {
  366: 		if (schedCancelby(root, taskREAD, criteria, param, hook))
  367: 			return -2;
  368: 		if (schedCancelby(root, taskWRITE, criteria, param, hook))
  369: 			return -2;
  370: 		if (schedCancelby(root, taskTIMER, criteria, param, hook))
  371: 			return -2;
  372: 		if (schedCancelby(root, taskEVENT, criteria, param, hook))
  373: 			return -2;
  374: 		if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
  375: 			return -2;
  376: 		if (schedCancelby(root, taskREADY, criteria, param, hook))
  377: 			return -2;
  378: 		return 0;
  379: 	}
  380: 	switch (type) {
  381: 		case taskREAD:
  382: 			queue = &root->root_read;
  383: 			break;
  384: 		case taskWRITE:
  385: 			queue = &root->root_write;
  386: 			break;
  387: 		case taskTIMER:
  388: 			queue = &root->root_timer;
  389: 			break;
  390: 		case taskEVENT:
  391: 			queue = &root->root_event;
  392: 			break;
  393: 		case taskEVENTLO:
  394: 			queue = &root->root_eventlo;
  395: 			break;
  396: 		case taskREADY:
  397: 			queue = &root->root_ready;
  398: 			break;
  399: 		default:
  400: 			return 0;
  401: 	}
  402: 
  403: #ifdef HAVE_LIBPTHREAD
  404: 	pthread_mutex_lock(&root->root_mtx[type]);
  405: #endif
  406: 	TAILQ_FOREACH(task, queue, task_node)
  407: 		if (criteria == CRITERIA_CALL) {
  408: 			if (task->task_func == (sched_task_func_t) param) {
  409: 				flg++;
  410: 				break;
  411: 			}
  412: 		} else if (criteria == CRITERIA_ARG) {
  413: 			if (task->task_arg == param) {
  414: 				flg++;
  415: 				break;
  416: 			}
  417: 		} else if (criteria == CRITERIA_FD) {
  418: 			if (TASK_FD(task) == (intptr_t) param) {
  419: 				flg++;
  420: 				break;
  421: 			}
  422: 		} else if (criteria == CRITERIA_VAL) {
  423: 			if (TASK_VAL(task) == (u_long) param) {
  424: 				flg++;
  425: 				break;
  426: 			}
  427: 		} else if (criteria == CRITERIA_TV) {
  428: 			if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -)) {
  429: 				flg++;
  430: 				break;
  431: 			}
  432: 		} else {
  433: #ifdef HAVE_LIBPTHREAD
  434: 			pthread_mutex_unlock(&root->root_mtx[type]);
  435: #endif
  436: 			sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
  437: 			return -1;
  438: 		}
  439: #ifdef HAVE_LIBPTHREAD
  440: 	pthread_mutex_unlock(&root->root_mtx[type]);
  441: #endif
  442: 	if (!flg || !task)	/* task not found */
  443: 		return 0;
  444: 
  445: 	if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  446: 		if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
  447: 			return -1;
  448: 	if (hook)
  449: 		if (hook(task, NULL))
  450: 			return -3;
  451: 
  452: #ifdef HAVE_LIBPTHREAD
  453: 	pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[type]);
  454: #endif
  455: 	TAILQ_REMOVE(queue, task, task_node);
  456: #ifdef HAVE_LIBPTHREAD
  457: 	pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[type]);
  458: #endif
  459: 
  460: 	if (TASK_TYPE(task) != taskUNUSE)
  461: 		_sched_unuseTask(task);
  462: 	return 0;
  463: }
  464: 
  465: /*
  466:  * schedRun() - Scheduler *run loop*
  467:  *
  468:  * @root = root task
  469:  * @killState = kill condition variable, if !=0 stop scheduler loop
  470:  * return: -1 error or 0 ok
  471:  */
  472: int
  473: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
  474: {
  475: 	sched_task_t *task;
  476: 
  477: 	if (!root)
  478: 		return -1;
  479: 
  480: 	if (root->root_hooks.hook_exec.run)
  481: 		if (root->root_hooks.hook_exec.run(root, NULL))
  482: 			return -1;
  483: 
  484: 	if (killState) {
  485: 		if (root->root_hooks.hook_exec.condition)
  486: 			/* condition scheduler loop */
  487: 			while (root && root->root_hooks.hook_exec.fetch && 
  488: 					root->root_hooks.hook_exec.condition && 
  489: 					root->root_hooks.hook_exec.condition(root, (void*) killState)) {
  490: 				if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  491: 					schedCall(task);
  492: 			}
  493: 		else
  494: 			/* trigger scheduler loop */
  495: 			while (!*killState && root && root->root_hooks.hook_exec.fetch) {
  496: 				if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  497: 					schedCall(task);
  498: 			}
  499: 	} else
  500: 		/* infinite scheduler loop */
  501: 		while (root && root->root_hooks.hook_exec.fetch)
  502: 			if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  503: 				schedCall(task);
  504: 
  505: 	return 0;
  506: }
  507: 
  508: /*
  509:  * schedPolling() - Polling timeout period if no timer task is present
  510:  *
  511:  * @root = root task
  512:  * @ts = timeout polling period, if ==NULL INFINIT timeout
  513:  * @tsold = old timeout polling if !=NULL
  514:  * return: -1 error or 0 ok
  515:  */
  516: inline int
  517: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts, 
  518: 		struct timespec * __restrict tsold)
  519: {
  520: 	if (!root)
  521: 		return -1;
  522: 
  523: 	if (tsold)
  524: 		*tsold = root->root_poll;
  525: 
  526: 	if (!ts)
  527: 		sched_timespecinf(&root->root_poll);
  528: 	else
  529: 		root->root_poll = *ts;
  530: 
  531: 	return 0;
  532: }
  533: 
  534: /*
  535:  * schedTermCondition() - Activate hook for scheduler condition kill
  536:  *
  537:  * @root = root task
  538:  * @condValue = condition value, kill schedRun() if condValue == killState
  539:  * return: -1 error ok 0 ok
  540:  */
  541: inline int
  542: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
  543: {
  544: 	if (!root)
  545: 		return -1;
  546: 
  547: 	root->root_cond = condValue;
  548: 	root->root_hooks.hook_exec.condition = sched_hook_condition;
  549: 	return 0;
  550: }

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