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.7.2.1 2012/05/15 15:47:12 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: #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: *
90: * @root = root task
91: * return: -1 error or 0 ok
92: */
93: int
94: schedRegisterHooks(sched_root_task_t * __restrict root)
95: {
96: assert(root);
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: root->root_hooks.hook_exec.exception = sched_hook_exception;
108:
109: root->root_hooks.hook_root.init = sched_hook_init;
110: root->root_hooks.hook_root.fini = sched_hook_fini;
111: return 0;
112: }
113:
114: /*
115: * schedInit() - Init scheduler
116: *
117: * @data = optional data if !=NULL
118: * @datlen = data len if data is set
119: * return: allocated root task if ok or NULL error
120: */
121: sched_root_task_t *
122: schedInit(void ** __restrict data, size_t datlen)
123: {
124: sched_root_task_t *root = NULL;
125: int (*func)(sched_root_task_t *);
126: #ifdef HAVE_LIBPTHREAD
127: register int i;
128: #endif
129:
130: root = malloc(sizeof(sched_root_task_t));
131: if (!root) {
132: LOGERR;
133: } else {
134: memset(root, 0, sizeof(sched_root_task_t));
135:
136: /* INFINIT polling period by default */
137: sched_timespecinf(&root->root_poll);
138:
139: #ifdef HAVE_LIBPTHREAD
140: for (i = 0; i < taskMAX; i++)
141: if (pthread_mutex_init(&root->root_mtx[i], NULL)) {
142: LOGERR;
143: while (i)
144: pthread_mutex_destroy(&root->root_mtx[--i]);
145: free(root);
146: return NULL;
147: }
148:
149: for (i = 0; i < taskMAX; i++)
150: pthread_mutex_lock(&root->root_mtx[i]);
151: #endif
152:
153: TAILQ_INIT(&root->root_read);
154: TAILQ_INIT(&root->root_write);
155: TAILQ_INIT(&root->root_timer);
156: TAILQ_INIT(&root->root_event);
157: TAILQ_INIT(&root->root_eventlo);
158: TAILQ_INIT(&root->root_ready);
159: TAILQ_INIT(&root->root_unuse);
160:
161: #ifdef HAVE_LIBPTHREAD
162: for (i = 0; i < taskMAX; i++)
163: pthread_mutex_unlock(&root->root_mtx[i]);
164: #endif
165:
166: if (data && *data) {
167: if (datlen) {
168: root->root_data.iov_base = *data;
169: root->root_data.iov_len = datlen;
170: } else { /* if datlen == 0, switch to callbacks init mode */
171: /* little hack :) for correct initialization of scheduler */
172: func = (int(*)(sched_root_task_t*)) data;
173: func(root);
174: }
175: }
176:
177: if (root->root_hooks.hook_root.init)
178: root->root_hooks.hook_root.init(root, NULL);
179: }
180:
181: return root;
182: }
183:
184: /*
185: * schedEnd() - End scheduler & free all resources
186: *
187: * @root = root task
188: * return: -1 error or 0 ok
189: */
190: int
191: schedEnd(sched_root_task_t ** __restrict root)
192: {
193: sched_task_t *task, *tmp;
194: #ifdef HAVE_LIBPTHREAD
195: register int i;
196: #endif
197:
198: if (!root || !*root)
199: return -1;
200:
201: TAILQ_FOREACH_SAFE(task, &(*root)->root_read, task_node, tmp) {
202: schedCancel(task);
203: }
204: TAILQ_FOREACH_SAFE(task, &(*root)->root_write, task_node, tmp) {
205: schedCancel(task);
206: }
207: TAILQ_FOREACH_SAFE(task, &(*root)->root_timer, task_node, tmp) {
208: schedCancel(task);
209: }
210: TAILQ_FOREACH_SAFE(task, &(*root)->root_event, task_node, tmp) {
211: schedCancel(task);
212: }
213: TAILQ_FOREACH_SAFE(task, &(*root)->root_eventlo, task_node, tmp) {
214: schedCancel(task);
215: }
216: TAILQ_FOREACH_SAFE(task, &(*root)->root_ready, task_node, tmp) {
217: schedCancel(task);
218: }
219:
220: #ifdef HAVE_LIBPTHREAD
221: pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
222: #endif
223: while ((task = TAILQ_FIRST(&(*root)->root_unuse))) {
224: TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
225: free(task);
226: }
227: #ifdef HAVE_LIBPTHREAD
228: pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
229: #endif
230:
231: if ((*root)->root_hooks.hook_root.fini)
232: (*root)->root_hooks.hook_root.fini(*root, NULL);
233:
234: #ifdef HAVE_LIBPTHREAD
235: for (i = 0; i < taskMAX; i++)
236: pthread_mutex_destroy(&(*root)->root_mtx[i]);
237: #endif
238:
239: free(*root);
240: *root = NULL;
241: return 0;
242: }
243:
244: /*
245: * schedCall() - Call task execution function
246: *
247: * @task = current task
248: * return: !=NULL error or =NULL ok
249: */
250: inline void *
251: schedCall(sched_task_t * __restrict task)
252: {
253: void *ptr = (void*) -1;
254:
255: if (!task)
256: return ptr;
257:
258: if (!TASK_ISLOCKED(task))
259: TASK_LOCK(task);
260:
261: task->task_id++;
262: ptr = task->task_func(task);
263:
264: TASK_UNLOCK(task);
265: return ptr;
266: }
267:
268: /*
269: * schedFetch() - Fetch ready task
270: *
271: * @root = root task
272: * return: =NULL error or !=NULL ready task
273: */
274: inline void *
275: schedFetch(sched_root_task_t * __restrict root)
276: {
277: void *ptr;
278:
279: if (!root)
280: return NULL;
281:
282: if (root->root_hooks.hook_exec.fetch)
283: ptr = root->root_hooks.hook_exec.fetch(root, NULL);
284: else
285: ptr = NULL;
286:
287: return ptr;
288: }
289:
290: /*
291: * schedCancel() - Cancel task from scheduler
292: *
293: * @task = task
294: * return: -1 error or 0 ok
295: */
296: int
297: schedCancel(sched_task_t * __restrict task)
298: {
299: sched_queue_t *queue;
300:
301: if (!task || !TASK_ROOT(task))
302: return -1;
303:
304: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
305: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
306: return -1;
307:
308: switch (TASK_TYPE(task)) {
309: case taskREAD:
310: queue = &TASK_ROOT(task)->root_read;
311: break;
312: case taskWRITE:
313: queue = &TASK_ROOT(task)->root_write;
314: break;
315: case taskTIMER:
316: queue = &TASK_ROOT(task)->root_timer;
317: break;
318: case taskEVENT:
319: queue = &TASK_ROOT(task)->root_event;
320: break;
321: case taskEVENTLO:
322: queue = &TASK_ROOT(task)->root_eventlo;
323: break;
324: case taskREADY:
325: queue = &TASK_ROOT(task)->root_ready;
326: break;
327: default:
328: queue = NULL;
329: }
330: if (queue) {
331: #ifdef HAVE_LIBPTHREAD
332: pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
333: #endif
334: TAILQ_REMOVE(queue, task, task_node);
335: #ifdef HAVE_LIBPTHREAD
336: pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
337: #endif
338: }
339: if (TASK_TYPE(task) != taskUNUSE)
340: _sched_unuseTask(task);
341:
342: return 0;
343: }
344:
345: /*
346: * schedCancelby() - Cancel task from scheduler by criteria
347: *
348: * @root = root task
349: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
350: * @criteria = find task by criteria [CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_TS]
351: * @param = search parameter
352: * @hook = custom cleanup hook function, may be NULL
353: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
354: */
355: int
356: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
357: u_char criteria, void *param, sched_hook_func_t hook)
358: {
359: sched_task_t *task, *tmp;
360: sched_queue_t *queue;
361: register int flg = 0;
362:
363: if (!root)
364: return -1;
365: if (type == taskMAX) {
366: if (schedCancelby(root, taskREAD, criteria, param, hook))
367: return -2;
368: if (schedCancelby(root, taskWRITE, criteria, param, hook))
369: return -2;
370: if (schedCancelby(root, taskTIMER, criteria, param, hook))
371: return -2;
372: if (schedCancelby(root, taskEVENT, criteria, param, hook))
373: return -2;
374: if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
375: return -2;
376: if (schedCancelby(root, taskREADY, criteria, param, hook))
377: return -2;
378: return 0;
379: }
380: switch (type) {
381: case taskREAD:
382: queue = &root->root_read;
383: break;
384: case taskWRITE:
385: queue = &root->root_write;
386: break;
387: case taskTIMER:
388: queue = &root->root_timer;
389: break;
390: case taskEVENT:
391: queue = &root->root_event;
392: break;
393: case taskEVENTLO:
394: queue = &root->root_eventlo;
395: break;
396: case taskREADY:
397: queue = &root->root_ready;
398: break;
399: default:
400: return 0;
401: }
402:
403: #ifdef HAVE_LIBPTHREAD
404: pthread_mutex_lock(&root->root_mtx[type]);
405: #endif
406: TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
407: flg ^= flg;
408: switch (criteria) {
409: case CRITERIA_CALL:
410: if (TASK_FUNC(task) == (sched_task_func_t) param)
411: flg = 1;
412: break;
413: case CRITERIA_ARG:
414: if (TASK_ARG(task) == param)
415: flg = 1;
416: break;
417: case CRITERIA_FD:
418: if (TASK_FD(task) == (intptr_t) param)
419: flg = 1;
420: break;
421: case CRITERIA_VAL:
422: if (TASK_VAL(task) == (u_long) param)
423: flg = 1;
424: break;
425: case CRITERIA_TS:
426: if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
427: flg = 1;
428: break;
429: default:
430: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
431: flg = -1;
432: }
433: if (flg < 0)
434: break;
435: /* cancel choosen task */
436: if (flg > 0) {
437: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
438: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
439: flg = -1;
440: break;
441: }
442: /* custom hook */
443: if (hook)
444: if (hook(task, NULL)) {
445: flg = -3;
446: break;
447: }
448:
449: TAILQ_REMOVE(queue, task, task_node);
450: if (TASK_TYPE(task) != taskUNUSE)
451: _sched_unuseTask(task);
452:
453: flg ^= flg; /* ok */
454: }
455: }
456: #ifdef HAVE_LIBPTHREAD
457: pthread_mutex_unlock(&root->root_mtx[type]);
458: #endif
459: return flg;
460: }
461:
462: /*
463: * schedRun() - Scheduler *run loop*
464: *
465: * @root = root task
466: * @killState = kill condition variable, if !=0 stop scheduler loop
467: * return: -1 error or 0 ok
468: */
469: int
470: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
471: {
472: sched_task_t *task;
473:
474: if (!root)
475: return -1;
476:
477: if (root->root_hooks.hook_exec.run)
478: if (root->root_hooks.hook_exec.run(root, NULL))
479: return -1;
480:
481: if (killState) {
482: if (root->root_hooks.hook_exec.condition)
483: /* condition scheduler loop */
484: while (root && root->root_hooks.hook_exec.fetch &&
485: root->root_hooks.hook_exec.condition &&
486: root->root_hooks.hook_exec.condition(root, (void*) killState)) {
487: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
488: schedCall(task);
489: }
490: else
491: /* trigger scheduler loop */
492: while (!*killState && root && root->root_hooks.hook_exec.fetch) {
493: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
494: schedCall(task);
495: }
496: } else
497: /* infinite scheduler loop */
498: while (root && root->root_hooks.hook_exec.fetch)
499: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
500: schedCall(task);
501:
502: return 0;
503: }
504:
505: /*
506: * schedPolling() - Polling timeout period if no timer task is present
507: *
508: * @root = root task
509: * @ts = timeout polling period, if ==NULL INFINIT timeout
510: * @tsold = old timeout polling if !=NULL
511: * return: -1 error or 0 ok
512: */
513: inline int
514: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts,
515: struct timespec * __restrict tsold)
516: {
517: if (!root)
518: return -1;
519:
520: if (tsold)
521: *tsold = root->root_poll;
522:
523: if (!ts)
524: sched_timespecinf(&root->root_poll);
525: else
526: root->root_poll = *ts;
527:
528: return 0;
529: }
530:
531: /*
532: * schedTermCondition() - Activate hook for scheduler condition kill
533: *
534: * @root = root task
535: * @condValue = condition value, kill schedRun() if condValue == killState
536: * return: -1 error ok 0 ok
537: */
538: inline int
539: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
540: {
541: if (!root)
542: return -1;
543:
544: root->root_cond = condValue;
545: root->root_hooks.hook_exec.condition = sched_hook_condition;
546: return 0;
547: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>