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.18.6.1 2013/08/15 17:58:31 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: root->root_hooks.hook_add.rtc = sched_hook_rtc;
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
112: #ifdef HAVE_LIBPTHREAD
113: root->root_hooks.hook_add.thread = sched_hook_thread;
114: #endif
115:
116: root->root_hooks.hook_exec.cancel = sched_hook_cancel;
117: root->root_hooks.hook_exec.fetch = sched_hook_fetch;
118: root->root_hooks.hook_exec.exception = sched_hook_exception;
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: /*
126: * schedInit() - Init scheduler
127: *
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 *);
137: #ifdef HAVE_LIBPTHREAD
138: register int i;
139: #endif
140:
141: root = malloc(sizeof(sched_root_task_t));
142: if (!root) {
143: LOGERR;
144: } else {
145: memset(root, 0, sizeof(sched_root_task_t));
146:
147: /* set default maximum regular task hit misses */
148: root->root_miss = MAX_TASK_MISS;
149:
150: /* INFINIT polling period by default */
151: sched_timespecinf(&root->root_poll);
152:
153: #ifdef HAVE_LIBPTHREAD
154: for (i = 0; i < taskMAX; i++)
155: if ((errno = pthread_mutex_init(&root->root_mtx[i], NULL))) {
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:
167: TAILQ_INIT(&root->root_read);
168: TAILQ_INIT(&root->root_write);
169: TAILQ_INIT(&root->root_timer);
170: TAILQ_INIT(&root->root_alarm);
171: TAILQ_INIT(&root->root_rtc);
172: TAILQ_INIT(&root->root_node);
173: TAILQ_INIT(&root->root_proc);
174: TAILQ_INIT(&root->root_signal);
175: TAILQ_INIT(&root->root_aio);
176: TAILQ_INIT(&root->root_lio);
177: TAILQ_INIT(&root->root_user);
178: TAILQ_INIT(&root->root_event);
179: TAILQ_INIT(&root->root_task);
180: TAILQ_INIT(&root->root_suspend);
181: TAILQ_INIT(&root->root_ready);
182: TAILQ_INIT(&root->root_unuse);
183: TAILQ_INIT(&root->root_thread);
184:
185: #ifdef HAVE_LIBPTHREAD
186: for (i = 0; i < taskMAX; i++)
187: pthread_mutex_unlock(&root->root_mtx[i]);
188: #endif
189:
190: if (data && *data) {
191: if (datlen) {
192: root->root_data.iov_base = *data;
193: root->root_data.iov_len = datlen;
194: } else { /* if datlen == 0, switch to callbacks init mode */
195: /* little hack :) for correct initialization of scheduler */
196: func = (int(*)(sched_root_task_t*)) data;
197: func(root);
198: }
199: }
200:
201: if (root->root_hooks.hook_root.init)
202: root->root_hooks.hook_root.init(root, NULL);
203: }
204:
205: return root;
206: }
207:
208: /*
209: * schedEnd() - End scheduler & free all resources
210: *
211: * @root = root task
212: * return: -1 error or 0 ok
213: */
214: int
215: schedEnd(sched_root_task_t ** __restrict root)
216: {
217: sched_task_t *task, *tmp;
218: #ifdef HAVE_LIBPTHREAD
219: register int i;
220: #endif
221:
222: if (!root || !*root)
223: return -1;
224:
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)
228: schedCancel(task);
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);
233: TAILQ_FOREACH_SAFE(task, &(*root)->root_rtc, task_node, tmp)
234: schedCancel(task);
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)
238: schedCancel(task);
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);
245: TAILQ_FOREACH_SAFE(task, &(*root)->root_user, task_node, tmp)
246: schedCancel(task);
247: TAILQ_FOREACH_SAFE(task, &(*root)->root_event, task_node, tmp)
248: schedCancel(task);
249: TAILQ_FOREACH_SAFE(task, &(*root)->root_suspend, task_node, tmp)
250: schedCancel(task);
251: TAILQ_FOREACH_SAFE(task, &(*root)->root_ready, task_node, tmp)
252: schedCancel(task);
253: TAILQ_FOREACH_SAFE(task, &(*root)->root_thread, task_node, tmp)
254: schedCancel(task);
255: TAILQ_FOREACH_SAFE(task, &(*root)->root_task, task_node, tmp)
256: schedCancel(task);
257:
258: #ifdef HAVE_LIBPTHREAD
259: pthread_mutex_lock(&(*root)->root_mtx[taskUNUSE]);
260: #endif
261: TAILQ_FOREACH_SAFE(task, &(*root)->root_unuse, task_node, tmp) {
262: TAILQ_REMOVE(&(*root)->root_unuse, task, task_node);
263: free(task);
264: }
265: #ifdef HAVE_LIBPTHREAD
266: pthread_mutex_unlock(&(*root)->root_mtx[taskUNUSE]);
267: #endif
268:
269: if ((*root)->root_hooks.hook_root.fini)
270: (*root)->root_hooks.hook_root.fini(*root, NULL);
271:
272: #ifdef HAVE_LIBPTHREAD
273: for (i = 0; i < taskMAX; i++)
274: pthread_mutex_destroy(&(*root)->root_mtx[i]);
275: #endif
276:
277: free(*root);
278: *root = NULL;
279: return 0;
280: }
281:
282: /*
283: * schedCall() - Call task execution function
284: *
285: * @task = current task
286: * return: !=NULL error or =NULL ok
287: */
288: void *
289: schedCall(sched_task_t * __restrict task)
290: {
291: void *ptr = (void*) -1;
292:
293: if (!task)
294: return ptr;
295:
296: if (!TASK_ISLOCKED(task))
297: TASK_LOCK(task);
298:
299: ptr = task->task_func(task);
300:
301: TASK_UNLOCK(task);
302: return ptr;
303: }
304:
305: /*
306: * schedFetch() - Fetch ready task
307: *
308: * @root = root task
309: * return: =NULL error or !=NULL ready task
310: */
311: void *
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: /*
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: /*
361: * schedCancel() - Cancel task from scheduler
362: *
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:
371: if (!task || !TASK_ROOT(task))
372: return -1;
373:
374: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel)
375: if (TASK_ROOT(task)->root_hooks.hook_exec.cancel(task, NULL))
376: return -1;
377:
378: switch (TASK_TYPE(task)) {
379: case taskREAD:
380: queue = &TASK_ROOT(task)->root_read;
381: break;
382: case taskWRITE:
383: queue = &TASK_ROOT(task)->root_write;
384: break;
385: case taskTIMER:
386: queue = &TASK_ROOT(task)->root_timer;
387: break;
388: case taskALARM:
389: queue = &TASK_ROOT(task)->root_alarm;
390: break;
391: case taskRTC:
392: queue = &TASK_ROOT(task)->root_rtc;
393: break;
394: case taskNODE:
395: queue = &TASK_ROOT(task)->root_node;
396: break;
397: case taskPROC:
398: queue = &TASK_ROOT(task)->root_proc;
399: break;
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;
409: case taskUSER:
410: queue = &TASK_ROOT(task)->root_user;
411: break;
412: case taskEVENT:
413: queue = &TASK_ROOT(task)->root_event;
414: break;
415: case taskTASK:
416: queue = &TASK_ROOT(task)->root_task;
417: break;
418: case taskSUSPEND:
419: queue = &TASK_ROOT(task)->root_suspend;
420: break;
421: case taskREADY:
422: queue = &TASK_ROOT(task)->root_ready;
423: break;
424: case taskTHREAD:
425: queue = &TASK_ROOT(task)->root_thread;
426: break;
427: default:
428: queue = NULL;
429: }
430: if (queue) {
431: #ifdef HAVE_LIBPTHREAD
432: pthread_mutex_lock(&TASK_ROOT(task)->root_mtx[TASK_TYPE(task)]);
433: #endif
434: TAILQ_REMOVE(queue, TASK_ID(task), task_node);
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)
440: sched_unuseTask(task);
441:
442: return 0;
443: }
444:
445: /*
446: * schedCancelby() - Cancel task from scheduler by criteria
447: *
448: * @root = root task
449: * @type = cancel from queue type, if =taskMAX cancel same task from all queues
450: * @criteria = find task by criteria
451: * [CRITERIA_ANY|CRITERIA_CALL|CRITERIA_ARG|CRITERIA_FD|CRITERIA_VAL|CRITERIA_ID|CRITERIA_TS|CRITERIA_DATA]
452: * @param = search parameter
453: * @hook = custom cleanup hook function, may be NULL
454: * return: -1 error, -2 error in sub-stage cancel execution, -3 error from custom hook or 0 ok
455: */
456: int
457: schedCancelby(sched_root_task_t * __restrict root, sched_task_type_t type,
458: u_char criteria, void *param, sched_hook_func_t hook)
459: {
460: sched_task_t *task, *tmp;
461: sched_queue_t *queue;
462: register int flg = 0;
463:
464: if (!root)
465: return -1;
466: /* if type == taskMAX check in all queues */
467: if (type == taskMAX) {
468: if (schedCancelby(root, taskREAD, criteria, param, hook))
469: return -2;
470: if (schedCancelby(root, taskWRITE, criteria, param, hook))
471: return -2;
472: if (schedCancelby(root, taskTIMER, criteria, param, hook))
473: return -2;
474: if (schedCancelby(root, taskALARM, criteria, param, hook))
475: return -2;
476: if (schedCancelby(root, taskRTC, criteria, param, hook))
477: return -2;
478: if (schedCancelby(root, taskNODE, criteria, param, hook))
479: return -2;
480: if (schedCancelby(root, taskPROC, criteria, param, hook))
481: return -2;
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;
488: if (schedCancelby(root, taskUSER, criteria, param, hook))
489: return -2;
490: if (schedCancelby(root, taskEVENT, criteria, param, hook))
491: return -2;
492: if (schedCancelby(root, taskTASK, criteria, param, hook))
493: return -2;
494: if (schedCancelby(root, taskSUSPEND, criteria, param, hook))
495: return -2;
496: if (schedCancelby(root, taskREADY, criteria, param, hook))
497: return -2;
498: if (schedCancelby(root, taskTHREAD, criteria, param, hook))
499: return -2;
500: return 0;
501: }
502: /* choosen queue */
503: switch (type) {
504: case taskREAD:
505: queue = &root->root_read;
506: break;
507: case taskWRITE:
508: queue = &root->root_write;
509: break;
510: case taskTIMER:
511: queue = &root->root_timer;
512: break;
513: case taskALARM:
514: queue = &root->root_alarm;
515: break;
516: case taskRTC:
517: queue = &root->root_rtc;
518: break;
519: case taskNODE:
520: queue = &root->root_node;
521: break;
522: case taskPROC:
523: queue = &root->root_proc;
524: break;
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;
534: case taskUSER:
535: queue = &root->root_user;
536: break;
537: case taskEVENT:
538: queue = &root->root_event;
539: break;
540: case taskTASK:
541: queue = &root->root_task;
542: break;
543: case taskSUSPEND:
544: queue = &root->root_suspend;
545: break;
546: case taskREADY:
547: queue = &root->root_ready;
548: break;
549: case taskTHREAD:
550: queue = &root->root_thread;
551: break;
552: default:
553: return 0;
554: }
555:
556: #ifdef HAVE_LIBPTHREAD
557: pthread_mutex_lock(&root->root_mtx[type]);
558: #endif
559: TAILQ_FOREACH_SAFE(task, queue, task_node, tmp) {
560: flg ^= flg;
561: switch (criteria) {
562: case CRITERIA_ANY:
563: flg = 1;
564: break;
565: case CRITERIA_CALL:
566: if (TASK_FUNC(task) == (sched_task_func_t) param)
567: flg = 1;
568: break;
569: case CRITERIA_ARG:
570: if (TASK_ARG(task) == param)
571: flg = 1;
572: break;
573: case CRITERIA_FD:
574: if (TASK_FD(task) == (intptr_t) param)
575: flg = 1;
576: break;
577: case CRITERIA_ID:
578: case CRITERIA_VAL:
579: if (TASK_VAL(task) == (u_long) param)
580: flg = 1;
581: break;
582: case CRITERIA_TS:
583: if (!sched_timespeccmp(&TASK_TS(task), (struct timespec*) param, -))
584: flg = 1;
585: break;
586: case CRITERIA_DATA:
587: if (TASK_DATA(task) == param)
588: flg = 1;
589: break;
590: default:
591: sched_SetErr(EINVAL, "Invalid parameter criteria %d", criteria);
592: flg = -1;
593: }
594: if (flg < 0) /* error */
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)
612: sched_unuseTask(task);
613:
614: flg ^= flg; /* ok */
615: }
616: }
617: #ifdef HAVE_LIBPTHREAD
618: pthread_mutex_unlock(&root->root_mtx[type]);
619: #endif
620: return flg;
621: }
622:
623: /*
624: * schedRun() - Scheduler *run loop*
625: *
626: * @root = root task
627: * @killState = kill condition variable, if !=0 stop scheduler loop
628: * return: -1 error or 0 ok
629: */
630: int
631: schedRun(sched_root_task_t *root, volatile intptr_t * __restrict killState)
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;
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)))
649: root->root_ret = schedCall(task);
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)))
655: root->root_ret = schedCall(task);
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)))
661: root->root_ret = schedCall(task);
662:
663: return 0;
664: }
665:
666: /*
667: * schedPolling() - Polling timeout period if no timer task is present
668: *
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: */
674: int
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: }
691:
692: /*
693: * schedTermCondition() - Activate hook for scheduler condition kill
694: *
695: * @root = root task
696: * @condValue = condition value, kill schedRun() if condValue == killState
697: * return: -1 error or 0 ok
698: */
699: int
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: }
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>