File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.24: download - view: text, annotated - select for diffs - revision graph
Tue Jan 28 13:17:33 2014 UTC (10 years, 9 months ago) by misho
Branches: MAIN
CVS tags: sched5_1, sched5_0, SCHED5_0, SCHED4_7, HEAD
version 4.7

    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.24 2014/01/28 13:17:33 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 - 2014
   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: #ifdef KQ_DISABLE
  339: 	sched_SetErr(ENOTSUP, "disabled kqueue support");
  340: 	return -1;
  341: #else
  342: #ifndef EVFILT_USER
  343: 	sched_SetErr(ENOTSUP, "Not supported kevent() filter");
  344: 	return -1;
  345: #else
  346: 	struct kevent chg[1];
  347: 	struct timespec timeout = { 0, 0 };
  348: 
  349: 	if (!task || !TASK_ROOT(task))
  350: 		return -1;
  351: 
  352: #ifdef __NetBSD__
  353: 	EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (intptr_t) TASK_VAL(task));
  354: #else
  355: 	EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (void*) TASK_VAL(task));
  356: #endif
  357: 	if (kevent(TASK_ROOT(task)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
  358: 		LOGERR;
  359: 		return -1;
  360: 	}
  361: 
  362: 	return 0;
  363: #endif
  364: #endif	/* KQ_DISABLE */
  365: }
  366: 
  367: /*
  368:  * schedQuery() - Query task in scheduler
  369:  *
  370:  * @task = task
  371:  * return: -1 error, 0 found  and 1 not found
  372:  */
  373: int
  374: schedQuery(sched_task_t * __restrict task)
  375: {
  376: 	sched_queue_t *queue;
  377: 	sched_task_t *t;
  378: 
  379: 	if (!task || !TASK_ROOT(task))
  380: 		return -1;	/* error */
  381: 
  382: 	switch (TASK_TYPE(task)) {
  383: 		case taskREAD:
  384: 			queue = &TASK_ROOT(task)->root_read;
  385: 			break;
  386: 		case taskWRITE:
  387: 			queue = &TASK_ROOT(task)->root_write;
  388: 			break;
  389: 		case taskTIMER:
  390: 			queue = &TASK_ROOT(task)->root_timer;
  391: 			break;
  392: 		case taskALARM:
  393: 			queue = &TASK_ROOT(task)->root_alarm;
  394: 			break;
  395: 		case taskRTC:
  396: 			queue = &TASK_ROOT(task)->root_rtc;
  397: 			break;
  398: 		case taskNODE:
  399: 			queue = &TASK_ROOT(task)->root_node;
  400: 			break;
  401: 		case taskPROC:
  402: 			queue = &TASK_ROOT(task)->root_proc;
  403: 			break;
  404: 		case taskSIGNAL:
  405: 			queue = &TASK_ROOT(task)->root_signal;
  406: 			break;
  407: 		case taskAIO:
  408: 			queue = &TASK_ROOT(task)->root_aio;
  409: 			break;
  410: 		case taskLIO:
  411: 			queue = &TASK_ROOT(task)->root_lio;
  412: 			break;
  413: 		case taskUSER:
  414: 			queue = &TASK_ROOT(task)->root_user;
  415: 			break;
  416: 		case taskEVENT:
  417: 			queue = &TASK_ROOT(task)->root_event;
  418: 			break;
  419: 		case taskTASK:
  420: 			queue = &TASK_ROOT(task)->root_task;
  421: 			break;
  422: 		case taskSUSPEND:
  423: 			queue = &TASK_ROOT(task)->root_suspend;
  424: 			break;
  425: 		case taskREADY:
  426: 			queue = &TASK_ROOT(task)->root_ready;
  427: 			break;
  428: 		case taskTHREAD:
  429: 			queue = &TASK_ROOT(task)->root_thread;
  430: 			break;
  431: 		default:
  432: 			return 1;	/* not in queue */
  433: 	}
  434: 	if (queue)
  435: 		TAILQ_FOREACH(t, queue, task_node)
  436: 			if (TASK_ID(t) == TASK_ID(task))
  437: 				return 0;	/* found */
  438: 
  439: 	return 1;	/* not in queue */
  440: }
  441: 
  442: /*
  443:  * schedQueryby() - Query task in scheduler by criteria
  444:  *
  445:  * @root = root task
  446:  * @type = query from queue type, if =taskMAX query same task from all queues
  447:  * @criteria = find task by criteria 
  448:  * 	[ CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|
  449:  * 		CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA|CRITERIA_DATLEN ]
  450:  * @param = search parameter
  451:  * return: -1 error, 0 found or 1 not found
  452:  */
  453: int
  454: schedQueryby(sched_root_task_t * __restrict root, sched_task_type_t type, 
  455: 		u_char criteria, void *param)
  456: {
  457: 	sched_task_t *task;
  458: 	sched_queue_t *queue;
  459: 	register int flg = 0;
  460: 
  461: 	if (!root)
  462: 		return -1;
  463: 	/* if type == taskMAX check in all queues */
  464: 	if (type == taskMAX) {
  465: 		if ((flg = schedQueryby(root, taskREAD, criteria, param)) < 1)
  466: 			return flg;
  467: 		if ((flg = schedQueryby(root, taskWRITE, criteria, param)) < 1)
  468: 			return flg;
  469: 		if ((flg = schedQueryby(root, taskTIMER, criteria, param)) < 1)
  470: 			return flg;
  471: 		if ((flg = schedQueryby(root, taskALARM, criteria, param)) < 1)
  472: 			return flg;
  473: 		if ((flg = schedQueryby(root, taskRTC, criteria, param)) < 1)
  474: 			return flg;
  475: 		if ((flg = schedQueryby(root, taskNODE, criteria, param)) < 1)
  476: 			return flg;
  477: 		if ((flg = schedQueryby(root, taskPROC, criteria, param)) < 1)
  478: 			return flg;
  479: 		if ((flg = schedQueryby(root, taskSIGNAL, criteria, param)) < 1)
  480: 			return flg;
  481: 		if ((flg = schedQueryby(root, taskAIO, criteria, param)) < 1)
  482: 			return flg;
  483: 		if ((flg = schedQueryby(root, taskLIO, criteria, param)) < 1)
  484: 			return flg;
  485: 		if ((flg = schedQueryby(root, taskUSER, criteria, param)) < 1)
  486: 			return flg;
  487: 		if ((flg = schedQueryby(root, taskEVENT, criteria, param)) < 1)
  488: 			return flg;
  489: 		if ((flg = schedQueryby(root, taskTASK, criteria, param)) < 1)
  490: 			return flg;
  491: 		if ((flg = schedQueryby(root, taskSUSPEND, criteria, param)) < 1)
  492: 			return flg;
  493: 		if ((flg = schedQueryby(root, taskREADY, criteria, param)) < 1)
  494: 			return flg;
  495: 		if ((flg = schedQueryby(root, taskTHREAD, criteria, param)) < 1)
  496: 			return flg;
  497: 		return 1;	/* not found */
  498: 	}
  499: 	/* choosen queue */
  500: 	switch (type) {
  501: 		case taskREAD:
  502: 			queue = &root->root_read;
  503: 			break;
  504: 		case taskWRITE:
  505: 			queue = &root->root_write;
  506: 			break;
  507: 		case taskTIMER:
  508: 			queue = &root->root_timer;
  509: 			break;
  510: 		case taskALARM:
  511: 			queue = &root->root_alarm;
  512: 			break;
  513: 		case taskRTC:
  514: 			queue = &root->root_rtc;
  515: 			break;
  516: 		case taskNODE:
  517: 			queue = &root->root_node;
  518: 			break;
  519: 		case taskPROC:
  520: 			queue = &root->root_proc;
  521: 			break;
  522: 		case taskSIGNAL:
  523: 			queue = &root->root_signal;
  524: 			break;
  525: 		case taskAIO:
  526: 			queue = &root->root_aio;
  527: 			break;
  528: 		case taskLIO:
  529: 			queue = &root->root_lio;
  530: 			break;
  531: 		case taskUSER:
  532: 			queue = &root->root_user;
  533: 			break;
  534: 		case taskEVENT:
  535: 			queue = &root->root_event;
  536: 			break;
  537: 		case taskTASK:
  538: 			queue = &root->root_task;
  539: 			break;
  540: 		case taskSUSPEND:
  541: 			queue = &root->root_suspend;
  542: 			break;
  543: 		case taskREADY:
  544: 			queue = &root->root_ready;
  545: 			break;
  546: 		case taskTHREAD:
  547: 			queue = &root->root_thread;
  548: 			break;
  549: 		default:
  550: 			return 1;	/* not found */
  551: 	}
  552: 
  553: 	TAILQ_FOREACH(task, queue, task_node) {
  554: 		switch (criteria) {
  555: 			case CRITERIA_ANY:
  556: 				return 0;		/* found */
  557: 			case CRITERIA_CALL:
  558: 				if (TASK_FUNC(task) == (sched_task_func_t) param)
  559: 					return 0;	/* found */
  560: 				break;
  561: 			case CRITERIA_ARG:
  562: 				if (TASK_ARG(task) == param)
  563: 					return 0;	/* found */
  564: 				break;
  565: 			case CRITERIA_FD:
  566: 				if (TASK_FD(task) == (intptr_t) param)
  567: 					return 0;	/* found */
  568: 				break;
  569: 			case CRITERIA_ID:
  570: 			case CRITERIA_VAL:
  571: 				if (TASK_VAL(task) == (u_long) param)
  572: 					return 0;	/* found */
  573: 				break;
  574: 			case CRITERIA_TS:
  575: 				if (!sched_timespeccmp(&TASK_TS(task), 
  576: 							(struct timespec*) param, -))
  577: 					return 0;	/* found */
  578: 				break;
  579: 			case CRITERIA_DATA:
  580: 				if (TASK_DATA(task) == param)
  581: 					return 0;	/* found */
  582: 				break;
  583: 			case CRITERIA_DATLEN:
  584: 				if (TASK_DATLEN(task) == (size_t) param)
  585: 					return 0;	/* found */
  586: 				break;
  587: 			default:
  588: 				sched_SetErr(EINVAL, "Invalid parameter criteria %d", 
  589: 						criteria);
  590: 				return 1;		/* not found */
  591: 		}
  592: 	}
  593: 
  594: 	return 1;	/* not found */
  595: }
  596: 
  597: /*
  598:  * schedCancel() - Cancel task from scheduler
  599:  *
  600:  * @task = task
  601:  * return: -1 error or 0 ok
  602:  */
  603: int
  604: schedCancel(sched_task_t * __restrict task)
  605: {
  606: 	sched_queue_t *queue;
  607: 
  608: 	if (!task || !TASK_ROOT(task))
  609: 		return -1;
  610: 
  611: 	if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  612: 		if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
  613: 			return -1;
  614: 
  615: 	switch (TASK_TYPE(task)) {
  616: 		case taskREAD:
  617: 			queue = &TASK_ROOT(task)->root_read;
  618: 			break;
  619: 		case taskWRITE:
  620: 			queue = &TASK_ROOT(task)->root_write;
  621: 			break;
  622: 		case taskTIMER:
  623: 			queue = &TASK_ROOT(task)->root_timer;
  624: 			break;
  625: 		case taskALARM:
  626: 			queue = &TASK_ROOT(task)->root_alarm;
  627: 			break;
  628: 		case taskRTC:
  629: 			queue = &TASK_ROOT(task)->root_rtc;
  630: 			break;
  631: 		case taskNODE:
  632: 			queue = &TASK_ROOT(task)->root_node;
  633: 			break;
  634: 		case taskPROC:
  635: 			queue = &TASK_ROOT(task)->root_proc;
  636: 			break;
  637: 		case taskSIGNAL:
  638: 			queue = &TASK_ROOT(task)->root_signal;
  639: 			break;
  640: 		case taskAIO:
  641: 			queue = &TASK_ROOT(task)->root_aio;
  642: 			break;
  643: 		case taskLIO:
  644: 			queue = &TASK_ROOT(task)->root_lio;
  645: 			break;
  646: 		case taskUSER:
  647: 			queue = &TASK_ROOT(task)->root_user;
  648: 			break;
  649: 		case taskEVENT:
  650: 			queue = &TASK_ROOT(task)->root_event;
  651: 			break;
  652: 		case taskTASK:
  653: 			queue = &TASK_ROOT(task)->root_task;
  654: 			break;
  655: 		case taskSUSPEND:
  656: 			queue = &TASK_ROOT(task)->root_suspend;
  657: 			break;
  658: 		case taskREADY:
  659: 			queue = &TASK_ROOT(task)->root_ready;
  660: 			break;
  661: 		case taskTHREAD:
  662: 			queue = &TASK_ROOT(task)->root_thread;
  663: 			break;
  664: 		default:
  665: 			queue = NULL;
  666: 	}
  667: 	if (queue) {
  668: #ifdef HAVE_LIBPTHREAD
  669: 		pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  670: #endif
  671: 		TAILQ_REMOVE(queue, TASK_ID(task), task_node);
  672: #ifdef HAVE_LIBPTHREAD
  673: 		pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  674: #endif
  675: 	}
  676: 	if (TASK_TYPE(task) != taskUNUSE)
  677: 		sched_unuseTask(task);
  678: 
  679: 	return 0;
  680: }
  681: 
  682: /*
  683:  * schedCancelby() - Cancel task from scheduler by criteria
  684:  *
  685:  * @root = root task
  686:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
  687:  * @criteria = find task by criteria 
  688:  * 	[ CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|
  689:  * 		CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA|CRITERIA_DATLEN ]
  690:  * @param = search parameter
  691:  * @hook = custom cleanup hook function, may be NULL
  692:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
  693:  */
  694: int
  695: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
  696: 		u_char criteria, void *param, sched_hook_func_t hook)
  697: {
  698: 	sched_task_t *task, *tmp;
  699: 	sched_queue_t *queue;
  700: 	register int flg = 0;
  701: 
  702: 	if (!root)
  703: 		return -1;
  704: 	/* if type == taskMAX check in all queues */
  705: 	if (type == taskMAX) {
  706: 		if (schedCancelby(root, taskREAD, criteria, param, hook))
  707: 			return -2;
  708: 		if (schedCancelby(root, taskWRITE, criteria, param, hook))
  709: 			return -2;
  710: 		if (schedCancelby(root, taskTIMER, criteria, param, hook))
  711: 			return -2;
  712: 		if (schedCancelby(root, taskALARM, criteria, param, hook))
  713: 			return -2;
  714: 		if (schedCancelby(root, taskRTC, criteria, param, hook))
  715: 			return -2;
  716: 		if (schedCancelby(root, taskNODE, criteria, param, hook))
  717: 			return -2;
  718: 		if (schedCancelby(root, taskPROC, criteria, param, hook))
  719: 			return -2;
  720: 		if (schedCancelby(root, taskSIGNAL, criteria, param, hook))
  721: 			return -2;
  722: 		if (schedCancelby(root, taskAIO, criteria, param, hook))
  723: 			return -2;
  724: 		if (schedCancelby(root, taskLIO, criteria, param, hook))
  725: 			return -2;
  726: 		if (schedCancelby(root, taskUSER, criteria, param, hook))
  727: 			return -2;
  728: 		if (schedCancelby(root, taskEVENT, criteria, param, hook))
  729: 			return -2;
  730: 		if (schedCancelby(root, taskTASK, criteria, param, hook))
  731: 			return -2;
  732: 		if (schedCancelby(root, taskSUSPEND, criteria, param, hook))
  733: 			return -2;
  734: 		if (schedCancelby(root, taskREADY, criteria, param, hook))
  735: 			return -2;
  736: 		if (schedCancelby(root, taskTHREAD, criteria, param, hook))
  737: 			return -2;
  738: 		return 0;
  739: 	}
  740: 	/* choosen queue */
  741: 	switch (type) {
  742: 		case taskREAD:
  743: 			queue = &root->root_read;
  744: 			break;
  745: 		case taskWRITE:
  746: 			queue = &root->root_write;
  747: 			break;
  748: 		case taskTIMER:
  749: 			queue = &root->root_timer;
  750: 			break;
  751: 		case taskALARM:
  752: 			queue = &root->root_alarm;
  753: 			break;
  754: 		case taskRTC:
  755: 			queue = &root->root_rtc;
  756: 			break;
  757: 		case taskNODE:
  758: 			queue = &root->root_node;
  759: 			break;
  760: 		case taskPROC:
  761: 			queue = &root->root_proc;
  762: 			break;
  763: 		case taskSIGNAL:
  764: 			queue = &root->root_signal;
  765: 			break;
  766: 		case taskAIO:
  767: 			queue = &root->root_aio;
  768: 			break;
  769: 		case taskLIO:
  770: 			queue = &root->root_lio;
  771: 			break;
  772: 		case taskUSER:
  773: 			queue = &root->root_user;
  774: 			break;
  775: 		case taskEVENT:
  776: 			queue = &root->root_event;
  777: 			break;
  778: 		case taskTASK:
  779: 			queue = &root->root_task;
  780: 			break;
  781: 		case taskSUSPEND:
  782: 			queue = &root->root_suspend;
  783: 			break;
  784: 		case taskREADY:
  785: 			queue = &root->root_ready;
  786: 			break;
  787: 		case taskTHREAD:
  788: 			queue = &root->root_thread;
  789: 			break;
  790: 		default:
  791: 			return 0;
  792: 	}
  793: 
  794: #ifdef HAVE_LIBPTHREAD
  795: 	pthread_mutex_lock(&root->root_mtx[type]);
  796: #endif
  797: 	TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
  798: 		flg ^= flg;
  799: 		switch (criteria) {
  800: 			case CRITERIA_ANY:
  801: 				flg = 1;
  802: 				break;
  803: 			case CRITERIA_CALL:
  804: 				if (TASK_FUNC(task) == (sched_task_func_t) param)
  805: 					flg = 1;
  806: 				break;
  807: 			case CRITERIA_ARG:
  808: 				if (TASK_ARG(task) == param)
  809: 					flg = 1;
  810: 				break;
  811: 			case CRITERIA_FD:
  812: 				if (TASK_FD(task) == (intptr_t) param)
  813: 					flg = 1;
  814: 				break;
  815: 			case CRITERIA_ID:
  816: 			case CRITERIA_VAL:
  817: 				if (TASK_VAL(task) == (u_long) param)
  818: 					flg = 1;
  819: 				break;
  820: 			case CRITERIA_TS:
  821: 				if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
  822: 					flg = 1;
  823: 				break;
  824: 			case CRITERIA_DATA:
  825: 				if (TASK_DATA(task) == param)
  826: 					flg = 1;
  827: 				break;
  828: 			case CRITERIA_DATLEN:
  829: 				if (TASK_DATLEN(task) == (size_t) param)
  830: 					flg = 1;
  831: 				break;
  832: 			default:
  833: 				sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
  834: 				flg = -1;
  835: 		}
  836: 		if (flg < 0)		/* error */
  837: 			break;
  838: 		/* cancel choosen task */
  839: 		if (flg > 0) {
  840: 			if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  841: 				if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
  842: 					flg = -1;
  843: 					break;
  844: 				}
  845: 			/* custom hook */
  846: 			if (hook)
  847: 				if (hook(task, NULL)) {
  848: 					flg = -3;
  849: 					break;
  850: 				}
  851: 
  852: 			TAILQ_REMOVE(queue, task, task_node);
  853: 			if (TASK_TYPE(task) != taskUNUSE)
  854: 				sched_unuseTask(task);
  855: 
  856: 			flg ^= flg;	/* ok */
  857: 		}
  858: 	}
  859: #ifdef HAVE_LIBPTHREAD
  860: 	pthread_mutex_unlock(&root->root_mtx[type]);
  861: #endif
  862: 	return flg;
  863: }
  864: 
  865: /*
  866:  * schedRun() - Scheduler *run loop*
  867:  *
  868:  * @root = root task
  869:  * @killState = kill condition variable, if !=0 stop scheduler loop
  870:  * return: -1 error or 0 ok
  871:  */
  872: int
  873: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
  874: {
  875: 	sched_task_t *task;
  876: 
  877: 	if (!root)
  878: 		return -1;
  879: 
  880: 	if (root->root_hooks.hook_exec.run)
  881: 		if (root->root_hooks.hook_exec.run(root, NULL))
  882: 			return -1;
  883: 
  884: 	if (killState) {
  885: 		if (root->root_hooks.hook_exec.condition)
  886: 			/* condition scheduler loop */
  887: 			while (root && root->root_hooks.hook_exec.fetch && 
  888: 					root->root_hooks.hook_exec.condition && 
  889: 					root->root_hooks.hook_exec.condition(root, (void*) killState)) {
  890: 				if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  891: 					root->root_ret = schedCall(task);
  892: 			}
  893: 		else
  894: 			/* trigger scheduler loop */
  895: 			while (!*killState && root && root->root_hooks.hook_exec.fetch) {
  896: 				if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  897: 					root->root_ret = schedCall(task);
  898: 			}
  899: 	} else
  900: 		/* infinite scheduler loop */
  901: 		while (root && root->root_hooks.hook_exec.fetch)
  902: 			if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  903: 				root->root_ret = schedCall(task);
  904: 
  905: 	return 0;
  906: }
  907: 
  908: /*
  909:  * schedPolling() - Polling timeout period if no timer task is present
  910:  *
  911:  * @root = root task
  912:  * @ts = timeout polling period, if ==NULL INFINIT timeout
  913:  * @tsold = old timeout polling if !=NULL
  914:  * return: -1 error or 0 ok
  915:  */
  916: int
  917: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts, 
  918: 		struct timespec * __restrict tsold)
  919: {
  920: 	if (!root)
  921: 		return -1;
  922: 
  923: 	if (tsold)
  924: 		*tsold = root->root_poll;
  925: 
  926: 	if (!ts)
  927: 		sched_timespecinf(&root->root_poll);
  928: 	else
  929: 		root->root_poll = *ts;
  930: 
  931: 	return 0;
  932: }
  933: 
  934: /*
  935:  * schedTermCondition() - Activate hook for scheduler condition kill
  936:  *
  937:  * @root = root task
  938:  * @condValue = condition value, kill schedRun() if condValue == killState
  939:  * return: -1 error or 0 ok
  940:  */
  941: int
  942: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
  943: {
  944: 	if (!root)
  945: 		return -1;
  946: 
  947: 	root->root_cond = condValue;
  948: 	root->root_hooks.hook_exec.condition = sched_hook_condition;
  949: 	return 0;
  950: }
  951: 
  952: /*
  953:  * schedResumeby() - Resume suspended task
  954:  *
  955:  * @root = root task
  956:  * @criteria = find task by criteria 
  957:  * 	[CRITERIA_ANY|CRITERIA_ID|CRITERIA_VAL|CRITERIA_DATA]
  958:  * @param = search parameter (sched_task_t *task| unsigned long id)
  959:  * return: -1 error or 0 resumed ok
  960:  */
  961: int
  962: schedResumeby(sched_root_task_t * __restrict root, u_char criteria, void *param)
  963: {
  964: 	sched_task_t *task, *tmp;
  965: 	register int flg = 0;
  966: 
  967: 	if (!root)
  968: 		return -1;
  969: 
  970: #ifdef HAVE_LIBPTHREAD
  971: 	pthread_mutex_lock(&root->root_mtx[taskSUSPEND]);
  972: #endif
  973: 	TAILQ_FOREACH_SAFE(task, &root->root_suspend, task_node, tmp) {
  974: 		flg ^= flg;
  975: 		switch (criteria) {
  976: 			case CRITERIA_ANY:
  977: 				flg = 1;
  978: 				break;
  979: 			case CRITERIA_ID:
  980: 			case CRITERIA_VAL:
  981: 				if (TASK_VAL(task) == (u_long) param)
  982: 					flg = 1;
  983: 				break;
  984: 			case CRITERIA_DATA:
  985: 				if (TASK_ID(task) == (sched_task_t*) param)
  986: 					flg = 1;
  987: 				break;
  988: 			default:
  989: 				sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
  990: 				flg = -1;
  991: 		}
  992: 		if (flg < 0)
  993: 			break;
  994: 		/* resume choosen task */
  995: 		if (flg > 0) {
  996: 			if (root->root_hooks.hook_exec.resume)
  997: 				if (root->root_hooks.hook_exec.resume(task, NULL)) {
  998: 					flg = -1;
  999: 					break;
 1000: 				}
 1001: 
 1002: 			TAILQ_REMOVE(&root->root_suspend, task, task_node);
 1003: 
 1004: 			task->task_type = taskREADY;
 1005: #ifdef HAVE_LIBPTHREAD
 1006: 			pthread_mutex_lock(&root->root_mtx[taskREADY]);
 1007: #endif
 1008: 			TAILQ_INSERT_TAIL(&root->root_ready, task, task_node);
 1009: #ifdef HAVE_LIBPTHREAD
 1010: 			pthread_mutex_unlock(&root->root_mtx[taskREADY]);
 1011: #endif
 1012: 
 1013: 			flg ^= flg;	/* ok */
 1014: 		}
 1015: 	}
 1016: #ifdef HAVE_LIBPTHREAD
 1017: 	pthread_mutex_unlock(&root->root_mtx[taskSUSPEND]);
 1018: #endif
 1019: 
 1020: 	return flg;
 1021: }

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