File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.5.2.1: download - view: text, annotated - select for diffs - revision graph
Tue Mar 13 10:00:37 2012 UTC (12 years, 7 months ago) by misho
Branches: sched1_4
Diff to: branchpoint 1.5: preferred, unified
add new hook - condition
add new condition variable into sched_root_task_t
add new API function for control condition kill of scheduler loop
add year

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

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