File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.11.2.3: download - view: text, annotated - select for diffs - revision graph
Thu Aug 2 13:45:02 2012 UTC (11 years, 11 months ago) by misho
Branches: sched2_6
Diff to: branchpoint 1.11: preferred, unified
redesign LIO tasks. Create separate scheduler queue for them

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

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