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

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.36    ! misho       6: * $Id: aitsched.h,v 1.35.2.3 2026/05/20 04:04:10 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.35      misho      15: Copyright 2004 - 2026
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>
1.25      misho      52: #include <sys/select.h>
1.2       misho      53: #include <sys/uio.h>
1.3       misho      54: #include <stdint.h>
1.5       misho      55: #include <pthread.h>
1.15      misho      56: #include <assert.h>
1.12      misho      57: #ifdef EVFILT_LIO
                     58: #include <aio.h>
                     59: #endif
1.36    ! misho      60: #ifdef ATOMIC_SUPPORT
        !            61: #ifndef __cplusplus
        !            62:         #include <stdatomic.h>
        !            63: #else
        !            64:         #include <atomic>
        !            65:         #define _Atomic(X) std::atomic<X>
        !            66: #endif
        !            67: #endif
1.2       misho      68: 
1.1       misho      69: /* criteria type */
1.10      misho      70: #define CRITERIA_ANY   0
                     71: #define CRITERIA_CALL  1
                     72: #define CRITERIA_ARG   2
                     73: #define CRITERIA_FD    3
                     74: #define CRITERIA_VAL   4
                     75: #define CRITERIA_TS    5
                     76: #define CRITERIA_DATA  6
1.24      misho      77: #define CRITERIA_DATLEN        7
                     78: #define CRITERIA_ID    8
1.1       misho      79: 
                     80: 
                     81: /* early declaration for root & task */
                     82: typedef struct sched_Task      sched_task_t;
                     83: typedef struct sched_RootTask  sched_root_task_t;
                     84: 
                     85: typedef enum {
                     86:        taskREAD = 0,
                     87:        taskWRITE,
                     88:        taskTIMER,
1.10      misho      89:        taskALARM,
                     90:        taskNODE,
                     91:        taskPROC,
1.12      misho      92:        taskSIGNAL,
                     93:        taskAIO,
                     94:        taskLIO,
1.10      misho      95:        taskUSER,
1.9       misho      96:        taskEVENT,
1.13      misho      97:        taskTASK,
1.11      misho      98:        taskSUSPEND, 
1.1       misho      99:        taskREADY,
                    100:        taskUNUSE,
1.15      misho     101:        taskTHREAD, 
1.20      misho     102:        taskRTC,
1.1       misho     103:        taskMAX
                    104: } sched_task_type_t;
                    105: 
                    106: /* hooks */
                    107: typedef void *(*sched_hook_func_t)(void *, void *);
                    108: struct sched_HooksTask {
                    109:        struct {
                    110:                /* read(sched_task_t *task, NULL) -> int */
                    111:                sched_hook_func_t       read;
                    112:                /* write(sched_task_t *task, NULL) -> int */
                    113:                sched_hook_func_t       write;
1.10      misho     114:                /* timer(sched_task_t *task, struct timespec *ts) -> int */
                    115:                sched_hook_func_t       timer;
                    116:                /* alarm(sched_task_t *task, NULL) -> int */
                    117:                sched_hook_func_t       alarm;
                    118:                /* node(sched_task_t *task, NULL) -> int */
                    119:                sched_hook_func_t       node;
                    120:                /* proc(sched_task_t *task, NULL) -> int */
                    121:                sched_hook_func_t       proc;
1.12      misho     122:                /* signal(sched_task_t *task, NULL) -> int */
                    123:                sched_hook_func_t       signal;
                    124:                /* aio(sched_task_t *task, NULL) -> int */
                    125:                sched_hook_func_t       aio;
                    126:                /* lio(sched_task_t *task, NULL) -> int */
                    127:                sched_hook_func_t       lio;
1.10      misho     128:                /* user(sched_task_t *task, NULL) -> int */
                    129:                sched_hook_func_t       user;
1.1       misho     130:                /* event(sched_task_t *task, NULL) -> int */
                    131:                sched_hook_func_t       event;
1.13      misho     132:                /* task(sched_task_t *task, NULL) -> int */
                    133:                sched_hook_func_t       task;
1.11      misho     134:                /* suspend(sched_task_t *task, NULL) -> int */
                    135:                sched_hook_func_t       suspend;
1.15      misho     136:                /* thread(sched_task_t *task, NULL) -> int */
                    137:                sched_hook_func_t       thread;
1.20      misho     138:                /* rtc(sched_task_t *task, NULL) -> int */
                    139:                sched_hook_func_t       rtc;
1.1       misho     140:        }       hook_add;
                    141:        struct {
1.15      misho     142:                /* exit(sched_task_t *task, void *exitValue) -> int */
                    143:                sched_hook_func_t       exit;
1.1       misho     144:                /* cancel(sched_task_t *task, NULL) -> int */
                    145:                sched_hook_func_t       cancel;
1.11      misho     146:                /* resume(sched_task_t *task, NULL) -> int */
                    147:                sched_hook_func_t       resume;
1.1       misho     148:                /* run(sched_root_task_t *root, NULL) -> int */
                    149:                sched_hook_func_t       run;
                    150:                /* fetch(sched_root_task_t *root, NULL) -> sched_task_t* */
                    151:                sched_hook_func_t       fetch;
1.3       misho     152:                /* exception(sched_root_task_t *root, NULL) -> int */
                    153:                sched_hook_func_t       exception;
1.6       misho     154:                /* condition(sched_root_task_t *root, intptr_t *stopValue) -> int */
                    155:                sched_hook_func_t       condition;
1.35      misho     156:                /* profile(sched_task_t *root, intptr_t *stageValue) -> int */
                    157:                sched_hook_func_t       profile;
1.1       misho     158:        }       hook_exec;
                    159:        struct {
                    160:                /* init(sched_root_task_t *root, void *data) -> int */
                    161:                sched_hook_func_t       init;
                    162:                /* fini(sched_root_task_t *root, NULL) -> int */
                    163:                sched_hook_func_t       fini;
1.3       misho     164:                /* error(sched_root_task_t *root, int errno) -> int */
                    165:                sched_hook_func_t       error;
1.1       misho     166:        }       hook_root;
                    167: };
                    168: typedef struct sched_HooksTask hooks_task_t;
                    169: 
                    170: /* task callback, like pthread callback! */
1.2       misho     171: typedef void *(*sched_task_func_t)(sched_task_t * /* current task data*/);
1.26      misho     172: #define SCHED_TASK_DEFINE(x)   void *(x)(sched_task_t*)
1.1       misho     173: 
1.4       misho     174: /* task lock helpers */
1.36    ! misho     175: #ifdef ATOMIC_SUPPORT
        !           176:        #define TASK_LOCK(x)            atomic_store_explicit(&(x)->task_lock, 42, memory_order_release)
        !           177:        #define TASK_UNLOCK(x)          atomic_store_explicit(&(x)->task_lock, 0, memory_order_release)
        !           178:        #define TASK_ISLOCKED(x)        atomic_load_explicit(&(x)->task_lock, memory_order_acquire)
        !           179: #else
        !           180:        #define TASK_LOCK(x)            ((x)->task_lock = 42)
        !           181:        #define TASK_UNLOCK(x)          ((x)->task_lock ^= (x)->task_lock)
        !           182:        #define TASK_ISLOCKED(x)        ((x)->task_lock)
        !           183: #endif
1.4       misho     184: 
1.1       misho     185: /* task & queue */
                    186: struct sched_Task {
1.10      misho     187:        uintptr_t                       task_id;
                    188: #define TASK_ID(x)     ((struct sched_Task*) (x)->task_id)
1.1       misho     189:        sched_task_type_t               task_type;
1.5       misho     190: #define TASK_TYPE(x)   (x)->task_type
1.36    ! misho     191: #ifdef ATOMIC_SUPPORT
        !           192:        atomic_int                      task_lock;
        !           193: #else
1.15      misho     194:        volatile int                    task_lock;
1.36    ! misho     195: #endif
1.1       misho     196: 
                    197:        sched_root_task_t               *task_root;
1.2       misho     198: #define TASK_ROOT(x)   (x)->task_root
1.1       misho     199:        sched_task_func_t               task_func;
1.8       misho     200: #define TASK_FUNC(x)   (x)->task_func
1.15      misho     201:        intptr_t                        task_ret;
                    202: #define TASK_RET(x)    (x)->task_ret
1.20      misho     203:        unsigned long                   task_flag;
1.15      misho     204: #define TASK_FLAG(x)   (x)->task_flag
1.1       misho     205: 
                    206:        void                            *task_arg;
                    207:        union {
                    208:                unsigned long   v;
1.3       misho     209:                intptr_t        fd;
1.5       misho     210:                struct timespec ts;
1.1       misho     211:        }                               task_val;
                    212: #define TASK_ARG(x)    (x)->task_arg
                    213: #define TASK_VAL(x)    (x)->task_val.v
                    214: #define TASK_FD(x)     (x)->task_val.fd
1.5       misho     215: #define TASK_TS(x)     (x)->task_val.ts
1.1       misho     216: 
1.25      misho     217: #define TASK_TS2TV(x, tvp)     (assert((tvp)), (tvp)->tv_sec = (x)->task_val.ts.tv_sec, \
                    218:                                        (tvp)->tv_usec = (x)->task_val.ts.tv_nsec / 1000)
                    219: 
1.1       misho     220:        struct iovec                    task_data;
                    221: #define TASK_DATA(x)   (x)->task_data.iov_base
                    222: #define TASK_DATLEN(x) (x)->task_data.iov_len
                    223: 
1.30      misho     224:        unsigned long                   task_harg;
                    225: #define TASK_HARG(x)   (x)->task_harg
                    226: 
1.1       misho     227:        TAILQ_ENTRY(sched_Task)         task_node;
                    228: };
                    229: typedef TAILQ_HEAD(, sched_Task) sched_queue_t;
1.5       misho     230: #define TASK_DATA_SET(x, _dp, _dl)     do { \
                    231:                                                if ((x)) { \
                    232:                                                        (x)->task_data.iov_base = (_dp); \
                    233:                                                        (x)->task_data.iov_len = _dl; \
                    234:                                                } \
                    235:                                        while (0)
1.1       misho     236: 
                    237: /* root task */
                    238: struct sched_RootTask {
                    239:        int             root_kq;
1.30      misho     240:        fd_set          root_fds[3];
1.5       misho     241:        struct timespec root_wait;
                    242:        struct timespec root_poll;
1.25      misho     243:        unsigned long   root_miss;
1.27      misho     244:        intptr_t        root_cond[1];
1.12      misho     245:        void            *root_ret;
1.32      misho     246: #ifdef HAVE_LIBPTHREAD
                    247:        pthread_mutex_t root_sigmtx;
                    248:        pthread_t       root_sigthr;
                    249:        sigset_t        root_sigset;
                    250:        sigset_t        root_oldset;
                    251: #endif
1.6       misho     252: 
1.5       misho     253:        pthread_mutex_t root_mtx[taskMAX];
1.1       misho     254: 
                    255:        sched_queue_t   root_read;
                    256:        sched_queue_t   root_write;
                    257:        sched_queue_t   root_timer;
1.9       misho     258:        sched_queue_t   root_alarm;
1.10      misho     259:        sched_queue_t   root_node;
                    260:        sched_queue_t   root_proc;
1.12      misho     261:        sched_queue_t   root_signal;
                    262:        sched_queue_t   root_aio;
                    263:        sched_queue_t   root_lio;
1.10      misho     264:        sched_queue_t   root_user;
1.1       misho     265:        sched_queue_t   root_event;
1.13      misho     266:        sched_queue_t   root_task;
1.11      misho     267:        sched_queue_t   root_suspend;
1.1       misho     268:        sched_queue_t   root_ready;
                    269:        sched_queue_t   root_unuse;
1.15      misho     270:        sched_queue_t   root_thread;
1.20      misho     271:        sched_queue_t   root_rtc;
1.1       misho     272: 
                    273:        hooks_task_t    root_hooks;
                    274:        struct iovec    root_data;
                    275: #define ROOT_DATA(x)   (x)->root_data.iov_base
                    276: #define ROOT_DATLEN(x) (x)->root_data.iov_len
                    277: };
1.7       misho     278: #define ROOT_QUEUE_EMPTY(x, _q)        TAILQ_EMPTY(&((x)->root_##_q))
1.12      misho     279: #define ROOT_RETURN(x) (x)->root_ret
1.35      misho     280: #define ROOT_PROFILING(x, _cb) (x)->root_hooks.hook_exec.profile = (_cb)
1.1       misho     281: 
1.34      misho     282: #ifdef __cplusplus
                    283: extern "C" {
                    284: #endif
1.1       misho     285: 
1.18      misho     286: int sched_GetErrno();
                    287: const char *sched_GetError();
1.2       misho     288: 
                    289: 
1.1       misho     290: /*
                    291:  * schedInit() - Init scheduler
1.6       misho     292:  *
1.1       misho     293:  * @data = optional data if !=NULL
                    294:  * @datlen = data len if data is set
                    295:  * return: allocated root task if ok or NULL error
                    296:  */
                    297: sched_root_task_t *schedInit(void ** __restrict data, size_t datlen);
1.2       misho     298: #define schedBegin()   schedInit((void**) &schedRegisterHooks, 0)
1.1       misho     299: /*
                    300:  * schedEnd() - End scheduler & free all resources
1.6       misho     301:  *
1.1       misho     302:  * @root = root task
                    303:  * return: -1 error or 0 ok
                    304:  */
1.2       misho     305: int schedEnd(sched_root_task_t ** __restrict root);
1.1       misho     306: /*
1.2       misho     307:  * schedRegisterHooks() - Register IO handles and bind tasks to it
1.6       misho     308:  *
1.1       misho     309:  * @root = root task
                    310:  * return: -1 error or 0 ok
                    311:  */
1.2       misho     312: int schedRegisterHooks(sched_root_task_t * __restrict root);
1.1       misho     313: /*
1.15      misho     314:  * sched_useTask() - Get and init new task
                    315:  *
                    316:  * @root = root task
                    317:  * return: NULL error or !=NULL prepared task
                    318:  */
1.18      misho     319: sched_task_t *sched_useTask(sched_root_task_t * __restrict root);
1.15      misho     320: /*
                    321:  * sched_unuseTask() - Unlock and put task to unuse queue
                    322:  *
                    323:  * @task = task
                    324:  * return: always is NULL
                    325:  */
1.18      misho     326: sched_task_t *sched_unuseTask(sched_task_t * __restrict task);
1.15      misho     327: /*
1.5       misho     328:  * schedPolling() - Polling timeout period if no timer task is present
1.6       misho     329:  *
1.5       misho     330:  * @root = root task
                    331:  * @ts = timeout polling period, if ==NULL INFINIT timeout
                    332:  * @tsold = old timeout polling if !=NULL
                    333:  * return: -1 error or 0 ok
                    334:  */
1.18      misho     335: int schedPolling(sched_root_task_t * __restrict root, 
1.5       misho     336:                struct timespec * __restrict ts, struct timespec * __restrict tsold);
                    337: /*
1.6       misho     338:  * schedTermCondition() - Activate hook for scheduler condition kill
                    339:  *
                    340:  * @root = root task
                    341:  * @condValue = condition value, kill schedRun() if condValue == killState
1.13      misho     342:  * return: -1 error or 0 ok
1.6       misho     343:  */
1.27      misho     344: int schedTermCondition(sched_root_task_t * __restrict root, intptr_t * __restrict condValue);
1.6       misho     345: /*
1.1       misho     346:  * schedCall() - Call task execution function
1.6       misho     347:  *
1.1       misho     348:  * @task = current task
                    349:  * return: !=NULL error or =NULL ok
                    350:  */
1.18      misho     351: void *schedCall(sched_task_t * __restrict task);
1.1       misho     352: /*
                    353:  * schedFetch() - Fetch ready task
1.6       misho     354:  *
1.1       misho     355:  * @root = root task
                    356:  * return: =NULL error or !=NULL ready task
                    357:  */
1.18      misho     358: void *schedFetch(sched_root_task_t * __restrict root);
1.1       misho     359: /*
                    360:  * schedRun() - Scheduler *run loop*
1.6       misho     361:  *
1.1       misho     362:  * @root = root task
1.2       misho     363:  * @killState = kill condition variable, if !=0 stop scheduler loop
1.1       misho     364:  * return: -1 error or 0 ok
                    365:  */
1.7       misho     366: int schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState);
1.1       misho     367: /*
                    368:  * schedCancel() - Cancel task from scheduler
1.6       misho     369:  *
1.1       misho     370:  * @task = task
                    371:  * return: -1 error or 0 ok
                    372:  */
                    373: int schedCancel(sched_task_t * __restrict task);
                    374: /*
                    375:  * schedCancelby() - Cancel task from scheduler by criteria
1.6       misho     376:  *
1.1       misho     377:  * @root = root task
1.5       misho     378:  * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.10      misho     379:  * @criteria = find task by criteria 
1.23      misho     380:  *     [ CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|
1.24      misho     381:  *             CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA|CRITERIA_DATLEN ]
1.1       misho     382:  * @param = search parameter
                    383:  * @hook = custom cleanup hook function, may be NULL
1.3       misho     384:  * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1       misho     385:  */
1.5       misho     386: int schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type, 
1.23      misho     387:                unsigned char criteria, void *param, sched_hook_func_t hook);
                    388: /*
                    389:  * schedQuery() - Query task in scheduler
                    390:  *
                    391:  * @task = task
                    392:  * return: -1 error, 0 found  and 1 not found
                    393:  */
                    394: int schedQuery(sched_task_t * __restrict task);
                    395: /*
                    396:  * schedQueryby() - Query task in scheduler by criteria
                    397:  *
                    398:  * @root = root task
                    399:  * @type = query from queue type, if =taskMAX query same task from all queues
                    400:  * @criteria = find task by criteria 
                    401:  *     [ CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|
1.24      misho     402:  *             CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA|CRITERIA_DATLEN ]
1.23      misho     403:  * @param = search parameter
1.31      misho     404:  * return: NULL not found or !=NULL task
1.23      misho     405:  */
1.31      misho     406: sched_task_t *schedQueryby(sched_root_task_t * __restrict root, sched_task_type_t type, 
1.23      misho     407:                unsigned char criteria, void *param);
1.32      misho     408: /*
                    409:  * schedSignalDispatch() - Activate or Deactivate signal dispatcher
                    410:  *
                    411:  * @root = root task
                    412:  * @on = Activate or =0 deactivate
1.33      misho     413:  * return: -1 error, 1 already started, 2 another thread already started or 0 ok
1.32      misho     414:  */
                    415: int schedSignalDispatch(sched_root_task_t * __restrict root, int on);
1.1       misho     416: 
                    417: 
                    418: /*
1.30      misho     419:  * schedReadExt() - Add READ I/O task to scheduler queue with custom event mask
                    420:  *
                    421:  * @root = root task
                    422:  * @func = task execution function
                    423:  * @arg = 1st func argument
                    424:  * @fd = fd handle
                    425:  * @opt_data = Optional data
                    426:  * @opt_dlen = Optional data length
                    427:  * @mask = Event mask
                    428:  * return: NULL error or !=NULL new queued task
                    429:  */
                    430: sched_task_t *schedReadExt(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    431:                int fd, void *opt_data, size_t opt_dlen, unsigned long mask);
                    432: /*
1.2       misho     433:  * schedRead() - Add READ I/O task to scheduler queue
1.6       misho     434:  *
1.1       misho     435:  * @root = root task
                    436:  * @func = task execution function
                    437:  * @arg = 1st func argument
1.2       misho     438:  * @fd = fd handle
1.5       misho     439:  * @opt_data = Optional data
                    440:  * @opt_dlen = Optional data length
1.1       misho     441:  * return: NULL error or !=NULL new queued task
                    442:  */
1.5       misho     443: sched_task_t *schedRead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    444:                int fd, void *opt_data, size_t opt_dlen);
1.30      misho     445: #define schedReadSelf(x)       schedReadExt(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    446:                TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)), TASK_HARG((x)))
                    447: /*
                    448:  * schedWriteExt() - Add WRITE I/O task to scheduler queue with custom event mask
                    449:  *
                    450:  * @root = root task
                    451:  * @func = task execution function
                    452:  * @arg = 1st func argument
                    453:  * @fd = fd handle
                    454:  * @opt_data = Optional data
                    455:  * @opt_dlen = Optional data length
                    456:  * @mask = Event mask
                    457:  * return: NULL error or !=NULL new queued task
                    458:  */
                    459: sched_task_t *schedWriteExt(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    460:                int fd, void *opt_data, size_t opt_dlen, unsigned long mask);
1.1       misho     461: /*
1.2       misho     462:  * schedWrite() - Add WRITE I/O task to scheduler queue
1.6       misho     463:  *
1.1       misho     464:  * @root = root task
                    465:  * @func = task execution function
                    466:  * @arg = 1st func argument
1.2       misho     467:  * @fd = fd handle
1.5       misho     468:  * @opt_data = Optional data
                    469:  * @opt_dlen = Optional data length
1.1       misho     470:  * return: NULL error or !=NULL new queued task
                    471:  */
1.5       misho     472: sched_task_t *schedWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    473:                int fd, void *opt_data, size_t opt_dlen);
1.30      misho     474: #define schedWriteSelf(x)      schedWriteExt(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    475:                TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)), TASK_HARG((x)))
1.1       misho     476: /*
1.9       misho     477:  * schedAlarm() - Add ALARM task to scheduler queue
                    478:  *
                    479:  * @root = root task
                    480:  * @func = task execution function
                    481:  * @arg = 1st func argument
                    482:  * @ts = timeout argument structure, minimum alarm timer resolution is 1msec!
1.20      misho     483:  * @opt_data = Alarm timer ID
1.9       misho     484:  * @opt_dlen = Optional data length
                    485:  * return: NULL error or !=NULL new queued task
                    486:  */
                    487: sched_task_t *schedAlarm(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    488:                struct timespec ts, void *opt_data, size_t opt_dlen);
                    489: #define schedAlarmSelf(x)      schedAlarm(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    490:                TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
                    491: /*
1.20      misho     492:  * schedRTC() - Add RTC task to scheduler queue
                    493:  *
                    494:  * @root = root task
                    495:  * @func = task execution function
                    496:  * @arg = 1st func argument
                    497:  * @ts = timeout argument structure, minimum alarm timer resolution is 1msec!
                    498:  * @opt_data = Optional RTC ID
1.21      misho     499:  * @opt_dlen = Optional data length
1.20      misho     500:  * return: NULL error or !=NULL new queued task
                    501:  */
                    502: sched_task_t *schedRTC(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    503:                struct timespec ts, void *opt_data, size_t opt_dlen);
                    504: #define schedRTCSelf(x)                schedRTC(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    505:                TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
                    506: /*
1.10      misho     507:  * schedNode() - Add NODE task to scheduler queue
                    508:  *
                    509:  * @root = root task
                    510:  * @func = task execution function
                    511:  * @arg = 1st func argument
                    512:  * @fd = fd handle
                    513:  * @opt_data = Optional data
                    514:  * @opt_dlen = Optional data length
                    515:  * return: NULL error or !=NULL new queued task
                    516:  */
                    517: sched_task_t *schedNode(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    518:                int fd, void *opt_data, size_t opt_dlen);
                    519: #define schedNodeSelf(x)       schedNode(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
1.29      misho     520:                TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
                    521: /*
                    522:  * schedNode2() - Add NODE task with all events to scheduler queue
                    523:  *
                    524:  * @root = root task
                    525:  * @func = task execution function
                    526:  * @arg = 1st func argument
                    527:  * @fd = fd handle
                    528:  * @opt_data = Optional data
                    529:  * @opt_dlen = Optional data length
                    530:  * return: NULL error or !=NULL new queued task
                    531:  */
                    532: sched_task_t *schedNode2(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    533:                int fd, void *opt_data, size_t opt_dlen);
                    534: #define schedNode2Self(x)      schedNode2(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
1.10      misho     535:                TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
                    536: /*
                    537:  * schedProc() - Add PROC task to scheduler queue
                    538:  *
                    539:  * @root = root task
                    540:  * @func = task execution function
                    541:  * @arg = 1st func argument
                    542:  * @pid = PID
                    543:  * @opt_data = Optional data
                    544:  * @opt_dlen = Optional data length
                    545:  * return: NULL error or !=NULL new queued task
                    546:  */
                    547: sched_task_t *schedProc(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    548:                unsigned long pid, void *opt_data, size_t opt_dlen);
                    549: #define schedProcSelf(x)       schedProc(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    550:                TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
                    551: /*
                    552:  * schedSignal() - Add SIGNAL task to scheduler queue
                    553:  *
                    554:  * @root = root task
                    555:  * @func = task execution function
                    556:  * @arg = 1st func argument
                    557:  * @sig = Signal
                    558:  * @opt_data = Optional data
                    559:  * @opt_dlen = Optional data length
                    560:  * return: NULL error or !=NULL new queued task
                    561:  */
                    562: sched_task_t *schedSignal(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    563:                unsigned long sig, void *opt_data, size_t opt_dlen);
                    564: #define schedSignalSelf(x)     schedSignal(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    565:                TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
                    566: 
1.12      misho     567: #ifdef EVFILT_LIO
                    568: /*
                    569:  * schedAIO() - Add AIO task to scheduler queue
                    570:  *
                    571:  * @root = root task
                    572:  * @func = task execution function
                    573:  * @arg = 1st func argument
                    574:  * @acb = AIO cb structure address
                    575:  * @opt_data = Optional data
                    576:  * @opt_dlen = Optional data length
                    577:  * return: NULL error or !=NULL new queued task
                    578:  */
                    579: sched_task_t *schedAIO(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    580:                struct aiocb * __restrict acb, void *opt_data, size_t opt_dlen);
                    581: /*
                    582:  * schedAIORead() - Add AIO read task to scheduler queue
                    583:  *
                    584:  * @root = root task
                    585:  * @func = task execution function
                    586:  * @arg = 1st func argument
                    587:  * @fd = file descriptor
                    588:  * @buffer = Buffer
                    589:  * @buflen = Buffer length
                    590:  * @offset = Offset from start of file, if =-1 from current position
                    591:  * return: NULL error or !=NULL new queued task
                    592:  */
1.18      misho     593: sched_task_t *schedAIORead(sched_root_task_t * __restrict root, sched_task_func_t func, 
1.12      misho     594:                void *arg, int fd, void *buffer, size_t buflen, off_t offset);
                    595: /*
                    596:  * schedAIOWrite() - Add AIO write task to scheduler queue
                    597:  *
                    598:  * @root = root task
                    599:  * @func = task execution function
                    600:  * @arg = 1st func argument
                    601:  * @fd = file descriptor
                    602:  * @buffer = Buffer
                    603:  * @buflen = Buffer length
                    604:  * @offset = Offset from start of file, if =-1 from current position
                    605:  * return: NULL error or !=NULL new queued task
                    606:  */
1.18      misho     607: sched_task_t *schedAIOWrite(sched_root_task_t * __restrict root, sched_task_func_t func, 
1.12      misho     608:                void *arg, int fd, void *buffer, size_t buflen, off_t offset);
                    609: 
                    610: /*
                    611:  * schedLIO() - Add AIO bulk tasks to scheduler queue
                    612:  *
                    613:  * @root = root task
                    614:  * @func = task execution function
                    615:  * @arg = 1st func argument
                    616:  * @acbs = AIO cb structure addresses
                    617:  * @opt_data = Optional data
                    618:  * @opt_dlen = Optional data length
                    619:  * return: NULL error or !=NULL new queued task
                    620:  */
                    621: sched_task_t *schedLIO(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    622:                struct aiocb ** __restrict acbs, void *opt_data, size_t opt_dlen);
                    623: /*
                    624:  * schedLIORead() - Add list of AIO read tasks to scheduler queue
                    625:  *
                    626:  * @root = root task
                    627:  * @func = task execution function
                    628:  * @arg = 1st func argument
                    629:  * @fd = file descriptor
                    630:  * @bufs = Buffer's list
                    631:  * @nbufs = Number of Buffers
                    632:  * @offset = Offset from start of file, if =-1 from current position
                    633:  * return: NULL error or !=NULL new queued task
                    634:  */
1.18      misho     635: sched_task_t *schedLIORead(sched_root_task_t * __restrict root, sched_task_func_t func, 
1.12      misho     636:                void *arg, int fd, struct iovec *bufs, size_t nbufs, off_t offset);
                    637: /*
                    638:  * schedLIOWrite() - Add list of AIO write tasks to scheduler queue
                    639:  *
                    640:  * @root = root task
                    641:  * @func = task execution function
                    642:  * @arg = 1st func argument
                    643:  * @fd = file descriptor
                    644:  * @bufs = Buffer's list
                    645:  * @nbufs = Number of Buffers
                    646:  * @offset = Offset from start of file, if =-1 from current position
                    647:  * return: NULL error or !=NULL new queued task
                    648:  */
1.18      misho     649: sched_task_t *schedLIOWrite(sched_root_task_t * __restrict root, sched_task_func_t func, 
1.12      misho     650:                void *arg, int fd, struct iovec *bufs, size_t nbufs, off_t offset);
                    651: #endif /* EVFILT_LIO */
                    652: 
1.10      misho     653: /*
                    654:  * schedUser() - Add trigger USER task to scheduler queue
                    655:  *
                    656:  * @root = root task
                    657:  * @func = task execution function
                    658:  * @arg = 1st func argument
                    659:  * @id = Trigger ID
                    660:  * @opt_data = Optional data
                    661:  * @opt_dlen = Optional user's trigger flags
                    662:  * return: NULL error or !=NULL new queued task
                    663:  */
                    664: sched_task_t *schedUser(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    665:                unsigned long id, void *opt_data, size_t opt_dlen);
                    666: #define schedUserSelf(x)       schedUser(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    667:                TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
                    668: /*
                    669:  * schedTrigger() - Triggering USER task
                    670:  *
                    671:  * @task = task
                    672:  * return: -1 error or 0 ok
                    673:  */
                    674: int schedTrigger(sched_task_t * __restrict task);
                    675: 
                    676: /*
1.1       misho     677:  * schedTimer() - Add TIMER task to scheduler queue
1.6       misho     678:  *
1.1       misho     679:  * @root = root task
                    680:  * @func = task execution function
                    681:  * @arg = 1st func argument
1.5       misho     682:  * @ts = timeout argument structure
                    683:  * @opt_data = Optional data
                    684:  * @opt_dlen = Optional data length
1.1       misho     685:  * return: NULL error or !=NULL new queued task
                    686:  */
1.5       misho     687: sched_task_t *schedTimer(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    688:                struct timespec ts, void *opt_data, size_t opt_dlen);
1.1       misho     689: /*
                    690:  * schedEvent() - Add EVENT task to scheduler queue
1.6       misho     691:  *
1.1       misho     692:  * @root = root task
                    693:  * @func = task execution function
                    694:  * @arg = 1st func argument
1.2       misho     695:  * @val = additional func argument
1.5       misho     696:  * @opt_data = Optional data
                    697:  * @opt_dlen = Optional data length
1.1       misho     698:  * return: NULL error or !=NULL new queued task
                    699:  */
1.5       misho     700: sched_task_t *schedEvent(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    701:                unsigned long val, void *opt_data, size_t opt_dlen);
1.8       misho     702: #define schedEventSelf(x)      schedEvent(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    703:                TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1       misho     704: /*
1.13      misho     705:  * schedTask() - Add regular task to scheduler queue
1.6       misho     706:  *
1.1       misho     707:  * @root = root task
                    708:  * @func = task execution function
                    709:  * @arg = 1st func argument
1.13      misho     710:  * @prio = regular task priority, 0 is hi priority for regular tasks
1.5       misho     711:  * @opt_data = Optional data
                    712:  * @opt_dlen = Optional data length
1.1       misho     713:  * return: NULL error or !=NULL new queued task
                    714:  */
1.13      misho     715: sched_task_t *schedTask(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    716:                unsigned long prio, void *opt_data, size_t opt_dlen);
                    717: #define schedTaskSelf(x)       schedTask(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
1.8       misho     718:                TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.11      misho     719: /*
                    720:  * schedSuspend() - Add Suspended task to scheduler queue
                    721:  *
                    722:  * @root = root task
                    723:  * @func = task execution function
                    724:  * @arg = 1st func argument
                    725:  * @id = Trigger ID
                    726:  * @opt_data = Optional data
                    727:  * @opt_dlen = Optional user's trigger flags
                    728:  * return: NULL error or !=NULL new queued task
                    729:  */
                    730: sched_task_t *schedSuspend(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    731:                unsigned long id, void *opt_data, size_t opt_dlen);
                    732: #define schedSuspendSelf(x)    schedUser(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    733:                TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
                    734: /*
                    735:  * schedResumeby() - Resume suspended task
                    736:  *
                    737:  * @root = root task
                    738:  * @criteria = find task by criteria 
1.24      misho     739:  *     [ CRITERIA_ANY|CRITERIA_ID|CRITERIA_VAL|CRITERIA_DATA ]
1.23      misho     740:  * @param = search parameter (sched_task_t *task| unsigned long id)
1.11      misho     741:  * return: -1 error or 0 resumed ok
                    742:  */
                    743: int schedResumeby(sched_root_task_t * __restrict root, unsigned char criteria, void *param);
1.10      misho     744: 
1.1       misho     745: /*
                    746:  * schedCallOnce() - Call once from scheduler
1.6       misho     747:  *
1.1       misho     748:  * @root = root task
                    749:  * @func = task execution function
                    750:  * @arg = 1st func argument
1.2       misho     751:  * @val = additional func argument
1.5       misho     752:  * @opt_data = Optional data
                    753:  * @opt_dlen = Optional data length
1.2       misho     754:  * return: return value from called func
1.1       misho     755:  */
1.5       misho     756: sched_task_t *schedCallOnce(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
                    757:                unsigned long val, void *opt_data, size_t opt_dlen);
1.8       misho     758: #define schedCallAgain(x)      schedCallOnce(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
                    759:                TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1       misho     760: 
1.15      misho     761: /*
                    762:  * schedThread() - Add thread task to scheduler queue
                    763:  *
                    764:  * @root = root task
                    765:  * @func = task execution function
                    766:  * @arg = 1st func argument
1.17      misho     767:  * @ss = stack size
1.15      misho     768:  * @opt_data = Optional data
                    769:  * @opt_dlen = Optional data length
                    770:  * return: NULL error or !=NULL new queued task
                    771:  */
                    772: sched_task_t *schedThread(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, 
1.22      misho     773:                size_t ss, void *opt_data, size_t opt_dlen);
1.15      misho     774: #define schedThreadSelf(x)     schedThread(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
1.22      misho     775:                (size_t) TASK_FLAG((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.15      misho     776: /*
1.16      misho     777:  * sched_taskExit() - Exit routine for scheduler task, explicit required for thread tasks
                    778:  *
                    779:  * @task = current task
                    780:  * @retcode = return code
                    781:  * return: return code
                    782:  */
1.18      misho     783: void *sched_taskExit(sched_task_t *task, intptr_t retcode);
1.16      misho     784: /*
                    785:  * taskExit() - Exit helper for scheduler task
1.15      misho     786:  *
                    787:  * @t = current executed task
                    788:  * @x = exit value for task
                    789:  * return: none
                    790:  */
1.16      misho     791: #define taskExit(t, x)         return sched_taskExit((t), (intptr_t) (x))
1.15      misho     792: 
1.34      misho     793: #ifdef __cplusplus
                    794: }
                    795: #endif
1.1       misho     796: 
                    797: #endif

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