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