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