File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / inc / aitsched.h
Revision 1.2: download - view: text, annotated - select for diffs - revision graph
Tue Oct 4 12:34:33 2011 UTC (12 years, 9 months ago) by misho
Branches: MAIN
CVS tags: sched1_1, SCHED1_0, HEAD
ver 1.0

    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.2 2011/10/04 12:34: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, 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: #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: 
   54: 
   55: /* criteria type */
   56: #define CRITERIA_CALL	0
   57: #define CRITERIA_ARG	1
   58: #define CRITERIA_FD	2
   59: #define CRITERIA_VAL	3
   60: #define CRITERIA_TV	4
   61: 
   62: 
   63: /* early declaration for root & task */
   64: typedef struct sched_Task	sched_task_t;
   65: typedef struct sched_RootTask	sched_root_task_t;
   66: 
   67: typedef enum {
   68: 	taskREAD = 0,
   69: 	taskWRITE,
   70: 	taskTIMER,
   71: 	taskEVENT, 
   72: 	taskREADY,
   73: 	taskUNUSE,
   74: 	taskMAX
   75: } sched_task_type_t;
   76: 
   77: /* hooks */
   78: typedef void *(*sched_hook_func_t)(void *, void *);
   79: struct sched_HooksTask {
   80: 	struct {
   81: 		/* read(sched_task_t *task, NULL) -> int */
   82: 		sched_hook_func_t	read;
   83: 		/* write(sched_task_t *task, NULL) -> int */
   84: 		sched_hook_func_t	write;
   85: 		/* event(sched_task_t *task, NULL) -> int */
   86: 		sched_hook_func_t	event;
   87: 		/* eventlo(sched_task_t *task, NULL) -> int */
   88: 		sched_hook_func_t	eventlo;
   89: 		/* timer(sched_task_t *task, struct timeval *tv) -> int */
   90: 		sched_hook_func_t	timer;
   91: 	}	hook_add;
   92: 	struct {
   93: 		/* cancel(sched_task_t *task, NULL) -> int */
   94: 		sched_hook_func_t	cancel;
   95: 		/* run(sched_root_task_t *root, NULL) -> int */
   96: 		sched_hook_func_t	run;
   97: 		/* fetch(sched_root_task_t *root, NULL) -> sched_task_t* */
   98: 		sched_hook_func_t	fetch;
   99: 	}	hook_exec;
  100: 	struct {
  101: 		/* init(sched_root_task_t *root, void *data) -> int */
  102: 		sched_hook_func_t	init;
  103: 		/* fini(sched_root_task_t *root, NULL) -> int */
  104: 		sched_hook_func_t	fini;
  105: 	}	hook_root;
  106: };
  107: typedef struct sched_HooksTask hooks_task_t;
  108: 
  109: /* task callback, like pthread callback! */
  110: typedef void *(*sched_task_func_t)(sched_task_t * /* current task data*/);
  111: 
  112: /* task & queue */
  113: struct sched_Task {
  114: 	unsigned int			task_id;
  115: 	sched_task_type_t		task_type;
  116: 
  117: 	sched_root_task_t		*task_root;
  118: #define TASK_ROOT(x)	(x)->task_root
  119: 	sched_task_func_t		task_func;
  120: 
  121: 	void				*task_arg;
  122: 	union {
  123: 		unsigned long	v;
  124: 		int		fd;
  125: 		struct timeval	tv;
  126: 	}				task_val;
  127: #define TASK_ARG(x)	(x)->task_arg
  128: #define TASK_VAL(x)	(x)->task_val.v
  129: #define TASK_FD(x)	(x)->task_val.fd
  130: #define TASK_TV(x)	(x)->task_val.tv
  131: 
  132: 	struct iovec			task_data;
  133: #define TASK_DATA(x)	(x)->task_data.iov_base
  134: #define TASK_DATLEN(x)	(x)->task_data.iov_len
  135: 
  136: 	TAILQ_ENTRY(sched_Task)		task_node;
  137: };
  138: typedef TAILQ_HEAD(, sched_Task) sched_queue_t;
  139: 
  140: /* root task */
  141: struct sched_RootTask {
  142: 	int		root_kq;
  143: 	struct timeval	root_wait;
  144: 
  145: 	sched_queue_t	root_read;
  146: 	sched_queue_t	root_write;
  147: 	sched_queue_t	root_timer;
  148: 	sched_queue_t	root_event;
  149: 	sched_queue_t	root_ready;
  150: 	sched_queue_t	root_unuse;
  151: 	sched_queue_t	root_eventlo;
  152: 	int		root_eventlo_miss;
  153: 
  154: 	hooks_task_t	root_hooks;
  155: 	struct iovec	root_data;
  156: #define ROOT_DATA(x)	(x)->root_data.iov_base
  157: #define ROOT_DATLEN(x)	(x)->root_data.iov_len
  158: };
  159: 
  160: 
  161: inline int sched_GetErrno();
  162: inline const char *sched_GetError();
  163: inline void sched_SetErr(int, char *, ...);
  164: 
  165: 
  166: /*
  167:  * schedInit() - Init scheduler
  168:  * @data = optional data if !=NULL
  169:  * @datlen = data len if data is set
  170:  * return: allocated root task if ok or NULL error
  171:  */
  172: sched_root_task_t *schedInit(void ** __restrict data, size_t datlen);
  173: #define schedBegin()	schedInit((void**) &schedRegisterHooks, 0)
  174: /*
  175:  * schedEnd() - End scheduler & free all resources
  176:  * @root = root task
  177:  * return: -1 error or 0 ok
  178:  */
  179: int schedEnd(sched_root_task_t ** __restrict root);
  180: /*
  181:  * schedRegisterHooks() - Register IO handles and bind tasks to it
  182:  * @root = root task
  183:  * return: -1 error or 0 ok
  184:  */
  185: int schedRegisterHooks(sched_root_task_t * __restrict root);
  186: /*
  187:  * schedCall() - Call task execution function
  188:  * @task = current task
  189:  * return: !=NULL error or =NULL ok
  190:  */
  191: inline void *schedCall(sched_task_t * __restrict task);
  192: /*
  193:  * schedFetch() - Fetch ready task
  194:  * @root = root task
  195:  * return: =NULL error or !=NULL ready task
  196:  */
  197: inline void *schedFetch(sched_root_task_t * __restrict root);
  198: /*
  199:  * schedRun() - Scheduler *run loop*
  200:  * @root = root task
  201:  * @killState = kill condition variable, if !=0 stop scheduler loop
  202:  * return: -1 error or 0 ok
  203:  */
  204: int schedRun(sched_root_task_t * __restrict root, volatile intptr_t * __restrict killState);
  205: /*
  206:  * schedCancel() - Cancel task from scheduler
  207:  * @task = task
  208:  * return: -1 error or 0 ok
  209:  */
  210: int schedCancel(sched_task_t * __restrict task);
  211: /*
  212:  * schedCancelby() - Cancel task from scheduler by criteria
  213:  * @root = root task
  214:  * @queue = cancel from queue, if =NULL cancel same task from all queues
  215:  * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
  216:  * @param = search parameter
  217:  * @hook = custom cleanup hook function, may be NULL
  218:  * return: -1 error or 0 ok
  219:  */
  220: int schedCancelby(sched_root_task_t * __restrict root, sched_queue_t * __restrict queue, 
  221: 		u_char criteria, void *param, sched_hook_func_t hook);
  222: 
  223: 
  224: /*
  225:  * schedRead() - Add READ I/O task to scheduler queue
  226:  * @root = root task
  227:  * @func = task execution function
  228:  * @arg = 1st func argument
  229:  * @fd = fd handle
  230:  * return: NULL error or !=NULL new queued task
  231:  */
  232: sched_task_t *schedRead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd);
  233: /*
  234:  * schedWrite() - Add WRITE I/O task to scheduler queue
  235:  * @root = root task
  236:  * @func = task execution function
  237:  * @arg = 1st func argument
  238:  * @fd = fd handle
  239:  * return: NULL error or !=NULL new queued task
  240:  */
  241: sched_task_t *schedWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, int fd);
  242: /*
  243:  * schedTimer() - Add TIMER task to scheduler queue
  244:  * @root = root task
  245:  * @func = task execution function
  246:  * @arg = 1st func argument
  247:  * @ms = arguments in microSecs, define period 1sec == 1000000
  248:  * return: NULL error or !=NULL new queued task
  249:  */
  250: sched_task_t *schedTimer(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, unsigned int ms);
  251: /*
  252:  * schedEvent() - Add EVENT task to scheduler queue
  253:  * @root = root task
  254:  * @func = task execution function
  255:  * @arg = 1st func argument
  256:  * @val = additional func argument
  257:  * return: NULL error or !=NULL new queued task
  258:  */
  259: sched_task_t *schedEvent(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, unsigned long val);
  260: /*
  261:  * schedEventLo() - Add EVENT_Lo task to scheduler queue
  262:  * @root = root task
  263:  * @func = task execution function
  264:  * @arg = 1st func argument
  265:  * @val = additional func argument
  266:  * return: NULL error or !=NULL new queued task
  267:  */
  268: sched_task_t *schedEventLo(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, unsigned long val);
  269: /*
  270:  * schedCallOnce() - Call once from scheduler
  271:  * @root = root task
  272:  * @func = task execution function
  273:  * @arg = 1st func argument
  274:  * @val = additional func argument
  275:  * return: return value from called func
  276:  */
  277: sched_task_t *schedCallOnce(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, unsigned long val);
  278: 
  279: 
  280: #endif

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