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

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

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