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