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