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

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

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