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