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.8 2012/05/19 00:46:53 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, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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/uio.h>
53: #include <stdint.h>
54: #include <pthread.h>
55:
56:
57: /* criteria type */
58: #define CRITERIA_CALL 0
59: #define CRITERIA_ARG 1
60: #define CRITERIA_FD 2
61: #define CRITERIA_VAL 3
62: #define CRITERIA_TS 4
63:
64:
65: /* early declaration for root & task */
66: typedef struct sched_Task sched_task_t;
67: typedef struct sched_RootTask sched_root_task_t;
68:
69: typedef enum {
70: taskREAD = 0,
71: taskWRITE,
72: taskTIMER,
73: taskEVENT,
74: taskEVENTLO,
75: taskREADY,
76: taskUNUSE,
77: taskMAX
78: } sched_task_type_t;
79:
80: /* hooks */
81: typedef void *(*sched_hook_func_t)(void *, void *);
82: struct sched_HooksTask {
83: struct {
84: /* read(sched_task_t *task, NULL) -> int */
85: sched_hook_func_t read;
86: /* write(sched_task_t *task, NULL) -> int */
87: sched_hook_func_t write;
88: /* event(sched_task_t *task, NULL) -> int */
89: sched_hook_func_t event;
90: /* eventlo(sched_task_t *task, NULL) -> int */
91: sched_hook_func_t eventlo;
92: /* timer(sched_task_t *task, struct timespec *ts) -> int */
93: sched_hook_func_t timer;
94: } hook_add;
95: struct {
96: /* cancel(sched_task_t *task, NULL) -> int */
97: sched_hook_func_t cancel;
98: /* run(sched_root_task_t *root, NULL) -> int */
99: sched_hook_func_t run;
100: /* fetch(sched_root_task_t *root, NULL) -> sched_task_t* */
101: sched_hook_func_t fetch;
102: /* exception(sched_root_task_t *root, NULL) -> int */
103: sched_hook_func_t exception;
104: /* condition(sched_root_task_t *root, intptr_t *stopValue) -> int */
105: sched_hook_func_t condition;
106: } hook_exec;
107: struct {
108: /* init(sched_root_task_t *root, void *data) -> int */
109: sched_hook_func_t init;
110: /* fini(sched_root_task_t *root, NULL) -> int */
111: sched_hook_func_t fini;
112: /* error(sched_root_task_t *root, int errno) -> int */
113: sched_hook_func_t error;
114: } hook_root;
115: };
116: typedef struct sched_HooksTask hooks_task_t;
117:
118: /* task callback, like pthread callback! */
119: typedef void *(*sched_task_func_t)(sched_task_t * /* current task data*/);
120:
121: /* task lock helpers */
122: #define TASK_LOCK(x) ((x)->task_lock++)
123: #define TASK_UNLOCK(x) ((x)->task_lock ^= (x)->task_lock)
124: #define TASK_ISLOCKED(x) ((x)->task_lock)
125:
126: /* task & queue */
127: struct sched_Task {
128: volatile int task_lock;
129: unsigned int task_id;
130: sched_task_type_t task_type;
131: #define TASK_TYPE(x) (x)->task_type
132:
133: sched_root_task_t *task_root;
134: #define TASK_ROOT(x) (x)->task_root
135: sched_task_func_t task_func;
136: #define TASK_FUNC(x) (x)->task_func
137:
138: void *task_arg;
139: union {
140: unsigned long v;
141: intptr_t fd;
142: struct timespec ts;
143: } task_val;
144: #define TASK_ARG(x) (x)->task_arg
145: #define TASK_VAL(x) (x)->task_val.v
146: #define TASK_FD(x) (x)->task_val.fd
147: #define TASK_TS(x) (x)->task_val.ts
148:
149: struct iovec task_data;
150: #define TASK_DATA(x) (x)->task_data.iov_base
151: #define TASK_DATLEN(x) (x)->task_data.iov_len
152:
153: TAILQ_ENTRY(sched_Task) task_node;
154: };
155: typedef TAILQ_HEAD(, sched_Task) sched_queue_t;
156: #define TASK_DATA_SET(x, _dp, _dl) do { \
157: if ((x)) { \
158: (x)->task_data.iov_base = (_dp); \
159: (x)->task_data.iov_len = _dl; \
160: } \
161: while (0)
162:
163: /* root task */
164: struct sched_RootTask {
165: int root_kq;
166: struct timespec root_wait;
167: struct timespec root_poll;
168: intptr_t root_cond;
169:
170: pthread_mutex_t root_mtx[taskMAX];
171:
172: sched_queue_t root_read;
173: sched_queue_t root_write;
174: sched_queue_t root_timer;
175: sched_queue_t root_event;
176: sched_queue_t root_ready;
177: sched_queue_t root_unuse;
178: sched_queue_t root_eventlo;
179: int root_eventlo_miss;
180:
181: hooks_task_t root_hooks;
182: struct iovec root_data;
183: #define ROOT_DATA(x) (x)->root_data.iov_base
184: #define ROOT_DATLEN(x) (x)->root_data.iov_len
185: };
186: #define ROOT_QUEUE_EMPTY(x, _q) TAILQ_EMPTY(&((x)->root_##_q))
187:
188:
189: inline int sched_GetErrno();
190: inline const char *sched_GetError();
191:
192:
193: /*
194: * schedInit() - Init scheduler
195: *
196: * @data = optional data if !=NULL
197: * @datlen = data len if data is set
198: * return: allocated root task if ok or NULL error
199: */
200: sched_root_task_t *schedInit(void ** __restrict data, size_t datlen);
201: #define schedBegin() schedInit((void**) &schedRegisterHooks, 0)
202: /*
203: * schedEnd() - End scheduler & free all resources
204: *
205: * @root = root task
206: * return: -1 error or 0 ok
207: */
208: int schedEnd(sched_root_task_t ** __restrict root);
209: /*
210: * schedRegisterHooks() - Register IO handles and bind tasks to it
211: *
212: * @root = root task
213: * return: -1 error or 0 ok
214: */
215: int schedRegisterHooks(sched_root_task_t * __restrict root);
216: /*
217: * schedPolling() - Polling timeout period if no timer task is present
218: *
219: * @root = root task
220: * @ts = timeout polling period, if ==NULL INFINIT timeout
221: * @tsold = old timeout polling if !=NULL
222: * return: -1 error or 0 ok
223: */
224: inline int schedPolling(sched_root_task_t * __restrict root,
225: struct timespec * __restrict ts, struct timespec * __restrict tsold);
226: /*
227: * schedTermCondition() - Activate hook for scheduler condition kill
228: *
229: * @root = root task
230: * @condValue = condition value, kill schedRun() if condValue == killState
231: * return: -1 error ok 0 ok
232: */
233: inline int schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue);
234: /*
235: * schedCall() - Call task execution function
236: *
237: * @task = current task
238: * return: !=NULL error or =NULL ok
239: */
240: inline void *schedCall(sched_task_t * __restrict task);
241: /*
242: * schedFetch() - Fetch ready task
243: *
244: * @root = root task
245: * return: =NULL error or !=NULL ready task
246: */
247: inline void *schedFetch(sched_root_task_t * __restrict root);
248: /*
249: * schedRun() - Scheduler *run loop*
250: *
251: * @root = root task
252: * @killState = kill condition variable, if !=0 stop scheduler loop
253: * return: -1 error or 0 ok
254: */
255: int schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState);
256: /*
257: * schedCancel() - Cancel task from scheduler
258: *
259: * @task = task
260: * return: -1 error or 0 ok
261: */
262: int schedCancel(sched_task_t * __restrict task);
263: /*
264: * schedCancelby() - Cancel task from scheduler by criteria
265: *
266: * @root = root task
267: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
268: * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TS]
269: * @param = search parameter
270: * @hook = custom cleanup hook function, may be NULL
271: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
272: */
273: int schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
274: u_char criteria, void *param, sched_hook_func_t hook);
275:
276:
277: /*
278: * schedRead() - Add READ I/O task to scheduler queue
279: *
280: * @root = root task
281: * @func = task execution function
282: * @arg = 1st func argument
283: * @fd = fd handle
284: * @opt_data = Optional data
285: * @opt_dlen = Optional data length
286: * return: NULL error or !=NULL new queued task
287: */
288: sched_task_t *schedRead(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
289: int fd, void *opt_data, size_t opt_dlen);
290: #define schedReadSelf(x) schedRead(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
291: TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
292: /*
293: * schedWrite() - Add WRITE I/O task to scheduler queue
294: *
295: * @root = root task
296: * @func = task execution function
297: * @arg = 1st func argument
298: * @fd = fd handle
299: * @opt_data = Optional data
300: * @opt_dlen = Optional data length
301: * return: NULL error or !=NULL new queued task
302: */
303: sched_task_t *schedWrite(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
304: int fd, void *opt_data, size_t opt_dlen);
305: #define schedWriteSelf(x) schedWrite(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
306: TASK_FD((x)), TASK_DATA((x)), TASK_DATLEN((x)))
307: /*
308: * schedTimer() - Add TIMER task to scheduler queue
309: *
310: * @root = root task
311: * @func = task execution function
312: * @arg = 1st func argument
313: * @ts = timeout argument structure
314: * @opt_data = Optional data
315: * @opt_dlen = Optional data length
316: * return: NULL error or !=NULL new queued task
317: */
318: sched_task_t *schedTimer(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
319: struct timespec ts, void *opt_data, size_t opt_dlen);
320: #define schedTimerSelf(x) schedTimer(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
321: TASK_TS((x)), TASK_DATA((x)), TASK_DATLEN((x)))
322: /*
323: * schedEvent() - Add EVENT task to scheduler queue
324: *
325: * @root = root task
326: * @func = task execution function
327: * @arg = 1st func argument
328: * @val = additional func argument
329: * @opt_data = Optional data
330: * @opt_dlen = Optional data length
331: * return: NULL error or !=NULL new queued task
332: */
333: sched_task_t *schedEvent(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
334: unsigned long val, void *opt_data, size_t opt_dlen);
335: #define schedEventSelf(x) schedEvent(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
336: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
337: /*
338: * schedEventLo() - Add EVENT_Lo task to scheduler queue
339: *
340: * @root = root task
341: * @func = task execution function
342: * @arg = 1st func argument
343: * @val = additional func argument
344: * @opt_data = Optional data
345: * @opt_dlen = Optional data length
346: * return: NULL error or !=NULL new queued task
347: */
348: sched_task_t *schedEventLo(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
349: unsigned long val, void *opt_data, size_t opt_dlen);
350: #define schedEventLoSelf(x) schedEventLo(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
351: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
352: /*
353: * schedCallOnce() - Call once from scheduler
354: *
355: * @root = root task
356: * @func = task execution function
357: * @arg = 1st func argument
358: * @val = additional func argument
359: * @opt_data = Optional data
360: * @opt_dlen = Optional data length
361: * return: return value from called func
362: */
363: sched_task_t *schedCallOnce(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg,
364: unsigned long val, void *opt_data, size_t opt_dlen);
365: #define schedCallAgain(x) schedCallOnce(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \
366: TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x)))
367:
368:
369: #endif
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>