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.1 2011/08/11 22:55:28 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:
131: if (data && *data) {
132: if (datlen) {
133: root->root_data.iov_base = *data;
134: root->root_data.iov_len = datlen;
135: } else {
136: func = *data;
137: func(root);
138: }
139: }
140:
141: if (root->root_hooks.hook_root.init)
142: root->root_hooks.hook_root.init(root, NULL);
143: }
144:
145: return root;
146: }
147:
148: /*
149: * schedEnd() - End scheduler & free all resources
150: * @root = root task
151: * return: -1 error or 0 ok
152: */
153: int
154: schedEnd(sched_root_task_t * __restrict root)
155: {
156: sched_task_t *task;
157:
158: if (!root)
159: return -1;
160:
161: TAILQ_FOREACH(task, &root->root_read, task_node) {
162: schedCancel(task);
163: }
164: TAILQ_FOREACH(task, &root->root_write, task_node) {
165: schedCancel(task);
166: }
167: TAILQ_FOREACH(task, &root->root_timer, task_node) {
168: schedCancel(task);
169: }
170: TAILQ_FOREACH(task, &root->root_event, task_node) {
171: schedCancel(task);
172: }
173: TAILQ_FOREACH(task, &root->root_ready, task_node) {
174: schedCancel(task);
175: }
176:
177: while ((task = TAILQ_FIRST(&root->root_unuse))) {
178: TAILQ_REMOVE(&root->root_unuse, task, task_node);
179: free(task);
180: }
181:
182: if (root->root_hooks.hook_root.fini)
183: root->root_hooks.hook_root.fini(root, NULL);
184:
185: free(root);
186: return 0;
187: }
188:
189: /*
190: * schedCall() - Call task execution function
191: * @task = current task
192: * return: !=NULL error or =NULL ok
193: */
194: inline void *
195: schedCall(sched_task_t * __restrict task)
196: {
197: if (!task)
198: return (void*) -1;
199:
200: task->task_id++;
201: return task->task_func(task);
202: }
203:
204: /*
205: * schedFetch() - Fetch ready task
206: * @root = root task
207: * return: =NULL error or !=NULL ready task
208: */
209: inline void *
210: schedFetch(sched_root_task_t * __restrict root)
211: {
212: void *ptr;
213:
214: if (!root)
215: return NULL;
216:
217: if (root->root_hooks.hook_exec.fetch)
218: ptr = root->root_hooks.hook_exec.fetch(root, NULL);
219: else
220: ptr = NULL;
221:
222: return ptr;
223: }
224:
225: /*
226: * schedCancel() - Cancel task from scheduler
227: * @task = task
228: * return: -1 error or 0 ok
229: */
230: int
231: schedCancel(sched_task_t * __restrict task)
232: {
233: sched_queue_t *queue;
234:
235: if (!task || !task->task_root)
236: return -1;
237:
238: if (task->task_root->root_hooks.hook_exec.cancel)
239: if (task->task_root->root_hooks.hook_exec.cancel(task, NULL))
240: return -1;
241:
242: switch (task->task_type) {
243: case taskREAD:
244: queue = &task->task_root->root_read;
245: break;
246: case taskWRITE:
247: queue = &task->task_root->root_write;
248: break;
249: case taskTIMER:
250: queue = &task->task_root->root_timer;
251: break;
252: case taskEVENT:
253: queue = &task->task_root->root_event;
254: break;
255: case taskREADY:
256: queue = &task->task_root->root_ready;
257: break;
258: default:
259: queue = NULL;
260: }
261: if (queue)
262: TAILQ_REMOVE(queue, task, task_node);
263: if (task->task_type != taskUNUSE) {
264: task->task_type = taskUNUSE;
265: TAILQ_INSERT_TAIL(&task->task_root->root_unuse, task, task_node);
266: }
267:
268: return 0;
269: }
270:
271: /*
272: * schedCancelby() - Cancel task from scheduler by criteria
273: * @root = root task
274: * @queue = cancel from queue, if =NULL cancel same task from all queues
275: * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TV]
276: * @param = search parameter
277: * @hook = custom cleanup hook function, may be NULL
278: * return: -1 error or 0 ok
279: */
280: int
281: schedCancelby(sched_root_task_t * __restrict root, sched_queue_t * __restrict queue,
282: u_char criteria, void *param, sched_hook_func_t hook)
283: {
284: sched_task_t *task;
285: int flg = 0;
286:
287: if (!root)
288: return -1;
289: if (!queue) {
290: if (schedCancelby(root, &root->root_read, criteria, param, hook))
291: return -2;
292: if (schedCancelby(root, &root->root_write, criteria, param, hook))
293: return -2;
294: if (schedCancelby(root, &root->root_timer, criteria, param, hook))
295: return -2;
296: if (schedCancelby(root, &root->root_event, criteria, param, hook))
297: return -2;
298: if (schedCancelby(root, &root->root_ready, criteria, param, hook))
299: return -2;
300: if (schedCancelby(root, &root->root_read, criteria, param, hook))
301: return -2;
302: return 0;
303: }
304:
305: TAILQ_FOREACH(task, queue, task_node)
306: if (criteria == CRITERIA_CALL) {
307: if (task->task_func == (sched_task_func_t) param) {
308: flg++;
309: break;
310: }
311: } else if (criteria == CRITERIA_ARG) {
312: if (task->task_arg == param) {
313: flg++;
314: break;
315: }
316: } else if (criteria == CRITERIA_FD) {
317: if (TASK_FD(task) == (u_long) param) {
318: flg++;
319: break;
320: }
321: } else if (criteria == CRITERIA_VAL) {
322: if (TASK_VAL(task) == (u_long) param) {
323: flg++;
324: break;
325: }
326: } else if (criteria == CRITERIA_TV) {
327: if (&TASK_TV(task) == (struct timeval*) param) {
328: flg++;
329: break;
330: }
331: } else {
332: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
333: return -1;
334: }
335: if (!flg || !task) /* task not found */
336: return 0;
337:
338: if (task->task_root->root_hooks.hook_exec.cancel)
339: if (task->task_root->root_hooks.hook_exec.cancel(task, NULL))
340: return -1;
341: if (hook)
342: if (hook(task, NULL))
343: return -3;
344:
345: TAILQ_REMOVE(queue, task, task_node);
346:
347: if (task->task_type != taskUNUSE) {
348: task->task_type = taskUNUSE;
349: TAILQ_INSERT_TAIL(&task->task_root->root_unuse, task, task_node);
350: }
351: return 0;
352: }
353:
354: /*
355: * schedRun() - Scheduler *run loop*
356: * @root = root task
357: * return: -1 error or 0 ok
358: */
359: int
360: schedRun(sched_root_task_t * __restrict root)
361: {
362: sched_task_t *task;
363:
364: if (!root)
365: return -1;
366:
367: if (root->root_hooks.hook_exec.run)
368: if (root->root_hooks.hook_exec.run(root, NULL))
369: return -1;
370: if (root->root_hooks.hook_exec.fetch)
371: while ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
372: schedCall(task);
373:
374: return 0;
375: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>