File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.22: download - view: text, annotated - select for diffs - revision graph
Thu Nov 14 21:37:27 2013 UTC (10 years, 7 months ago) by misho
Branches: MAIN
CVS tags: sched4_6, SCHED4_5, HEAD
version 4.5

    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.22 2013/11/14 21:37:27 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, 2013
   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: int
   60: sched_GetErrno()
   61: {
   62: 	return sched_Errno;
   63: }
   64: 
   65: // sched_GetError() Get error text of last operation
   66: const char *
   67: sched_GetError()
   68: {
   69: 	return sched_Error;
   70: }
   71: 
   72: // sched_SetErr() Set error to variables for internal use!!!
   73: 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: #if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME)
  106: 	root->root_hooks.hook_add.rtc = sched_hook_rtc;
  107: #endif
  108: 	root->root_hooks.hook_add.node = sched_hook_node;
  109: 	root->root_hooks.hook_add.proc = sched_hook_proc;
  110: 	root->root_hooks.hook_add.signal = sched_hook_signal;
  111: #ifdef EVFILT_USER
  112: 	root->root_hooks.hook_add.user = sched_hook_user;
  113: #endif
  114: #ifdef HAVE_LIBPTHREAD
  115: 	root->root_hooks.hook_add.thread = sched_hook_thread;
  116: #endif
  117: 
  118: 	root->root_hooks.hook_exec.cancel = sched_hook_cancel;
  119: 	root->root_hooks.hook_exec.fetch = sched_hook_fetch;
  120: 	root->root_hooks.hook_exec.exception = sched_hook_exception;
  121: 
  122: 	root->root_hooks.hook_root.init = sched_hook_init;
  123: 	root->root_hooks.hook_root.fini = sched_hook_fini;
  124: 	return 0;
  125: }
  126: 
  127: /*
  128:  * schedInit() - Init scheduler
  129:  *
  130:  * @data = optional data if !=NULL
  131:  * @datlen = data len if data is set
  132:  * return: allocated root task if ok or NULL error
  133:  */
  134: sched_root_task_t *
  135: schedInit(void ** __restrict data, size_t datlen)
  136: {
  137: 	sched_root_task_t *root = NULL;
  138: 	int (*func)(sched_root_task_t *);
  139: #ifdef HAVE_LIBPTHREAD
  140: 	register int i;
  141: #endif
  142: 
  143: 	root = malloc(sizeof(sched_root_task_t));
  144: 	if (!root) {
  145: 		LOGERR;
  146: 	} else {
  147: 		memset(root, 0, sizeof(sched_root_task_t));
  148: 
  149: 		/* set default maximum regular task hit misses */
  150: 		root->root_miss = MAX_TASK_MISS;
  151: 
  152: 		/* INFINIT polling period by default */
  153: 		sched_timespecinf(&root->root_poll);
  154: 
  155: #ifdef HAVE_LIBPTHREAD
  156: 		for (i = 0; i < taskMAX; i++)
  157: 			if ((errno = pthread_mutex_init(&root->root_mtx[i], NULL))) {
  158: 				LOGERR;
  159: 				while (i)
  160: 					pthread_mutex_destroy(&root->root_mtx[--i]);
  161: 				free(root);
  162: 				return NULL;
  163: 			}
  164: 
  165: 		for (i = 0; i < taskMAX; i++)
  166: 			pthread_mutex_lock(&root->root_mtx[i]);
  167: #endif
  168: 
  169: 		TAILQ_INIT(&root->root_read);
  170: 		TAILQ_INIT(&root->root_write);
  171: 		TAILQ_INIT(&root->root_timer);
  172: 		TAILQ_INIT(&root->root_alarm);
  173: 		TAILQ_INIT(&root->root_rtc);
  174: 		TAILQ_INIT(&root->root_node);
  175: 		TAILQ_INIT(&root->root_proc);
  176: 		TAILQ_INIT(&root->root_signal);
  177: 		TAILQ_INIT(&root->root_aio);
  178: 		TAILQ_INIT(&root->root_lio);
  179: 		TAILQ_INIT(&root->root_user);
  180: 		TAILQ_INIT(&root->root_event);
  181: 		TAILQ_INIT(&root->root_task);
  182: 		TAILQ_INIT(&root->root_suspend);
  183: 		TAILQ_INIT(&root->root_ready);
  184: 		TAILQ_INIT(&root->root_unuse);
  185: 		TAILQ_INIT(&root->root_thread);
  186: 
  187: #ifdef HAVE_LIBPTHREAD
  188: 		for (i = 0; i < taskMAX; i++)
  189: 			pthread_mutex_unlock(&root->root_mtx[i]);
  190: #endif
  191: 
  192: 		if (data && *data) {
  193: 			if (datlen) {
  194: 				root->root_data.iov_base = *data;
  195: 				root->root_data.iov_len = datlen;
  196: 			} else { /* if datlen == 0, switch to callbacks init mode */
  197: 				 /* little hack :) for correct initialization of scheduler */
  198: 				func = (int(*)(sched_root_task_t*)) data;
  199: 				func(root);
  200: 			}
  201: 		}
  202: 
  203: 		if (root->root_hooks.hook_root.init)
  204: 			root->root_hooks.hook_root.init(root, NULL);
  205: 	}
  206: 
  207: 	return root;
  208: }
  209: 
  210: /*
  211:  * schedEnd() - End scheduler & free all resources
  212:  *
  213:  * @root = root task
  214:  * return: -1 error or 0 ok
  215:  */
  216: int
  217: schedEnd(sched_root_task_t ** __restrict root)
  218: {
  219: 	sched_task_t *task, *tmp;
  220: #ifdef HAVE_LIBPTHREAD
  221: 	register int i;
  222: #endif
  223: 
  224: 	if (!root || !*root)
  225: 		return -1;
  226: 
  227: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_read, task_node, tmp)
  228: 		schedCancel(task);
  229: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_write, task_node, tmp)
  230: 		schedCancel(task);
  231: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_timer, task_node, tmp)
  232: 		schedCancel(task);
  233: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_alarm, task_node, tmp)
  234: 		schedCancel(task);
  235: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_rtc, task_node, tmp)
  236: 		schedCancel(task);
  237: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_node, task_node, tmp)
  238: 		schedCancel(task);
  239: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_proc, task_node, tmp)
  240: 		schedCancel(task);
  241: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_signal, task_node, tmp)
  242: 		schedCancel(task);
  243: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_aio, task_node, tmp)
  244: 		schedCancel(task);
  245: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_lio, task_node, tmp)
  246: 		schedCancel(task);
  247: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_user, task_node, tmp)
  248: 		schedCancel(task);
  249: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_event, task_node, tmp)
  250: 		schedCancel(task);
  251: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_suspend, task_node, tmp)
  252: 		schedCancel(task);
  253: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_ready, task_node, tmp)
  254: 		schedCancel(task);
  255: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_thread, task_node, tmp)
  256: 		schedCancel(task);
  257: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_task, task_node, tmp)
  258: 		schedCancel(task);
  259: 
  260: #ifdef HAVE_LIBPTHREAD
  261: 	pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
  262: #endif
  263: 	TAILQ_FOREACH_SAFE(task, &(*root)->root_unuse, task_node, tmp) {
  264: 		TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
  265: 		free(task);
  266: 	}
  267: #ifdef HAVE_LIBPTHREAD
  268: 	pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
  269: #endif
  270: 
  271: 	if ((*root)->root_hooks.hook_root.fini)
  272: 		(*root)->root_hooks.hook_root.fini(*root, NULL);
  273: 
  274: #ifdef HAVE_LIBPTHREAD
  275: 	for (i = 0; i < taskMAX; i++)
  276: 		pthread_mutex_destroy(&(*root)->root_mtx[i]);
  277: #endif
  278: 
  279: 	free(*root);
  280: 	*root = NULL;
  281: 	return 0;
  282: }
  283: 
  284: /*
  285:  * schedCall() - Call task execution function
  286:  *
  287:  * @task = current task
  288:  * return: !=NULL error or =NULL ok
  289:  */
  290: void *
  291: schedCall(sched_task_t * __restrict task)
  292: {
  293: 	void *ptr = (void*) -1;
  294: 
  295: 	if (!task)
  296: 		return ptr;
  297: 
  298: 	if (!TASK_ISLOCKED(task))
  299: 		TASK_LOCK(task);
  300: 
  301: 	ptr = task->task_func(task);
  302: 
  303: 	TASK_UNLOCK(task);
  304: 	return ptr;
  305: }
  306: 
  307: /*
  308:  * schedFetch() - Fetch ready task
  309:  *
  310:  * @root = root task
  311:  * return: =NULL error or !=NULL ready task
  312:  */
  313: void *
  314: schedFetch(sched_root_task_t * __restrict root)
  315: {
  316: 	void *ptr;
  317: 
  318: 	if (!root)
  319: 		return NULL;
  320: 
  321: 	if (root->root_hooks.hook_exec.fetch)
  322: 		ptr = root->root_hooks.hook_exec.fetch(root, NULL);
  323: 	else
  324: 		ptr = NULL;
  325: 
  326: 	return ptr;
  327: }
  328: 
  329: /*
  330:  * schedTrigger() - Triggering USER task
  331:  *
  332:  * @task = task
  333:  * return: -1 error or 0 ok
  334:  */
  335: int
  336: schedTrigger(sched_task_t * __restrict task)
  337: {
  338: #ifndef EVFILT_USER
  339: 	sched_SetErr(ENOTSUP, "Not supported kevent() filter");
  340: 	return -1;
  341: #else
  342: 	struct kevent chg[1];
  343: 	struct timespec timeout = { 0, 0 };
  344: 
  345: 	if (!task || !TASK_ROOT(task))
  346: 		return -1;
  347: 
  348: #ifdef __NetBSD__
  349: 	EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (intptr_t) TASK_VAL(task));
  350: #else
  351: 	EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (void*) TASK_VAL(task));
  352: #endif
  353: 	if (kevent(TASK_ROOT(task)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  354: 		LOGERR;
  355: 		return -1;
  356: 	}
  357: 
  358: 	return 0;
  359: #endif
  360: }
  361: 
  362: /*
  363:  * schedQuery() - Query task in scheduler
  364:  *
  365:  * @task = task
  366:  * return: -1 error, 0 found  and 1 not found
  367:  */
  368: int
  369: schedQuery(sched_task_t * __restrict task)
  370: {
  371: 	sched_queue_t *queue;
  372: 	sched_task_t *t;
  373: 
  374: 	if (!task || !TASK_ROOT(task))
  375: 		return -1;	/* error */
  376: 
  377: 	switch (TASK_TYPE(task)) {
  378: 		case taskREAD:
  379: 			queue = &TASK_ROOT(task)->root_read;
  380: 			break;
  381: 		case taskWRITE:
  382: 			queue = &TASK_ROOT(task)->root_write;
  383: 			break;
  384: 		case taskTIMER:
  385: 			queue = &TASK_ROOT(task)->root_timer;
  386: 			break;
  387: 		case taskALARM:
  388: 			queue = &TASK_ROOT(task)->root_alarm;
  389: 			break;
  390: 		case taskRTC:
  391: 			queue = &TASK_ROOT(task)->root_rtc;
  392: 			break;
  393: 		case taskNODE:
  394: 			queue = &TASK_ROOT(task)->root_node;
  395: 			break;
  396: 		case taskPROC:
  397: 			queue = &TASK_ROOT(task)->root_proc;
  398: 			break;
  399: 		case taskSIGNAL:
  400: 			queue = &TASK_ROOT(task)->root_signal;
  401: 			break;
  402: 		case taskAIO:
  403: 			queue = &TASK_ROOT(task)->root_aio;
  404: 			break;
  405: 		case taskLIO:
  406: 			queue = &TASK_ROOT(task)->root_lio;
  407: 			break;
  408: 		case taskUSER:
  409: 			queue = &TASK_ROOT(task)->root_user;
  410: 			break;
  411: 		case taskEVENT:
  412: 			queue = &TASK_ROOT(task)->root_event;
  413: 			break;
  414: 		case taskTASK:
  415: 			queue = &TASK_ROOT(task)->root_task;
  416: 			break;
  417: 		case taskSUSPEND:
  418: 			queue = &TASK_ROOT(task)->root_suspend;
  419: 			break;
  420: 		case taskREADY:
  421: 			queue = &TASK_ROOT(task)->root_ready;
  422: 			break;
  423: 		case taskTHREAD:
  424: 			queue = &TASK_ROOT(task)->root_thread;
  425: 			break;
  426: 		default:
  427: 			return 1;	/* not in queue */
  428: 	}
  429: 	if (queue)
  430: 		TAILQ_FOREACH(t, queue, task_node)
  431: 			if (TASK_ID(t) == TASK_ID(task))
  432: 				return 0;	/* found */
  433: 
  434: 	return 1;	/* not in queue */
  435: }
  436: 
  437: /*
  438:  * schedQueryby() - Query task in scheduler by criteria
  439:  *
  440:  * @root = root task
  441:  * @type = query from queue type, if =taskMAX query same task from all queues
  442:  * @criteria = find task by criteria 
  443:  * 	[ CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|
  444:  * 		CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA ]
  445:  * @param = search parameter
  446:  * return: -1 error, 0 found or 1 not found
  447:  */
  448: int
  449: schedQueryby(sched_root_task_t * __restrict root, sched_task_type_t type, 
  450: 		u_char criteria, void *param)
  451: {
  452: 	sched_task_t *task;
  453: 	sched_queue_t *queue;
  454: 	register int flg = 0;
  455: 
  456: 	if (!root)
  457: 		return -1;
  458: 	/* if type == taskMAX check in all queues */
  459: 	if (type == taskMAX) {
  460: 		if ((flg = schedQueryby(root, taskREAD, criteria, param)) < 1)
  461: 			return flg;
  462: 		if ((flg = schedQueryby(root, taskWRITE, criteria, param)) < 1)
  463: 			return flg;
  464: 		if ((flg = schedQueryby(root, taskTIMER, criteria, param)) < 1)
  465: 			return flg;
  466: 		if ((flg = schedQueryby(root, taskALARM, criteria, param)) < 1)
  467: 			return flg;
  468: 		if ((flg = schedQueryby(root, taskRTC, criteria, param)) < 1)
  469: 			return flg;
  470: 		if ((flg = schedQueryby(root, taskNODE, criteria, param)) < 1)
  471: 			return flg;
  472: 		if ((flg = schedQueryby(root, taskPROC, criteria, param)) < 1)
  473: 			return flg;
  474: 		if ((flg = schedQueryby(root, taskSIGNAL, criteria, param)) < 1)
  475: 			return flg;
  476: 		if ((flg = schedQueryby(root, taskAIO, criteria, param)) < 1)
  477: 			return flg;
  478: 		if ((flg = schedQueryby(root, taskLIO, criteria, param)) < 1)
  479: 			return flg;
  480: 		if ((flg = schedQueryby(root, taskUSER, criteria, param)) < 1)
  481: 			return flg;
  482: 		if ((flg = schedQueryby(root, taskEVENT, criteria, param)) < 1)
  483: 			return flg;
  484: 		if ((flg = schedQueryby(root, taskTASK, criteria, param)) < 1)
  485: 			return flg;
  486: 		if ((flg = schedQueryby(root, taskSUSPEND, criteria, param)) < 1)
  487: 			return flg;
  488: 		if ((flg = schedQueryby(root, taskREADY, criteria, param)) < 1)
  489: 			return flg;
  490: 		if ((flg = schedQueryby(root, taskTHREAD, criteria, param)) < 1)
  491: 			return flg;
  492: 		return 1;	/* not found */
  493: 	}
  494: 	/* choosen queue */
  495: 	switch (type) {
  496: 		case taskREAD:
  497: 			queue = &root->root_read;
  498: 			break;
  499: 		case taskWRITE:
  500: 			queue = &root->root_write;
  501: 			break;
  502: 		case taskTIMER:
  503: 			queue = &root->root_timer;
  504: 			break;
  505: 		case taskALARM:
  506: 			queue = &root->root_alarm;
  507: 			break;
  508: 		case taskRTC:
  509: 			queue = &root->root_rtc;
  510: 			break;
  511: 		case taskNODE:
  512: 			queue = &root->root_node;
  513: 			break;
  514: 		case taskPROC:
  515: 			queue = &root->root_proc;
  516: 			break;
  517: 		case taskSIGNAL:
  518: 			queue = &root->root_signal;
  519: 			break;
  520: 		case taskAIO:
  521: 			queue = &root->root_aio;
  522: 			break;
  523: 		case taskLIO:
  524: 			queue = &root->root_lio;
  525: 			break;
  526: 		case taskUSER:
  527: 			queue = &root->root_user;
  528: 			break;
  529: 		case taskEVENT:
  530: 			queue = &root->root_event;
  531: 			break;
  532: 		case taskTASK:
  533: 			queue = &root->root_task;
  534: 			break;
  535: 		case taskSUSPEND:
  536: 			queue = &root->root_suspend;
  537: 			break;
  538: 		case taskREADY:
  539: 			queue = &root->root_ready;
  540: 			break;
  541: 		case taskTHREAD:
  542: 			queue = &root->root_thread;
  543: 			break;
  544: 		default:
  545: 			return 1;	/* not found */
  546: 	}
  547: 
  548: 	TAILQ_FOREACH(task, queue, task_node) {
  549: 		switch (criteria) {
  550: 			case CRITERIA_ANY:
  551: 				return 0;		/* found */
  552: 			case CRITERIA_CALL:
  553: 				if (TASK_FUNC(task) == (sched_task_func_t) param)
  554: 					return 0;	/* found */
  555: 				break;
  556: 			case CRITERIA_ARG:
  557: 				if (TASK_ARG(task) == param)
  558: 					return 0;	/* found */
  559: 				break;
  560: 			case CRITERIA_FD:
  561: 				if (TASK_FD(task) == (intptr_t) param)
  562: 					return 0;	/* found */
  563: 				break;
  564: 			case CRITERIA_ID:
  565: 			case CRITERIA_VAL:
  566: 				if (TASK_VAL(task) == (u_long) param)
  567: 					return 0;	/* found */
  568: 				break;
  569: 			case CRITERIA_TS:
  570: 				if (!sched_timespeccmp(&TASK_TS(task), 
  571: 							(struct timespec*) param, -))
  572: 					return 0;	/* found */
  573: 				break;
  574: 			case CRITERIA_DATA:
  575: 				if (TASK_DATA(task) == param)
  576: 					return 0;	/* found */
  577: 				break;
  578: 			default:
  579: 				sched_SetErr(EINVAL, "Invalid parameter criteria %d", 
  580: 						criteria);
  581: 				return 1;		/* not found */
  582: 		}
  583: 	}
  584: 
  585: 	return 1;	/* not found */
  586: }
  587: 
  588: /*
  589:  * schedCancel() - Cancel task from scheduler
  590:  *
  591:  * @task = task
  592:  * return: -1 error or 0 ok
  593:  */
  594: int
  595: schedCancel(sched_task_t * __restrict task)
  596: {
  597: 	sched_queue_t *queue;
  598: 
  599: 	if (!task || !TASK_ROOT(task))
  600: 		return -1;
  601: 
  602: 	if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  603: 		if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
  604: 			return -1;
  605: 
  606: 	switch (TASK_TYPE(task)) {
  607: 		case taskREAD:
  608: 			queue = &TASK_ROOT(task)->root_read;
  609: 			break;
  610: 		case taskWRITE:
  611: 			queue = &TASK_ROOT(task)->root_write;
  612: 			break;
  613: 		case taskTIMER:
  614: 			queue = &TASK_ROOT(task)->root_timer;
  615: 			break;
  616: 		case taskALARM:
  617: 			queue = &TASK_ROOT(task)->root_alarm;
  618: 			break;
  619: 		case taskRTC:
  620: 			queue = &TASK_ROOT(task)->root_rtc;
  621: 			break;
  622: 		case taskNODE:
  623: 			queue = &TASK_ROOT(task)->root_node;
  624: 			break;
  625: 		case taskPROC:
  626: 			queue = &TASK_ROOT(task)->root_proc;
  627: 			break;
  628: 		case taskSIGNAL:
  629: 			queue = &TASK_ROOT(task)->root_signal;
  630: 			break;
  631: 		case taskAIO:
  632: 			queue = &TASK_ROOT(task)->root_aio;
  633: 			break;
  634: 		case taskLIO:
  635: 			queue = &TASK_ROOT(task)->root_lio;
  636: 			break;
  637: 		case taskUSER:
  638: 			queue = &TASK_ROOT(task)->root_user;
  639: 			break;
  640: 		case taskEVENT:
  641: 			queue = &TASK_ROOT(task)->root_event;
  642: 			break;
  643: 		case taskTASK:
  644: 			queue = &TASK_ROOT(task)->root_task;
  645: 			break;
  646: 		case taskSUSPEND:
  647: 			queue = &TASK_ROOT(task)->root_suspend;
  648: 			break;
  649: 		case taskREADY:
  650: 			queue = &TASK_ROOT(task)->root_ready;
  651: 			break;
  652: 		case taskTHREAD:
  653: 			queue = &TASK_ROOT(task)->root_thread;
  654: 			break;
  655: 		default:
  656: 			queue = NULL;
  657: 	}
  658: 	if (queue) {
  659: #ifdef HAVE_LIBPTHREAD
  660: 		pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  661: #endif
  662: 		TAILQ_REMOVE(queue, TASK_ID(task), task_node);
  663: #ifdef HAVE_LIBPTHREAD
  664: 		pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  665: #endif
  666: 	}
  667: 	if (TASK_TYPE(task) != taskUNUSE)
  668: 		sched_unuseTask(task);
  669: 
  670: 	return 0;
  671: }
  672: 
  673: /*
  674:  * schedCancelby() - Cancel task from scheduler by criteria
  675:  *
  676:  * @root = root task
  677:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
  678:  * @criteria = find task by criteria 
  679:  * 	[ CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|
  680:  * 		CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA ]
  681:  * @param = search parameter
  682:  * @hook = custom cleanup hook function, may be NULL
  683:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
  684:  */
  685: int
  686: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
  687: 		u_char criteria, void *param, sched_hook_func_t hook)
  688: {
  689: 	sched_task_t *task, *tmp;
  690: 	sched_queue_t *queue;
  691: 	register int flg = 0;
  692: 
  693: 	if (!root)
  694: 		return -1;
  695: 	/* if type == taskMAX check in all queues */
  696: 	if (type == taskMAX) {
  697: 		if (schedCancelby(root, taskREAD, criteria, param, hook))
  698: 			return -2;
  699: 		if (schedCancelby(root, taskWRITE, criteria, param, hook))
  700: 			return -2;
  701: 		if (schedCancelby(root, taskTIMER, criteria, param, hook))
  702: 			return -2;
  703: 		if (schedCancelby(root, taskALARM, criteria, param, hook))
  704: 			return -2;
  705: 		if (schedCancelby(root, taskRTC, criteria, param, hook))
  706: 			return -2;
  707: 		if (schedCancelby(root, taskNODE, criteria, param, hook))
  708: 			return -2;
  709: 		if (schedCancelby(root, taskPROC, criteria, param, hook))
  710: 			return -2;
  711: 		if (schedCancelby(root, taskSIGNAL, criteria, param, hook))
  712: 			return -2;
  713: 		if (schedCancelby(root, taskAIO, criteria, param, hook))
  714: 			return -2;
  715: 		if (schedCancelby(root, taskLIO, criteria, param, hook))
  716: 			return -2;
  717: 		if (schedCancelby(root, taskUSER, criteria, param, hook))
  718: 			return -2;
  719: 		if (schedCancelby(root, taskEVENT, criteria, param, hook))
  720: 			return -2;
  721: 		if (schedCancelby(root, taskTASK, criteria, param, hook))
  722: 			return -2;
  723: 		if (schedCancelby(root, taskSUSPEND, criteria, param, hook))
  724: 			return -2;
  725: 		if (schedCancelby(root, taskREADY, criteria, param, hook))
  726: 			return -2;
  727: 		if (schedCancelby(root, taskTHREAD, criteria, param, hook))
  728: 			return -2;
  729: 		return 0;
  730: 	}
  731: 	/* choosen queue */
  732: 	switch (type) {
  733: 		case taskREAD:
  734: 			queue = &root->root_read;
  735: 			break;
  736: 		case taskWRITE:
  737: 			queue = &root->root_write;
  738: 			break;
  739: 		case taskTIMER:
  740: 			queue = &root->root_timer;
  741: 			break;
  742: 		case taskALARM:
  743: 			queue = &root->root_alarm;
  744: 			break;
  745: 		case taskRTC:
  746: 			queue = &root->root_rtc;
  747: 			break;
  748: 		case taskNODE:
  749: 			queue = &root->root_node;
  750: 			break;
  751: 		case taskPROC:
  752: 			queue = &root->root_proc;
  753: 			break;
  754: 		case taskSIGNAL:
  755: 			queue = &root->root_signal;
  756: 			break;
  757: 		case taskAIO:
  758: 			queue = &root->root_aio;
  759: 			break;
  760: 		case taskLIO:
  761: 			queue = &root->root_lio;
  762: 			break;
  763: 		case taskUSER:
  764: 			queue = &root->root_user;
  765: 			break;
  766: 		case taskEVENT:
  767: 			queue = &root->root_event;
  768: 			break;
  769: 		case taskTASK:
  770: 			queue = &root->root_task;
  771: 			break;
  772: 		case taskSUSPEND:
  773: 			queue = &root->root_suspend;
  774: 			break;
  775: 		case taskREADY:
  776: 			queue = &root->root_ready;
  777: 			break;
  778: 		case taskTHREAD:
  779: 			queue = &root->root_thread;
  780: 			break;
  781: 		default:
  782: 			return 0;
  783: 	}
  784: 
  785: #ifdef HAVE_LIBPTHREAD
  786: 	pthread_mutex_lock(&root->root_mtx[type]);
  787: #endif
  788: 	TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
  789: 		flg ^= flg;
  790: 		switch (criteria) {
  791: 			case CRITERIA_ANY:
  792: 				flg = 1;
  793: 				break;
  794: 			case CRITERIA_CALL:
  795: 				if (TASK_FUNC(task) == (sched_task_func_t) param)
  796: 					flg = 1;
  797: 				break;
  798: 			case CRITERIA_ARG:
  799: 				if (TASK_ARG(task) == param)
  800: 					flg = 1;
  801: 				break;
  802: 			case CRITERIA_FD:
  803: 				if (TASK_FD(task) == (intptr_t) param)
  804: 					flg = 1;
  805: 				break;
  806: 			case CRITERIA_ID:
  807: 			case CRITERIA_VAL:
  808: 				if (TASK_VAL(task) == (u_long) param)
  809: 					flg = 1;
  810: 				break;
  811: 			case CRITERIA_TS:
  812: 				if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
  813: 					flg = 1;
  814: 				break;
  815: 			case CRITERIA_DATA:
  816: 				if (TASK_DATA(task) == param)
  817: 					flg = 1;
  818: 				break;
  819: 			default:
  820: 				sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
  821: 				flg = -1;
  822: 		}
  823: 		if (flg < 0)		/* error */
  824: 			break;
  825: 		/* cancel choosen task */
  826: 		if (flg > 0) {
  827: 			if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  828: 				if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
  829: 					flg = -1;
  830: 					break;
  831: 				}
  832: 			/* custom hook */
  833: 			if (hook)
  834: 				if (hook(task, NULL)) {
  835: 					flg = -3;
  836: 					break;
  837: 				}
  838: 
  839: 			TAILQ_REMOVE(queue, task, task_node);
  840: 			if (TASK_TYPE(task) != taskUNUSE)
  841: 				sched_unuseTask(task);
  842: 
  843: 			flg ^= flg;	/* ok */
  844: 		}
  845: 	}
  846: #ifdef HAVE_LIBPTHREAD
  847: 	pthread_mutex_unlock(&root->root_mtx[type]);
  848: #endif
  849: 	return flg;
  850: }
  851: 
  852: /*
  853:  * schedRun() - Scheduler *run loop*
  854:  *
  855:  * @root = root task
  856:  * @killState = kill condition variable, if !=0 stop scheduler loop
  857:  * return: -1 error or 0 ok
  858:  */
  859: int
  860: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
  861: {
  862: 	sched_task_t *task;
  863: 
  864: 	if (!root)
  865: 		return -1;
  866: 
  867: 	if (root->root_hooks.hook_exec.run)
  868: 		if (root->root_hooks.hook_exec.run(root, NULL))
  869: 			return -1;
  870: 
  871: 	if (killState) {
  872: 		if (root->root_hooks.hook_exec.condition)
  873: 			/* condition scheduler loop */
  874: 			while (root && root->root_hooks.hook_exec.fetch && 
  875: 					root->root_hooks.hook_exec.condition && 
  876: 					root->root_hooks.hook_exec.condition(root, (void*) killState)) {
  877: 				if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  878: 					root->root_ret = schedCall(task);
  879: 			}
  880: 		else
  881: 			/* trigger scheduler loop */
  882: 			while (!*killState && root && root->root_hooks.hook_exec.fetch) {
  883: 				if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  884: 					root->root_ret = schedCall(task);
  885: 			}
  886: 	} else
  887: 		/* infinite scheduler loop */
  888: 		while (root && root->root_hooks.hook_exec.fetch)
  889: 			if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  890: 				root->root_ret = schedCall(task);
  891: 
  892: 	return 0;
  893: }
  894: 
  895: /*
  896:  * schedPolling() - Polling timeout period if no timer task is present
  897:  *
  898:  * @root = root task
  899:  * @ts = timeout polling period, if ==NULL INFINIT timeout
  900:  * @tsold = old timeout polling if !=NULL
  901:  * return: -1 error or 0 ok
  902:  */
  903: int
  904: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts, 
  905: 		struct timespec * __restrict tsold)
  906: {
  907: 	if (!root)
  908: 		return -1;
  909: 
  910: 	if (tsold)
  911: 		*tsold = root->root_poll;
  912: 
  913: 	if (!ts)
  914: 		sched_timespecinf(&root->root_poll);
  915: 	else
  916: 		root->root_poll = *ts;
  917: 
  918: 	return 0;
  919: }
  920: 
  921: /*
  922:  * schedTermCondition() - Activate hook for scheduler condition kill
  923:  *
  924:  * @root = root task
  925:  * @condValue = condition value, kill schedRun() if condValue == killState
  926:  * return: -1 error or 0 ok
  927:  */
  928: int
  929: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
  930: {
  931: 	if (!root)
  932: 		return -1;
  933: 
  934: 	root->root_cond = condValue;
  935: 	root->root_hooks.hook_exec.condition = sched_hook_condition;
  936: 	return 0;
  937: }
  938: 
  939: /*
  940:  * schedResumeby() - Resume suspended task
  941:  *
  942:  * @root = root task
  943:  * @criteria = find task by criteria 
  944:  * 	[CRITERIA_ANY|CRITERIA_ID|CRITERIA_DATA]
  945:  * @param = search parameter (sched_task_t *task| unsigned long id)
  946:  * return: -1 error or 0 resumed ok
  947:  */
  948: int
  949: schedResumeby(sched_root_task_t * __restrict root, u_char criteria, void *param)
  950: {
  951: 	sched_task_t *task, *tmp;
  952: 	register int flg = 0;
  953: 
  954: 	if (!root)
  955: 		return -1;
  956: 
  957: #ifdef HAVE_LIBPTHREAD
  958: 	pthread_mutex_lock(&root->root_mtx[taskSUSPEND]);
  959: #endif
  960: 	TAILQ_FOREACH_SAFE(task, &root->root_suspend, task_node, tmp) {
  961: 		flg ^= flg;
  962: 		switch (criteria) {
  963: 			case CRITERIA_ANY:
  964: 				flg = 1;
  965: 				break;
  966: 			case CRITERIA_ID:
  967: 				if (TASK_VAL(task) == (u_long) param)
  968: 					flg = 1;
  969: 				break;
  970: 			case CRITERIA_DATA:
  971: 				if (TASK_ID(task) == (sched_task_t*) param)
  972: 					flg = 1;
  973: 				break;
  974: 			default:
  975: 				sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
  976: 				flg = -1;
  977: 		}
  978: 		if (flg < 0)
  979: 			break;
  980: 		/* resume choosen task */
  981: 		if (flg > 0) {
  982: 			if (root->root_hooks.hook_exec.resume)
  983: 				if (root->root_hooks.hook_exec.resume(task, NULL)) {
  984: 					flg = -1;
  985: 					break;
  986: 				}
  987: 
  988: 			TAILQ_REMOVE(&root->root_suspend, task, task_node);
  989: 
  990: 			task->task_type = taskREADY;
  991: #ifdef HAVE_LIBPTHREAD
  992: 			pthread_mutex_lock(&root->root_mtx[taskREADY]);
  993: #endif
  994: 			TAILQ_INSERT_TAIL(&root->root_ready, task, task_node);
  995: #ifdef HAVE_LIBPTHREAD
  996: 			pthread_mutex_unlock(&root->root_mtx[taskREADY]);
  997: #endif
  998: 
  999: 			flg ^= flg;	/* ok */
 1000: 		}
 1001: 	}
 1002: #ifdef HAVE_LIBPTHREAD
 1003: 	pthread_mutex_unlock(&root->root_mtx[taskSUSPEND]);
 1004: #endif
 1005: 
 1006: 	return flg;
 1007: }

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