File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / aitsched.c
Revision 1.4.2.3: download - view: text, annotated - select for diffs - revision graph
Sun Jan 8 03:28:26 2012 UTC (12 years, 5 months ago) by misho
Branches: sched1_3
Diff to: branchpoint 1.4: preferred, unified
add pthread safe

    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.3 2012/01/08 03:28:26 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: #ifdef HAVE_LIBPTHREAD
  136: 		for (i = 0; i < taskMAX; i++)
  137: 			if (pthread_mutex_init(&root->root_mtx[i], NULL)) {
  138: 				LOGERR;
  139: 				while (i)
  140: 					pthread_mutex_destroy(&root->root_mtx[--i]);
  141: 				free(root);
  142: 				return NULL;
  143: 			}
  144: 
  145: 		for (i = 0; i < taskMAX; i++)
  146: 			pthread_mutex_lock(&root->root_mtx[i]);
  147: #endif
  148: 
  149: 		TAILQ_INIT(&root->root_read);
  150: 		TAILQ_INIT(&root->root_write);
  151: 		TAILQ_INIT(&root->root_timer);
  152: 		TAILQ_INIT(&root->root_event);
  153: 		TAILQ_INIT(&root->root_eventlo);
  154: 		TAILQ_INIT(&root->root_ready);
  155: 		TAILQ_INIT(&root->root_unuse);
  156: 
  157: #ifdef HAVE_LIBPTHREAD
  158: 		for (i = 0; i < taskMAX; i++)
  159: 			pthread_mutex_unlock(&root->root_mtx[i]);
  160: #endif
  161: 
  162: 		if (data && *data) {
  163: 			if (datlen) {
  164: 				root->root_data.iov_base = *data;
  165: 				root->root_data.iov_len = datlen;
  166: 			} else { /* if datlen == 0, switch to callbacks init mode */
  167: 				 /* little hack :) for correct initialization of scheduler */
  168: 				func = (int(*)(sched_root_task_t*)) data;
  169: 				func(root);
  170: 			}
  171: 		}
  172: 
  173: 		if (root->root_hooks.hook_root.init)
  174: 			root->root_hooks.hook_root.init(root, NULL);
  175: 	}
  176: 
  177: 	return root;
  178: }
  179: 
  180: /*
  181:  * schedEnd() - End scheduler & free all resources
  182:  * @root = root task
  183:  * return: -1 error or 0 ok
  184:  */
  185: int
  186: schedEnd(sched_root_task_t ** __restrict root)
  187: {
  188: 	sched_task_t *task;
  189: #ifdef HAVE_LIBPTHREAD
  190: 	register int i;
  191: #endif
  192: 
  193: 	if (!root || !*root)
  194: 		return -1;
  195: 
  196: 	TAILQ_FOREACH(task, &(*root)->root_read, task_node) {
  197: 		schedCancel(task);
  198: 	}
  199: 	TAILQ_FOREACH(task, &(*root)->root_write, task_node) {
  200: 		schedCancel(task);
  201: 	}
  202: 	TAILQ_FOREACH(task, &(*root)->root_timer, task_node) {
  203: 		schedCancel(task);
  204: 	}
  205: 	TAILQ_FOREACH(task, &(*root)->root_event, task_node) {
  206: 		schedCancel(task);
  207: 	}
  208: 	TAILQ_FOREACH(task, &(*root)->root_eventlo, task_node) {
  209: 		schedCancel(task);
  210: 	}
  211: 	TAILQ_FOREACH(task, &(*root)->root_ready, task_node) {
  212: 		schedCancel(task);
  213: 	}
  214: 
  215: #ifdef HAVE_LIBPTHREAD
  216: 	pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
  217: #endif
  218: 	while ((task = TAILQ_FIRST(&(*root)->root_unuse))) {
  219: 		TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
  220: 		free(task);
  221: 	}
  222: #ifdef HAVE_LIBPTHREAD
  223: 	pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
  224: #endif
  225: 
  226: 	if ((*root)->root_hooks.hook_root.fini)
  227: 		(*root)->root_hooks.hook_root.fini(*root, NULL);
  228: 
  229: #ifdef HAVE_LIBPTHREAD
  230: 	for (i = 0; i < taskMAX; i++)
  231: 		pthread_mutex_destroy(&(*root)->root_mtx[i]);
  232: #endif
  233: 
  234: 	free(*root);
  235: 	*root = NULL;
  236: 	return 0;
  237: }
  238: 
  239: /*
  240:  * schedCall() - Call task execution function
  241:  * @task = current task
  242:  * return: !=NULL error or =NULL ok
  243:  */
  244: inline void *
  245: schedCall(sched_task_t * __restrict task)
  246: {
  247: 	void *ptr = (void*) -1;
  248: 
  249: 	if (!task)
  250: 		return ptr;
  251: 
  252: 	if (!TASK_ISLOCKED(task))
  253: 		TASK_LOCK(task);
  254: 
  255: 	task->task_id++;
  256: 	ptr = task->task_func(task);
  257: 
  258: 	TASK_UNLOCK(task);
  259: 	return ptr;
  260: }
  261: 
  262: /*
  263:  * schedFetch() - Fetch ready task
  264:  * @root = root task
  265:  * return: =NULL error or !=NULL ready task
  266:  */
  267: inline void *
  268: schedFetch(sched_root_task_t * __restrict root)
  269: {
  270: 	void *ptr;
  271: 
  272: 	if (!root)
  273: 		return NULL;
  274: 
  275: 	if (root->root_hooks.hook_exec.fetch)
  276: 		ptr = root->root_hooks.hook_exec.fetch(root, NULL);
  277: 	else
  278: 		ptr = NULL;
  279: 
  280: 	return ptr;
  281: }
  282: 
  283: /*
  284:  * schedCancel() - Cancel task from scheduler
  285:  * @task = task
  286:  * return: -1 error or 0 ok
  287:  */
  288: int
  289: schedCancel(sched_task_t * __restrict task)
  290: {
  291: 	sched_queue_t *queue;
  292: 
  293: 	if (!task || !TASK_ROOT(task))
  294: 		return -1;
  295: 
  296: 	if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  297: 		if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
  298: 			return -1;
  299: 
  300: 	switch (TASK_TYPE(task)) {
  301: 		case taskREAD:
  302: 			queue = &TASK_ROOT(task)->root_read;
  303: 			break;
  304: 		case taskWRITE:
  305: 			queue = &TASK_ROOT(task)->root_write;
  306: 			break;
  307: 		case taskTIMER:
  308: 			queue = &TASK_ROOT(task)->root_timer;
  309: 			break;
  310: 		case taskEVENT:
  311: 			queue = &TASK_ROOT(task)->root_event;
  312: 			break;
  313: 		case taskEVENTLO:
  314: 			queue = &TASK_ROOT(task)->root_eventlo;
  315: 			break;
  316: 		case taskREADY:
  317: 			queue = &TASK_ROOT(task)->root_ready;
  318: 			break;
  319: 		default:
  320: 			queue = NULL;
  321: 	}
  322: 	if (queue) {
  323: #ifdef HAVE_LIBPTHREAD
  324: 		pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  325: #endif
  326: 		TAILQ_REMOVE(queue, task, task_node);
  327: #ifdef HAVE_LIBPTHREAD
  328: 		pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
  329: #endif
  330: 	}
  331: 	if (TASK_TYPE(task) != taskUNUSE)
  332: 		_sched_unuseTask(task);
  333: 
  334: 	return 0;
  335: }
  336: 
  337: /*
  338:  * schedCancelby() - Cancel task from scheduler by criteria
  339:  * @root = root task
  340:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
  341:  * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
  342:  * @param = search parameter
  343:  * @hook = custom cleanup hook function, may be NULL
  344:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
  345:  */
  346: int
  347: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
  348: 		u_char criteria, void *param, sched_hook_func_t hook)
  349: {
  350: 	sched_task_t *task;
  351: 	sched_queue_t *queue;
  352: 	int flg = 0;
  353: 
  354: 	if (!root)
  355: 		return -1;
  356: 	if (type == taskMAX) {
  357: 		if (schedCancelby(root, taskREAD, criteria, param, hook))
  358: 			return -2;
  359: 		if (schedCancelby(root, taskWRITE, criteria, param, hook))
  360: 			return -2;
  361: 		if (schedCancelby(root, taskTIMER, criteria, param, hook))
  362: 			return -2;
  363: 		if (schedCancelby(root, taskEVENT, criteria, param, hook))
  364: 			return -2;
  365: 		if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
  366: 			return -2;
  367: 		if (schedCancelby(root, taskREADY, criteria, param, hook))
  368: 			return -2;
  369: 		return 0;
  370: 	}
  371: 	switch (type) {
  372: 		case taskREAD:
  373: 			queue = &root->root_read;
  374: 			break;
  375: 		case taskWRITE:
  376: 			queue = &root->root_write;
  377: 			break;
  378: 		case taskTIMER:
  379: 			queue = &root->root_timer;
  380: 			break;
  381: 		case taskEVENT:
  382: 			queue = &root->root_event;
  383: 			break;
  384: 		case taskEVENTLO:
  385: 			queue = &root->root_eventlo;
  386: 			break;
  387: 		case taskREADY:
  388: 			queue = &root->root_ready;
  389: 			break;
  390: 		default:
  391: 			return 0;
  392: 	}
  393: 
  394: #ifdef HAVE_LIBPTHREAD
  395: 	pthread_mutex_lock(&root->root_mtx[type]);
  396: #endif
  397: 	TAILQ_FOREACH(task, queue, task_node)
  398: 		if (criteria == CRITERIA_CALL) {
  399: 			if (task->task_func == (sched_task_func_t) param) {
  400: 				flg++;
  401: 				break;
  402: 			}
  403: 		} else if (criteria == CRITERIA_ARG) {
  404: 			if (task->task_arg == param) {
  405: 				flg++;
  406: 				break;
  407: 			}
  408: 		} else if (criteria == CRITERIA_FD) {
  409: 			if (TASK_FD(task) == (intptr_t) param) {
  410: 				flg++;
  411: 				break;
  412: 			}
  413: 		} else if (criteria == CRITERIA_VAL) {
  414: 			if (TASK_VAL(task) == (u_long) param) {
  415: 				flg++;
  416: 				break;
  417: 			}
  418: 		} else if (criteria == CRITERIA_TV) {
  419: 			if (!timercmp(&TASK_TV(task), (struct timeval*) param, -)) {
  420: 				flg++;
  421: 				break;
  422: 			}
  423: 		} else {
  424: #ifdef HAVE_LIBPTHREAD
  425: 			pthread_mutex_unlock(&root->root_mtx[type]);
  426: #endif
  427: 			sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
  428: 			return -1;
  429: 		}
  430: #ifdef HAVE_LIBPTHREAD
  431: 	pthread_mutex_unlock(&root->root_mtx[type]);
  432: #endif
  433: 	if (!flg || !task)	/* task not found */
  434: 		return 0;
  435: 
  436: 	if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
  437: 		if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
  438: 			return -1;
  439: 	if (hook)
  440: 		if (hook(task, NULL))
  441: 			return -3;
  442: 
  443: #ifdef HAVE_LIBPTHREAD
  444: 	pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[type]);
  445: #endif
  446: 	TAILQ_REMOVE(queue, task, task_node);
  447: #ifdef HAVE_LIBPTHREAD
  448: 	pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[type]);
  449: #endif
  450: 
  451: 	if (TASK_TYPE(task) != taskUNUSE)
  452: 		_sched_unuseTask(task);
  453: 	return 0;
  454: }
  455: 
  456: /*
  457:  * schedRun() - Scheduler *run loop*
  458:  * @root = root task
  459:  * @killState = kill condition variable, if !=0 stop scheduler loop
  460:  * return: -1 error or 0 ok
  461:  */
  462: int
  463: schedRun(sched_root_task_t * __restrict root, volatile intptr_t * __restrict killState)
  464: {
  465: 	sched_task_t *task;
  466: 
  467: 	if (!root)
  468: 		return -1;
  469: 
  470: 	if (root->root_hooks.hook_exec.run)
  471: 		if (root->root_hooks.hook_exec.run(root, NULL))
  472: 			return -1;
  473: 	if (root->root_hooks.hook_exec.fetch) {
  474: 		if (killState)
  475: 			while (!*killState) {
  476: 				if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  477: 					schedCall(task);
  478: 			}
  479: 		else
  480: 			while ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
  481: 				schedCall(task);
  482: 	}
  483: 
  484: 	return 0;
  485: }

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