Annotation of libaitsched/src/aitsched.c, revision 1.23
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.23 ! misho 6: * $Id: aitsched.c,v 1.22.2.1 2013/11/21 14:38:30 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.18 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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: {
338: #ifndef EVFILT_USER
339: sched_SetErr(ENOTSUP, "Not supported kevent() filter");
340: return -1;
341: #else
342: struct kevent chg[1];
343: struct timespec timeout = { 0, 0 };
344:
345: if (!task || !TASK_ROOT(task))
346: return -1;
347:
348: #ifdef __NetBSD__
349: EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (intptr_t) TASK_VAL(task));
350: #else
351: EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (void*) TASK_VAL(task));
352: #endif
353: if (kevent(TASK_ROOT(task)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
354: LOGERR;
355: return -1;
356: }
357:
358: return 0;
359: #endif
360: }
361:
362: /*
1.20 misho 363: * schedQuery() - Query task in scheduler
364: *
365: * @task = task
366: * return: -1 error, 0 found and 1 not found
367: */
368: int
369: schedQuery(sched_task_t * __restrict task)
370: {
371: sched_queue_t *queue;
372: sched_task_t *t;
373:
374: if (!task || !TASK_ROOT(task))
375: return -1; /* error */
376:
377: switch (TASK_TYPE(task)) {
378: case taskREAD:
379: queue = &TASK_ROOT(task)->root_read;
380: break;
381: case taskWRITE:
382: queue = &TASK_ROOT(task)->root_write;
383: break;
384: case taskTIMER:
385: queue = &TASK_ROOT(task)->root_timer;
386: break;
387: case taskALARM:
388: queue = &TASK_ROOT(task)->root_alarm;
389: break;
390: case taskRTC:
391: queue = &TASK_ROOT(task)->root_rtc;
392: break;
393: case taskNODE:
394: queue = &TASK_ROOT(task)->root_node;
395: break;
396: case taskPROC:
397: queue = &TASK_ROOT(task)->root_proc;
398: break;
399: case taskSIGNAL:
400: queue = &TASK_ROOT(task)->root_signal;
401: break;
402: case taskAIO:
403: queue = &TASK_ROOT(task)->root_aio;
404: break;
405: case taskLIO:
406: queue = &TASK_ROOT(task)->root_lio;
407: break;
408: case taskUSER:
409: queue = &TASK_ROOT(task)->root_user;
410: break;
411: case taskEVENT:
412: queue = &TASK_ROOT(task)->root_event;
413: break;
414: case taskTASK:
415: queue = &TASK_ROOT(task)->root_task;
416: break;
417: case taskSUSPEND:
418: queue = &TASK_ROOT(task)->root_suspend;
419: break;
420: case taskREADY:
421: queue = &TASK_ROOT(task)->root_ready;
422: break;
423: case taskTHREAD:
424: queue = &TASK_ROOT(task)->root_thread;
425: break;
426: default:
427: return 1; /* not in queue */
428: }
429: if (queue)
430: TAILQ_FOREACH(t, queue, task_node)
431: if (TASK_ID(t) == TASK_ID(task))
432: return 0; /* found */
433:
434: return 1; /* not in queue */
435: }
436:
437: /*
438: * schedQueryby() - Query task in scheduler by criteria
439: *
440: * @root = root task
441: * @type = query from queue type, if =taskMAX query same task from all queues
442: * @criteria = find task by criteria
443: * [ CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|
1.23 ! misho 444: * CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA|CRITERIA_DATLEN ]
1.20 misho 445: * @param = search parameter
446: * return: -1 error, 0 found or 1 not found
447: */
448: int
449: schedQueryby(sched_root_task_t * __restrict root, sched_task_type_t type,
450: u_char criteria, void *param)
451: {
1.21 misho 452: sched_task_t *task;
1.20 misho 453: sched_queue_t *queue;
454: register int flg = 0;
455:
456: if (!root)
457: return -1;
458: /* if type == taskMAX check in all queues */
459: if (type == taskMAX) {
460: if ((flg = schedQueryby(root, taskREAD, criteria, param)) < 1)
461: return flg;
462: if ((flg = schedQueryby(root, taskWRITE, criteria, param)) < 1)
463: return flg;
464: if ((flg = schedQueryby(root, taskTIMER, criteria, param)) < 1)
465: return flg;
466: if ((flg = schedQueryby(root, taskALARM, criteria, param)) < 1)
467: return flg;
468: if ((flg = schedQueryby(root, taskRTC, criteria, param)) < 1)
469: return flg;
470: if ((flg = schedQueryby(root, taskNODE, criteria, param)) < 1)
471: return flg;
472: if ((flg = schedQueryby(root, taskPROC, criteria, param)) < 1)
473: return flg;
474: if ((flg = schedQueryby(root, taskSIGNAL, criteria, param)) < 1)
475: return flg;
476: if ((flg = schedQueryby(root, taskAIO, criteria, param)) < 1)
477: return flg;
478: if ((flg = schedQueryby(root, taskLIO, criteria, param)) < 1)
479: return flg;
480: if ((flg = schedQueryby(root, taskUSER, criteria, param)) < 1)
481: return flg;
482: if ((flg = schedQueryby(root, taskEVENT, criteria, param)) < 1)
483: return flg;
484: if ((flg = schedQueryby(root, taskTASK, criteria, param)) < 1)
485: return flg;
486: if ((flg = schedQueryby(root, taskSUSPEND, criteria, param)) < 1)
487: return flg;
488: if ((flg = schedQueryby(root, taskREADY, criteria, param)) < 1)
489: return flg;
490: if ((flg = schedQueryby(root, taskTHREAD, criteria, param)) < 1)
491: return flg;
492: return 1; /* not found */
493: }
494: /* choosen queue */
495: switch (type) {
496: case taskREAD:
497: queue = &root->root_read;
498: break;
499: case taskWRITE:
500: queue = &root->root_write;
501: break;
502: case taskTIMER:
503: queue = &root->root_timer;
504: break;
505: case taskALARM:
506: queue = &root->root_alarm;
507: break;
508: case taskRTC:
509: queue = &root->root_rtc;
510: break;
511: case taskNODE:
512: queue = &root->root_node;
513: break;
514: case taskPROC:
515: queue = &root->root_proc;
516: break;
517: case taskSIGNAL:
518: queue = &root->root_signal;
519: break;
520: case taskAIO:
521: queue = &root->root_aio;
522: break;
523: case taskLIO:
524: queue = &root->root_lio;
525: break;
526: case taskUSER:
527: queue = &root->root_user;
528: break;
529: case taskEVENT:
530: queue = &root->root_event;
531: break;
532: case taskTASK:
533: queue = &root->root_task;
534: break;
535: case taskSUSPEND:
536: queue = &root->root_suspend;
537: break;
538: case taskREADY:
539: queue = &root->root_ready;
540: break;
541: case taskTHREAD:
542: queue = &root->root_thread;
543: break;
544: default:
545: return 1; /* not found */
546: }
547:
548: TAILQ_FOREACH(task, queue, task_node) {
549: switch (criteria) {
550: case CRITERIA_ANY:
551: return 0; /* found */
552: case CRITERIA_CALL:
553: if (TASK_FUNC(task) == (sched_task_func_t) param)
554: return 0; /* found */
555: break;
556: case CRITERIA_ARG:
557: if (TASK_ARG(task) == param)
558: return 0; /* found */
559: break;
560: case CRITERIA_FD:
561: if (TASK_FD(task) == (intptr_t) param)
562: return 0; /* found */
563: break;
564: case CRITERIA_ID:
565: case CRITERIA_VAL:
566: if (TASK_VAL(task) == (u_long) param)
567: return 0; /* found */
568: break;
569: case CRITERIA_TS:
570: if (!sched_timespeccmp(&TASK_TS(task),
571: (struct timespec*) param, -))
572: return 0; /* found */
573: break;
574: case CRITERIA_DATA:
575: if (TASK_DATA(task) == param)
576: return 0; /* found */
577: break;
1.23 ! misho 578: case CRITERIA_DATLEN:
! 579: if (TASK_DATLEN(task) == (size_t) param)
! 580: return 0; /* found */
! 581: break;
1.20 misho 582: default:
583: sched_SetErr(EINVAL, "Invalid parameter criteria %d",
584: criteria);
585: return 1; /* not found */
586: }
587: }
588:
589: return 1; /* not found */
590: }
591:
592: /*
1.1 misho 593: * schedCancel() - Cancel task from scheduler
1.6 misho 594: *
1.1 misho 595: * @task = task
596: * return: -1 error or 0 ok
597: */
598: int
599: schedCancel(sched_task_t * __restrict task)
600: {
601: sched_queue_t *queue;
602:
1.5 misho 603: if (!task || !TASK_ROOT(task))
1.1 misho 604: return -1;
605:
1.5 misho 606: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
607: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1 misho 608: return -1;
609:
1.5 misho 610: switch (TASK_TYPE(task)) {
1.1 misho 611: case taskREAD:
1.5 misho 612: queue = &TASK_ROOT(task)->root_read;
1.1 misho 613: break;
614: case taskWRITE:
1.5 misho 615: queue = &TASK_ROOT(task)->root_write;
1.1 misho 616: break;
1.10 misho 617: case taskTIMER:
618: queue = &TASK_ROOT(task)->root_timer;
619: break;
1.9 misho 620: case taskALARM:
621: queue = &TASK_ROOT(task)->root_alarm;
622: break;
1.19 misho 623: case taskRTC:
624: queue = &TASK_ROOT(task)->root_rtc;
625: break;
1.10 misho 626: case taskNODE:
627: queue = &TASK_ROOT(task)->root_node;
628: break;
629: case taskPROC:
630: queue = &TASK_ROOT(task)->root_proc;
631: break;
1.12 misho 632: case taskSIGNAL:
633: queue = &TASK_ROOT(task)->root_signal;
634: break;
635: case taskAIO:
636: queue = &TASK_ROOT(task)->root_aio;
637: break;
638: case taskLIO:
639: queue = &TASK_ROOT(task)->root_lio;
640: break;
1.10 misho 641: case taskUSER:
642: queue = &TASK_ROOT(task)->root_user;
643: break;
1.1 misho 644: case taskEVENT:
1.5 misho 645: queue = &TASK_ROOT(task)->root_event;
646: break;
1.13 misho 647: case taskTASK:
648: queue = &TASK_ROOT(task)->root_task;
1.1 misho 649: break;
1.11 misho 650: case taskSUSPEND:
651: queue = &TASK_ROOT(task)->root_suspend;
652: break;
1.1 misho 653: case taskREADY:
1.5 misho 654: queue = &TASK_ROOT(task)->root_ready;
1.1 misho 655: break;
1.15 misho 656: case taskTHREAD:
657: queue = &TASK_ROOT(task)->root_thread;
658: break;
1.1 misho 659: default:
660: queue = NULL;
661: }
1.5 misho 662: if (queue) {
663: #ifdef HAVE_LIBPTHREAD
664: pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
665: #endif
1.10 misho 666: TAILQ_REMOVE(queue, TASK_ID(task), task_node);
1.5 misho 667: #ifdef HAVE_LIBPTHREAD
668: pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
669: #endif
670: }
671: if (TASK_TYPE(task) != taskUNUSE)
1.15 misho 672: sched_unuseTask(task);
1.1 misho 673:
674: return 0;
675: }
676:
677: /*
678: * schedCancelby() - Cancel task from scheduler by criteria
1.6 misho 679: *
1.1 misho 680: * @root = root task
1.5 misho 681: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.10 misho 682: * @criteria = find task by criteria
1.20 misho 683: * [ CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|
1.23 ! misho 684: * CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA|CRITERIA_DATLEN ]
1.1 misho 685: * @param = search parameter
686: * @hook = custom cleanup hook function, may be NULL
1.3 misho 687: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1 misho 688: */
689: int
1.5 misho 690: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
1.1 misho 691: u_char criteria, void *param, sched_hook_func_t hook)
692: {
1.8 misho 693: sched_task_t *task, *tmp;
1.5 misho 694: sched_queue_t *queue;
1.8 misho 695: register int flg = 0;
1.1 misho 696:
697: if (!root)
698: return -1;
1.10 misho 699: /* if type == taskMAX check in all queues */
1.5 misho 700: if (type == taskMAX) {
701: if (schedCancelby(root, taskREAD, criteria, param, hook))
1.1 misho 702: return -2;
1.5 misho 703: if (schedCancelby(root, taskWRITE, criteria, param, hook))
1.1 misho 704: return -2;
1.10 misho 705: if (schedCancelby(root, taskTIMER, criteria, param, hook))
706: return -2;
1.9 misho 707: if (schedCancelby(root, taskALARM, criteria, param, hook))
708: return -2;
1.19 misho 709: if (schedCancelby(root, taskRTC, criteria, param, hook))
710: return -2;
1.10 misho 711: if (schedCancelby(root, taskNODE, criteria, param, hook))
712: return -2;
713: if (schedCancelby(root, taskPROC, criteria, param, hook))
714: return -2;
1.12 misho 715: if (schedCancelby(root, taskSIGNAL, criteria, param, hook))
716: return -2;
717: if (schedCancelby(root, taskAIO, criteria, param, hook))
718: return -2;
719: if (schedCancelby(root, taskLIO, criteria, param, hook))
720: return -2;
1.10 misho 721: if (schedCancelby(root, taskUSER, criteria, param, hook))
722: return -2;
1.5 misho 723: if (schedCancelby(root, taskEVENT, criteria, param, hook))
1.1 misho 724: return -2;
1.13 misho 725: if (schedCancelby(root, taskTASK, criteria, param, hook))
1.1 misho 726: return -2;
1.11 misho 727: if (schedCancelby(root, taskSUSPEND, criteria, param, hook))
728: return -2;
1.5 misho 729: if (schedCancelby(root, taskREADY, criteria, param, hook))
1.1 misho 730: return -2;
1.15 misho 731: if (schedCancelby(root, taskTHREAD, criteria, param, hook))
732: return -2;
1.1 misho 733: return 0;
734: }
1.10 misho 735: /* choosen queue */
1.5 misho 736: switch (type) {
737: case taskREAD:
738: queue = &root->root_read;
739: break;
740: case taskWRITE:
741: queue = &root->root_write;
742: break;
1.10 misho 743: case taskTIMER:
744: queue = &root->root_timer;
745: break;
1.9 misho 746: case taskALARM:
747: queue = &root->root_alarm;
748: break;
1.19 misho 749: case taskRTC:
750: queue = &root->root_rtc;
751: break;
1.10 misho 752: case taskNODE:
753: queue = &root->root_node;
754: break;
755: case taskPROC:
756: queue = &root->root_proc;
757: break;
1.12 misho 758: case taskSIGNAL:
759: queue = &root->root_signal;
760: break;
761: case taskAIO:
762: queue = &root->root_aio;
763: break;
764: case taskLIO:
765: queue = &root->root_lio;
766: break;
1.10 misho 767: case taskUSER:
768: queue = &root->root_user;
769: break;
1.5 misho 770: case taskEVENT:
771: queue = &root->root_event;
772: break;
1.13 misho 773: case taskTASK:
774: queue = &root->root_task;
1.5 misho 775: break;
1.11 misho 776: case taskSUSPEND:
777: queue = &root->root_suspend;
778: break;
1.5 misho 779: case taskREADY:
780: queue = &root->root_ready;
781: break;
1.15 misho 782: case taskTHREAD:
783: queue = &root->root_thread;
784: break;
1.5 misho 785: default:
786: return 0;
787: }
1.1 misho 788:
1.5 misho 789: #ifdef HAVE_LIBPTHREAD
790: pthread_mutex_lock(&root->root_mtx[type]);
791: #endif
1.8 misho 792: TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
793: flg ^= flg;
794: switch (criteria) {
1.10 misho 795: case CRITERIA_ANY:
796: flg = 1;
797: break;
1.8 misho 798: case CRITERIA_CALL:
799: if (TASK_FUNC(task) == (sched_task_func_t) param)
800: flg = 1;
1.1 misho 801: break;
1.8 misho 802: case CRITERIA_ARG:
803: if (TASK_ARG(task) == param)
804: flg = 1;
1.1 misho 805: break;
1.8 misho 806: case CRITERIA_FD:
807: if (TASK_FD(task) == (intptr_t) param)
808: flg = 1;
1.1 misho 809: break;
1.11 misho 810: case CRITERIA_ID:
1.8 misho 811: case CRITERIA_VAL:
812: if (TASK_VAL(task) == (u_long) param)
813: flg = 1;
1.1 misho 814: break;
1.8 misho 815: case CRITERIA_TS:
816: if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
817: flg = 1;
1.1 misho 818: break;
1.10 misho 819: case CRITERIA_DATA:
820: if (TASK_DATA(task) == param)
821: flg = 1;
822: break;
1.23 ! misho 823: case CRITERIA_DATLEN:
! 824: if (TASK_DATLEN(task) == (size_t) param)
! 825: flg = 1;
! 826: break;
1.8 misho 827: default:
828: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
829: flg = -1;
830: }
1.10 misho 831: if (flg < 0) /* error */
1.8 misho 832: break;
833: /* cancel choosen task */
834: if (flg > 0) {
835: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
836: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
837: flg = -1;
838: break;
839: }
840: /* custom hook */
841: if (hook)
842: if (hook(task, NULL)) {
843: flg = -3;
844: break;
845: }
846:
847: TAILQ_REMOVE(queue, task, task_node);
848: if (TASK_TYPE(task) != taskUNUSE)
1.15 misho 849: sched_unuseTask(task);
1.8 misho 850:
851: flg ^= flg; /* ok */
1.1 misho 852: }
1.8 misho 853: }
1.5 misho 854: #ifdef HAVE_LIBPTHREAD
855: pthread_mutex_unlock(&root->root_mtx[type]);
856: #endif
1.8 misho 857: return flg;
1.1 misho 858: }
859:
860: /*
861: * schedRun() - Scheduler *run loop*
1.6 misho 862: *
1.1 misho 863: * @root = root task
1.2 misho 864: * @killState = kill condition variable, if !=0 stop scheduler loop
1.1 misho 865: * return: -1 error or 0 ok
866: */
867: int
1.7 misho 868: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
1.1 misho 869: {
870: sched_task_t *task;
871:
872: if (!root)
873: return -1;
874:
875: if (root->root_hooks.hook_exec.run)
876: if (root->root_hooks.hook_exec.run(root, NULL))
877: return -1;
1.7 misho 878:
879: if (killState) {
880: if (root->root_hooks.hook_exec.condition)
881: /* condition scheduler loop */
882: while (root && root->root_hooks.hook_exec.fetch &&
883: root->root_hooks.hook_exec.condition &&
884: root->root_hooks.hook_exec.condition(root, (void*) killState)) {
885: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.12 misho 886: root->root_ret = schedCall(task);
1.7 misho 887: }
888: else
889: /* trigger scheduler loop */
890: while (!*killState && root && root->root_hooks.hook_exec.fetch) {
891: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.12 misho 892: root->root_ret = schedCall(task);
1.7 misho 893: }
894: } else
895: /* infinite scheduler loop */
896: while (root && root->root_hooks.hook_exec.fetch)
897: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.12 misho 898: root->root_ret = schedCall(task);
1.1 misho 899:
900: return 0;
901: }
1.5 misho 902:
903: /*
904: * schedPolling() - Polling timeout period if no timer task is present
1.6 misho 905: *
1.5 misho 906: * @root = root task
907: * @ts = timeout polling period, if ==NULL INFINIT timeout
908: * @tsold = old timeout polling if !=NULL
909: * return: -1 error or 0 ok
910: */
1.18 misho 911: int
1.5 misho 912: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts,
913: struct timespec * __restrict tsold)
914: {
915: if (!root)
916: return -1;
917:
918: if (tsold)
919: *tsold = root->root_poll;
920:
921: if (!ts)
922: sched_timespecinf(&root->root_poll);
923: else
924: root->root_poll = *ts;
925:
926: return 0;
927: }
1.6 misho 928:
929: /*
930: * schedTermCondition() - Activate hook for scheduler condition kill
931: *
932: * @root = root task
933: * @condValue = condition value, kill schedRun() if condValue == killState
1.13 misho 934: * return: -1 error or 0 ok
1.6 misho 935: */
1.18 misho 936: int
1.6 misho 937: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
938: {
939: if (!root)
940: return -1;
941:
942: root->root_cond = condValue;
943: root->root_hooks.hook_exec.condition = sched_hook_condition;
944: return 0;
945: }
1.11 misho 946:
947: /*
948: * schedResumeby() - Resume suspended task
949: *
950: * @root = root task
951: * @criteria = find task by criteria
1.23 ! misho 952: * [CRITERIA_ANY|CRITERIA_ID|CRITERIA_VAL|CRITERIA_DATA]
1.20 misho 953: * @param = search parameter (sched_task_t *task| unsigned long id)
1.11 misho 954: * return: -1 error or 0 resumed ok
955: */
956: int
957: schedResumeby(sched_root_task_t * __restrict root, u_char criteria, void *param)
958: {
959: sched_task_t *task, *tmp;
960: register int flg = 0;
961:
962: if (!root)
963: return -1;
964:
965: #ifdef HAVE_LIBPTHREAD
966: pthread_mutex_lock(&root->root_mtx[taskSUSPEND]);
967: #endif
968: TAILQ_FOREACH_SAFE(task, &root->root_suspend, task_node, tmp) {
969: flg ^= flg;
970: switch (criteria) {
971: case CRITERIA_ANY:
972: flg = 1;
973: break;
974: case CRITERIA_ID:
1.23 ! misho 975: case CRITERIA_VAL:
1.11 misho 976: if (TASK_VAL(task) == (u_long) param)
977: flg = 1;
978: break;
979: case CRITERIA_DATA:
980: if (TASK_ID(task) == (sched_task_t*) param)
981: flg = 1;
982: break;
983: default:
984: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
985: flg = -1;
986: }
987: if (flg < 0)
988: break;
989: /* resume choosen task */
990: if (flg > 0) {
991: if (root->root_hooks.hook_exec.resume)
992: if (root->root_hooks.hook_exec.resume(task, NULL)) {
993: flg = -1;
994: break;
995: }
996:
997: TAILQ_REMOVE(&root->root_suspend, task, task_node);
998:
999: task->task_type = taskREADY;
1000: #ifdef HAVE_LIBPTHREAD
1001: pthread_mutex_lock(&root->root_mtx[taskREADY]);
1002: #endif
1003: TAILQ_INSERT_TAIL(&root->root_ready, task, task_node);
1004: #ifdef HAVE_LIBPTHREAD
1005: pthread_mutex_unlock(&root->root_mtx[taskREADY]);
1006: #endif
1007:
1008: flg ^= flg; /* ok */
1009: }
1010: }
1011: #ifdef HAVE_LIBPTHREAD
1012: pthread_mutex_unlock(&root->root_mtx[taskSUSPEND]);
1013: #endif
1014:
1015: return flg;
1016: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>