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