File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.11.2.2: download - view: text, annotated - select for diffs - revision graph
Wed Aug 1 12:49:26 2012 UTC (11 years, 11 months ago) by misho
Branches: sched2_6
Diff to: branchpoint 1.11: preferred, unified
initial AIO kevent scheduler support

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

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