File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.15.2.3: download - view: text, annotated - select for diffs - revision graph
Thu Aug 23 00:31:41 2012 UTC (11 years, 10 months ago) by misho
Branches: sched3_3
Diff to: branchpoint 1.15: preferred, unified
search solution for fast cancels

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

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