File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / inc / aitsched.h
Revision 1.10: download - view: text, annotated - select for diffs - revision graph
Thu May 31 22:31:48 2012 UTC (12 years, 1 month ago) by misho
Branches: MAIN
CVS tags: sched2_5, sched2_4, sched2_3, SCHED2_4, SCHED2_3, SCHED2_2, HEAD
version 2.2 (prod)

    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.h,v 1.10 2012/05/31 22:31:48 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: #ifndef __AITSCHED_H
   47: #define __AITSCHED_H
   48: 
   49: 
   50: #include <sys/types.h>
   51: #include <sys/queue.h>
   52: #include <sys/uio.h>
   53: #include <stdint.h>
   54: #include <pthread.h>
   55: 
   56: 
   57: /* criteria type */
   58: #define CRITERIA_ANY	0
   59: #define CRITERIA_CALL	1
   60: #define CRITERIA_ARG	2
   61: #define CRITERIA_FD	3
   62: #define CRITERIA_VAL	4
   63: #define CRITERIA_TS	5
   64: #define CRITERIA_DATA	6
   65: 
   66: 
   67: /* early declaration for root & task */
   68: typedef struct sched_Task	sched_task_t;
   69: typedef struct sched_RootTask	sched_root_task_t;
   70: 
   71: typedef enum {
   72: 	taskREAD = 0,
   73: 	taskWRITE,
   74: 	taskTIMER,
   75: 	taskALARM,
   76: 	taskNODE,
   77: 	taskPROC,
   78: 	taskUSER,
   79: 	taskSIGNAL,
   80: 	taskEVENT,
   81: 	taskEVENTLO,
   82: 	taskREADY,
   83: 	taskUNUSE,
   84: 	taskMAX
   85: } sched_task_type_t;
   86: 
   87: /* hooks */
   88: typedef void *(*sched_hook_func_t)(void *, void *);
   89: struct sched_HooksTask {
   90: 	struct {
   91: 		/* read(sched_task_t *task, NULL) -> int */
   92: 		sched_hook_func_t	read;
   93: 		/* write(sched_task_t *task, NULL) -> int */
   94: 		sched_hook_func_t	write;
   95: 		/* timer(sched_task_t *task, struct timespec *ts) -> int */
   96: 		sched_hook_func_t	timer;
   97: 		/* alarm(sched_task_t *task, NULL) -> int */
   98: 		sched_hook_func_t	alarm;
   99: 		/* node(sched_task_t *task, NULL) -> int */
  100: 		sched_hook_func_t	node;
  101: 		/* proc(sched_task_t *task, NULL) -> int */
  102: 		sched_hook_func_t	proc;
  103: 		/* user(sched_task_t *task, NULL) -> int */
  104: 		sched_hook_func_t	user;
  105: 		/* signal(sched_task_t *task, NULL) -> int */
  106: 		sched_hook_func_t	signal;
  107: 		/* event(sched_task_t *task, NULL) -> int */
  108: 		sched_hook_func_t	event;
  109: 		/* eventlo(sched_task_t *task, NULL) -> int */
  110: 		sched_hook_func_t	eventlo;
  111: 	}	hook_add;
  112: 	struct {
  113: 		/* cancel(sched_task_t *task, NULL) -> int */
  114: 		sched_hook_func_t	cancel;
  115: 		/* run(sched_root_task_t *root, NULL) -> int */
  116: 		sched_hook_func_t	run;
  117: 		/* fetch(sched_root_task_t *root, NULL) -> sched_task_t* */
  118: 		sched_hook_func_t	fetch;
  119: 		/* exception(sched_root_task_t *root, NULL) -> int */
  120: 		sched_hook_func_t	exception;
  121: 		/* condition(sched_root_task_t *root, intptr_t *stopValue) -> int */
  122: 		sched_hook_func_t	condition;
  123: 	}	hook_exec;
  124: 	struct {
  125: 		/* init(sched_root_task_t *root, void *data) -> int */
  126: 		sched_hook_func_t	init;
  127: 		/* fini(sched_root_task_t *root, NULL) -> int */
  128: 		sched_hook_func_t	fini;
  129: 		/* error(sched_root_task_t *root, int errno) -> int */
  130: 		sched_hook_func_t	error;
  131: 	}	hook_root;
  132: };
  133: typedef struct sched_HooksTask hooks_task_t;
  134: 
  135: /* task callback, like pthread callback! */
  136: typedef void *(*sched_task_func_t)(sched_task_t * /* current task data*/);
  137: 
  138: /* task lock helpers */
  139: #define TASK_LOCK(x)		((x)->task_lock = 42)
  140: #define TASK_UNLOCK(x)		((x)->task_lock ^= (x)->task_lock)
  141: #define TASK_ISLOCKED(x)	((x)->task_lock)
  142: 
  143: /* task & queue */
  144: struct sched_Task {
  145: 	volatile int			task_lock;
  146: 	uintptr_t			task_id;
  147: #define TASK_ID(x)	((struct sched_Task*) (x)->task_id)
  148: 	sched_task_type_t		task_type;
  149: #define TASK_TYPE(x)	(x)->task_type
  150: 
  151: 	sched_root_task_t		*task_root;
  152: #define TASK_ROOT(x)	(x)->task_root
  153: 	sched_task_func_t		task_func;
  154: #define TASK_FUNC(x)	(x)->task_func
  155: 
  156: 	void				*task_arg;
  157: 	union {
  158: 		unsigned long	v;
  159: 		intptr_t	fd;
  160: 		struct timespec	ts;
  161: 	}				task_val;
  162: #define TASK_ARG(x)	(x)->task_arg
  163: #define TASK_VAL(x)	(x)->task_val.v
  164: #define TASK_FD(x)	(x)->task_val.fd
  165: #define TASK_TS(x)	(x)->task_val.ts
  166: 
  167: 	struct iovec			task_data;
  168: #define TASK_DATA(x)	(x)->task_data.iov_base
  169: #define TASK_DATLEN(x)	(x)->task_data.iov_len
  170: 
  171: 	TAILQ_ENTRY(sched_Task)		task_node;
  172: };
  173: typedef TAILQ_HEAD(, sched_Task) sched_queue_t;
  174: #define TASK_DATA_SET(x, _dp, _dl)	do { \
  175: 						if ((x)) { \
  176: 							(x)->task_data.iov_base = (_dp); \
  177: 							(x)->task_data.iov_len = _dl; \
  178: 						} \
  179: 					while (0)
  180: 
  181: /* root task */
  182: struct sched_RootTask {
  183: 	int		root_kq;
  184: 	struct timespec	root_wait;
  185: 	struct timespec	root_poll;
  186: 	intptr_t	root_cond;
  187: 
  188: 	pthread_mutex_t	root_mtx[taskMAX];
  189: 
  190: 	sched_queue_t	root_read;
  191: 	sched_queue_t	root_write;
  192: 	sched_queue_t	root_timer;
  193: 	sched_queue_t	root_alarm;
  194: 	sched_queue_t	root_node;
  195: 	sched_queue_t	root_proc;
  196: 	sched_queue_t	root_user;
  197: 	sched_queue_t	root_signal;
  198: 	sched_queue_t	root_event;
  199: 	sched_queue_t	root_eventlo;
  200: 	sched_queue_t	root_ready;
  201: 	sched_queue_t	root_unuse;
  202: 	int		root_eventlo_miss;
  203: 
  204: 	hooks_task_t	root_hooks;
  205: 	struct iovec	root_data;
  206: #define ROOT_DATA(x)	(x)->root_data.iov_base
  207: #define ROOT_DATLEN(x)	(x)->root_data.iov_len
  208: };
  209: #define ROOT_QUEUE_EMPTY(x, _q)	TAILQ_EMPTY(&((x)->root_##_q))
  210: 
  211: 
  212: inline int sched_GetErrno();
  213: inline const char *sched_GetError();
  214: 
  215: 
  216: /*
  217:  * schedInit() - Init scheduler
  218:  *
  219:  * @data = optional data if !=NULL
  220:  * @datlen = data len if data is set
  221:  * return: allocated root task if ok or NULL error
  222:  */
  223: sched_root_task_t *schedInit(void ** __restrict data, size_t datlen);
  224: #define schedBegin()	schedInit((void**) &schedRegisterHooks, 0)
  225: /*
  226:  * schedEnd() - End scheduler & free all resources
  227:  *
  228:  * @root = root task
  229:  * return: -1 error or 0 ok
  230:  */
  231: int schedEnd(sched_root_task_t ** __restrict root);
  232: /*
  233:  * schedRegisterHooks() - Register IO handles and bind tasks to it
  234:  *
  235:  * @root = root task
  236:  * return: -1 error or 0 ok
  237:  */
  238: int schedRegisterHooks(sched_root_task_t * __restrict root);
  239: /*
  240:  * schedPolling() - Polling timeout period if no timer task is present
  241:  *
  242:  * @root = root task
  243:  * @ts = timeout polling period, if ==NULL INFINIT timeout
  244:  * @tsold = old timeout polling if !=NULL
  245:  * return: -1 error or 0 ok
  246:  */
  247: inline int schedPolling(sched_root_task_t * __restrict root, 
  248: 		struct timespec * __restrict ts, struct timespec * __restrict tsold);
  249: /*
  250:  * schedTermCondition() - Activate hook for scheduler condition kill
  251:  *
  252:  * @root = root task
  253:  * @condValue = condition value, kill schedRun() if condValue == killState
  254:  * return: -1 error ok 0 ok
  255:  */
  256: inline int schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue);
  257: /*
  258:  * schedCall() - Call task execution function
  259:  *
  260:  * @task = current task
  261:  * return: !=NULL error or =NULL ok
  262:  */
  263: inline void *schedCall(sched_task_t * __restrict task);
  264: /*
  265:  * schedFetch() - Fetch ready task
  266:  *
  267:  * @root = root task
  268:  * return: =NULL error or !=NULL ready task
  269:  */
  270: inline void *schedFetch(sched_root_task_t * __restrict root);
  271: /*
  272:  * schedRun() - Scheduler *run loop*
  273:  *
  274:  * @root = root task
  275:  * @killState = kill condition variable, if !=0 stop scheduler loop
  276:  * return: -1 error or 0 ok
  277:  */
  278: int schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState);
  279: /*
  280:  * schedCancel() - Cancel task from scheduler
  281:  *
  282:  * @task = task
  283:  * return: -1 error or 0 ok
  284:  */
  285: int schedCancel(sched_task_t * __restrict task);
  286: /*
  287:  * schedCancelby() - Cancel task from scheduler by criteria
  288:  *
  289:  * @root = root task
  290:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
  291:  * @criteria = find task by criteria 
  292:  * 	[CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TS|CRITERIA_DATA]
  293:  * @param = search parameter
  294:  * @hook = custom cleanup hook function, may be NULL
  295:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
  296:  */
  297: int schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
  298: 		u_char criteria, void *param, sched_hook_func_t hook);
  299: 
  300: 
  301: /*
  302:  * schedRead() - Add READ I/O task to scheduler queue
  303:  *
  304:  * @root = root task
  305:  * @func = task execution function
  306:  * @arg = 1st func argument
  307:  * @fd = fd handle
  308:  * @opt_data = Optional data
  309:  * @opt_dlen = Optional data length
  310:  * return: NULL error or !=NULL new queued task
  311:  */
  312: sched_task_t *schedRead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  313: 		int fd, void *opt_data, size_t opt_dlen);
  314: #define schedReadSelf(x)	schedRead(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  315: 		TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  316: /*
  317:  * schedWrite() - Add WRITE I/O task to scheduler queue
  318:  *
  319:  * @root = root task
  320:  * @func = task execution function
  321:  * @arg = 1st func argument
  322:  * @fd = fd handle
  323:  * @opt_data = Optional data
  324:  * @opt_dlen = Optional data length
  325:  * return: NULL error or !=NULL new queued task
  326:  */
  327: sched_task_t *schedWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  328: 		int fd, void *opt_data, size_t opt_dlen);
  329: #define schedWriteSelf(x)	schedWrite(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  330: 		TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  331: /*
  332:  * schedAlarm() - Add ALARM task to scheduler queue
  333:  *
  334:  * @root = root task
  335:  * @func = task execution function
  336:  * @arg = 1st func argument
  337:  * @ts = timeout argument structure, minimum alarm timer resolution is 1msec!
  338:  * @opt_data = Optional data
  339:  * @opt_dlen = Optional data length
  340:  * return: NULL error or !=NULL new queued task
  341:  */
  342: sched_task_t *schedAlarm(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  343: 		struct timespec ts, void *opt_data, size_t opt_dlen);
  344: #define schedAlarmSelf(x)	schedAlarm(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  345: 		TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  346: /*
  347:  * schedNode() - Add NODE task to scheduler queue
  348:  *
  349:  * @root = root task
  350:  * @func = task execution function
  351:  * @arg = 1st func argument
  352:  * @fd = fd handle
  353:  * @opt_data = Optional data
  354:  * @opt_dlen = Optional data length
  355:  * return: NULL error or !=NULL new queued task
  356:  */
  357: sched_task_t *schedNode(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  358: 		int fd, void *opt_data, size_t opt_dlen);
  359: #define schedNodeSelf(x)	schedNode(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  360: 		TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  361: /*
  362:  * schedProc() - Add PROC task to scheduler queue
  363:  *
  364:  * @root = root task
  365:  * @func = task execution function
  366:  * @arg = 1st func argument
  367:  * @pid = PID
  368:  * @opt_data = Optional data
  369:  * @opt_dlen = Optional data length
  370:  * return: NULL error or !=NULL new queued task
  371:  */
  372: sched_task_t *schedProc(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  373: 		unsigned long pid, void *opt_data, size_t opt_dlen);
  374: #define schedProcSelf(x)	schedProc(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  375: 		TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  376: /*
  377:  * schedSignal() - Add SIGNAL task to scheduler queue
  378:  *
  379:  * @root = root task
  380:  * @func = task execution function
  381:  * @arg = 1st func argument
  382:  * @sig = Signal
  383:  * @opt_data = Optional data
  384:  * @opt_dlen = Optional data length
  385:  * return: NULL error or !=NULL new queued task
  386:  */
  387: sched_task_t *schedSignal(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  388: 		unsigned long sig, void *opt_data, size_t opt_dlen);
  389: #define schedSignalSelf(x)	schedSignal(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  390: 		TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  391: 
  392: /*
  393:  * schedUser() - Add trigger USER task to scheduler queue
  394:  *
  395:  * @root = root task
  396:  * @func = task execution function
  397:  * @arg = 1st func argument
  398:  * @id = Trigger ID
  399:  * @opt_data = Optional data
  400:  * @opt_dlen = Optional user's trigger flags
  401:  * return: NULL error or !=NULL new queued task
  402:  */
  403: sched_task_t *schedUser(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  404: 		unsigned long id, void *opt_data, size_t opt_dlen);
  405: #define schedUserSelf(x)	schedUser(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  406: 		TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  407: /*
  408:  * schedTrigger() - Triggering USER task
  409:  *
  410:  * @task = task
  411:  * return: -1 error or 0 ok
  412:  */
  413: int schedTrigger(sched_task_t * __restrict task);
  414: 
  415: /*
  416:  * schedTimer() - Add TIMER task to scheduler queue
  417:  *
  418:  * @root = root task
  419:  * @func = task execution function
  420:  * @arg = 1st func argument
  421:  * @ts = timeout argument structure
  422:  * @opt_data = Optional data
  423:  * @opt_dlen = Optional data length
  424:  * return: NULL error or !=NULL new queued task
  425:  */
  426: sched_task_t *schedTimer(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  427: 		struct timespec ts, void *opt_data, size_t opt_dlen);
  428: #define schedTimerSelf(x)	schedTimer(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  429: 		TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  430: /*
  431:  * schedEvent() - Add EVENT task to scheduler queue
  432:  *
  433:  * @root = root task
  434:  * @func = task execution function
  435:  * @arg = 1st func argument
  436:  * @val = additional func argument
  437:  * @opt_data = Optional data
  438:  * @opt_dlen = Optional data length
  439:  * return: NULL error or !=NULL new queued task
  440:  */
  441: sched_task_t *schedEvent(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  442: 		unsigned long val, void *opt_data, size_t opt_dlen);
  443: #define schedEventSelf(x)	schedEvent(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  444: 		TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  445: /*
  446:  * schedEventLo() - Add EVENT_Lo task to scheduler queue
  447:  *
  448:  * @root = root task
  449:  * @func = task execution function
  450:  * @arg = 1st func argument
  451:  * @val = additional func argument
  452:  * @opt_data = Optional data
  453:  * @opt_dlen = Optional data length
  454:  * return: NULL error or !=NULL new queued task
  455:  */
  456: sched_task_t *schedEventLo(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  457: 		unsigned long val, void *opt_data, size_t opt_dlen);
  458: #define schedEventLoSelf(x)	schedEventLo(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  459: 		TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  460: 
  461: /*
  462:  * schedCallOnce() - Call once from scheduler
  463:  *
  464:  * @root = root task
  465:  * @func = task execution function
  466:  * @arg = 1st func argument
  467:  * @val = additional func argument
  468:  * @opt_data = Optional data
  469:  * @opt_dlen = Optional data length
  470:  * return: return value from called func
  471:  */
  472: sched_task_t *schedCallOnce(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
  473: 		unsigned long val, void *opt_data, size_t opt_dlen);
  474: #define schedCallAgain(x)	schedCallOnce(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
  475: 		TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
  476: 
  477: 
  478: #endif

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