Annotation of libaitsched/src/aitsched.c, revision 1.11.2.2
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.11.2.2! misho 6: * $Id: aitsched.c,v 1.11.2.1 2012/07/30 00:20:27 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.6 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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
59: inline int
60: sched_GetErrno()
61: {
62: return sched_Errno;
63: }
64:
65: // sched_GetError() Get error text of last operation
66: inline const char *
67: sched_GetError()
68: {
69: return sched_Error;
70: }
71:
72: // sched_SetErr() Set error to variables for internal use!!!
73: inline 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: /* 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.10 misho 105: root->root_hooks.hook_add.node = sched_hook_node;
106: root->root_hooks.hook_add.proc = sched_hook_proc;
107: root->root_hooks.hook_add.signal = sched_hook_signal;
108: #ifdef EVFILT_USER
109: root->root_hooks.hook_add.user = sched_hook_user;
110: #endif
1.2 misho 111:
112: root->root_hooks.hook_exec.cancel = sched_hook_cancel;
113: root->root_hooks.hook_exec.fetch = sched_hook_fetch;
1.3 misho 114: root->root_hooks.hook_exec.exception = sched_hook_exception;
1.2 misho 115:
116: root->root_hooks.hook_root.init = sched_hook_init;
117: root->root_hooks.hook_root.fini = sched_hook_fini;
118: return 0;
119: }
120:
121: /*
1.1 misho 122: * schedInit() - Init scheduler
1.6 misho 123: *
1.1 misho 124: * @data = optional data if !=NULL
125: * @datlen = data len if data is set
126: * return: allocated root task if ok or NULL error
127: */
128: sched_root_task_t *
129: schedInit(void ** __restrict data, size_t datlen)
130: {
131: sched_root_task_t *root = NULL;
132: int (*func)(sched_root_task_t *);
1.5 misho 133: #ifdef HAVE_LIBPTHREAD
134: register int i;
135: #endif
1.1 misho 136:
137: root = malloc(sizeof(sched_root_task_t));
1.2 misho 138: if (!root) {
139: LOGERR;
140: } else {
1.1 misho 141: memset(root, 0, sizeof(sched_root_task_t));
1.5 misho 142:
143: /* INFINIT polling period by default */
144: sched_timespecinf(&root->root_poll);
145:
146: #ifdef HAVE_LIBPTHREAD
147: for (i = 0; i < taskMAX; i++)
148: if (pthread_mutex_init(&root->root_mtx[i], NULL)) {
149: LOGERR;
150: while (i)
151: pthread_mutex_destroy(&root->root_mtx[--i]);
152: free(root);
153: return NULL;
154: }
155:
156: for (i = 0; i < taskMAX; i++)
157: pthread_mutex_lock(&root->root_mtx[i]);
158: #endif
159:
1.2 misho 160: TAILQ_INIT(&root->root_read);
161: TAILQ_INIT(&root->root_write);
1.10 misho 162: TAILQ_INIT(&root->root_timer);
1.9 misho 163: TAILQ_INIT(&root->root_alarm);
1.10 misho 164: TAILQ_INIT(&root->root_node);
165: TAILQ_INIT(&root->root_proc);
166: TAILQ_INIT(&root->root_signal);
1.11.2.2! misho 167: TAILQ_INIT(&root->root_aio);
! 168: TAILQ_INIT(&root->root_user);
1.2 misho 169: TAILQ_INIT(&root->root_event);
170: TAILQ_INIT(&root->root_eventlo);
1.11 misho 171: TAILQ_INIT(&root->root_suspend);
1.2 misho 172: TAILQ_INIT(&root->root_ready);
173: TAILQ_INIT(&root->root_unuse);
1.1 misho 174:
1.5 misho 175: #ifdef HAVE_LIBPTHREAD
176: for (i = 0; i < taskMAX; i++)
177: pthread_mutex_unlock(&root->root_mtx[i]);
178: #endif
179:
1.1 misho 180: if (data && *data) {
181: if (datlen) {
182: root->root_data.iov_base = *data;
183: root->root_data.iov_len = datlen;
1.3 misho 184: } else { /* if datlen == 0, switch to callbacks init mode */
185: /* little hack :) for correct initialization of scheduler */
1.2 misho 186: func = (int(*)(sched_root_task_t*)) data;
1.1 misho 187: func(root);
188: }
189: }
1.2 misho 190:
191: if (root->root_hooks.hook_root.init)
192: root->root_hooks.hook_root.init(root, NULL);
1.1 misho 193: }
194:
195: return root;
196: }
197:
198: /*
199: * schedEnd() - End scheduler & free all resources
1.6 misho 200: *
1.1 misho 201: * @root = root task
202: * return: -1 error or 0 ok
203: */
204: int
1.2 misho 205: schedEnd(sched_root_task_t ** __restrict root)
1.1 misho 206: {
1.7 misho 207: sched_task_t *task, *tmp;
1.5 misho 208: #ifdef HAVE_LIBPTHREAD
209: register int i;
210: #endif
1.1 misho 211:
1.2 misho 212: if (!root || !*root)
1.1 misho 213: return -1;
214:
1.10 misho 215: TAILQ_FOREACH_SAFE(task, &(*root)->root_read, task_node, tmp)
216: schedCancel(task);
217: TAILQ_FOREACH_SAFE(task, &(*root)->root_write, task_node, tmp)
1.1 misho 218: schedCancel(task);
1.10 misho 219: TAILQ_FOREACH_SAFE(task, &(*root)->root_timer, task_node, tmp)
220: schedCancel(task);
221: TAILQ_FOREACH_SAFE(task, &(*root)->root_alarm, task_node, tmp)
222: schedCancel(task);
223: TAILQ_FOREACH_SAFE(task, &(*root)->root_node, task_node, tmp)
224: schedCancel(task);
225: TAILQ_FOREACH_SAFE(task, &(*root)->root_proc, task_node, tmp)
1.1 misho 226: schedCancel(task);
1.10 misho 227: TAILQ_FOREACH_SAFE(task, &(*root)->root_signal, task_node, tmp)
1.1 misho 228: schedCancel(task);
1.11.2.2! misho 229: TAILQ_FOREACH_SAFE(task, &(*root)->root_aio, task_node, tmp)
! 230: schedCancel(task);
! 231: TAILQ_FOREACH_SAFE(task, &(*root)->root_user, task_node, tmp)
! 232: schedCancel(task);
1.10 misho 233: TAILQ_FOREACH_SAFE(task, &(*root)->root_event, task_node, tmp)
1.1 misho 234: schedCancel(task);
1.10 misho 235: TAILQ_FOREACH_SAFE(task, &(*root)->root_eventlo, task_node, tmp)
1.5 misho 236: schedCancel(task);
1.11 misho 237: TAILQ_FOREACH_SAFE(task, &(*root)->root_suspend, task_node, tmp)
238: schedCancel(task);
1.10 misho 239: TAILQ_FOREACH_SAFE(task, &(*root)->root_ready, task_node, tmp)
1.1 misho 240: schedCancel(task);
241:
1.5 misho 242: #ifdef HAVE_LIBPTHREAD
243: pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
244: #endif
1.10 misho 245: TAILQ_FOREACH_SAFE(task, &(*root)->root_unuse, task_node, tmp) {
1.2 misho 246: TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
1.1 misho 247: free(task);
248: }
1.5 misho 249: #ifdef HAVE_LIBPTHREAD
250: pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
251: #endif
1.1 misho 252:
1.2 misho 253: if ((*root)->root_hooks.hook_root.fini)
254: (*root)->root_hooks.hook_root.fini(*root, NULL);
1.1 misho 255:
1.5 misho 256: #ifdef HAVE_LIBPTHREAD
257: for (i = 0; i < taskMAX; i++)
258: pthread_mutex_destroy(&(*root)->root_mtx[i]);
259: #endif
260:
1.2 misho 261: free(*root);
262: *root = NULL;
1.1 misho 263: return 0;
264: }
265:
266: /*
267: * schedCall() - Call task execution function
1.6 misho 268: *
1.1 misho 269: * @task = current task
270: * return: !=NULL error or =NULL ok
271: */
272: inline void *
273: schedCall(sched_task_t * __restrict task)
274: {
1.4 misho 275: void *ptr = (void*) -1;
276:
1.1 misho 277: if (!task)
1.4 misho 278: return ptr;
279:
280: if (!TASK_ISLOCKED(task))
281: TASK_LOCK(task);
1.1 misho 282:
1.4 misho 283: ptr = task->task_func(task);
284:
285: TASK_UNLOCK(task);
286: return ptr;
1.1 misho 287: }
288:
289: /*
290: * schedFetch() - Fetch ready task
1.6 misho 291: *
1.1 misho 292: * @root = root task
293: * return: =NULL error or !=NULL ready task
294: */
295: inline void *
296: schedFetch(sched_root_task_t * __restrict root)
297: {
298: void *ptr;
299:
300: if (!root)
301: return NULL;
302:
303: if (root->root_hooks.hook_exec.fetch)
304: ptr = root->root_hooks.hook_exec.fetch(root, NULL);
305: else
306: ptr = NULL;
307:
308: return ptr;
309: }
310:
311: /*
1.10 misho 312: * schedTrigger() - Triggering USER task
313: *
314: * @task = task
315: * return: -1 error or 0 ok
316: */
317: int
318: schedTrigger(sched_task_t * __restrict task)
319: {
320: #ifndef EVFILT_USER
321: sched_SetErr(ENOTSUP, "Not supported kevent() filter");
322: return -1;
323: #else
324: struct kevent chg[1];
325: struct timespec timeout = { 0, 0 };
326:
327: if (!task || !TASK_ROOT(task))
328: return -1;
329:
330: #ifdef __NetBSD__
331: EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (intptr_t) TASK_VAL(task));
332: #else
333: EV_SET(chg, TASK_VAL(task), EVFILT_USER, 0, NOTE_TRIGGER, 0, (void*) TASK_VAL(task));
334: #endif
335: if (kevent(TASK_ROOT(task)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
336: LOGERR;
337: return -1;
338: }
339:
340: return 0;
341: #endif
342: }
343:
344: /*
1.1 misho 345: * schedCancel() - Cancel task from scheduler
1.6 misho 346: *
1.1 misho 347: * @task = task
348: * return: -1 error or 0 ok
349: */
350: int
351: schedCancel(sched_task_t * __restrict task)
352: {
353: sched_queue_t *queue;
354:
1.5 misho 355: if (!task || !TASK_ROOT(task))
1.1 misho 356: return -1;
357:
1.5 misho 358: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
359: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
1.1 misho 360: return -1;
361:
1.5 misho 362: switch (TASK_TYPE(task)) {
1.1 misho 363: case taskREAD:
1.5 misho 364: queue = &TASK_ROOT(task)->root_read;
1.1 misho 365: break;
366: case taskWRITE:
1.5 misho 367: queue = &TASK_ROOT(task)->root_write;
1.1 misho 368: break;
1.10 misho 369: case taskTIMER:
370: queue = &TASK_ROOT(task)->root_timer;
371: break;
1.9 misho 372: case taskALARM:
373: queue = &TASK_ROOT(task)->root_alarm;
374: break;
1.10 misho 375: case taskNODE:
376: queue = &TASK_ROOT(task)->root_node;
377: break;
378: case taskPROC:
379: queue = &TASK_ROOT(task)->root_proc;
380: break;
381: case taskSIGNAL:
382: queue = &TASK_ROOT(task)->root_signal;
1.1 misho 383: break;
1.11.2.2! misho 384: case taskAIO:
! 385: queue = &TASK_ROOT(task)->root_aio;
! 386: break;
! 387: case taskUSER:
! 388: queue = &TASK_ROOT(task)->root_user;
! 389: break;
1.1 misho 390: case taskEVENT:
1.5 misho 391: queue = &TASK_ROOT(task)->root_event;
392: break;
393: case taskEVENTLO:
394: queue = &TASK_ROOT(task)->root_eventlo;
1.1 misho 395: break;
1.11 misho 396: case taskSUSPEND:
397: queue = &TASK_ROOT(task)->root_suspend;
398: break;
1.1 misho 399: case taskREADY:
1.5 misho 400: queue = &TASK_ROOT(task)->root_ready;
1.1 misho 401: break;
402: default:
403: queue = NULL;
404: }
1.5 misho 405: if (queue) {
406: #ifdef HAVE_LIBPTHREAD
407: pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
408: #endif
1.10 misho 409: TAILQ_REMOVE(queue, TASK_ID(task), task_node);
1.5 misho 410: #ifdef HAVE_LIBPTHREAD
411: pthread_mutex_unlock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
412: #endif
413: }
414: if (TASK_TYPE(task) != taskUNUSE)
1.4 misho 415: _sched_unuseTask(task);
1.1 misho 416:
417: return 0;
418: }
419:
420: /*
421: * schedCancelby() - Cancel task from scheduler by criteria
1.6 misho 422: *
1.1 misho 423: * @root = root task
1.5 misho 424: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
1.10 misho 425: * @criteria = find task by criteria
1.11 misho 426: * [CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA]
1.1 misho 427: * @param = search parameter
428: * @hook = custom cleanup hook function, may be NULL
1.3 misho 429: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
1.1 misho 430: */
431: int
1.5 misho 432: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
1.1 misho 433: u_char criteria, void *param, sched_hook_func_t hook)
434: {
1.8 misho 435: sched_task_t *task, *tmp;
1.5 misho 436: sched_queue_t *queue;
1.8 misho 437: register int flg = 0;
1.1 misho 438:
439: if (!root)
440: return -1;
1.10 misho 441: /* if type == taskMAX check in all queues */
1.5 misho 442: if (type == taskMAX) {
443: if (schedCancelby(root, taskREAD, criteria, param, hook))
1.1 misho 444: return -2;
1.5 misho 445: if (schedCancelby(root, taskWRITE, criteria, param, hook))
1.1 misho 446: return -2;
1.10 misho 447: if (schedCancelby(root, taskTIMER, criteria, param, hook))
448: return -2;
1.9 misho 449: if (schedCancelby(root, taskALARM, criteria, param, hook))
450: return -2;
1.10 misho 451: if (schedCancelby(root, taskNODE, criteria, param, hook))
452: return -2;
453: if (schedCancelby(root, taskPROC, criteria, param, hook))
454: return -2;
455: if (schedCancelby(root, taskSIGNAL, criteria, param, hook))
1.1 misho 456: return -2;
1.11.2.2! misho 457: if (schedCancelby(root, taskAIO, criteria, param, hook))
! 458: return -2;
! 459: if (schedCancelby(root, taskUSER, criteria, param, hook))
! 460: return -2;
1.5 misho 461: if (schedCancelby(root, taskEVENT, criteria, param, hook))
1.1 misho 462: return -2;
1.5 misho 463: if (schedCancelby(root, taskEVENTLO, criteria, param, hook))
1.1 misho 464: return -2;
1.11 misho 465: if (schedCancelby(root, taskSUSPEND, criteria, param, hook))
466: return -2;
1.5 misho 467: if (schedCancelby(root, taskREADY, criteria, param, hook))
1.1 misho 468: return -2;
469: return 0;
470: }
1.10 misho 471: /* choosen queue */
1.5 misho 472: switch (type) {
473: case taskREAD:
474: queue = &root->root_read;
475: break;
476: case taskWRITE:
477: queue = &root->root_write;
478: break;
1.10 misho 479: case taskTIMER:
480: queue = &root->root_timer;
481: break;
1.9 misho 482: case taskALARM:
483: queue = &root->root_alarm;
484: break;
1.10 misho 485: case taskNODE:
486: queue = &root->root_node;
487: break;
488: case taskPROC:
489: queue = &root->root_proc;
490: break;
491: case taskSIGNAL:
492: queue = &root->root_signal;
1.5 misho 493: break;
1.11.2.2! misho 494: case taskAIO:
! 495: queue = &root->root_aio;
! 496: break;
! 497: case taskUSER:
! 498: queue = &root->root_user;
! 499: break;
1.5 misho 500: case taskEVENT:
501: queue = &root->root_event;
502: break;
503: case taskEVENTLO:
504: queue = &root->root_eventlo;
505: break;
1.11 misho 506: case taskSUSPEND:
507: queue = &root->root_suspend;
508: break;
1.5 misho 509: case taskREADY:
510: queue = &root->root_ready;
511: break;
512: default:
513: return 0;
514: }
1.1 misho 515:
1.5 misho 516: #ifdef HAVE_LIBPTHREAD
517: pthread_mutex_lock(&root->root_mtx[type]);
518: #endif
1.8 misho 519: TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
520: flg ^= flg;
521: switch (criteria) {
1.10 misho 522: case CRITERIA_ANY:
523: flg = 1;
524: break;
1.8 misho 525: case CRITERIA_CALL:
526: if (TASK_FUNC(task) == (sched_task_func_t) param)
527: flg = 1;
1.1 misho 528: break;
1.8 misho 529: case CRITERIA_ARG:
530: if (TASK_ARG(task) == param)
531: flg = 1;
1.1 misho 532: break;
1.8 misho 533: case CRITERIA_FD:
534: if (TASK_FD(task) == (intptr_t) param)
535: flg = 1;
1.1 misho 536: break;
1.11 misho 537: case CRITERIA_ID:
1.8 misho 538: case CRITERIA_VAL:
539: if (TASK_VAL(task) == (u_long) param)
540: flg = 1;
1.1 misho 541: break;
1.8 misho 542: case CRITERIA_TS:
543: if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
544: flg = 1;
1.1 misho 545: break;
1.10 misho 546: case CRITERIA_DATA:
547: if (TASK_DATA(task) == param)
548: flg = 1;
549: break;
1.8 misho 550: default:
551: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
552: flg = -1;
553: }
1.10 misho 554: if (flg < 0) /* error */
1.8 misho 555: break;
556: /* cancel choosen task */
557: if (flg > 0) {
558: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
559: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL)) {
560: flg = -1;
561: break;
562: }
563: /* custom hook */
564: if (hook)
565: if (hook(task, NULL)) {
566: flg = -3;
567: break;
568: }
569:
570: TAILQ_REMOVE(queue, task, task_node);
571: if (TASK_TYPE(task) != taskUNUSE)
572: _sched_unuseTask(task);
573:
574: flg ^= flg; /* ok */
1.1 misho 575: }
1.8 misho 576: }
1.5 misho 577: #ifdef HAVE_LIBPTHREAD
578: pthread_mutex_unlock(&root->root_mtx[type]);
579: #endif
1.8 misho 580: return flg;
1.1 misho 581: }
582:
583: /*
584: * schedRun() - Scheduler *run loop*
1.6 misho 585: *
1.1 misho 586: * @root = root task
1.2 misho 587: * @killState = kill condition variable, if !=0 stop scheduler loop
1.1 misho 588: * return: -1 error or 0 ok
589: */
590: int
1.7 misho 591: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
1.1 misho 592: {
593: sched_task_t *task;
594:
595: if (!root)
596: return -1;
597:
598: if (root->root_hooks.hook_exec.run)
599: if (root->root_hooks.hook_exec.run(root, NULL))
600: return -1;
1.7 misho 601:
602: if (killState) {
603: if (root->root_hooks.hook_exec.condition)
604: /* condition scheduler loop */
605: while (root && root->root_hooks.hook_exec.fetch &&
606: root->root_hooks.hook_exec.condition &&
607: root->root_hooks.hook_exec.condition(root, (void*) killState)) {
608: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.11.2.1 misho 609: root->root_ret = schedCall(task);
1.7 misho 610: }
611: else
612: /* trigger scheduler loop */
613: while (!*killState && root && root->root_hooks.hook_exec.fetch) {
614: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.11.2.1 misho 615: root->root_ret = schedCall(task);
1.7 misho 616: }
617: } else
618: /* infinite scheduler loop */
619: while (root && root->root_hooks.hook_exec.fetch)
620: if ((task = root->root_hooks.hook_exec.fetch(root, NULL)))
1.11.2.1 misho 621: root->root_ret = schedCall(task);
1.1 misho 622:
623: return 0;
624: }
1.5 misho 625:
626: /*
627: * schedPolling() - Polling timeout period if no timer task is present
1.6 misho 628: *
1.5 misho 629: * @root = root task
630: * @ts = timeout polling period, if ==NULL INFINIT timeout
631: * @tsold = old timeout polling if !=NULL
632: * return: -1 error or 0 ok
633: */
634: inline int
635: schedPolling(sched_root_task_t * __restrict root, struct timespec * __restrict ts,
636: struct timespec * __restrict tsold)
637: {
638: if (!root)
639: return -1;
640:
641: if (tsold)
642: *tsold = root->root_poll;
643:
644: if (!ts)
645: sched_timespecinf(&root->root_poll);
646: else
647: root->root_poll = *ts;
648:
649: return 0;
650: }
1.6 misho 651:
652: /*
653: * schedTermCondition() - Activate hook for scheduler condition kill
654: *
655: * @root = root task
656: * @condValue = condition value, kill schedRun() if condValue == killState
657: * return: -1 error ok 0 ok
658: */
659: inline int
660: schedTermCondition(sched_root_task_t * __restrict root, intptr_t condValue)
661: {
662: if (!root)
663: return -1;
664:
665: root->root_cond = condValue;
666: root->root_hooks.hook_exec.condition = sched_hook_condition;
667: return 0;
668: }
1.11 misho 669:
670: /*
671: * schedResumeby() - Resume suspended task
672: *
673: * @root = root task
674: * @criteria = find task by criteria
675: * [CRITERIA_ANY|CRITERIA_ID|CRITERIA_DATA]
676: * @param = search parameter (sched_task_t *task| u_long id)
677: * return: -1 error or 0 resumed ok
678: */
679: int
680: schedResumeby(sched_root_task_t * __restrict root, u_char criteria, void *param)
681: {
682: sched_task_t *task, *tmp;
683: register int flg = 0;
684:
685: if (!root)
686: return -1;
687:
688: #ifdef HAVE_LIBPTHREAD
689: pthread_mutex_lock(&root->root_mtx[taskSUSPEND]);
690: #endif
691: TAILQ_FOREACH_SAFE(task, &root->root_suspend, task_node, tmp) {
692: flg ^= flg;
693: switch (criteria) {
694: case CRITERIA_ANY:
695: flg = 1;
696: break;
697: case CRITERIA_ID:
698: if (TASK_VAL(task) == (u_long) param)
699: flg = 1;
700: break;
701: case CRITERIA_DATA:
702: if (TASK_ID(task) == (sched_task_t*) param)
703: flg = 1;
704: break;
705: default:
706: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
707: flg = -1;
708: }
709: if (flg < 0)
710: break;
711: /* resume choosen task */
712: if (flg > 0) {
713: if (root->root_hooks.hook_exec.resume)
714: if (root->root_hooks.hook_exec.resume(task, NULL)) {
715: flg = -1;
716: break;
717: }
718:
719: TAILQ_REMOVE(&root->root_suspend, task, task_node);
720:
721: task->task_type = taskREADY;
722: #ifdef HAVE_LIBPTHREAD
723: pthread_mutex_lock(&root->root_mtx[taskREADY]);
724: #endif
725: TAILQ_INSERT_TAIL(&root->root_ready, task, task_node);
726: #ifdef HAVE_LIBPTHREAD
727: pthread_mutex_unlock(&root->root_mtx[taskREADY]);
728: #endif
729:
730: flg ^= flg; /* ok */
731: }
732: }
733: #ifdef HAVE_LIBPTHREAD
734: pthread_mutex_unlock(&root->root_mtx[taskSUSPEND]);
735: #endif
736:
737: return flg;
738: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>