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