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