Annotation of libaitsched/inc/aitsched.h, revision 1.4.2.5

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

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