File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.4.2.6: download - view: text, annotated - select for diffs - revision graph
Tue Jan 24 14:51:03 2012 UTC (12 years, 9 months ago) by misho
Branches: sched1_3
Diff to: branchpoint 1.4: preferred, unified
add polling period support

    1: /*************************************************************************
    2: * (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitsched.c,v 1.4.2.6 2012/01/24 14:51:03 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
   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:  * @root = root task
   90:  * return: -1 error or 0 ok
   91:  */
   92: int
   93: schedRegisterHooks(sched_root_task_t * __restrict root)
   94: {
   95: 	if (!root || (root->root_data.iov_base && root->root_data.iov_len))
   96: 		return -1;
   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: 
  105: 	root->root_hooks.hook_exec.cancel = sched_hook_cancel;
  106: 	root->root_hooks.hook_exec.fetch = sched_hook_fetch;
  107: 	root->root_hooks.hook_exec.exception = sched_hook_exception;
  108: 
  109: 	root->root_hooks.hook_root.init = sched_hook_init;
  110: 	root->root_hooks.hook_root.fini = sched_hook_fini;
  111: 	return 0;
  112: }
  113: 
  114: /*
  115:  * schedInit() - Init scheduler
  116:  * @data = optional data if !=NULL
  117:  * @datlen = data len if data is set
  118:  * return: allocated root task if ok or NULL error
  119:  */
  120: sched_root_task_t *
  121: schedInit(void ** __restrict data, size_t datlen)
  122: {
  123: 	sched_root_task_t *root = NULL;
  124: 	int (*func)(sched_root_task_t *);
  125: #ifdef HAVE_LIBPTHREAD
  126: 	register int i;
  127: #endif
  128: 
  129: 	root = malloc(sizeof(sched_root_task_t));
  130: 	if (!root) {
  131: 		LOGERR;
  132: 	} else {
  133: 		memset(root, 0, sizeof(sched_root_task_t));
  134: 
  135: 		/* INFINIT polling period by default */
  136: 		root->root_poll.tv_sec = root->root_poll.tv_nsec = -1;
  137: 
  138: #ifdef HAVE_LIBPTHREAD
  139: 		for (i = 0; i < taskMAX; i++)
  140: 			if (pthread_mutex_init(&root->root_mtx[i], NULL)) {
  141: 				LOGERR;
  142: 				while (i)
  143: 					pthread_mutex_destroy(&root->root_mtx[--i]);
  144: 				free(root);
  145: 				return NULL;
  146: 			}
  147: 
  148: 		for (i = 0; i < taskMAX; i++)
  149: 			pthread_mutex_lock(&root->root_mtx[i]);
  150: #endif
  151: 
  152: 		TAILQ_INIT(&root->root_read);
  153: 		TAILQ_INIT(&root->root_write);
  154: 		TAILQ_INIT(&root->root_timer);
  155: 		TAILQ_INIT(&root->root_event);
  156: 		TAILQ_INIT(&root->root_eventlo);
  157: 		TAILQ_INIT(&root->root_ready);
  158: 		TAILQ_INIT(&root->root_unuse);
  159: 
  160: #ifdef HAVE_LIBPTHREAD
  161: 		for (i = 0; i < taskMAX; i++)
  162: 			pthread_mutex_unlock(&root->root_mtx[i]);
  163: #endif
  164: 
  165: 		if (data && *data) {
  166: 			if (datlen) {
  167: 				root->root_data.iov_base = *data;
  168: 				root->root_data.iov_len = datlen;
  169: 			} else { /* if datlen == 0, switch to callbacks init mode */
  170: 				 /* little hack :) for correct initialization of scheduler */
  171: 				func = (int(*)(sched_root_task_t*)) data;
  172: 				func(root);
  173: 			}
  174: 		}
  175: 
  176: 		if (root->root_hooks.hook_root.init)
  177: 			root->root_hooks.hook_root.init(root, NULL);
  178: 	}
  179: 
  180: 	return root;
  181: }
  182: 
  183: /*
  184:  * schedEnd() - End scheduler & free all resources
  185:  * @root = root task
  186:  * return: -1 error or 0 ok
  187:  */
  188: int
  189: schedEnd(sched_root_task_t ** __restrict root)
  190: {
  191: 	sched_task_t *task;
  192: #ifdef HAVE_LIBPTHREAD
  193: 	register int i;
  194: #endif
  195: 
  196: 	if (!root || !*root)
  197: 		return -1;
  198: 
  199: 	TAILQ_FOREACH(task, &(*root)->root_read, task_node) {
  200: 		schedCancel(task);
  201: 	}
  202: 	TAILQ_FOREACH(task, &(*root)->root_write, task_node) {
  203: 		schedCancel(task);
  204: 	}
  205: 	TAILQ_FOREACH(task, &(*root)->root_timer, task_node) {
  206: 		schedCancel(task);
  207: 	}
  208: 	TAILQ_FOREACH(task, &(*root)->root_event, task_node) {
  209: 		schedCancel(task);
  210: 	}
  211: 	TAILQ_FOREACH(task, &(*root)->root_eventlo, task_node) {
  212: 		schedCancel(task);
  213: 	}
  214: 	TAILQ_FOREACH(task, &(*root)->root_ready, task_node) {
  215: 		schedCancel(task);
  216: 	}
  217: 
  218: #ifdef HAVE_LIBPTHREAD
  219: 	pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
  220: #endif
  221: 	while ((task = TAILQ_FIRST(&(*root)->root_unuse))) {
  222: 		TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
  223: 		free(task);
  224: 	}
  225: #ifdef HAVE_LIBPTHREAD
  226: 	pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
  227: #endif
  228: 
  229: 	if ((*root)->root_hooks.hook_root.fini)
  230: 		(*root)->root_hooks.hook_root.fini(*root, NULL);
  231: 
  232: #ifdef HAVE_LIBPTHREAD
  233: 	for (i = 0; i < taskMAX; i++)
  234: 		pthread_mutex_destroy(&(*root)->root_mtx[i]);
  235: #endif
  236: 
  237: 	free(*root);
  238: 	*root = NULL;
  239: 	return 0;
  240: }
  241: 
  242: /*
  243:  * schedCall() - Call task execution function
  244:  * @task = current task
  245:  * return: !=NULL error or =NULL ok
  246:  */
  247: inline void *
  248: schedCall(sched_task_t * __restrict task)
  249: {
  250: 	void *ptr = (void*) -1;
  251: 
  252: 	if (!task)
  253: 		return ptr;
  254: 
  255: 	if (!TASK_ISLOCKED(task))
  256: 		TASK_LOCK(task);
  257: 
  258: 	task->task_id++;
  259: 	ptr = task->task_func(task);
  260: 
  261: 	TASK_UNLOCK(task);
  262: 	return ptr;
  263: }
  264: 
  265: /*
  266:  * schedFetch() - Fetch ready task
  267:  * @root = root task
  268:  * return: =NULL error or !=NULL ready task
  269:  */
  270: inline void *
  271: schedFetch(sched_root_task_t * __restrict root)
  272: {
  273: 	void *ptr;
  274: 
  275: 	if (!root)
  276: 		return NULL;
  277: 
  278: 	if (root->root_hooks.hook_exec.fetch)
  279: 		ptr = root->root_hooks.hook_exec.fetch(root, NULL);
  280: 	else
  281: 		ptr = NULL;
  282: 
  283: 	return ptr;
  284: }
  285: 
  286: /*
  287:  * schedCancel() - Cancel task from scheduler
  288:  * @task = task
  289:  * return: -1 error or 0 ok
  290:  */
  291: int
  292: schedCancel(sched_task_t * __restrict task)
  293: {
  294: 	sched_queue_t *queue;
  295: 
  296: 	if (!task || !TASK_ROOT(task))
  297: 		return -1;
  298: 
  299: 	if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  300: 		if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
  301: 			return -1;
  302: 
  303: 	switch (TASK_TYPE(task)) {
  304: 		case taskREAD:
  305: 			queue = &TASK_ROOT(task)->root_read;
  306: 			break;
  307: 		case taskWRITE:
  308: 			queue = &TASK_ROOT(task)->root_write;
  309: 			break;
  310: 		case taskTIMER:
  311: 			queue = &TASK_ROOT(task)->root_timer;
  312: 			break;
  313: 		case taskEVENT:
  314: 			queue = &TASK_ROOT(task)->root_event;
  315: 			break;
  316: 		case taskEVENTLO:
  317: 			queue = &TASK_ROOT(task)->root_eventlo;
  318: 			break;
  319: 		case taskREADY:
  320: 			queue = &TASK_ROOT(task)->root_ready;
  321: 			break;
  322: 		default:
  323: 			queue = NULL;
  324: 	}
  325: 	if (queue) {
  326: #ifdef HAVE_LIBPTHREAD
  327: 		pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  328: #endif
  329: 		TAILQ_REMOVE(queue, task, task_node);
  330: #ifdef HAVE_LIBPTHREAD
  331: 		pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  332: #endif
  333: 	}
  334: 	if (TASK_TYPE(task) != taskUNUSE)
  335: 		_sched_unuseTask(task);
  336: 
  337: 	return 0;
  338: }
  339: 
  340: /*
  341:  * schedCancelby() - Cancel task from scheduler by criteria
  342:  * @root = root task
  343:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
  344:  * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
  345:  * @param = search parameter
  346:  * @hook = custom cleanup hook function, may be NULL
  347:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
  348:  */
  349: int
  350: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
  351: 		u_char criteria, void *param, sched_hook_func_t hook)
  352: {
  353: 	sched_task_t *task;
  354: 	sched_queue_t *queue;
  355: 	int flg = 0;
  356: 
  357: 	if (!root)
  358: 		return -1;
  359: 	if (type == taskMAX) {
  360: 		if (schedCancelby(root, taskREAD, criteria, param, hook))
  361: 			return -2;
  362: 		if (schedCancelby(root, taskWRITE, criteria, param, hook))
  363: 			return -2;
  364: 		if (schedCancelby(root, taskTIMER, criteria, param, hook))
  365: 			return -2;
  366: 		if (schedCancelby(root, taskEVENT, criteria, param, hook))
  367: 			return -2;
  368: 		if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
  369: 			return -2;
  370: 		if (schedCancelby(root, taskREADY, criteria, param, hook))
  371: 			return -2;
  372: 		return 0;
  373: 	}
  374: 	switch (type) {
  375: 		case taskREAD:
  376: 			queue = &root->root_read;
  377: 			break;
  378: 		case taskWRITE:
  379: 			queue = &root->root_write;
  380: 			break;
  381: 		case taskTIMER:
  382: 			queue = &root->root_timer;
  383: 			break;
  384: 		case taskEVENT:
  385: 			queue = &root->root_event;
  386: 			break;
  387: 		case taskEVENTLO:
  388: 			queue = &root->root_eventlo;
  389: 			break;
  390: 		case taskREADY:
  391: 			queue = &root->root_ready;
  392: 			break;
  393: 		default:
  394: 			return 0;
  395: 	}
  396: 
  397: #ifdef HAVE_LIBPTHREAD
  398: 	pthread_mutex_lock(&root->root_mtx[type]);
  399: #endif
  400: 	TAILQ_FOREACH(task, queue, task_node)
  401: 		if (criteria == CRITERIA_CALL) {
  402: 			if (task->task_func == (sched_task_func_t) param) {
  403: 				flg++;
  404: 				break;
  405: 			}
  406: 		} else if (criteria == CRITERIA_ARG) {
  407: 			if (task->task_arg == param) {
  408: 				flg++;
  409: 				break;
  410: 			}
  411: 		} else if (criteria == CRITERIA_FD) {
  412: 			if (TASK_FD(task) == (intptr_t) param) {
  413: 				flg++;
  414: 				break;
  415: 			}
  416: 		} else if (criteria == CRITERIA_VAL) {
  417: 			if (TASK_VAL(task) == (u_long) param) {
  418: 				flg++;
  419: 				break;
  420: 			}
  421: 		} else if (criteria == CRITERIA_TV) {
  422: 			if (!timespeccmp(&TASK_TS(task), (struct timespec*) param, -)) {
  423: 				flg++;
  424: 				break;
  425: 			}
  426: 		} else {
  427: #ifdef HAVE_LIBPTHREAD
  428: 			pthread_mutex_unlock(&root->root_mtx[type]);
  429: #endif
  430: 			sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
  431: 			return -1;
  432: 		}
  433: #ifdef HAVE_LIBPTHREAD
  434: 	pthread_mutex_unlock(&root->root_mtx[type]);
  435: #endif
  436: 	if (!flg || !task)	/* task not found */
  437: 		return 0;
  438: 
  439: 	if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  440: 		if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
  441: 			return -1;
  442: 	if (hook)
  443: 		if (hook(task, NULL))
  444: 			return -3;
  445: 
  446: #ifdef HAVE_LIBPTHREAD
  447: 	pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[type]);
  448: #endif
  449: 	TAILQ_REMOVE(queue, task, task_node);
  450: #ifdef HAVE_LIBPTHREAD
  451: 	pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[type]);
  452: #endif
  453: 
  454: 	if (TASK_TYPE(task) != taskUNUSE)
  455: 		_sched_unuseTask(task);
  456: 	return 0;
  457: }
  458: 
  459: /*
  460:  * schedRun() - Scheduler *run loop*
  461:  * @root = root task
  462:  * @killState = kill condition variable, if !=0 stop scheduler loop
  463:  * return: -1 error or 0 ok
  464:  */
  465: int
  466: schedRun(sched_root_task_t * __restrict root, volatile intptr_t * __restrict killState)
  467: {
  468: 	sched_task_t *task;
  469: 
  470: 	if (!root)
  471: 		return -1;
  472: 
  473: 	if (root->root_hooks.hook_exec.run)
  474: 		if (root->root_hooks.hook_exec.run(root, NULL))
  475: 			return -1;
  476: 	if (root->root_hooks.hook_exec.fetch) {
  477: 		if (killState)
  478: 			while (!*killState) {
  479: 				if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  480: 					schedCall(task);
  481: 			}
  482: 		else
  483: 			while ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  484: 				schedCall(task);
  485: 	}
  486: 
  487: 	return 0;
  488: }
  489: 
  490: /*
  491:  * schedPolling() - Polling timeout period if no timer task is present
  492:  * @root = root task
  493:  * @ts = timeout polling period, if ==NULL INFINIT timeout
  494:  * @tsold = old timeout polling if !=NULL
  495:  * return: -1 error or 0 ok
  496:  */
  497: inline int
  498: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts, 
  499: 		struct timespec * __restrict tsold)
  500: {
  501: 	if (!root)
  502: 		return -1;
  503: 
  504: 	if (tsold)
  505: 		*tsold = root->root_poll;
  506: 
  507: 	if (!ts)
  508: 		root->root_poll.tv_sec = root->root_poll.tv_nsec = -1;
  509: 	else
  510: 		root->root_poll = *ts;
  511: 
  512: 	return 0;
  513: }

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