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.c,v 1.1.1.1.2.2 2011/08/12 23:06:56 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
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: #include "global.h"
47: #include "hooks.h"
48:
49:
50: #pragma GCC visibility push(hidden)
51:
52: int sched_Errno;
53: char sched_Error[STRSIZ];
54:
55: #pragma GCC visibility pop
56:
57:
58: // sched_GetErrno() Get error code of last operation
59: inline int
60: sched_GetErrno()
61: {
62: return sched_Errno;
63: }
64:
65: // sched_GetError() Get error text of last operation
66: inline const char *
67: sched_GetError()
68: {
69: return sched_Error;
70: }
71:
72: // sched_SetErr() Set error to variables for internal use!!!
73: inline void
74: sched_SetErr(int eno, char *estr, ...)
75: {
76: va_list lst;
77:
78: sched_Errno = eno;
79: memset(sched_Error, 0, sizeof sched_Error);
80: va_start(lst, estr);
81: vsnprintf(sched_Error, sizeof sched_Error, estr, lst);
82: va_end(lst);
83: }
84:
85: /* Init and prepare scheduler functions */
86:
87: /*
88: * schedRegisterHooks() - Register IO handles and bind tasks to it
89: * @root = root task
90: * return: -1 error or 0 ok
91: */
92: int
93: schedRegisterHooks(sched_root_task_t * __restrict root)
94: {
95: if (!root || (root->root_data.iov_base && root->root_data.iov_len))
96: return -1;
97:
98: if (root->root_hooks.hook_root.fini)
99: root->root_hooks.hook_root.fini(root, NULL);
100: memset(&root->root_hooks, 0, sizeof root->root_hooks);
101:
102: root->root_hooks.hook_add.read = sched_hook_read;
103: root->root_hooks.hook_add.write = sched_hook_write;
104:
105: root->root_hooks.hook_exec.cancel = sched_hook_cancel;
106: root->root_hooks.hook_exec.fetch = sched_hook_fetch;
107:
108: root->root_hooks.hook_root.init = sched_hook_init;
109: root->root_hooks.hook_root.fini = sched_hook_fini;
110: return 0;
111: }
112:
113: /*
114: * schedInit() - Init scheduler
115: * @data = optional data if !=NULL
116: * @datlen = data len if data is set
117: * return: allocated root task if ok or NULL error
118: */
119: sched_root_task_t *
120: schedInit(void ** __restrict data, size_t datlen)
121: {
122: sched_root_task_t *root = NULL;
123: int (*func)(sched_root_task_t *);
124:
125: root = malloc(sizeof(sched_root_task_t));
126: if (!root) {
127: LOGERR;
128: } else {
129: memset(root, 0, sizeof(sched_root_task_t));
130: TAILQ_INIT(&root->root_read);
131: TAILQ_INIT(&root->root_write);
132: TAILQ_INIT(&root->root_timer);
133: TAILQ_INIT(&root->root_event);
134: TAILQ_INIT(&root->root_eventlo);
135: TAILQ_INIT(&root->root_ready);
136: TAILQ_INIT(&root->root_unuse);
137:
138: if (data && *data) {
139: if (datlen) {
140: root->root_data.iov_base = *data;
141: root->root_data.iov_len = datlen;
142: } else {
143: func = (int(*)(sched_root_task_t*)) data;
144: func(root);
145: }
146: }
147:
148: if (root->root_hooks.hook_root.init)
149: root->root_hooks.hook_root.init(root, NULL);
150: }
151:
152: return root;
153: }
154:
155: /*
156: * schedEnd() - End scheduler & free all resources
157: * @root = root task
158: * return: -1 error or 0 ok
159: */
160: int
161: schedEnd(sched_root_task_t * __restrict root)
162: {
163: sched_task_t *task;
164:
165: if (!root)
166: return -1;
167:
168: TAILQ_FOREACH(task, &root->root_read, task_node) {
169: schedCancel(task);
170: }
171: TAILQ_FOREACH(task, &root->root_write, task_node) {
172: schedCancel(task);
173: }
174: TAILQ_FOREACH(task, &root->root_timer, task_node) {
175: schedCancel(task);
176: }
177: TAILQ_FOREACH(task, &root->root_event, task_node) {
178: schedCancel(task);
179: }
180: TAILQ_FOREACH(task, &root->root_ready, task_node) {
181: schedCancel(task);
182: }
183:
184: while ((task = TAILQ_FIRST(&root->root_unuse))) {
185: TAILQ_REMOVE(&root->root_unuse, task, task_node);
186: free(task);
187: }
188:
189: if (root->root_hooks.hook_root.fini)
190: root->root_hooks.hook_root.fini(root, NULL);
191:
192: free(root);
193: return 0;
194: }
195:
196: /*
197: * schedCall() - Call task execution function
198: * @task = current task
199: * return: !=NULL error or =NULL ok
200: */
201: inline void *
202: schedCall(sched_task_t * __restrict task)
203: {
204: if (!task)
205: return (void*) -1;
206:
207: task->task_id++;
208: return task->task_func(task);
209: }
210:
211: /*
212: * schedFetch() - Fetch ready task
213: * @root = root task
214: * return: =NULL error or !=NULL ready task
215: */
216: inline void *
217: schedFetch(sched_root_task_t * __restrict root)
218: {
219: void *ptr;
220:
221: if (!root)
222: return NULL;
223:
224: if (root->root_hooks.hook_exec.fetch)
225: ptr = root->root_hooks.hook_exec.fetch(root, NULL);
226: else
227: ptr = NULL;
228:
229: return ptr;
230: }
231:
232: /*
233: * schedCancel() - Cancel task from scheduler
234: * @task = task
235: * return: -1 error or 0 ok
236: */
237: int
238: schedCancel(sched_task_t * __restrict task)
239: {
240: sched_queue_t *queue;
241:
242: if (!task || !task->task_root)
243: return -1;
244:
245: if (task->task_root->root_hooks.hook_exec.cancel)
246: if (task->task_root->root_hooks.hook_exec.cancel(task, NULL))
247: return -1;
248:
249: switch (task->task_type) {
250: case taskREAD:
251: queue = &task->task_root->root_read;
252: break;
253: case taskWRITE:
254: queue = &task->task_root->root_write;
255: break;
256: case taskTIMER:
257: queue = &task->task_root->root_timer;
258: break;
259: case taskEVENT:
260: queue = &task->task_root->root_event;
261: break;
262: case taskREADY:
263: queue = &task->task_root->root_ready;
264: break;
265: default:
266: queue = NULL;
267: }
268: if (queue)
269: TAILQ_REMOVE(queue, task, task_node);
270: if (task->task_type != taskUNUSE) {
271: task->task_type = taskUNUSE;
272: TAILQ_INSERT_TAIL(&task->task_root->root_unuse, task, task_node);
273: }
274:
275: return 0;
276: }
277:
278: /*
279: * schedCancelby() - Cancel task from scheduler by criteria
280: * @root = root task
281: * @queue = cancel from queue, if =NULL cancel same task from all queues
282: * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
283: * @param = search parameter
284: * @hook = custom cleanup hook function, may be NULL
285: * return: -1 error or 0 ok
286: */
287: int
288: schedCancelby(sched_root_task_t * __restrict root, sched_queue_t * __restrict queue,
289: u_char criteria, void *param, sched_hook_func_t hook)
290: {
291: sched_task_t *task;
292: int flg = 0;
293:
294: if (!root)
295: return -1;
296: if (!queue) {
297: if (schedCancelby(root, &root->root_read, criteria, param, hook))
298: return -2;
299: if (schedCancelby(root, &root->root_write, criteria, param, hook))
300: return -2;
301: if (schedCancelby(root, &root->root_timer, criteria, param, hook))
302: return -2;
303: if (schedCancelby(root, &root->root_event, criteria, param, hook))
304: return -2;
305: if (schedCancelby(root, &root->root_ready, criteria, param, hook))
306: return -2;
307: if (schedCancelby(root, &root->root_read, criteria, param, hook))
308: return -2;
309: return 0;
310: }
311:
312: TAILQ_FOREACH(task, queue, task_node)
313: if (criteria == CRITERIA_CALL) {
314: if (task->task_func == (sched_task_func_t) param) {
315: flg++;
316: break;
317: }
318: } else if (criteria == CRITERIA_ARG) {
319: if (task->task_arg == param) {
320: flg++;
321: break;
322: }
323: } else if (criteria == CRITERIA_FD) {
324: if (TASK_FD(task) == (u_long) param) {
325: flg++;
326: break;
327: }
328: } else if (criteria == CRITERIA_VAL) {
329: if (TASK_VAL(task) == (u_long) param) {
330: flg++;
331: break;
332: }
333: } else if (criteria == CRITERIA_TV) {
334: if (&TASK_TV(task) == (struct timeval*) param) {
335: flg++;
336: break;
337: }
338: } else {
339: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
340: return -1;
341: }
342: if (!flg || !task) /* task not found */
343: return 0;
344:
345: if (task->task_root->root_hooks.hook_exec.cancel)
346: if (task->task_root->root_hooks.hook_exec.cancel(task, NULL))
347: return -1;
348: if (hook)
349: if (hook(task, NULL))
350: return -3;
351:
352: TAILQ_REMOVE(queue, task, task_node);
353:
354: if (task->task_type != taskUNUSE) {
355: task->task_type = taskUNUSE;
356: TAILQ_INSERT_TAIL(&task->task_root->root_unuse, task, task_node);
357: }
358: return 0;
359: }
360:
361: /*
362: * schedRun() - Scheduler *run loop*
363: * @root = root task
364: * return: -1 error or 0 ok
365: */
366: int
367: schedRun(sched_root_task_t * __restrict root)
368: {
369: sched_task_t *task;
370:
371: if (!root)
372: return -1;
373:
374: if (root->root_hooks.hook_exec.run)
375: if (root->root_hooks.hook_exec.run(root, NULL))
376: return -1;
377: if (root->root_hooks.hook_exec.fetch)
378: while ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
379: schedCall(task);
380:
381: return 0;
382: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>