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.28.14.1 2022/10/03 22:16:36 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 - 2022
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: int
60: sched_GetErrno()
61: {
62: return sched_Errno;
63: }
64:
65: // sched_GetError() Get error text of last operation
66: const char *
67: sched_GetError()
68: {
69: return sched_Error;
70: }
71:
72: // sched_SetErr() Set error to variables for internal use!!!
73: 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:
86: /* string support functions directly imported from OpenBSD */
87:
88: #ifndef HAVE_STRLCAT
89: /*
90: * Appends src to string dst of size siz (unlike strncat, siz is the
91: * full size of dst, not space left). At most siz-1 characters
92: * will be copied. Always NUL terminates (unless siz <= strlen(dst)).
93: * Returns strlen(src) + MIN(siz, strlen(initial dst)).
94: * If retval >= siz, truncation occurred.
95: */
96: size_t
97: strlcat(char * __restrict dst, const char * __restrict src, size_t siz)
98: {
99: char *d = dst;
100: const char *s = src;
101: size_t n = siz;
102: size_t dlen;
103:
104: /* Find the end of dst and adjust bytes left but don't go past end */
105: while (n-- != 0 && *d != '\0')
106: d++;
107: dlen = d - dst;
108: n = siz - dlen;
109:
110: if (n == 0)
111: return(dlen + strlen(s));
112: while (*s != '\0') {
113: if (n != 1) {
114: *d++ = *s;
115: n--;
116: }
117: s++;
118: }
119: *d = '\0';
120:
121: return(dlen + (s - src)); /* count does not include NUL */
122: }
123: #endif
124: #ifndef HAVE_STRLCPY
125: /*
126: * Copy src to string dst of size siz. At most siz-1 characters
127: * will be copied. Always NUL terminates (unless siz == 0).
128: * Returns strlen(src); if retval >= siz, truncation occurred.
129: */
130: size_t
131: strlcpy(char * __restrict dst, const char * __restrict src, size_t siz)
132: {
133: char *d = dst;
134: const char *s = src;
135: size_t n = siz;
136:
137: /* Copy as many bytes as will fit */
138: if (n != 0) {
139: while (--n != 0) {
140: if ((*d++ = *s++) == '\0')
141: break;
142: }
143: }
144:
145: /* Not enough room in dst, add NUL and traverse rest of src */
146: if (n == 0) {
147: if (siz != 0)
148: *d = '\0'; /* NUL-terminate dst */
149: while (*s++)
150: ;
151: }
152:
153: return(s - src - 1); /* count does not include NUL */
154: }
155: #endif
156:
157:
158: /* Init and prepare scheduler functions */
159:
160: /*
161: * schedRegisterHooks() - Register IO handles and bind tasks to it
162: *
163: * @root = root task
164: * return: -1 error or 0 ok
165: */
166: int
167: schedRegisterHooks(sched_root_task_t * __restrict root)
168: {
169: assert(root);
170:
171: if (root->root_hooks.hook_root.fini)
172: root->root_hooks.hook_root.fini(root, NULL);
173: memset(&root->root_hooks, 0, sizeof root->root_hooks);
174:
175: root->root_hooks.hook_add.read = sched_hook_read;
176: root->root_hooks.hook_add.write = sched_hook_write;
177: #if defined(HAVE_TIMER_CREATE) && defined(HAVE_TIMER_SETTIME) && defined(HAVE_TIMER_DELETE)
178: root->root_hooks.hook_add.rtc = sched_hook_rtc;
179: #endif
180: #if SUP_ENABLE == KQ_SUPPORT
181: root->root_hooks.hook_add.alarm = sched_hook_alarm;
182: root->root_hooks.hook_add.node = sched_hook_node;
183: root->root_hooks.hook_add.proc = sched_hook_proc;
184: root->root_hooks.hook_add.signal = sched_hook_signal;
185: #ifdef EVFILT_USER
186: root->root_hooks.hook_add.user = sched_hook_user;
187: #endif
188: #endif /* KQ_SUPPORT */
189: #ifdef HAVE_LIBPTHREAD
190: root->root_hooks.hook_add.thread = sched_hook_thread;
191: #endif
192:
193: root->root_hooks.hook_exec.cancel = sched_hook_cancel;
194: root->root_hooks.hook_exec.fetch = sched_hook_fetch;
195: root->root_hooks.hook_exec.exception = sched_hook_exception;
196:
197: root->root_hooks.hook_root.init = sched_hook_init;
198: root->root_hooks.hook_root.fini = sched_hook_fini;
199: return 0;
200: }
201:
202: /*
203: * schedInit() - Init scheduler
204: *
205: * @data = optional data if !=NULL
206: * @datlen = data len if data is set
207: * return: allocated root task if ok or NULL error
208: */
209: sched_root_task_t *
210: schedInit(void ** __restrict data, size_t datlen)
211: {
212: sched_root_task_t *root = NULL;
213: int (*func)(sched_root_task_t *);
214: #ifdef HAVE_LIBPTHREAD
215: register int i;
216: #endif
217:
218: root = e_malloc(sizeof(sched_root_task_t));
219: if (!root) {
220: LOGERR;
221: } else {
222: memset(root, 0, sizeof(sched_root_task_t));
223:
224: /* set default maximum regular task hit misses */
225: root->root_miss = MAX_TASK_MISS;
226:
227: /* INFINIT polling period by default */
228: sched_timespecinf(&root->root_poll);
229:
230: #ifdef HAVE_LIBPTHREAD
231: for (i = 0; i < taskMAX; i++)
232: if ((errno = pthread_mutex_init(&root->root_mtx[i], NULL))) {
233: LOGERR;
234: while (i)
235: pthread_mutex_destroy(&root->root_mtx[--i]);
236: e_free(root);
237: return NULL;
238: }
239:
240: for (i = 0; i < taskMAX; i++)
241: pthread_mutex_lock(&root->root_mtx[i]);
242: #endif
243:
244: TAILQ_INIT(&root->root_read);
245: TAILQ_INIT(&root->root_write);
246: TAILQ_INIT(&root->root_timer);
247: TAILQ_INIT(&root->root_alarm);
248: TAILQ_INIT(&root->root_rtc);
249: TAILQ_INIT(&root->root_node);
250: TAILQ_INIT(&root->root_proc);
251: TAILQ_INIT(&root->root_signal);
252: TAILQ_INIT(&root->root_aio);
253: TAILQ_INIT(&root->root_lio);
254: TAILQ_INIT(&root->root_user);
255: TAILQ_INIT(&root->root_event);
256: TAILQ_INIT(&root->root_task);
257: TAILQ_INIT(&root->root_suspend);
258: TAILQ_INIT(&root->root_ready);
259: TAILQ_INIT(&root->root_unuse);
260: TAILQ_INIT(&root->root_thread);
261:
262: #ifdef HAVE_LIBPTHREAD
263: for (i = 0; i < taskMAX; i++)
264: pthread_mutex_unlock(&root->root_mtx[i]);
265: #endif
266:
267: if (data && *data) {
268: if (datlen) {
269: root->root_data.iov_base = *data;
270: root->root_data.iov_len = datlen;
271: } else { /* if datlen == 0, switch to callbacks init mode */
272: /* little hack :) for correct initialization of scheduler */
273: func = (int(*)(sched_root_task_t*)) data;
274: func(root);
275: }
276: }
277:
278: if (root->root_hooks.hook_root.init)
279: root->root_hooks.hook_root.init(root, NULL);
280: }
281:
282: return root;
283: }
284:
285: /*
286: * schedEnd() - End scheduler & free all resources
287: *
288: * @root = root task
289: * return: -1 error or 0 ok
290: */
291: int
292: schedEnd(sched_root_task_t ** __restrict root)
293: {
294: sched_task_t *task, *tmp;
295: #ifdef HAVE_LIBPTHREAD
296: register int i;
297: #endif
298:
299: if (!root || !*root)
300: return -1;
301:
302: #if 0
303: TAILQ_FOREACH_SAFE(task, &(*root)->root_read, task_node, tmp)
304: printf("read=%p\n", task);
305: TAILQ_FOREACH_SAFE(task, &(*root)->root_write, task_node, tmp)
306: printf("write=%p\n", task);
307: TAILQ_FOREACH_SAFE(task, &(*root)->root_timer, task_node, tmp)
308: printf("timer=%p\n", task);
309: TAILQ_FOREACH_SAFE(task, &(*root)->root_alarm, task_node, tmp)
310: printf("alarm=%p\n", task);
311: TAILQ_FOREACH_SAFE(task, &(*root)->root_rtc, task_node, tmp)
312: printf("rtc=%p\n", task);
313: TAILQ_FOREACH_SAFE(task, &(*root)->root_node, task_node, tmp)
314: printf("node=%p\n", task);
315: TAILQ_FOREACH_SAFE(task, &(*root)->root_proc, task_node, tmp)
316: printf("proc=%p\n", task);
317: TAILQ_FOREACH_SAFE(task, &(*root)->root_signal, task_node, tmp)
318: printf("signal=%p\n", task);
319: TAILQ_FOREACH_SAFE(task, &(*root)->root_aio, task_node, tmp)
320: printf("aio=%p\n", task);
321: TAILQ_FOREACH_SAFE(task, &(*root)->root_lio, task_node, tmp)
322: printf("lio=%p\n", task);
323: TAILQ_FOREACH_SAFE(task, &(*root)->root_user, task_node, tmp)
324: printf("user=%p\n", task);
325: TAILQ_FOREACH_SAFE(task, &(*root)->root_event, task_node, tmp)
326: printf("event=%p\n", task);
327: TAILQ_FOREACH_SAFE(task, &(*root)->root_suspend, task_node, tmp)
328: printf("suspend=%p\n", task);
329: TAILQ_FOREACH_SAFE(task, &(*root)->root_ready, task_node, tmp)
330: printf("ready=%p\n", task);
331: TAILQ_FOREACH_SAFE(task, &(*root)->root_thread, task_node, tmp)
332: printf("thread=%p\n", task);
333: TAILQ_FOREACH_SAFE(task, &(*root)->root_task, task_node, tmp)
334: printf("task=%p\n", task);
335: TAILQ_FOREACH_SAFE(task, &(*root)->root_unuse, task_node, tmp)
336: printf("unuse=%p\n", task);
337: fflush(stdout);
338: #endif
339:
340: TAILQ_FOREACH_SAFE(task, &(*root)->root_read, task_node, tmp)
341: schedCancel(task);
342: TAILQ_FOREACH_SAFE(task, &(*root)->root_write, task_node, tmp)
343: schedCancel(task);
344: TAILQ_FOREACH_SAFE(task, &(*root)->root_timer, task_node, tmp)
345: schedCancel(task);
346: TAILQ_FOREACH_SAFE(task, &(*root)->root_alarm, task_node, tmp)
347: schedCancel(task);
348: TAILQ_FOREACH_SAFE(task, &(*root)->root_rtc, task_node, tmp)
349: schedCancel(task);
350: TAILQ_FOREACH_SAFE(task, &(*root)->root_node, task_node, tmp)
351: schedCancel(task);
352: TAILQ_FOREACH_SAFE(task, &(*root)->root_proc, task_node, tmp)
353: schedCancel(task);
354: TAILQ_FOREACH_SAFE(task, &(*root)->root_signal, task_node, tmp)
355: schedCancel(task);
356: TAILQ_FOREACH_SAFE(task, &(*root)->root_aio, task_node, tmp)
357: schedCancel(task);
358: TAILQ_FOREACH_SAFE(task, &(*root)->root_lio, task_node, tmp)
359: schedCancel(task);
360: TAILQ_FOREACH_SAFE(task, &(*root)->root_user, task_node, tmp)
361: schedCancel(task);
362: TAILQ_FOREACH_SAFE(task, &(*root)->root_event, task_node, tmp)
363: schedCancel(task);
364: TAILQ_FOREACH_SAFE(task, &(*root)->root_suspend, task_node, tmp)
365: schedCancel(task);
366: TAILQ_FOREACH_SAFE(task, &(*root)->root_ready, task_node, tmp)
367: schedCancel(task);
368: TAILQ_FOREACH_SAFE(task, &(*root)->root_thread, task_node, tmp)
369: schedCancel(task);
370: TAILQ_FOREACH_SAFE(task, &(*root)->root_task, task_node, tmp)
371: schedCancel(task);
372:
373: SCHED_QLOCK((*root), taskUNUSE);
374: TAILQ_FOREACH_SAFE(task, &(*root)->root_unuse, task_node, tmp) {
375: TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
376: e_free(task);
377: }
378: SCHED_QUNLOCK((*root), taskUNUSE);
379:
380: if ((*root)->root_hooks.hook_root.fini)
381: (*root)->root_hooks.hook_root.fini(*root, NULL);
382:
383: #ifdef HAVE_LIBPTHREAD
384: for (i = 0; i < taskMAX; i++) {
385: SCHED_QTRYLOCK(*root, i);
386: SCHED_QUNLOCK(*root, i);
387: pthread_mutex_destroy(&(*root)->root_mtx[i]);
388: }
389: #endif
390:
391: e_free(*root);
392: *root = NULL;
393: return 0;
394: }
395:
396: /*
397: * schedCall() - Call task execution function
398: *
399: * @task = current task
400: * return: !=NULL error or =NULL ok
401: */
402: void *
403: schedCall(sched_task_t * __restrict task)
404: {
405: void *ptr = (void*) -1;
406:
407: if (!task)
408: return ptr;
409:
410: if (!TASK_ISLOCKED(task))
411: TASK_LOCK(task);
412:
413: ptr = task->task_func(task);
414:
415: TASK_UNLOCK(task);
416: return ptr;
417: }
418:
419: /*
420: * schedFetch() - Fetch ready task
421: *
422: * @root = root task
423: * return: =NULL error or !=NULL ready task
424: */
425: void *
426: schedFetch(sched_root_task_t * __restrict root)
427: {
428: void *ptr;
429:
430: if (!root)
431: return NULL;
432:
433: if (root->root_hooks.hook_exec.fetch)
434: ptr = root->root_hooks.hook_exec.fetch(root, NULL);
435: else
436: ptr = NULL;
437:
438: return ptr;
439: }
440:
441: /*
442: * schedTrigger() - Triggering USER task
443: *
444: * @task = task
445: * return: -1 error or 0 ok
446: */
447: int
448: schedTrigger(sched_task_t * __restrict task)
449: {
450: #if SUP_ENABLE != KQ_SUPPORT
451: sched_SetErr(ENOTSUP, "disabled kqueue support");
452: return -1;
453: #else
454: #ifndef EVFILT_USER
455: sched_SetErr(ENOTSUP, "Not supported kevent() filter");
456: return -1;
457: #else
458: struct kevent chg[1];
459: struct timespec timeout = { 0, 0 };
460:
461: if (!task || !TASK_ROOT(task))
462: return -1;
463:
464: #ifdef __NetBSD__
465: EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (intptr_t) TASK_VAL(task));
466: #else
467: EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (void*) TASK_VAL(task));
468: #endif
469: if (kevent(TASK_ROOT(task)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
470: LOGERR;
471: return -1;
472: }
473:
474: return 0;
475: #endif
476: #endif /* KQ_SUPPORT */
477: }
478:
479: /*
480: * schedQuery() - Query task in scheduler
481: *
482: * @task = task
483: * return: -1 error, 0 found and 1 not found
484: */
485: int
486: schedQuery(sched_task_t * __restrict task)
487: {
488: sched_queue_t *queue;
489: sched_task_t *t;
490:
491: if (!task || !TASK_ROOT(task))
492: return -1; /* error */
493:
494: switch (TASK_TYPE(task)) {
495: case taskREAD:
496: queue = &TASK_ROOT(task)->root_read;
497: break;
498: case taskWRITE:
499: queue = &TASK_ROOT(task)->root_write;
500: break;
501: case taskTIMER:
502: queue = &TASK_ROOT(task)->root_timer;
503: break;
504: case taskALARM:
505: queue = &TASK_ROOT(task)->root_alarm;
506: break;
507: case taskRTC:
508: queue = &TASK_ROOT(task)->root_rtc;
509: break;
510: case taskNODE:
511: queue = &TASK_ROOT(task)->root_node;
512: break;
513: case taskPROC:
514: queue = &TASK_ROOT(task)->root_proc;
515: break;
516: case taskSIGNAL:
517: queue = &TASK_ROOT(task)->root_signal;
518: break;
519: case taskAIO:
520: queue = &TASK_ROOT(task)->root_aio;
521: break;
522: case taskLIO:
523: queue = &TASK_ROOT(task)->root_lio;
524: break;
525: case taskUSER:
526: queue = &TASK_ROOT(task)->root_user;
527: break;
528: case taskEVENT:
529: queue = &TASK_ROOT(task)->root_event;
530: break;
531: case taskTASK:
532: queue = &TASK_ROOT(task)->root_task;
533: break;
534: case taskSUSPEND:
535: queue = &TASK_ROOT(task)->root_suspend;
536: break;
537: case taskREADY:
538: queue = &TASK_ROOT(task)->root_ready;
539: break;
540: case taskTHREAD:
541: queue = &TASK_ROOT(task)->root_thread;
542: break;
543: default:
544: return 1; /* not in queue */
545: }
546: if (queue)
547: TAILQ_FOREACH(t, queue, task_node)
548: if (TASK_ID(t) == TASK_ID(task))
549: return 0; /* found */
550:
551: return 1; /* not in queue */
552: }
553:
554: /*
555: * schedQueryby() - Query task in scheduler by criteria
556: *
557: * @root = root task
558: * @type = query from queue type, if =taskMAX query same task from all queues
559: * @criteria = find task by criteria
560: * [ CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|
561: * CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA|CRITERIA_DATLEN ]
562: * @param = search parameter
563: * return: -1 error, 0 found or 1 not found
564: */
565: int
566: schedQueryby(sched_root_task_t * __restrict root, sched_task_type_t type,
567: u_char criteria, void *param)
568: {
569: sched_task_t *task;
570: sched_queue_t *queue;
571: register int flg = 0;
572:
573: if (!root)
574: return -1;
575: /* if type == taskMAX check in all queues */
576: if (type == taskMAX) {
577: if ((flg = schedQueryby(root, taskREAD, criteria, param)) < 1)
578: return flg;
579: if ((flg = schedQueryby(root, taskWRITE, criteria, param)) < 1)
580: return flg;
581: if ((flg = schedQueryby(root, taskTIMER, criteria, param)) < 1)
582: return flg;
583: if ((flg = schedQueryby(root, taskALARM, criteria, param)) < 1)
584: return flg;
585: if ((flg = schedQueryby(root, taskRTC, criteria, param)) < 1)
586: return flg;
587: if ((flg = schedQueryby(root, taskNODE, criteria, param)) < 1)
588: return flg;
589: if ((flg = schedQueryby(root, taskPROC, criteria, param)) < 1)
590: return flg;
591: if ((flg = schedQueryby(root, taskSIGNAL, criteria, param)) < 1)
592: return flg;
593: if ((flg = schedQueryby(root, taskAIO, criteria, param)) < 1)
594: return flg;
595: if ((flg = schedQueryby(root, taskLIO, criteria, param)) < 1)
596: return flg;
597: if ((flg = schedQueryby(root, taskUSER, criteria, param)) < 1)
598: return flg;
599: if ((flg = schedQueryby(root, taskEVENT, criteria, param)) < 1)
600: return flg;
601: if ((flg = schedQueryby(root, taskTASK, criteria, param)) < 1)
602: return flg;
603: if ((flg = schedQueryby(root, taskSUSPEND, criteria, param)) < 1)
604: return flg;
605: if ((flg = schedQueryby(root, taskREADY, criteria, param)) < 1)
606: return flg;
607: if ((flg = schedQueryby(root, taskTHREAD, criteria, param)) < 1)
608: return flg;
609: return 1; /* not found */
610: }
611: /* choosen queue */
612: switch (type) {
613: case taskREAD:
614: queue = &root->root_read;
615: break;
616: case taskWRITE:
617: queue = &root->root_write;
618: break;
619: case taskTIMER:
620: queue = &root->root_timer;
621: break;
622: case taskALARM:
623: queue = &root->root_alarm;
624: break;
625: case taskRTC:
626: queue = &root->root_rtc;
627: break;
628: case taskNODE:
629: queue = &root->root_node;
630: break;
631: case taskPROC:
632: queue = &root->root_proc;
633: break;
634: case taskSIGNAL:
635: queue = &root->root_signal;
636: break;
637: case taskAIO:
638: queue = &root->root_aio;
639: break;
640: case taskLIO:
641: queue = &root->root_lio;
642: break;
643: case taskUSER:
644: queue = &root->root_user;
645: break;
646: case taskEVENT:
647: queue = &root->root_event;
648: break;
649: case taskTASK:
650: queue = &root->root_task;
651: break;
652: case taskSUSPEND:
653: queue = &root->root_suspend;
654: break;
655: case taskREADY:
656: queue = &root->root_ready;
657: break;
658: case taskTHREAD:
659: queue = &root->root_thread;
660: break;
661: default:
662: return 1; /* not found */
663: }
664:
665: TAILQ_FOREACH(task, queue, task_node) {
666: switch (criteria) {
667: case CRITERIA_ANY:
668: return 0; /* found */
669: case CRITERIA_CALL:
670: if (TASK_FUNC(task) == (sched_task_func_t) param)
671: return 0; /* found */
672: break;
673: case CRITERIA_ARG:
674: if (TASK_ARG(task) == param)
675: return 0; /* found */
676: break;
677: case CRITERIA_FD:
678: if (TASK_FD(task) == (intptr_t) param)
679: return 0; /* found */
680: break;
681: case CRITERIA_ID:
682: case CRITERIA_VAL:
683: if (TASK_VAL(task) == (u_long) param)
684: return 0; /* found */
685: break;
686: case CRITERIA_TS:
687: if (!sched_timespeccmp(&TASK_TS(task),
688: (struct timespec*) param, -))
689: return 0; /* found */
690: break;
691: case CRITERIA_DATA:
692: if (TASK_DATA(task) == param)
693: return 0; /* found */
694: break;
695: case CRITERIA_DATLEN:
696: if (TASK_DATLEN(task) == (size_t) param)
697: return 0; /* found */
698: break;
699: default:
700: sched_SetErr(EINVAL, "Invalid parameter criteria %d",
701: criteria);
702: return 1; /* not found */
703: }
704: }
705:
706: return 1; /* not found */
707: }
708:
709: /*
710: * schedCancel() - Cancel task from scheduler
711: *
712: * @task = task
713: * return: -1 error or 0 ok
714: */
715: int
716: schedCancel(sched_task_t * __restrict task)
717: {
718: sched_queue_t *queue;
719:
720: if (!task || !TASK_ROOT(task))
721: return -1;
722:
723: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
724: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
725: return -1;
726:
727: switch (TASK_TYPE(task)) {
728: case taskREAD:
729: queue = &TASK_ROOT(task)->root_read;
730: break;
731: case taskWRITE:
732: queue = &TASK_ROOT(task)->root_write;
733: break;
734: case taskTIMER:
735: queue = &TASK_ROOT(task)->root_timer;
736: break;
737: case taskALARM:
738: queue = &TASK_ROOT(task)->root_alarm;
739: break;
740: case taskRTC:
741: queue = &TASK_ROOT(task)->root_rtc;
742: break;
743: case taskNODE:
744: queue = &TASK_ROOT(task)->root_node;
745: break;
746: case taskPROC:
747: queue = &TASK_ROOT(task)->root_proc;
748: break;
749: case taskSIGNAL:
750: queue = &TASK_ROOT(task)->root_signal;
751: break;
752: case taskAIO:
753: queue = &TASK_ROOT(task)->root_aio;
754: break;
755: case taskLIO:
756: queue = &TASK_ROOT(task)->root_lio;
757: break;
758: case taskUSER:
759: queue = &TASK_ROOT(task)->root_user;
760: break;
761: case taskEVENT:
762: queue = &TASK_ROOT(task)->root_event;
763: break;
764: case taskTASK:
765: queue = &TASK_ROOT(task)->root_task;
766: break;
767: case taskSUSPEND:
768: queue = &TASK_ROOT(task)->root_suspend;
769: break;
770: case taskREADY:
771: queue = &TASK_ROOT(task)->root_ready;
772: break;
773: case taskTHREAD:
774: queue = &TASK_ROOT(task)->root_thread;
775: break;
776: default:
777: queue = NULL;
778: }
779: if (queue)
780: remove_task_from(task, queue);
781: if (TASK_TYPE(task) != taskUNUSE)
782: sched_unuseTask(task);
783:
784: return 0;
785: }
786:
787: /*
788: * schedCancelby() - Cancel task from scheduler by criteria
789: *
790: * @root = root task
791: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
792: * @criteria = find task by criteria
793: * [ CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|
794: * CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA|CRITERIA_DATLEN ]
795: * @param = search parameter
796: * @hook = custom cleanup hook function, may be NULL
797: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
798: */
799: int
800: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
801: u_char criteria, void *param, sched_hook_func_t hook)
802: {
803: sched_task_t *task, *tmp;
804: sched_queue_t *queue;
805: register int flg = 0;
806:
807: if (!root)
808: return -1;
809: /* if type == taskMAX check in all queues */
810: if (type == taskMAX) {
811: if (schedCancelby(root, taskREAD, criteria, param, hook))
812: return -2;
813: if (schedCancelby(root, taskWRITE, criteria, param, hook))
814: return -2;
815: if (schedCancelby(root, taskTIMER, criteria, param, hook))
816: return -2;
817: if (schedCancelby(root, taskALARM, criteria, param, hook))
818: return -2;
819: if (schedCancelby(root, taskRTC, criteria, param, hook))
820: return -2;
821: if (schedCancelby(root, taskNODE, criteria, param, hook))
822: return -2;
823: if (schedCancelby(root, taskPROC, criteria, param, hook))
824: return -2;
825: if (schedCancelby(root, taskSIGNAL, criteria, param, hook))
826: return -2;
827: if (schedCancelby(root, taskAIO, criteria, param, hook))
828: return -2;
829: if (schedCancelby(root, taskLIO, criteria, param, hook))
830: return -2;
831: if (schedCancelby(root, taskUSER, criteria, param, hook))
832: return -2;
833: if (schedCancelby(root, taskEVENT, criteria, param, hook))
834: return -2;
835: if (schedCancelby(root, taskTASK, criteria, param, hook))
836: return -2;
837: if (schedCancelby(root, taskSUSPEND, criteria, param, hook))
838: return -2;
839: if (schedCancelby(root, taskREADY, criteria, param, hook))
840: return -2;
841: if (schedCancelby(root, taskTHREAD, criteria, param, hook))
842: return -2;
843: return 0;
844: }
845: /* choosen queue */
846: switch (type) {
847: case taskREAD:
848: queue = &root->root_read;
849: break;
850: case taskWRITE:
851: queue = &root->root_write;
852: break;
853: case taskTIMER:
854: queue = &root->root_timer;
855: break;
856: case taskALARM:
857: queue = &root->root_alarm;
858: break;
859: case taskRTC:
860: queue = &root->root_rtc;
861: break;
862: case taskNODE:
863: queue = &root->root_node;
864: break;
865: case taskPROC:
866: queue = &root->root_proc;
867: break;
868: case taskSIGNAL:
869: queue = &root->root_signal;
870: break;
871: case taskAIO:
872: queue = &root->root_aio;
873: break;
874: case taskLIO:
875: queue = &root->root_lio;
876: break;
877: case taskUSER:
878: queue = &root->root_user;
879: break;
880: case taskEVENT:
881: queue = &root->root_event;
882: break;
883: case taskTASK:
884: queue = &root->root_task;
885: break;
886: case taskSUSPEND:
887: queue = &root->root_suspend;
888: break;
889: case taskREADY:
890: queue = &root->root_ready;
891: break;
892: case taskTHREAD:
893: queue = &root->root_thread;
894: break;
895: default:
896: return 0;
897: }
898:
899: SCHED_QLOCK(root, type);
900: TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
901: flg ^= flg;
902: switch (criteria) {
903: case CRITERIA_ANY:
904: flg = 1;
905: break;
906: case CRITERIA_CALL:
907: if (TASK_FUNC(task) == (sched_task_func_t) param)
908: flg = 1;
909: break;
910: case CRITERIA_ARG:
911: if (TASK_ARG(task) == param)
912: flg = 1;
913: break;
914: case CRITERIA_FD:
915: if (TASK_FD(task) == (intptr_t) param)
916: flg = 1;
917: break;
918: case CRITERIA_ID:
919: case CRITERIA_VAL:
920: if (TASK_VAL(task) == (u_long) param)
921: flg = 1;
922: break;
923: case CRITERIA_TS:
924: if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
925: flg = 1;
926: break;
927: case CRITERIA_DATA:
928: if (TASK_DATA(task) == param)
929: flg = 1;
930: break;
931: case CRITERIA_DATLEN:
932: if (TASK_DATLEN(task) == (size_t) param)
933: flg = 1;
934: break;
935: default:
936: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
937: flg = -1;
938: }
939: if (flg < 0) /* error */
940: break;
941: /* cancel choosen task */
942: if (flg > 0) {
943: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
944: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
945: flg = -1;
946: break;
947: }
948: /* custom hook */
949: if (hook)
950: if (hook(task, NULL)) {
951: flg = -3;
952: break;
953: }
954:
955: TAILQ_REMOVE(queue, task, task_node);
956: if (TASK_TYPE(task) != taskUNUSE)
957: sched_unuseTask(task);
958:
959: flg ^= flg; /* ok */
960: }
961: }
962: SCHED_QUNLOCK(root, type);
963:
964: return flg;
965: }
966:
967: /*
968: * schedRun() - Scheduler *run loop*
969: *
970: * @root = root task
971: * @killState = kill condition variable, if !=0 stop scheduler loop
972: * return: -1 error or 0 ok
973: */
974: int
975: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
976: {
977: sched_task_t *task;
978:
979: if (!root)
980: return -1;
981:
982: if (root->root_hooks.hook_exec.run)
983: if (root->root_hooks.hook_exec.run(root, NULL))
984: return -1;
985:
986: if (killState) {
987: if (root->root_hooks.hook_exec.condition)
988: /* condition scheduler loop */
989: while (root && root->root_hooks.hook_exec.fetch &&
990: root->root_hooks.hook_exec.condition &&
991: root->root_hooks.hook_exec.condition(root, (void*) killState)) {
992: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
993: root->root_ret = schedCall(task);
994: }
995: else
996: /* trigger scheduler loop */
997: while (!*killState && root && root->root_hooks.hook_exec.fetch) {
998: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
999: root->root_ret = schedCall(task);
1000: }
1001: } else
1002: /* infinite scheduler loop */
1003: while (root && root->root_hooks.hook_exec.fetch)
1004: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1005: root->root_ret = schedCall(task);
1006:
1007: return 0;
1008: }
1009:
1010: /*
1011: * schedPolling() - Polling timeout period if no timer task is present
1012: *
1013: * @root = root task
1014: * @ts = timeout polling period, if ==NULL INFINIT timeout
1015: * @tsold = old timeout polling if !=NULL
1016: * return: -1 error or 0 ok
1017: */
1018: int
1019: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts,
1020: struct timespec * __restrict tsold)
1021: {
1022: if (!root)
1023: return -1;
1024:
1025: if (tsold)
1026: *tsold = root->root_poll;
1027:
1028: if (!ts)
1029: sched_timespecinf(&root->root_poll);
1030: else
1031: root->root_poll = *ts;
1032:
1033: return 0;
1034: }
1035:
1036: /*
1037: * schedTermCondition() - Activate hook for scheduler condition kill
1038: *
1039: * @root = root task
1040: * @condValue = condition value, kill schedRun() if condValue == killState
1041: * return: -1 error or 0 ok
1042: */
1043: int
1044: schedTermCondition(sched_root_task_t * __restrict root, intptr_t * __restrict condValue)
1045: {
1046: if (!root && !condValue)
1047: return -1;
1048:
1049: *root->root_cond = *condValue;
1050: root->root_hooks.hook_exec.condition = sched_hook_condition;
1051: return 0;
1052: }
1053:
1054: /*
1055: * schedResumeby() - Resume suspended task
1056: *
1057: * @root = root task
1058: * @criteria = find task by criteria
1059: * [CRITERIA_ANY|CRITERIA_ID|CRITERIA_VAL|CRITERIA_DATA]
1060: * @param = search parameter (sched_task_t *task| unsigned long id)
1061: * return: -1 error or 0 resumed ok
1062: */
1063: int
1064: schedResumeby(sched_root_task_t * __restrict root, u_char criteria, void *param)
1065: {
1066: sched_task_t *task, *tmp;
1067: register int flg = 0;
1068:
1069: if (!root)
1070: return -1;
1071:
1072: SCHED_QLOCK(root, taskSUSPEND);
1073: TAILQ_FOREACH_SAFE(task, &root->root_suspend, task_node, tmp) {
1074: flg ^= flg;
1075: switch (criteria) {
1076: case CRITERIA_ANY:
1077: flg = 1;
1078: break;
1079: case CRITERIA_ID:
1080: case CRITERIA_VAL:
1081: if (TASK_VAL(task) == (u_long) param)
1082: flg = 1;
1083: break;
1084: case CRITERIA_DATA:
1085: if (TASK_ID(task) == (sched_task_t*) param)
1086: flg = 1;
1087: break;
1088: default:
1089: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
1090: flg = -1;
1091: }
1092: if (flg < 0)
1093: break;
1094: /* resume choosen task */
1095: if (flg > 0) {
1096: if (root->root_hooks.hook_exec.resume)
1097: if (root->root_hooks.hook_exec.resume(task, NULL)) {
1098: flg = -1;
1099: break;
1100: }
1101:
1102: TAILQ_REMOVE(&root->root_suspend, task, task_node);
1103:
1104: task->task_type = taskREADY;
1105: insert_task_to(task, &root->root_ready);
1106:
1107: flg ^= flg; /* ok */
1108: }
1109: }
1110: SCHED_QUNLOCK(root, taskSUSPEND);
1111:
1112: return flg;
1113: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>