Annotation of libaitsched/inc/aitsched.h, revision 1.9.2.3
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.9.2.3 ! misho 6: * $Id: aitsched.h,v 1.9.2.2 2012/05/31 14:45:10 misho Exp $
1.1 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.6 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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>
52: #include <sys/uio.h>
1.3 misho 53: #include <stdint.h>
1.5 misho 54: #include <pthread.h>
1.2 misho 55:
56:
1.1 misho 57: /* criteria type */
1.9.2.3 ! misho 58: #define CRITERIA_ANY 0
! 59: #define CRITERIA_CALL 1
! 60: #define CRITERIA_ARG 2
! 61: #define CRITERIA_FD 3
! 62: #define CRITERIA_VAL 4
! 63: #define CRITERIA_TS 5
! 64: #define CRITERIA_DATA 6
1.1 misho 65:
66:
67: /* early declaration for root & task */
68: typedef struct sched_Task sched_task_t;
69: typedef struct sched_RootTask sched_root_task_t;
70:
71: typedef enum {
72: taskREAD = 0,
73: taskWRITE,
74: taskTIMER,
1.9.2.1 misho 75: taskALARM,
76: taskNODE,
77: taskPROC,
78: taskUSER,
79: taskSIGNAL,
1.9 misho 80: taskEVENT,
81: taskEVENTLO,
1.1 misho 82: taskREADY,
83: taskUNUSE,
84: taskMAX
85: } sched_task_type_t;
86:
87: /* hooks */
88: typedef void *(*sched_hook_func_t)(void *, void *);
89: struct sched_HooksTask {
90: struct {
91: /* read(sched_task_t *task, NULL) -> int */
92: sched_hook_func_t read;
93: /* write(sched_task_t *task, NULL) -> int */
94: sched_hook_func_t write;
1.9.2.1 misho 95: /* timer(sched_task_t *task, struct timespec *ts) -> int */
96: sched_hook_func_t timer;
97: /* alarm(sched_task_t *task, NULL) -> int */
98: sched_hook_func_t alarm;
99: /* node(sched_task_t *task, NULL) -> int */
100: sched_hook_func_t node;
101: /* proc(sched_task_t *task, NULL) -> int */
102: sched_hook_func_t proc;
103: /* user(sched_task_t *task, NULL) -> int */
104: sched_hook_func_t user;
105: /* signal(sched_task_t *task, NULL) -> int */
106: sched_hook_func_t signal;
1.1 misho 107: /* event(sched_task_t *task, NULL) -> int */
108: sched_hook_func_t event;
109: /* eventlo(sched_task_t *task, NULL) -> int */
110: sched_hook_func_t eventlo;
111: } hook_add;
112: struct {
113: /* cancel(sched_task_t *task, NULL) -> int */
114: sched_hook_func_t cancel;
115: /* run(sched_root_task_t *root, NULL) -> int */
116: sched_hook_func_t run;
117: /* fetch(sched_root_task_t *root, NULL) -> sched_task_t* */
118: sched_hook_func_t fetch;
1.3 misho 119: /* exception(sched_root_task_t *root, NULL) -> int */
120: sched_hook_func_t exception;
1.6 misho 121: /* condition(sched_root_task_t *root, intptr_t *stopValue) -> int */
122: sched_hook_func_t condition;
1.1 misho 123: } hook_exec;
124: struct {
125: /* init(sched_root_task_t *root, void *data) -> int */
126: sched_hook_func_t init;
127: /* fini(sched_root_task_t *root, NULL) -> int */
128: sched_hook_func_t fini;
1.3 misho 129: /* error(sched_root_task_t *root, int errno) -> int */
130: sched_hook_func_t error;
1.1 misho 131: } hook_root;
132: };
133: typedef struct sched_HooksTask hooks_task_t;
134:
135: /* task callback, like pthread callback! */
1.2 misho 136: typedef void *(*sched_task_func_t)(sched_task_t * /* current task data*/);
1.1 misho 137:
1.4 misho 138: /* task lock helpers */
1.9.2.3 ! misho 139: #define TASK_LOCK(x) ((x)->task_lock = 42)
1.4 misho 140: #define TASK_UNLOCK(x) ((x)->task_lock ^= (x)->task_lock)
141: #define TASK_ISLOCKED(x) ((x)->task_lock)
142:
1.1 misho 143: /* task & queue */
144: struct sched_Task {
1.4 misho 145: volatile int task_lock;
1.9.2.3 ! misho 146: uintptr_t task_id;
! 147: #define TASK_ID(x) ((struct sched_Task*) (x)->task_id)
1.1 misho 148: sched_task_type_t task_type;
1.5 misho 149: #define TASK_TYPE(x) (x)->task_type
1.1 misho 150:
151: sched_root_task_t *task_root;
1.2 misho 152: #define TASK_ROOT(x) (x)->task_root
1.1 misho 153: sched_task_func_t task_func;
1.8 misho 154: #define TASK_FUNC(x) (x)->task_func
1.1 misho 155:
156: void *task_arg;
157: union {
158: unsigned long v;
1.3 misho 159: intptr_t fd;
1.5 misho 160: struct timespec ts;
1.1 misho 161: } task_val;
162: #define TASK_ARG(x) (x)->task_arg
163: #define TASK_VAL(x) (x)->task_val.v
164: #define TASK_FD(x) (x)->task_val.fd
1.5 misho 165: #define TASK_TS(x) (x)->task_val.ts
1.1 misho 166:
167: struct iovec task_data;
168: #define TASK_DATA(x) (x)->task_data.iov_base
169: #define TASK_DATLEN(x) (x)->task_data.iov_len
170:
171: TAILQ_ENTRY(sched_Task) task_node;
172: };
173: typedef TAILQ_HEAD(, sched_Task) sched_queue_t;
1.5 misho 174: #define TASK_DATA_SET(x, _dp, _dl) do { \
175: if ((x)) { \
176: (x)->task_data.iov_base = (_dp); \
177: (x)->task_data.iov_len = _dl; \
178: } \
179: while (0)
1.1 misho 180:
181: /* root task */
182: struct sched_RootTask {
183: int root_kq;
1.5 misho 184: struct timespec root_wait;
185: struct timespec root_poll;
1.6 misho 186: intptr_t root_cond;
187:
1.5 misho 188: pthread_mutex_t root_mtx[taskMAX];
1.1 misho 189:
190: sched_queue_t root_read;
191: sched_queue_t root_write;
192: sched_queue_t root_timer;
1.9 misho 193: sched_queue_t root_alarm;
1.9.2.1 misho 194: sched_queue_t root_node;
195: sched_queue_t root_proc;
196: sched_queue_t root_user;
197: sched_queue_t root_signal;
1.1 misho 198: sched_queue_t root_event;
1.9.2.1 misho 199: sched_queue_t root_eventlo;
1.1 misho 200: sched_queue_t root_ready;
201: sched_queue_t root_unuse;
202: int root_eventlo_miss;
203:
204: hooks_task_t root_hooks;
205: struct iovec root_data;
206: #define ROOT_DATA(x) (x)->root_data.iov_base
207: #define ROOT_DATLEN(x) (x)->root_data.iov_len
208: };
1.7 misho 209: #define ROOT_QUEUE_EMPTY(x, _q) TAILQ_EMPTY(&((x)->root_##_q))
1.1 misho 210:
211:
1.2 misho 212: inline int sched_GetErrno();
213: inline const char *sched_GetError();
214:
215:
1.1 misho 216: /*
217: * schedInit() - Init scheduler
1.6 misho 218: *
1.1 misho 219: * @data = optional data if !=NULL
220: * @datlen = data len if data is set
221: * return: allocated root task if ok or NULL error
222: */
223: sched_root_task_t *schedInit(void ** __restrict data, size_t datlen);
1.2 misho 224: #define schedBegin() schedInit((void**) &schedRegisterHooks, 0)
1.1 misho 225: /*
226: * schedEnd() - End scheduler & free all resources
1.6 misho 227: *
1.1 misho 228: * @root = root task
229: * return: -1 error or 0 ok
230: */
1.2 misho 231: int schedEnd(sched_root_task_t ** __restrict root);
1.1 misho 232: /*
1.2 misho 233: * schedRegisterHooks() - Register IO handles and bind tasks to it
1.6 misho 234: *
1.1 misho 235: * @root = root task
236: * return: -1 error or 0 ok
237: */
1.2 misho 238: int schedRegisterHooks(sched_root_task_t * __restrict root);
1.1 misho 239: /*
1.5 misho 240: * schedPolling() - Polling timeout period if no timer task is present
1.6 misho 241: *
1.5 misho 242: * @root = root task
243: * @ts = timeout polling period, if ==NULL INFINIT timeout
244: * @tsold = old timeout polling if !=NULL
245: * return: -1 error or 0 ok
246: */
247: inline int schedPolling(sched_root_task_t * __restrict root,
248: struct timespec * __restrict ts, struct timespec * __restrict tsold);
249: /*
1.6 misho 250: * schedTermCondition() - Activate hook for scheduler condition kill
251: *
252: * @root = root task
253: * @condValue = condition value, kill schedRun() if condValue == killState
254: * return: -1 error ok 0 ok
255: */
256: inline int schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue);
257: /*
1.1 misho 258: * schedCall() - Call task execution function
1.6 misho 259: *
1.1 misho 260: * @task = current task
261: * return: !=NULL error or =NULL ok
262: */
263: inline void *schedCall(sched_task_t * __restrict task);
264: /*
265: * schedFetch() - Fetch ready task
1.6 misho 266: *
1.1 misho 267: * @root = root task
268: * return: =NULL error or !=NULL ready task
269: */
270: inline void *schedFetch(sched_root_task_t * __restrict root);
271: /*
272: * schedRun() - Scheduler *run loop*
1.6 misho 273: *
1.1 misho 274: * @root = root task
1.2 misho 275: * @killState = kill condition variable, if !=0 stop scheduler loop
1.1 misho 276: * return: -1 error or 0 ok
277: */
1.7 misho 278: int schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState);
1.1 misho 279: /*
280: * schedCancel() - Cancel task from scheduler
1.6 misho 281: *
1.1 misho 282: * @task = task
283: * return: -1 error or 0 ok
284: */
285: int schedCancel(sched_task_t * __restrict task);
286: /*
287: * schedCancelby() - Cancel task from scheduler by criteria
1.6 misho 288: *
1.1 misho 289: * @root = root task
1.5 misho 290: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.9.2.3 ! misho 291: * @criteria = find task by criteria
! 292: * [CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TS|CRITERIA_DATA]
1.1 misho 293: * @param = search parameter
294: * @hook = custom cleanup hook function, may be NULL
1.3 misho 295: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1 misho 296: */
1.5 misho 297: int schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
1.1 misho 298: u_char criteria, void *param, sched_hook_func_t hook);
299:
300:
301: /*
1.2 misho 302: * schedRead() - Add READ I/O task to scheduler queue
1.6 misho 303: *
1.1 misho 304: * @root = root task
305: * @func = task execution function
306: * @arg = 1st func argument
1.2 misho 307: * @fd = fd handle
1.5 misho 308: * @opt_data = Optional data
309: * @opt_dlen = Optional data length
1.1 misho 310: * return: NULL error or !=NULL new queued task
311: */
1.5 misho 312: sched_task_t *schedRead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
313: int fd, void *opt_data, size_t opt_dlen);
1.8 misho 314: #define schedReadSelf(x) schedRead(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
315: TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1 misho 316: /*
1.2 misho 317: * schedWrite() - Add WRITE I/O task to scheduler queue
1.6 misho 318: *
1.1 misho 319: * @root = root task
320: * @func = task execution function
321: * @arg = 1st func argument
1.2 misho 322: * @fd = fd handle
1.5 misho 323: * @opt_data = Optional data
324: * @opt_dlen = Optional data length
1.1 misho 325: * return: NULL error or !=NULL new queued task
326: */
1.5 misho 327: sched_task_t *schedWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
328: int fd, void *opt_data, size_t opt_dlen);
1.8 misho 329: #define schedWriteSelf(x) schedWrite(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
330: TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1 misho 331: /*
1.9 misho 332: * schedAlarm() - Add ALARM task to scheduler queue
333: *
334: * @root = root task
335: * @func = task execution function
336: * @arg = 1st func argument
337: * @ts = timeout argument structure, minimum alarm timer resolution is 1msec!
338: * @opt_data = Optional data
339: * @opt_dlen = Optional data length
340: * return: NULL error or !=NULL new queued task
341: */
342: sched_task_t *schedAlarm(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
343: struct timespec ts, void *opt_data, size_t opt_dlen);
344: #define schedAlarmSelf(x) schedAlarm(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
345: TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
346: /*
1.9.2.1 misho 347: * schedNode() - Add NODE task to scheduler queue
348: *
349: * @root = root task
350: * @func = task execution function
351: * @arg = 1st func argument
352: * @fd = fd handle
353: * @opt_data = Optional data
354: * @opt_dlen = Optional data length
355: * return: NULL error or !=NULL new queued task
356: */
357: sched_task_t *schedNode(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
358: int fd, void *opt_data, size_t opt_dlen);
359: #define schedNodeSelf(x) schedNode(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
360: TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
361: /*
362: * schedProc() - Add PROC task to scheduler queue
363: *
364: * @root = root task
365: * @func = task execution function
366: * @arg = 1st func argument
367: * @pid = PID
368: * @opt_data = Optional data
369: * @opt_dlen = Optional data length
370: * return: NULL error or !=NULL new queued task
371: */
372: sched_task_t *schedProc(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
373: unsigned long pid, void *opt_data, size_t opt_dlen);
374: #define schedProcSelf(x) schedProc(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
375: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
376: /*
377: * schedSignal() - Add SIGNAL task to scheduler queue
378: *
379: * @root = root task
380: * @func = task execution function
381: * @arg = 1st func argument
382: * @sig = Signal
383: * @opt_data = Optional data
384: * @opt_dlen = Optional data length
385: * return: NULL error or !=NULL new queued task
386: */
387: sched_task_t *schedSignal(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
388: unsigned long sig, void *opt_data, size_t opt_dlen);
389: #define schedSignalSelf(x) schedSignal(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
390: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
391:
392: /*
393: * schedUser() - Add trigger USER task to scheduler queue
394: *
395: * @root = root task
396: * @func = task execution function
397: * @arg = 1st func argument
398: * @id = Trigger ID
399: * @opt_data = Optional data
1.9.2.2 misho 400: * @opt_dlen = Optional user's trigger flags
1.9.2.1 misho 401: * return: NULL error or !=NULL new queued task
402: */
403: sched_task_t *schedUser(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
404: unsigned long id, void *opt_data, size_t opt_dlen);
405: #define schedUserSelf(x) schedUser(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
406: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
407: /*
408: * schedTrigger() - Triggering USER task
409: *
410: * @task = task
411: * return: -1 error or 0 ok
412: */
413: int schedTrigger(sched_task_t * __restrict task);
414:
415: /*
1.1 misho 416: * schedTimer() - Add TIMER task to scheduler queue
1.6 misho 417: *
1.1 misho 418: * @root = root task
419: * @func = task execution function
420: * @arg = 1st func argument
1.5 misho 421: * @ts = timeout argument structure
422: * @opt_data = Optional data
423: * @opt_dlen = Optional data length
1.1 misho 424: * return: NULL error or !=NULL new queued task
425: */
1.5 misho 426: sched_task_t *schedTimer(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
427: struct timespec ts, void *opt_data, size_t opt_dlen);
1.8 misho 428: #define schedTimerSelf(x) schedTimer(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
429: TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1 misho 430: /*
431: * schedEvent() - Add EVENT task to scheduler queue
1.6 misho 432: *
1.1 misho 433: * @root = root task
434: * @func = task execution function
435: * @arg = 1st func argument
1.2 misho 436: * @val = additional func argument
1.5 misho 437: * @opt_data = Optional data
438: * @opt_dlen = Optional data length
1.1 misho 439: * return: NULL error or !=NULL new queued task
440: */
1.5 misho 441: sched_task_t *schedEvent(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
442: unsigned long val, void *opt_data, size_t opt_dlen);
1.8 misho 443: #define schedEventSelf(x) schedEvent(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
444: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1 misho 445: /*
446: * schedEventLo() - Add EVENT_Lo task to scheduler queue
1.6 misho 447: *
1.1 misho 448: * @root = root task
449: * @func = task execution function
450: * @arg = 1st func argument
1.2 misho 451: * @val = additional func argument
1.5 misho 452: * @opt_data = Optional data
453: * @opt_dlen = Optional data length
1.1 misho 454: * return: NULL error or !=NULL new queued task
455: */
1.5 misho 456: sched_task_t *schedEventLo(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
457: unsigned long val, void *opt_data, size_t opt_dlen);
1.8 misho 458: #define schedEventLoSelf(x) schedEventLo(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
459: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.9.2.1 misho 460:
1.1 misho 461: /*
462: * schedCallOnce() - Call once from scheduler
1.6 misho 463: *
1.1 misho 464: * @root = root task
465: * @func = task execution function
466: * @arg = 1st func argument
1.2 misho 467: * @val = additional func argument
1.5 misho 468: * @opt_data = Optional data
469: * @opt_dlen = Optional data length
1.2 misho 470: * return: return value from called func
1.1 misho 471: */
1.5 misho 472: sched_task_t *schedCallOnce(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
473: unsigned long val, void *opt_data, size_t opt_dlen);
1.8 misho 474: #define schedCallAgain(x) schedCallOnce(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
475: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
1.1 misho 476:
477:
478: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>