File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.10.6.1: download - view: text, annotated - select for diffs - revision graph
Tue Jul 24 13:47:01 2012 UTC (12 years, 3 months ago) by misho
Branches: sched2_5
Diff to: branchpoint 1.10: preferred, unified
present new feature for elwix scheduler
 - task SUSPEND for suspended tasks with user resume functionality

    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.10.6.1 2012/07/24 13:47:01 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: 	root->root_hooks.hook_add.alarm = sched_hook_alarm;
  105: 	root->root_hooks.hook_add.node = sched_hook_node;
  106: 	root->root_hooks.hook_add.proc = sched_hook_proc;
  107: 	root->root_hooks.hook_add.signal = sched_hook_signal;
  108: #ifdef EVFILT_USER
  109: 	root->root_hooks.hook_add.user = sched_hook_user;
  110: #endif
  111: 
  112: 	root->root_hooks.hook_exec.cancel = sched_hook_cancel;
  113: 	root->root_hooks.hook_exec.fetch = sched_hook_fetch;
  114: 	root->root_hooks.hook_exec.exception = sched_hook_exception;
  115: 
  116: 	root->root_hooks.hook_root.init = sched_hook_init;
  117: 	root->root_hooks.hook_root.fini = sched_hook_fini;
  118: 	return 0;
  119: }
  120: 
  121: /*
  122:  * schedInit() - Init scheduler
  123:  *
  124:  * @data = optional data if !=NULL
  125:  * @datlen = data len if data is set
  126:  * return: allocated root task if ok or NULL error
  127:  */
  128: sched_root_task_t *
  129: schedInit(void ** __restrict data, size_t datlen)
  130: {
  131: 	sched_root_task_t *root = NULL;
  132: 	int (*func)(sched_root_task_t *);
  133: #ifdef HAVE_LIBPTHREAD
  134: 	register int i;
  135: #endif
  136: 
  137: 	root = malloc(sizeof(sched_root_task_t));
  138: 	if (!root) {
  139: 		LOGERR;
  140: 	} else {
  141: 		memset(root, 0, sizeof(sched_root_task_t));
  142: 
  143: 		/* INFINIT polling period by default */
  144: 		sched_timespecinf(&root->root_poll);
  145: 
  146: #ifdef HAVE_LIBPTHREAD
  147: 		for (i = 0; i < taskMAX; i++)
  148: 			if (pthread_mutex_init(&root->root_mtx[i], NULL)) {
  149: 				LOGERR;
  150: 				while (i)
  151: 					pthread_mutex_destroy(&root->root_mtx[--i]);
  152: 				free(root);
  153: 				return NULL;
  154: 			}
  155: 
  156: 		for (i = 0; i < taskMAX; i++)
  157: 			pthread_mutex_lock(&root->root_mtx[i]);
  158: #endif
  159: 
  160: 		TAILQ_INIT(&root->root_read);
  161: 		TAILQ_INIT(&root->root_write);
  162: 		TAILQ_INIT(&root->root_timer);
  163: 		TAILQ_INIT(&root->root_alarm);
  164: 		TAILQ_INIT(&root->root_node);
  165: 		TAILQ_INIT(&root->root_proc);
  166: 		TAILQ_INIT(&root->root_user);
  167: 		TAILQ_INIT(&root->root_signal);
  168: 		TAILQ_INIT(&root->root_event);
  169: 		TAILQ_INIT(&root->root_eventlo);
  170: 		TAILQ_INIT(&root->root_suspend);
  171: 		TAILQ_INIT(&root->root_ready);
  172: 		TAILQ_INIT(&root->root_unuse);
  173: 
  174: #ifdef HAVE_LIBPTHREAD
  175: 		for (i = 0; i < taskMAX; i++)
  176: 			pthread_mutex_unlock(&root->root_mtx[i]);
  177: #endif
  178: 
  179: 		if (data && *data) {
  180: 			if (datlen) {
  181: 				root->root_data.iov_base = *data;
  182: 				root->root_data.iov_len = datlen;
  183: 			} else { /* if datlen == 0, switch to callbacks init mode */
  184: 				 /* little hack :) for correct initialization of scheduler */
  185: 				func = (int(*)(sched_root_task_t*)) data;
  186: 				func(root);
  187: 			}
  188: 		}
  189: 
  190: 		if (root->root_hooks.hook_root.init)
  191: 			root->root_hooks.hook_root.init(root, NULL);
  192: 	}
  193: 
  194: 	return root;
  195: }
  196: 
  197: /*
  198:  * schedEnd() - End scheduler & free all resources
  199:  *
  200:  * @root = root task
  201:  * return: -1 error or 0 ok
  202:  */
  203: int
  204: schedEnd(sched_root_task_t ** __restrict root)
  205: {
  206: 	sched_task_t *task, *tmp;
  207: #ifdef HAVE_LIBPTHREAD
  208: 	register int i;
  209: #endif
  210: 
  211: 	if (!root || !*root)
  212: 		return -1;
  213: 
  214: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_read, task_node, tmp)
  215: 		schedCancel(task);
  216: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_write, task_node, tmp)
  217: 		schedCancel(task);
  218: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_timer, task_node, tmp)
  219: 		schedCancel(task);
  220: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_alarm, task_node, tmp)
  221: 		schedCancel(task);
  222: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_node, task_node, tmp)
  223: 		schedCancel(task);
  224: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_proc, task_node, tmp)
  225: 		schedCancel(task);
  226: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_user, task_node, tmp)
  227: 		schedCancel(task);
  228: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_signal, task_node, tmp)
  229: 		schedCancel(task);
  230: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_event, task_node, tmp)
  231: 		schedCancel(task);
  232: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_eventlo, task_node, tmp)
  233: 		schedCancel(task);
  234: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_suspend, task_node, tmp)
  235: 		schedCancel(task);
  236: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_ready, task_node, tmp)
  237: 		schedCancel(task);
  238: 
  239: #ifdef HAVE_LIBPTHREAD
  240: 	pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
  241: #endif
  242: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_unuse, task_node, tmp) {
  243: 		TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
  244: 		free(task);
  245: 	}
  246: #ifdef HAVE_LIBPTHREAD
  247: 	pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
  248: #endif
  249: 
  250: 	if ((*root)->root_hooks.hook_root.fini)
  251: 		(*root)->root_hooks.hook_root.fini(*root, NULL);
  252: 
  253: #ifdef HAVE_LIBPTHREAD
  254: 	for (i = 0; i < taskMAX; i++)
  255: 		pthread_mutex_destroy(&(*root)->root_mtx[i]);
  256: #endif
  257: 
  258: 	free(*root);
  259: 	*root = NULL;
  260: 	return 0;
  261: }
  262: 
  263: /*
  264:  * schedCall() - Call task execution function
  265:  *
  266:  * @task = current task
  267:  * return: !=NULL error or =NULL ok
  268:  */
  269: inline void *
  270: schedCall(sched_task_t * __restrict task)
  271: {
  272: 	void *ptr = (void*) -1;
  273: 
  274: 	if (!task)
  275: 		return ptr;
  276: 
  277: 	if (!TASK_ISLOCKED(task))
  278: 		TASK_LOCK(task);
  279: 
  280: 	ptr = task->task_func(task);
  281: 
  282: 	TASK_UNLOCK(task);
  283: 	return ptr;
  284: }
  285: 
  286: /*
  287:  * schedFetch() - Fetch ready task
  288:  *
  289:  * @root = root task
  290:  * return: =NULL error or !=NULL ready task
  291:  */
  292: inline void *
  293: schedFetch(sched_root_task_t * __restrict root)
  294: {
  295: 	void *ptr;
  296: 
  297: 	if (!root)
  298: 		return NULL;
  299: 
  300: 	if (root->root_hooks.hook_exec.fetch)
  301: 		ptr = root->root_hooks.hook_exec.fetch(root, NULL);
  302: 	else
  303: 		ptr = NULL;
  304: 
  305: 	return ptr;
  306: }
  307: 
  308: /*
  309:  * schedTrigger() - Triggering USER task
  310:  *
  311:  * @task = task
  312:  * return: -1 error or 0 ok
  313:  */
  314: int
  315: schedTrigger(sched_task_t * __restrict task)
  316: {
  317: #ifndef EVFILT_USER
  318: 	sched_SetErr(ENOTSUP, "Not supported kevent() filter");
  319: 	return -1;
  320: #else
  321: 	struct kevent chg[1];
  322: 	struct timespec timeout = { 0, 0 };
  323: 
  324: 	if (!task || !TASK_ROOT(task))
  325: 		return -1;
  326: 
  327: #ifdef __NetBSD__
  328: 	EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (intptr_t) TASK_VAL(task));
  329: #else
  330: 	EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (void*) TASK_VAL(task));
  331: #endif
  332: 	if (kevent(TASK_ROOT(task)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  333: 		LOGERR;
  334: 		return -1;
  335: 	}
  336: 
  337: 	return 0;
  338: #endif
  339: }
  340: 
  341: /*
  342:  * schedCancel() - Cancel task from scheduler
  343:  *
  344:  * @task = task
  345:  * return: -1 error or 0 ok
  346:  */
  347: int
  348: schedCancel(sched_task_t * __restrict task)
  349: {
  350: 	sched_queue_t *queue;
  351: 
  352: 	if (!task || !TASK_ROOT(task))
  353: 		return -1;
  354: 
  355: 	if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  356: 		if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
  357: 			return -1;
  358: 
  359: 	switch (TASK_TYPE(task)) {
  360: 		case taskREAD:
  361: 			queue = &TASK_ROOT(task)->root_read;
  362: 			break;
  363: 		case taskWRITE:
  364: 			queue = &TASK_ROOT(task)->root_write;
  365: 			break;
  366: 		case taskTIMER:
  367: 			queue = &TASK_ROOT(task)->root_timer;
  368: 			break;
  369: 		case taskALARM:
  370: 			queue = &TASK_ROOT(task)->root_alarm;
  371: 			break;
  372: 		case taskNODE:
  373: 			queue = &TASK_ROOT(task)->root_node;
  374: 			break;
  375: 		case taskPROC:
  376: 			queue = &TASK_ROOT(task)->root_proc;
  377: 			break;
  378: 		case taskUSER:
  379: 			queue = &TASK_ROOT(task)->root_user;
  380: 			break;
  381: 		case taskSIGNAL:
  382: 			queue = &TASK_ROOT(task)->root_signal;
  383: 			break;
  384: 		case taskEVENT:
  385: 			queue = &TASK_ROOT(task)->root_event;
  386: 			break;
  387: 		case taskEVENTLO:
  388: 			queue = &TASK_ROOT(task)->root_eventlo;
  389: 			break;
  390: 		case taskSUSPEND:
  391: 			queue = &TASK_ROOT(task)->root_suspend;
  392: 			break;
  393: 		case taskREADY:
  394: 			queue = &TASK_ROOT(task)->root_ready;
  395: 			break;
  396: 		default:
  397: 			queue = NULL;
  398: 	}
  399: 	if (queue) {
  400: #ifdef HAVE_LIBPTHREAD
  401: 		pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  402: #endif
  403: 		TAILQ_REMOVE(queue, TASK_ID(task), task_node);
  404: #ifdef HAVE_LIBPTHREAD
  405: 		pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  406: #endif
  407: 	}
  408: 	if (TASK_TYPE(task) != taskUNUSE)
  409: 		_sched_unuseTask(task);
  410: 
  411: 	return 0;
  412: }
  413: 
  414: /*
  415:  * schedCancelby() - Cancel task from scheduler by criteria
  416:  *
  417:  * @root = root task
  418:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
  419:  * @criteria = find task by criteria 
  420:  * 	[CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA]
  421:  * @param = search parameter
  422:  * @hook = custom cleanup hook function, may be NULL
  423:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
  424:  */
  425: int
  426: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
  427: 		u_char criteria, void *param, sched_hook_func_t hook)
  428: {
  429: 	sched_task_t *task, *tmp;
  430: 	sched_queue_t *queue;
  431: 	register int flg = 0;
  432: 
  433: 	if (!root)
  434: 		return -1;
  435: 	/* if type == taskMAX check in all queues */
  436: 	if (type == taskMAX) {
  437: 		if (schedCancelby(root, taskREAD, criteria, param, hook))
  438: 			return -2;
  439: 		if (schedCancelby(root, taskWRITE, criteria, param, hook))
  440: 			return -2;
  441: 		if (schedCancelby(root, taskTIMER, criteria, param, hook))
  442: 			return -2;
  443: 		if (schedCancelby(root, taskALARM, criteria, param, hook))
  444: 			return -2;
  445: 		if (schedCancelby(root, taskNODE, criteria, param, hook))
  446: 			return -2;
  447: 		if (schedCancelby(root, taskPROC, criteria, param, hook))
  448: 			return -2;
  449: 		if (schedCancelby(root, taskUSER, criteria, param, hook))
  450: 			return -2;
  451: 		if (schedCancelby(root, taskSIGNAL, criteria, param, hook))
  452: 			return -2;
  453: 		if (schedCancelby(root, taskEVENT, criteria, param, hook))
  454: 			return -2;
  455: 		if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
  456: 			return -2;
  457: 		if (schedCancelby(root, taskSUSPEND, criteria, param, hook))
  458: 			return -2;
  459: 		if (schedCancelby(root, taskREADY, criteria, param, hook))
  460: 			return -2;
  461: 		return 0;
  462: 	}
  463: 	/* choosen queue */
  464: 	switch (type) {
  465: 		case taskREAD:
  466: 			queue = &root->root_read;
  467: 			break;
  468: 		case taskWRITE:
  469: 			queue = &root->root_write;
  470: 			break;
  471: 		case taskTIMER:
  472: 			queue = &root->root_timer;
  473: 			break;
  474: 		case taskALARM:
  475: 			queue = &root->root_alarm;
  476: 			break;
  477: 		case taskNODE:
  478: 			queue = &root->root_node;
  479: 			break;
  480: 		case taskPROC:
  481: 			queue = &root->root_proc;
  482: 			break;
  483: 		case taskUSER:
  484: 			queue = &root->root_user;
  485: 			break;
  486: 		case taskSIGNAL:
  487: 			queue = &root->root_signal;
  488: 			break;
  489: 		case taskEVENT:
  490: 			queue = &root->root_event;
  491: 			break;
  492: 		case taskEVENTLO:
  493: 			queue = &root->root_eventlo;
  494: 			break;
  495: 		case taskSUSPEND:
  496: 			queue = &root->root_suspend;
  497: 			break;
  498: 		case taskREADY:
  499: 			queue = &root->root_ready;
  500: 			break;
  501: 		default:
  502: 			return 0;
  503: 	}
  504: 
  505: #ifdef HAVE_LIBPTHREAD
  506: 	pthread_mutex_lock(&root->root_mtx[type]);
  507: #endif
  508: 	TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
  509: 		flg ^= flg;
  510: 		switch (criteria) {
  511: 			case CRITERIA_ANY:
  512: 				flg = 1;
  513: 				break;
  514: 			case CRITERIA_CALL:
  515: 				if (TASK_FUNC(task) == (sched_task_func_t) param)
  516: 					flg = 1;
  517: 				break;
  518: 			case CRITERIA_ARG:
  519: 				if (TASK_ARG(task) == param)
  520: 					flg = 1;
  521: 				break;
  522: 			case CRITERIA_FD:
  523: 				if (TASK_FD(task) == (intptr_t) param)
  524: 					flg = 1;
  525: 				break;
  526: 			case CRITERIA_ID:
  527: 			case CRITERIA_VAL:
  528: 				if (TASK_VAL(task) == (u_long) param)
  529: 					flg = 1;
  530: 				break;
  531: 			case CRITERIA_TS:
  532: 				if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
  533: 					flg = 1;
  534: 				break;
  535: 			case CRITERIA_DATA:
  536: 				if (TASK_DATA(task) == param)
  537: 					flg = 1;
  538: 				break;
  539: 			default:
  540: 				sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
  541: 				flg = -1;
  542: 		}
  543: 		if (flg < 0)		/* error */
  544: 			break;
  545: 		/* cancel choosen task */
  546: 		if (flg > 0) {
  547: 			if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  548: 				if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
  549: 					flg = -1;
  550: 					break;
  551: 				}
  552: 			/* custom hook */
  553: 			if (hook)
  554: 				if (hook(task, NULL)) {
  555: 					flg = -3;
  556: 					break;
  557: 				}
  558: 
  559: 			TAILQ_REMOVE(queue, task, task_node);
  560: 			if (TASK_TYPE(task) != taskUNUSE)
  561: 				_sched_unuseTask(task);
  562: 
  563: 			flg ^= flg;	/* ok */
  564: 		}
  565: 	}
  566: #ifdef HAVE_LIBPTHREAD
  567: 	pthread_mutex_unlock(&root->root_mtx[type]);
  568: #endif
  569: 	return flg;
  570: }
  571: 
  572: /*
  573:  * schedRun() - Scheduler *run loop*
  574:  *
  575:  * @root = root task
  576:  * @killState = kill condition variable, if !=0 stop scheduler loop
  577:  * return: -1 error or 0 ok
  578:  */
  579: int
  580: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
  581: {
  582: 	sched_task_t *task;
  583: 
  584: 	if (!root)
  585: 		return -1;
  586: 
  587: 	if (root->root_hooks.hook_exec.run)
  588: 		if (root->root_hooks.hook_exec.run(root, NULL))
  589: 			return -1;
  590: 
  591: 	if (killState) {
  592: 		if (root->root_hooks.hook_exec.condition)
  593: 			/* condition scheduler loop */
  594: 			while (root && root->root_hooks.hook_exec.fetch && 
  595: 					root->root_hooks.hook_exec.condition && 
  596: 					root->root_hooks.hook_exec.condition(root, (void*) killState)) {
  597: 				if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  598: 					schedCall(task);
  599: 			}
  600: 		else
  601: 			/* trigger scheduler loop */
  602: 			while (!*killState && root && root->root_hooks.hook_exec.fetch) {
  603: 				if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  604: 					schedCall(task);
  605: 			}
  606: 	} else
  607: 		/* infinite scheduler loop */
  608: 		while (root && root->root_hooks.hook_exec.fetch)
  609: 			if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  610: 				schedCall(task);
  611: 
  612: 	return 0;
  613: }
  614: 
  615: /*
  616:  * schedPolling() - Polling timeout period if no timer task is present
  617:  *
  618:  * @root = root task
  619:  * @ts = timeout polling period, if ==NULL INFINIT timeout
  620:  * @tsold = old timeout polling if !=NULL
  621:  * return: -1 error or 0 ok
  622:  */
  623: inline int
  624: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts, 
  625: 		struct timespec * __restrict tsold)
  626: {
  627: 	if (!root)
  628: 		return -1;
  629: 
  630: 	if (tsold)
  631: 		*tsold = root->root_poll;
  632: 
  633: 	if (!ts)
  634: 		sched_timespecinf(&root->root_poll);
  635: 	else
  636: 		root->root_poll = *ts;
  637: 
  638: 	return 0;
  639: }
  640: 
  641: /*
  642:  * schedTermCondition() - Activate hook for scheduler condition kill
  643:  *
  644:  * @root = root task
  645:  * @condValue = condition value, kill schedRun() if condValue == killState
  646:  * return: -1 error ok 0 ok
  647:  */
  648: inline int
  649: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
  650: {
  651: 	if (!root)
  652: 		return -1;
  653: 
  654: 	root->root_cond = condValue;
  655: 	root->root_hooks.hook_exec.condition = sched_hook_condition;
  656: 	return 0;
  657: }
  658: 
  659: /*
  660:  * schedResumeby() - Resume suspended task
  661:  *
  662:  * @root = root task
  663:  * @criteria = find task by criteria 
  664:  * 	[CRITERIA_ANY|CRITERIA_ID|CRITERIA_DATA]
  665:  * @param = search parameter (sched_task_t *task| u_long id)
  666:  * return: -1 error or 0 resumed ok
  667:  */
  668: int
  669: schedResumeby(sched_root_task_t * __restrict root, u_char criteria, void *param)
  670: {
  671: 	sched_task_t *task, *tmp;
  672: 	register int flg = 0;
  673: 
  674: 	if (!root)
  675: 		return -1;
  676: 
  677: #ifdef HAVE_LIBPTHREAD
  678: 	pthread_mutex_lock(&root->root_mtx[taskSUSPEND]);
  679: #endif
  680: 	TAILQ_FOREACH_SAFE(task, &root->root_suspend, task_node, tmp) {
  681: 		flg ^= flg;
  682: 		switch (criteria) {
  683: 			case CRITERIA_ANY:
  684: 				flg = 1;
  685: 				break;
  686: 			case CRITERIA_ID:
  687: 				if (TASK_VAL(task) == (u_long) param)
  688: 					flg = 1;
  689: 				break;
  690: 			case CRITERIA_DATA:
  691: 				if (TASK_ID(task) == (sched_task_t*) param)
  692: 					flg = 1;
  693: 				break;
  694: 			default:
  695: 				sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
  696: 				flg = -1;
  697: 		}
  698: 		if (flg < 0)
  699: 			break;
  700: 		/* resume choosen task */
  701: 		if (flg > 0) {
  702: 			if (root->root_hooks.hook_exec.resume)
  703: 				if (root->root_hooks.hook_exec.resume(task, NULL)) {
  704: 					flg = -1;
  705: 					break;
  706: 				}
  707: 
  708: 			TAILQ_REMOVE(&root->root_suspend, task, task_node);
  709: 
  710: 			task->task_type = taskREADY;
  711: #ifdef HAVE_LIBPTHREAD
  712: 			pthread_mutex_lock(&root->root_mtx[taskREADY]);
  713: #endif
  714: 			TAILQ_INSERT_TAIL(&root->root_ready, task, task_node);
  715: #ifdef HAVE_LIBPTHREAD
  716: 			pthread_mutex_unlock(&root->root_mtx[taskREADY]);
  717: #endif
  718: 
  719: 			flg ^= flg;	/* ok */
  720: 		}
  721: 	}
  722: #ifdef HAVE_LIBPTHREAD
  723: 	pthread_mutex_unlock(&root->root_mtx[taskSUSPEND]);
  724: #endif
  725: 
  726: 	return flg;
  727: }

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