File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / inc / aitsched.h
Revision 1.1: download - view: text, annotated - select for diffs - revision graph
Fri Aug 5 15:52:00 2011 UTC (12 years, 11 months ago) by misho
Branches: MAIN
CVS tags: HEAD
Initial revision

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

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