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