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