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