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

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

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