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

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.8.2.2 ! misho       6: * $Id: aitsched.h,v 1.8.2.1 2012/05/30 08:07:44 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: 
1.6       misho      15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.1       misho      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.5       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
1.8       misho      62: #define CRITERIA_TS    4
1.1       misho      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,
1.8.2.1   misho      73:        taskEVENT,
                     74:        taskEVENTLO,
                     75:        taskALARM,
1.1       misho      76:        taskREADY,
                     77:        taskUNUSE,
                     78:        taskMAX
                     79: } sched_task_type_t;
                     80: 
                     81: /* hooks */
                     82: typedef void *(*sched_hook_func_t)(void *, void *);
                     83: struct sched_HooksTask {
                     84:        struct {
                     85:                /* read(sched_task_t *task, NULL) -> int */
                     86:                sched_hook_func_t       read;
                     87:                /* write(sched_task_t *task, NULL) -> int */
                     88:                sched_hook_func_t       write;
                     89:                /* event(sched_task_t *task, NULL) -> int */
                     90:                sched_hook_func_t       event;
                     91:                /* eventlo(sched_task_t *task, NULL) -> int */
                     92:                sched_hook_func_t       eventlo;
1.8.2.1   misho      93:                /* alarm(sched_task_t *task, NULL) -> int */
                     94:                sched_hook_func_t       alarm;
1.5       misho      95:                /* timer(sched_task_t *task, struct timespec *ts) -> int */
1.1       misho      96:                sched_hook_func_t       timer;
                     97:        }       hook_add;
                     98:        struct {
                     99:                /* cancel(sched_task_t *task, NULL) -> int */
                    100:                sched_hook_func_t       cancel;
                    101:                /* run(sched_root_task_t *root, NULL) -> int */
                    102:                sched_hook_func_t       run;
                    103:                /* fetch(sched_root_task_t *root, NULL) -> sched_task_t* */
                    104:                sched_hook_func_t       fetch;
1.3       misho     105:                /* exception(sched_root_task_t *root, NULL) -> int */
                    106:                sched_hook_func_t       exception;
1.6       misho     107:                /* condition(sched_root_task_t *root, intptr_t *stopValue) -> int */
                    108:                sched_hook_func_t       condition;
1.1       misho     109:        }       hook_exec;
                    110:        struct {
                    111:                /* init(sched_root_task_t *root, void *data) -> int */
                    112:                sched_hook_func_t       init;
                    113:                /* fini(sched_root_task_t *root, NULL) -> int */
                    114:                sched_hook_func_t       fini;
1.3       misho     115:                /* error(sched_root_task_t *root, int errno) -> int */
                    116:                sched_hook_func_t       error;
1.1       misho     117:        }       hook_root;
                    118: };
                    119: typedef struct sched_HooksTask hooks_task_t;
                    120: 
                    121: /* task callback, like pthread callback! */
1.2       misho     122: typedef void *(*sched_task_func_t)(sched_task_t * /* current task data*/);
1.1       misho     123: 
1.4       misho     124: /* task lock helpers */
                    125: #define TASK_LOCK(x)           ((x)->task_lock++)
                    126: #define TASK_UNLOCK(x)         ((x)->task_lock ^= (x)->task_lock)
                    127: #define TASK_ISLOCKED(x)       ((x)->task_lock)
                    128: 
1.1       misho     129: /* task & queue */
                    130: struct sched_Task {
1.4       misho     131:        volatile int                    task_lock;
1.1       misho     132:        unsigned int                    task_id;
                    133:        sched_task_type_t               task_type;
1.5       misho     134: #define TASK_TYPE(x)   (x)->task_type
1.1       misho     135: 
                    136:        sched_root_task_t               *task_root;
1.2       misho     137: #define TASK_ROOT(x)   (x)->task_root
1.1       misho     138:        sched_task_func_t               task_func;
1.8       misho     139: #define TASK_FUNC(x)   (x)->task_func
1.1       misho     140: 
                    141:        void                            *task_arg;
                    142:        union {
                    143:                unsigned long   v;
1.3       misho     144:                intptr_t        fd;
1.5       misho     145:                struct timespec ts;
1.1       misho     146:        }                               task_val;
                    147: #define TASK_ARG(x)    (x)->task_arg
                    148: #define TASK_VAL(x)    (x)->task_val.v
                    149: #define TASK_FD(x)     (x)->task_val.fd
1.5       misho     150: #define TASK_TS(x)     (x)->task_val.ts
1.1       misho     151: 
                    152:        struct iovec                    task_data;
                    153: #define TASK_DATA(x)   (x)->task_data.iov_base
                    154: #define TASK_DATLEN(x) (x)->task_data.iov_len
                    155: 
                    156:        TAILQ_ENTRY(sched_Task)         task_node;
                    157: };
                    158: typedef TAILQ_HEAD(, sched_Task) sched_queue_t;
1.5       misho     159: #define TASK_DATA_SET(x, _dp, _dl)     do { \
                    160:                                                if ((x)) { \
                    161:                                                        (x)->task_data.iov_base = (_dp); \
                    162:                                                        (x)->task_data.iov_len = _dl; \
                    163:                                                } \
                    164:                                        while (0)
1.1       misho     165: 
                    166: /* root task */
                    167: struct sched_RootTask {
                    168:        int             root_kq;
1.5       misho     169:        struct timespec root_wait;
                    170:        struct timespec root_poll;
1.6       misho     171:        intptr_t        root_cond;
                    172: 
1.5       misho     173:        pthread_mutex_t root_mtx[taskMAX];
1.1       misho     174: 
                    175:        sched_queue_t   root_read;
                    176:        sched_queue_t   root_write;
                    177:        sched_queue_t   root_timer;
1.8.2.1   misho     178:        sched_queue_t   root_alarm;
1.1       misho     179:        sched_queue_t   root_event;
                    180:        sched_queue_t   root_ready;
                    181:        sched_queue_t   root_unuse;
                    182:        sched_queue_t   root_eventlo;
                    183:        int             root_eventlo_miss;
                    184: 
                    185:        hooks_task_t    root_hooks;
                    186:        struct iovec    root_data;
                    187: #define ROOT_DATA(x)   (x)->root_data.iov_base
                    188: #define ROOT_DATLEN(x) (x)->root_data.iov_len
                    189: };
1.7       misho     190: #define ROOT_QUEUE_EMPTY(x, _q)        TAILQ_EMPTY(&((x)->root_##_q))
1.1       misho     191: 
                    192: 
1.2       misho     193: inline int sched_GetErrno();
                    194: inline const char *sched_GetError();
                    195: 
                    196: 
1.1       misho     197: /*
                    198:  * schedInit() - Init scheduler
1.6       misho     199:  *
1.1       misho     200:  * @data = optional data if !=NULL
                    201:  * @datlen = data len if data is set
                    202:  * return: allocated root task if ok or NULL error
                    203:  */
                    204: sched_root_task_t *schedInit(void ** __restrict data, size_t datlen);
1.2       misho     205: #define schedBegin()   schedInit((void**) &schedRegisterHooks, 0)
1.1       misho     206: /*
                    207:  * schedEnd() - End scheduler & free all resources
1.6       misho     208:  *
1.1       misho     209:  * @root = root task
                    210:  * return: -1 error or 0 ok
                    211:  */
1.2       misho     212: int schedEnd(sched_root_task_t ** __restrict root);
1.1       misho     213: /*
1.2       misho     214:  * schedRegisterHooks() - Register IO handles and bind tasks to it
1.6       misho     215:  *
1.1       misho     216:  * @root = root task
                    217:  * return: -1 error or 0 ok
                    218:  */
1.2       misho     219: int schedRegisterHooks(sched_root_task_t * __restrict root);
1.1       misho     220: /*
1.5       misho     221:  * schedPolling() - Polling timeout period if no timer task is present
1.6       misho     222:  *
1.5       misho     223:  * @root = root task
                    224:  * @ts = timeout polling period, if ==NULL INFINIT timeout
                    225:  * @tsold = old timeout polling if !=NULL
                    226:  * return: -1 error or 0 ok
                    227:  */
                    228: inline int schedPolling(sched_root_task_t * __restrict root, 
                    229:                struct timespec * __restrict ts, struct timespec * __restrict tsold);
                    230: /*
1.6       misho     231:  * schedTermCondition() - Activate hook for scheduler condition kill
                    232:  *
                    233:  * @root = root task
                    234:  * @condValue = condition value, kill schedRun() if condValue == killState
                    235:  * return: -1 error ok 0 ok
                    236:  */
                    237: inline int schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue);
                    238: /*
1.1       misho     239:  * schedCall() - Call task execution function
1.6       misho     240:  *
1.1       misho     241:  * @task = current task
                    242:  * return: !=NULL error or =NULL ok
                    243:  */
                    244: inline void *schedCall(sched_task_t * __restrict task);
                    245: /*
                    246:  * schedFetch() - Fetch ready task
1.6       misho     247:  *
1.1       misho     248:  * @root = root task
                    249:  * return: =NULL error or !=NULL ready task
                    250:  */
                    251: inline void *schedFetch(sched_root_task_t * __restrict root);
                    252: /*
                    253:  * schedRun() - Scheduler *run loop*
1.6       misho     254:  *
1.1       misho     255:  * @root = root task
1.2       misho     256:  * @killState = kill condition variable, if !=0 stop scheduler loop
1.1       misho     257:  * return: -1 error or 0 ok
                    258:  */
1.7       misho     259: int schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState);
1.1       misho     260: /*
                    261:  * schedCancel() - Cancel task from scheduler
1.6       misho     262:  *
1.1       misho     263:  * @task = task
                    264:  * return: -1 error or 0 ok
                    265:  */
                    266: int schedCancel(sched_task_t * __restrict task);
                    267: /*
                    268:  * schedCancelby() - Cancel task from scheduler by criteria
1.6       misho     269:  *
1.1       misho     270:  * @root = root task
1.5       misho     271:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.8       misho     272:  * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TS]
1.1       misho     273:  * @param = search parameter
                    274:  * @hook = custom cleanup hook function, may be NULL
1.3       misho     275:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1       misho     276:  */
1.5       misho     277: int schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
1.1       misho     278:                u_char criteria, void *param, sched_hook_func_t hook);
                    279: 
                    280: 
                    281: /*
1.2       misho     282:  * schedRead() - Add READ I/O task to scheduler queue
1.6       misho     283:  *
1.1       misho     284:  * @root = root task
                    285:  * @func = task execution function
                    286:  * @arg = 1st func argument
1.2       misho     287:  * @fd = fd handle
1.5       misho     288:  * @opt_data = Optional data
                    289:  * @opt_dlen = Optional data length
1.1       misho     290:  * return: NULL error or !=NULL new queued task
                    291:  */
1.5       misho     292: sched_task_t *schedRead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    293:                int fd, void *opt_data, size_t opt_dlen);
1.8       misho     294: #define schedReadSelf(x)       schedRead(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    295:                TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1       misho     296: /*
1.2       misho     297:  * schedWrite() - Add WRITE I/O task to scheduler queue
1.6       misho     298:  *
1.1       misho     299:  * @root = root task
                    300:  * @func = task execution function
                    301:  * @arg = 1st func argument
1.2       misho     302:  * @fd = fd handle
1.5       misho     303:  * @opt_data = Optional data
                    304:  * @opt_dlen = Optional data length
1.1       misho     305:  * return: NULL error or !=NULL new queued task
                    306:  */
1.5       misho     307: sched_task_t *schedWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    308:                int fd, void *opt_data, size_t opt_dlen);
1.8       misho     309: #define schedWriteSelf(x)      schedWrite(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    310:                TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1       misho     311: /*
1.8.2.1   misho     312:  * schedAlarm() - Add ALARM task to scheduler queue
                    313:  *
                    314:  * @root = root task
                    315:  * @func = task execution function
                    316:  * @arg = 1st func argument
1.8.2.2 ! misho     317:  * @ts = timeout argument structure, minimum alarm timer resolution is 1msec!
1.8.2.1   misho     318:  * @opt_data = Optional data
                    319:  * @opt_dlen = Optional data length
                    320:  * return: NULL error or !=NULL new queued task
                    321:  */
                    322: sched_task_t *schedAlarm(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    323:                struct timespec ts, void *opt_data, size_t opt_dlen);
                    324: #define schedAlarmSelf(x)      schedAlarm(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    325:                TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
                    326: /*
1.1       misho     327:  * schedTimer() - Add TIMER task to scheduler queue
1.6       misho     328:  *
1.1       misho     329:  * @root = root task
                    330:  * @func = task execution function
                    331:  * @arg = 1st func argument
1.5       misho     332:  * @ts = timeout argument structure
                    333:  * @opt_data = Optional data
                    334:  * @opt_dlen = Optional data length
1.1       misho     335:  * return: NULL error or !=NULL new queued task
                    336:  */
1.5       misho     337: sched_task_t *schedTimer(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    338:                struct timespec ts, void *opt_data, size_t opt_dlen);
1.8       misho     339: #define schedTimerSelf(x)      schedTimer(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    340:                TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1       misho     341: /*
                    342:  * schedEvent() - Add EVENT task to scheduler queue
1.6       misho     343:  *
1.1       misho     344:  * @root = root task
                    345:  * @func = task execution function
                    346:  * @arg = 1st func argument
1.2       misho     347:  * @val = additional func argument
1.5       misho     348:  * @opt_data = Optional data
                    349:  * @opt_dlen = Optional data length
1.1       misho     350:  * return: NULL error or !=NULL new queued task
                    351:  */
1.5       misho     352: sched_task_t *schedEvent(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    353:                unsigned long val, void *opt_data, size_t opt_dlen);
1.8       misho     354: #define schedEventSelf(x)      schedEvent(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    355:                TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1       misho     356: /*
                    357:  * schedEventLo() - Add EVENT_Lo task to scheduler queue
1.6       misho     358:  *
1.1       misho     359:  * @root = root task
                    360:  * @func = task execution function
                    361:  * @arg = 1st func argument
1.2       misho     362:  * @val = additional func argument
1.5       misho     363:  * @opt_data = Optional data
                    364:  * @opt_dlen = Optional data length
1.1       misho     365:  * return: NULL error or !=NULL new queued task
                    366:  */
1.5       misho     367: sched_task_t *schedEventLo(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    368:                unsigned long val, void *opt_data, size_t opt_dlen);
1.8       misho     369: #define schedEventLoSelf(x)    schedEventLo(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    370:                TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1       misho     371: /*
                    372:  * schedCallOnce() - Call once from scheduler
1.6       misho     373:  *
1.1       misho     374:  * @root = root task
                    375:  * @func = task execution function
                    376:  * @arg = 1st func argument
1.2       misho     377:  * @val = additional func argument
1.5       misho     378:  * @opt_data = Optional data
                    379:  * @opt_dlen = Optional data length
1.2       misho     380:  * return: return value from called func
1.1       misho     381:  */
1.5       misho     382: sched_task_t *schedCallOnce(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    383:                unsigned long val, void *opt_data, size_t opt_dlen);
1.8       misho     384: #define schedCallAgain(x)      schedCallOnce(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    385:                TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1       misho     386: 
                    387: 
                    388: #endif

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