File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.16.2.1: download - view: text, annotated - select for diffs - revision graph
Fri Aug 31 13:00:41 2012 UTC (11 years, 9 months ago) by misho
Branches: sched3_4
Diff to: branchpoint 1.16: preferred, unified
added properly return code

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

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