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