File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / inc / aitsched.h
Revision 1.4.2.1: download - view: text, annotated - select for diffs - revision graph
Sun Jan 8 02:01:41 2012 UTC (12 years, 6 months ago) by misho
Branches: sched1_3
Diff to: branchpoint 1.4: preferred, unified
add mutexes init/destroy

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

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