Annotation of libaitsched/src/aitsched.c, revision 1.19
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.19 ! misho 6: * $Id: aitsched.c,v 1.18.6.2 2013/08/15 19:06:20 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.1 misho 363: * schedCancel() - Cancel task from scheduler
1.6 misho 364: *
1.1 misho 365: * @task = task
366: * return: -1 error or 0 ok
367: */
368: int
369: schedCancel(sched_task_t * __restrict task)
370: {
371: sched_queue_t *queue;
372:
1.5 misho 373: if (!task || !TASK_ROOT(task))
1.1 misho 374: return -1;
375:
1.5 misho 376: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
377: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1 misho 378: return -1;
379:
1.5 misho 380: switch (TASK_TYPE(task)) {
1.1 misho 381: case taskREAD:
1.5 misho 382: queue = &TASK_ROOT(task)->root_read;
1.1 misho 383: break;
384: case taskWRITE:
1.5 misho 385: queue = &TASK_ROOT(task)->root_write;
1.1 misho 386: break;
1.10 misho 387: case taskTIMER:
388: queue = &TASK_ROOT(task)->root_timer;
389: break;
1.9 misho 390: case taskALARM:
391: queue = &TASK_ROOT(task)->root_alarm;
392: break;
1.19 ! misho 393: case taskRTC:
! 394: queue = &TASK_ROOT(task)->root_rtc;
! 395: break;
1.10 misho 396: case taskNODE:
397: queue = &TASK_ROOT(task)->root_node;
398: break;
399: case taskPROC:
400: queue = &TASK_ROOT(task)->root_proc;
401: break;
1.12 misho 402: case taskSIGNAL:
403: queue = &TASK_ROOT(task)->root_signal;
404: break;
405: case taskAIO:
406: queue = &TASK_ROOT(task)->root_aio;
407: break;
408: case taskLIO:
409: queue = &TASK_ROOT(task)->root_lio;
410: break;
1.10 misho 411: case taskUSER:
412: queue = &TASK_ROOT(task)->root_user;
413: break;
1.1 misho 414: case taskEVENT:
1.5 misho 415: queue = &TASK_ROOT(task)->root_event;
416: break;
1.13 misho 417: case taskTASK:
418: queue = &TASK_ROOT(task)->root_task;
1.1 misho 419: break;
1.11 misho 420: case taskSUSPEND:
421: queue = &TASK_ROOT(task)->root_suspend;
422: break;
1.1 misho 423: case taskREADY:
1.5 misho 424: queue = &TASK_ROOT(task)->root_ready;
1.1 misho 425: break;
1.15 misho 426: case taskTHREAD:
427: queue = &TASK_ROOT(task)->root_thread;
428: break;
1.1 misho 429: default:
430: queue = NULL;
431: }
1.5 misho 432: if (queue) {
433: #ifdef HAVE_LIBPTHREAD
434: pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
435: #endif
1.10 misho 436: TAILQ_REMOVE(queue, TASK_ID(task), task_node);
1.5 misho 437: #ifdef HAVE_LIBPTHREAD
438: pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
439: #endif
440: }
441: if (TASK_TYPE(task) != taskUNUSE)
1.15 misho 442: sched_unuseTask(task);
1.1 misho 443:
444: return 0;
445: }
446:
447: /*
448: * schedCancelby() - Cancel task from scheduler by criteria
1.6 misho 449: *
1.1 misho 450: * @root = root task
1.5 misho 451: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.10 misho 452: * @criteria = find task by criteria
1.11 misho 453: * [CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA]
1.1 misho 454: * @param = search parameter
455: * @hook = custom cleanup hook function, may be NULL
1.3 misho 456: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1 misho 457: */
458: int
1.5 misho 459: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
1.1 misho 460: u_char criteria, void *param, sched_hook_func_t hook)
461: {
1.8 misho 462: sched_task_t *task, *tmp;
1.5 misho 463: sched_queue_t *queue;
1.8 misho 464: register int flg = 0;
1.1 misho 465:
466: if (!root)
467: return -1;
1.10 misho 468: /* if type == taskMAX check in all queues */
1.5 misho 469: if (type == taskMAX) {
470: if (schedCancelby(root, taskREAD, criteria, param, hook))
1.1 misho 471: return -2;
1.5 misho 472: if (schedCancelby(root, taskWRITE, criteria, param, hook))
1.1 misho 473: return -2;
1.10 misho 474: if (schedCancelby(root, taskTIMER, criteria, param, hook))
475: return -2;
1.9 misho 476: if (schedCancelby(root, taskALARM, criteria, param, hook))
477: return -2;
1.19 ! misho 478: if (schedCancelby(root, taskRTC, criteria, param, hook))
! 479: return -2;
1.10 misho 480: if (schedCancelby(root, taskNODE, criteria, param, hook))
481: return -2;
482: if (schedCancelby(root, taskPROC, criteria, param, hook))
483: return -2;
1.12 misho 484: if (schedCancelby(root, taskSIGNAL, criteria, param, hook))
485: return -2;
486: if (schedCancelby(root, taskAIO, criteria, param, hook))
487: return -2;
488: if (schedCancelby(root, taskLIO, criteria, param, hook))
489: return -2;
1.10 misho 490: if (schedCancelby(root, taskUSER, criteria, param, hook))
491: return -2;
1.5 misho 492: if (schedCancelby(root, taskEVENT, criteria, param, hook))
1.1 misho 493: return -2;
1.13 misho 494: if (schedCancelby(root, taskTASK, criteria, param, hook))
1.1 misho 495: return -2;
1.11 misho 496: if (schedCancelby(root, taskSUSPEND, criteria, param, hook))
497: return -2;
1.5 misho 498: if (schedCancelby(root, taskREADY, criteria, param, hook))
1.1 misho 499: return -2;
1.15 misho 500: if (schedCancelby(root, taskTHREAD, criteria, param, hook))
501: return -2;
1.1 misho 502: return 0;
503: }
1.10 misho 504: /* choosen queue */
1.5 misho 505: switch (type) {
506: case taskREAD:
507: queue = &root->root_read;
508: break;
509: case taskWRITE:
510: queue = &root->root_write;
511: break;
1.10 misho 512: case taskTIMER:
513: queue = &root->root_timer;
514: break;
1.9 misho 515: case taskALARM:
516: queue = &root->root_alarm;
517: break;
1.19 ! misho 518: case taskRTC:
! 519: queue = &root->root_rtc;
! 520: break;
1.10 misho 521: case taskNODE:
522: queue = &root->root_node;
523: break;
524: case taskPROC:
525: queue = &root->root_proc;
526: break;
1.12 misho 527: case taskSIGNAL:
528: queue = &root->root_signal;
529: break;
530: case taskAIO:
531: queue = &root->root_aio;
532: break;
533: case taskLIO:
534: queue = &root->root_lio;
535: break;
1.10 misho 536: case taskUSER:
537: queue = &root->root_user;
538: break;
1.5 misho 539: case taskEVENT:
540: queue = &root->root_event;
541: break;
1.13 misho 542: case taskTASK:
543: queue = &root->root_task;
1.5 misho 544: break;
1.11 misho 545: case taskSUSPEND:
546: queue = &root->root_suspend;
547: break;
1.5 misho 548: case taskREADY:
549: queue = &root->root_ready;
550: break;
1.15 misho 551: case taskTHREAD:
552: queue = &root->root_thread;
553: break;
1.5 misho 554: default:
555: return 0;
556: }
1.1 misho 557:
1.5 misho 558: #ifdef HAVE_LIBPTHREAD
559: pthread_mutex_lock(&root->root_mtx[type]);
560: #endif
1.8 misho 561: TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
562: flg ^= flg;
563: switch (criteria) {
1.10 misho 564: case CRITERIA_ANY:
565: flg = 1;
566: break;
1.8 misho 567: case CRITERIA_CALL:
568: if (TASK_FUNC(task) == (sched_task_func_t) param)
569: flg = 1;
1.1 misho 570: break;
1.8 misho 571: case CRITERIA_ARG:
572: if (TASK_ARG(task) == param)
573: flg = 1;
1.1 misho 574: break;
1.8 misho 575: case CRITERIA_FD:
576: if (TASK_FD(task) == (intptr_t) param)
577: flg = 1;
1.1 misho 578: break;
1.11 misho 579: case CRITERIA_ID:
1.8 misho 580: case CRITERIA_VAL:
581: if (TASK_VAL(task) == (u_long) param)
582: flg = 1;
1.1 misho 583: break;
1.8 misho 584: case CRITERIA_TS:
585: if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
586: flg = 1;
1.1 misho 587: break;
1.10 misho 588: case CRITERIA_DATA:
589: if (TASK_DATA(task) == param)
590: flg = 1;
591: break;
1.8 misho 592: default:
593: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
594: flg = -1;
595: }
1.10 misho 596: if (flg < 0) /* error */
1.8 misho 597: break;
598: /* cancel choosen task */
599: if (flg > 0) {
600: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
601: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
602: flg = -1;
603: break;
604: }
605: /* custom hook */
606: if (hook)
607: if (hook(task, NULL)) {
608: flg = -3;
609: break;
610: }
611:
612: TAILQ_REMOVE(queue, task, task_node);
613: if (TASK_TYPE(task) != taskUNUSE)
1.15 misho 614: sched_unuseTask(task);
1.8 misho 615:
616: flg ^= flg; /* ok */
1.1 misho 617: }
1.8 misho 618: }
1.5 misho 619: #ifdef HAVE_LIBPTHREAD
620: pthread_mutex_unlock(&root->root_mtx[type]);
621: #endif
1.8 misho 622: return flg;
1.1 misho 623: }
624:
625: /*
626: * schedRun() - Scheduler *run loop*
1.6 misho 627: *
1.1 misho 628: * @root = root task
1.2 misho 629: * @killState = kill condition variable, if !=0 stop scheduler loop
1.1 misho 630: * return: -1 error or 0 ok
631: */
632: int
1.7 misho 633: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
1.1 misho 634: {
635: sched_task_t *task;
636:
637: if (!root)
638: return -1;
639:
640: if (root->root_hooks.hook_exec.run)
641: if (root->root_hooks.hook_exec.run(root, NULL))
642: return -1;
1.7 misho 643:
644: if (killState) {
645: if (root->root_hooks.hook_exec.condition)
646: /* condition scheduler loop */
647: while (root && root->root_hooks.hook_exec.fetch &&
648: root->root_hooks.hook_exec.condition &&
649: root->root_hooks.hook_exec.condition(root, (void*) killState)) {
650: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.12 misho 651: root->root_ret = schedCall(task);
1.7 misho 652: }
653: else
654: /* trigger scheduler loop */
655: while (!*killState && root && root->root_hooks.hook_exec.fetch) {
656: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.12 misho 657: root->root_ret = schedCall(task);
1.7 misho 658: }
659: } else
660: /* infinite scheduler loop */
661: while (root && root->root_hooks.hook_exec.fetch)
662: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.12 misho 663: root->root_ret = schedCall(task);
1.1 misho 664:
665: return 0;
666: }
1.5 misho 667:
668: /*
669: * schedPolling() - Polling timeout period if no timer task is present
1.6 misho 670: *
1.5 misho 671: * @root = root task
672: * @ts = timeout polling period, if ==NULL INFINIT timeout
673: * @tsold = old timeout polling if !=NULL
674: * return: -1 error or 0 ok
675: */
1.18 misho 676: int
1.5 misho 677: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts,
678: struct timespec * __restrict tsold)
679: {
680: if (!root)
681: return -1;
682:
683: if (tsold)
684: *tsold = root->root_poll;
685:
686: if (!ts)
687: sched_timespecinf(&root->root_poll);
688: else
689: root->root_poll = *ts;
690:
691: return 0;
692: }
1.6 misho 693:
694: /*
695: * schedTermCondition() - Activate hook for scheduler condition kill
696: *
697: * @root = root task
698: * @condValue = condition value, kill schedRun() if condValue == killState
1.13 misho 699: * return: -1 error or 0 ok
1.6 misho 700: */
1.18 misho 701: int
1.6 misho 702: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
703: {
704: if (!root)
705: return -1;
706:
707: root->root_cond = condValue;
708: root->root_hooks.hook_exec.condition = sched_hook_condition;
709: return 0;
710: }
1.11 misho 711:
712: /*
713: * schedResumeby() - Resume suspended task
714: *
715: * @root = root task
716: * @criteria = find task by criteria
717: * [CRITERIA_ANY|CRITERIA_ID|CRITERIA_DATA]
718: * @param = search parameter (sched_task_t *task| u_long id)
719: * return: -1 error or 0 resumed ok
720: */
721: int
722: schedResumeby(sched_root_task_t * __restrict root, u_char criteria, void *param)
723: {
724: sched_task_t *task, *tmp;
725: register int flg = 0;
726:
727: if (!root)
728: return -1;
729:
730: #ifdef HAVE_LIBPTHREAD
731: pthread_mutex_lock(&root->root_mtx[taskSUSPEND]);
732: #endif
733: TAILQ_FOREACH_SAFE(task, &root->root_suspend, task_node, tmp) {
734: flg ^= flg;
735: switch (criteria) {
736: case CRITERIA_ANY:
737: flg = 1;
738: break;
739: case CRITERIA_ID:
740: if (TASK_VAL(task) == (u_long) param)
741: flg = 1;
742: break;
743: case CRITERIA_DATA:
744: if (TASK_ID(task) == (sched_task_t*) param)
745: flg = 1;
746: break;
747: default:
748: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
749: flg = -1;
750: }
751: if (flg < 0)
752: break;
753: /* resume choosen task */
754: if (flg > 0) {
755: if (root->root_hooks.hook_exec.resume)
756: if (root->root_hooks.hook_exec.resume(task, NULL)) {
757: flg = -1;
758: break;
759: }
760:
761: TAILQ_REMOVE(&root->root_suspend, task, task_node);
762:
763: task->task_type = taskREADY;
764: #ifdef HAVE_LIBPTHREAD
765: pthread_mutex_lock(&root->root_mtx[taskREADY]);
766: #endif
767: TAILQ_INSERT_TAIL(&root->root_ready, task, task_node);
768: #ifdef HAVE_LIBPTHREAD
769: pthread_mutex_unlock(&root->root_mtx[taskREADY]);
770: #endif
771:
772: flg ^= flg; /* ok */
773: }
774: }
775: #ifdef HAVE_LIBPTHREAD
776: pthread_mutex_unlock(&root->root_mtx[taskSUSPEND]);
777: #endif
778:
779: return flg;
780: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>