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: hooks.c,v 1.7.2.4 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: /*
51: * sched_hook_init() - Default INIT hook
52: *
53: * @root = root task
54: * @arg = unused
55: * return: <0 errors and 0 ok
56: */
57: void *
58: sched_hook_init(void *root, void *arg __unused)
59: {
60: sched_root_task_t *r = root;
61:
62: if (!r)
63: return (void*) -1;
64:
65: r->root_kq = kqueue();
66: if (r->root_kq == -1) {
67: LOGERR;
68: return (void*) -1;
69: }
70:
71: return NULL;
72: }
73:
74: /*
75: * sched_hook_fini() - Default FINI hook
76: *
77: * @root = root task
78: * @arg = unused
79: * return: <0 errors and 0 ok
80: */
81: void *
82: sched_hook_fini(void *root, void *arg __unused)
83: {
84: sched_root_task_t *r = root;
85:
86: if (!r)
87: return (void*) -1;
88:
89: if (r->root_kq > 2) {
90: close(r->root_kq);
91: r->root_kq = 0;
92: }
93:
94: return NULL;
95: }
96:
97: /*
98: * sched_hook_cancel() - Default CANCEL hook
99: *
100: * @task = current task
101: * @arg = unused
102: * return: <0 errors and 0 ok
103: */
104: void *
105: sched_hook_cancel(void *task, void *arg __unused)
106: {
107: sched_task_t *t = task;
108: struct kevent chg[1];
109: struct timespec timeout = { 0, 0 };
110:
111: if (!t || !TASK_ROOT(t))
112: return (void*) -1;
113:
114: switch (TASK_TYPE(t)) {
115: case taskREAD:
116: #ifdef __NetBSD__
117: EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
118: #else
119: EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (void*) TASK_FD(t));
120: #endif
121: break;
122: case taskWRITE:
123: #ifdef __NetBSD__
124: EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
125: #else
126: EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (void*) TASK_FD(t));
127: #endif
128: break;
129: case taskALARM:
130: #ifdef __NetBSD__
131: EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_DELETE,
132: 0, 0, (intptr_t) TASK_DATA(t));
133: #else
134: EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_DELETE,
135: 0, 0, (void*) TASK_DATA(t));
136: #endif
137: break;
138: case taskNODE:
139: #ifdef __NetBSD__
140: EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
141: #else
142: EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (void*) TASK_FD(t));
143: #endif
144: break;
145: case taskPROC:
146: #ifdef __NetBSD__
147: EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
148: #else
149: EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
150: #endif
151: break;
152: case taskSIGNAL:
153: #ifdef __NetBSD__
154: EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
155: #else
156: EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
157: #endif
158: break;
159: #ifdef EVFILT_USER
160: case taskUSER:
161: #ifdef __NetBSD__
162: EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
163: #else
164: EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
165: #endif
166: #endif
167: break;
168: default:
169: return NULL;
170: }
171:
172: kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout);
173: return NULL;
174: }
175:
176: /*
177: * sched_hook_read() - Default READ hook
178: *
179: * @task = current task
180: * @arg = unused
181: * return: <0 errors and 0 ok
182: */
183: void *
184: sched_hook_read(void *task, void *arg __unused)
185: {
186: sched_task_t *t = task;
187: struct kevent chg[1];
188: struct timespec timeout = { 0, 0 };
189:
190: if (!t || !TASK_ROOT(t))
191: return (void*) -1;
192:
193: #ifdef __NetBSD__
194: EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t));
195: #else
196: EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t));
197: #endif
198: if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
199: if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
200: TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
201: else
202: LOGERR;
203: return (void*) -1;
204: }
205:
206: return NULL;
207: }
208:
209: /*
210: * sched_hook_write() - Default WRITE hook
211: *
212: * @task = current task
213: * @arg = unused
214: * return: <0 errors and 0 ok
215: */
216: void *
217: sched_hook_write(void *task, void *arg __unused)
218: {
219: sched_task_t *t = task;
220: struct kevent chg[1];
221: struct timespec timeout = { 0, 0 };
222:
223: if (!t || !TASK_ROOT(t))
224: return (void*) -1;
225:
226: #ifdef __NetBSD__
227: EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t));
228: #else
229: EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t));
230: #endif
231: if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
232: if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
233: TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
234: else
235: LOGERR;
236: return (void*) -1;
237: }
238:
239: return NULL;
240: }
241:
242: /*
243: * sched_hook_alarm() - Default ALARM hook
244: *
245: * @task = current task
246: * @arg = unused
247: * return: <0 errors and 0 ok
248: */
249: void *
250: sched_hook_alarm(void *task, void *arg __unused)
251: {
252: sched_task_t *t = task;
253: struct kevent chg[1];
254: struct timespec timeout = { 0, 0 };
255:
256: if (!t || !TASK_ROOT(t))
257: return (void*) -1;
258:
259: #ifdef __NetBSD__
260: EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0,
261: t->task_val.ts.tv_sec * 1000 + t->task_val.ts.tv_nsec / 1000000,
262: (intptr_t) TASK_DATA(t));
263: #else
264: EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0,
265: t->task_val.ts.tv_sec * 1000 + t->task_val.ts.tv_nsec / 1000000,
266: (void*) TASK_DATA(t));
267: #endif
268: if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
269: if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
270: TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
271: else
272: LOGERR;
273: return (void*) -1;
274: }
275:
276: return NULL;
277: }
278:
279: /*
280: * sched_hook_node() - Default NODE hook
281: *
282: * @task = current task
283: * @arg = unused
284: * return: <0 errors and 0 ok
285: */
286: void *
287: sched_hook_node(void *task, void *arg __unused)
288: {
289: sched_task_t *t = task;
290: struct kevent chg[1];
291: struct timespec timeout = { 0, 0 };
292:
293: if (!t || !TASK_ROOT(t))
294: return (void*) -1;
295:
296: #ifdef __NetBSD__
297: EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_ADD | EV_CLEAR,
298: NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB |
299: NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, (intptr_t) TASK_FD(t));
300: #else
301: EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_ADD | EV_CLEAR,
302: NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB |
303: NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, (void*) TASK_FD(t));
304: #endif
305: if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
306: if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
307: TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
308: else
309: LOGERR;
310: return (void*) -1;
311: }
312:
313: return NULL;
314: }
315:
316: /*
317: * sched_hook_proc() - Default PROC hook
318: *
319: * @task = current task
320: * @arg = unused
321: * return: <0 errors and 0 ok
322: */
323: void *
324: sched_hook_proc(void *task, void *arg __unused)
325: {
326: sched_task_t *t = task;
327: struct kevent chg[1];
328: struct timespec timeout = { 0, 0 };
329:
330: if (!t || !TASK_ROOT(t))
331: return (void*) -1;
332:
333: #ifdef __NetBSD__
334: EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_ADD | EV_CLEAR,
335: NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, (intptr_t) TASK_VAL(t));
336: #else
337: EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_ADD | EV_CLEAR,
338: NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, (void*) TASK_VAL(t));
339: #endif
340: if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
341: if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
342: TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
343: else
344: LOGERR;
345: return (void*) -1;
346: }
347:
348: return NULL;
349: }
350:
351: /*
352: * sched_hook_signal() - Default SIGNAL hook
353: *
354: * @task = current task
355: * @arg = unused
356: * return: <0 errors and 0 ok
357: */
358: void *
359: sched_hook_signal(void *task, void *arg __unused)
360: {
361: sched_task_t *t = task;
362: struct kevent chg[1];
363: struct timespec timeout = { 0, 0 };
364:
365: if (!t || !TASK_ROOT(t))
366: return (void*) -1;
367:
368: #ifdef __NetBSD__
369: EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD, 0, 0, (intptr_t) TASK_VAL(t));
370: #else
371: EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD, 0, 0, (void*) TASK_VAL(t));
372: #endif
373: if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
374: if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
375: TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
376: else
377: LOGERR;
378: return (void*) -1;
379: }
380:
381: return NULL;
382: }
383:
384: /*
385: * sched_hook_user() - Default USER hook
386: *
387: * @task = current task
388: * @arg = unused
389: * return: <0 errors and 0 ok
390: */
391: #ifdef EVFILT_USER
392: void *
393: sched_hook_user(void *task, void *arg __unused)
394: {
395: sched_task_t *t = task;
396: struct kevent chg[1];
397: struct timespec timeout = { 0, 0 };
398:
399: if (!t || !TASK_ROOT(t))
400: return (void*) -1;
401:
402: #ifdef __NetBSD__
403: EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_ADD | EV_CLEAR, TASK_DATLEN(t),
404: 0, (intptr_t) TASK_VAL(t));
405: #else
406: EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_ADD | EV_CLEAR, TASK_DATLEN(t),
407: 0, (void*) TASK_VAL(t));
408: #endif
409: if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
410: if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
411: TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
412: else
413: LOGERR;
414: return (void*) -1;
415: }
416:
417: return NULL;
418: }
419: #endif
420:
421: /*
422: * sched_hook_fetch() - Default FETCH hook
423: *
424: * @root = root task
425: * @arg = unused
426: * return: NULL error or !=NULL fetched task
427: */
428: void *
429: sched_hook_fetch(void *root, void *arg __unused)
430: {
431: sched_root_task_t *r = root;
432: sched_task_t *task, *tmp;
433: struct timespec now, m, mtmp;
434: struct timespec *timeout;
435: struct kevent evt[1], res[KQ_EVENTS];
436: register int i;
437: int en;
438:
439: if (!r)
440: return NULL;
441:
442: /* get new task by queue priority */
443: while ((task = TAILQ_FIRST(&r->root_event))) {
444: #ifdef HAVE_LIBPTHREAD
445: pthread_mutex_lock(&r->root_mtx[taskEVENT]);
446: #endif
447: TAILQ_REMOVE(&r->root_event, task, task_node);
448: #ifdef HAVE_LIBPTHREAD
449: pthread_mutex_unlock(&r->root_mtx[taskEVENT]);
450: #endif
451: task->task_type = taskUNUSE;
452: #ifdef HAVE_LIBPTHREAD
453: pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
454: #endif
455: TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
456: #ifdef HAVE_LIBPTHREAD
457: pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
458: #endif
459: return task;
460: }
461: while ((task = TAILQ_FIRST(&r->root_ready))) {
462: #ifdef HAVE_LIBPTHREAD
463: pthread_mutex_lock(&r->root_mtx[taskREADY]);
464: #endif
465: TAILQ_REMOVE(&r->root_ready, task, task_node);
466: #ifdef HAVE_LIBPTHREAD
467: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
468: #endif
469: task->task_type = taskUNUSE;
470: #ifdef HAVE_LIBPTHREAD
471: pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
472: #endif
473: TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
474: #ifdef HAVE_LIBPTHREAD
475: pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
476: #endif
477: return task;
478: }
479:
480: #ifdef TIMER_WITHOUT_SORT
481: clock_gettime(CLOCK_MONOTONIC, &now);
482:
483: sched_timespecclear(&r->root_wait);
484: TAILQ_FOREACH(task, &r->root_timer, task_node) {
485: if (!sched_timespecisset(&r->root_wait))
486: r->root_wait = TASK_TS(task);
487: else if (sched_timespeccmp(&TASK_TS(task), &r->root_wait, -) < 0)
488: r->root_wait = TASK_TS(task);
489: }
490:
491: if (TAILQ_FIRST(&r->root_timer)) {
492: m = r->root_wait;
493: sched_timespecsub(&m, &now, &mtmp);
494: r->root_wait = mtmp;
495: } else {
496: /* set wait INFTIM */
497: sched_timespecinf(&r->root_wait);
498: }
499: #else
500: if (!TAILQ_FIRST(&r->root_eventlo) && (task = TAILQ_FIRST(&r->root_timer))) {
501: clock_gettime(CLOCK_MONOTONIC, &now);
502:
503: m = TASK_TS(task);
504: sched_timespecsub(&m, &now, &mtmp);
505: r->root_wait = mtmp;
506: } else {
507: /* set wait INFTIM */
508: sched_timespecinf(&r->root_wait);
509: }
510: #endif
511: /* if present member of eventLo, set NOWAIT */
512: if (TAILQ_FIRST(&r->root_eventlo))
513: sched_timespecclear(&r->root_wait);
514:
515: if (r->root_wait.tv_sec != -1 && r->root_wait.tv_nsec != -1)
516: timeout = &r->root_wait;
517: else if (sched_timespecisinf(&r->root_poll))
518: timeout = NULL;
519: else
520: timeout = &r->root_poll;
521: if ((en = kevent(r->root_kq, NULL, 0, res, KQ_EVENTS, timeout)) == -1) {
522: if (r->root_hooks.hook_exec.exception) {
523: if (r->root_hooks.hook_exec.exception(r, NULL))
524: return NULL;
525: } else if (errno != EINTR)
526: LOGERR;
527: return NULL;
528: }
529:
530: now.tv_sec = now.tv_nsec = 0;
531: /* Go and catch the cat into pipes ... */
532: for (i = 0; i < en; i++) {
533: memcpy(evt, &res[i], sizeof evt);
534: evt->flags = EV_DELETE;
535: /* Put read/write task to ready queue */
536: switch (res[i].filter) {
537: case EVFILT_READ:
538: TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) {
539: if (TASK_FD(task) != ((intptr_t) res[i].udata))
540: continue;
541: /* remove read handle */
542: #ifdef HAVE_LIBPTHREAD
543: pthread_mutex_lock(&r->root_mtx[taskREAD]);
544: #endif
545: TAILQ_REMOVE(&r->root_read, task, task_node);
546: #ifdef HAVE_LIBPTHREAD
547: pthread_mutex_unlock(&r->root_mtx[taskREAD]);
548: #endif
549: if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) {
550: if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) {
551: task->task_type = taskUNUSE;
552: #ifdef HAVE_LIBPTHREAD
553: pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
554: #endif
555: TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
556: #ifdef HAVE_LIBPTHREAD
557: pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
558: #endif
559: } else {
560: task->task_type = taskREADY;
561: #ifdef HAVE_LIBPTHREAD
562: pthread_mutex_lock(&r->root_mtx[taskREADY]);
563: #endif
564: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
565: #ifdef HAVE_LIBPTHREAD
566: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
567: #endif
568: }
569: } else {
570: task->task_type = taskREADY;
571: #ifdef HAVE_LIBPTHREAD
572: pthread_mutex_lock(&r->root_mtx[taskREADY]);
573: #endif
574: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
575: #ifdef HAVE_LIBPTHREAD
576: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
577: #endif
578: }
579: break;
580: }
581: break;
582: case EVFILT_WRITE:
583: TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) {
584: if (TASK_FD(task) != ((intptr_t) res[i].udata))
585: continue;
586: /* remove write handle */
587: #ifdef HAVE_LIBPTHREAD
588: pthread_mutex_lock(&r->root_mtx[taskWRITE]);
589: #endif
590: TAILQ_REMOVE(&r->root_write, task, task_node);
591: #ifdef HAVE_LIBPTHREAD
592: pthread_mutex_unlock(&r->root_mtx[taskWRITE]);
593: #endif
594: if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) {
595: if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) {
596: task->task_type = taskUNUSE;
597: #ifdef HAVE_LIBPTHREAD
598: pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
599: #endif
600: TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
601: #ifdef HAVE_LIBPTHREAD
602: pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
603: #endif
604: } else {
605: task->task_type = taskREADY;
606: #ifdef HAVE_LIBPTHREAD
607: pthread_mutex_lock(&r->root_mtx[taskREADY]);
608: #endif
609: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
610: #ifdef HAVE_LIBPTHREAD
611: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
612: #endif
613: }
614: } else {
615: task->task_type = taskREADY;
616: #ifdef HAVE_LIBPTHREAD
617: pthread_mutex_lock(&r->root_mtx[taskREADY]);
618: #endif
619: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
620: #ifdef HAVE_LIBPTHREAD
621: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
622: #endif
623: }
624: break;
625: }
626: break;
627: case EVFILT_TIMER:
628: TAILQ_FOREACH_SAFE(task, &r->root_alarm, task_node, tmp) {
629: if ((uintptr_t) TASK_DATA(task) != ((uintptr_t) res[i].udata))
630: continue;
631: /* remove alarm handle */
632: #ifdef HAVE_LIBPTHREAD
633: pthread_mutex_lock(&r->root_mtx[taskALARM]);
634: #endif
635: TAILQ_REMOVE(&r->root_alarm, task, task_node);
636: #ifdef HAVE_LIBPTHREAD
637: pthread_mutex_unlock(&r->root_mtx[taskALARM]);
638: #endif
639: task->task_type = taskREADY;
640: #ifdef HAVE_LIBPTHREAD
641: pthread_mutex_lock(&r->root_mtx[taskREADY]);
642: #endif
643: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
644: #ifdef HAVE_LIBPTHREAD
645: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
646: #endif
647: break;
648: }
649: break;
650: case EVFILT_VNODE:
651: TAILQ_FOREACH_SAFE(task, &r->root_node, task_node, tmp) {
652: if (TASK_FD(task) != ((intptr_t) res[i].udata))
653: continue;
654: else {
655: TASK_DATA(task) = (void*) res[i].data;
656: TASK_DATLEN(task) = res[i].fflags;
657: }
658: /* remove node handle */
659: #ifdef HAVE_LIBPTHREAD
660: pthread_mutex_lock(&r->root_mtx[taskNODE]);
661: #endif
662: TAILQ_REMOVE(&r->root_node, task, task_node);
663: #ifdef HAVE_LIBPTHREAD
664: pthread_mutex_unlock(&r->root_mtx[taskNODE]);
665: #endif
666: task->task_type = taskREADY;
667: #ifdef HAVE_LIBPTHREAD
668: pthread_mutex_lock(&r->root_mtx[taskREADY]);
669: #endif
670: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
671: #ifdef HAVE_LIBPTHREAD
672: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
673: #endif
674: break;
675: }
676: break;
677: case EVFILT_PROC:
678: TAILQ_FOREACH_SAFE(task, &r->root_proc, task_node, tmp) {
679: if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
680: continue;
681: else {
682: TASK_DATA(task) = (void*) res[i].data;
683: TASK_DATLEN(task) = res[i].fflags;
684: }
685: /* remove proc handle */
686: #ifdef HAVE_LIBPTHREAD
687: pthread_mutex_lock(&r->root_mtx[taskPROC]);
688: #endif
689: TAILQ_REMOVE(&r->root_proc, task, task_node);
690: #ifdef HAVE_LIBPTHREAD
691: pthread_mutex_unlock(&r->root_mtx[taskPROC]);
692: #endif
693: task->task_type = taskREADY;
694: #ifdef HAVE_LIBPTHREAD
695: pthread_mutex_lock(&r->root_mtx[taskREADY]);
696: #endif
697: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
698: #ifdef HAVE_LIBPTHREAD
699: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
700: #endif
701: break;
702: }
703: break;
704: case EVFILT_SIGNAL:
705: TAILQ_FOREACH_SAFE(task, &r->root_signal, task_node, tmp) {
706: if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
707: continue;
708: /* remove signal handle */
709: #ifdef HAVE_LIBPTHREAD
710: pthread_mutex_lock(&r->root_mtx[taskSIGNAL]);
711: #endif
712: TAILQ_REMOVE(&r->root_signal, task, task_node);
713: #ifdef HAVE_LIBPTHREAD
714: pthread_mutex_unlock(&r->root_mtx[taskSIGNAL]);
715: #endif
716: task->task_type = taskREADY;
717: #ifdef HAVE_LIBPTHREAD
718: pthread_mutex_lock(&r->root_mtx[taskREADY]);
719: #endif
720: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
721: #ifdef HAVE_LIBPTHREAD
722: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
723: #endif
724: break;
725: }
726: break;
727: #ifdef EVFILT_USER
728: case EVFILT_USER:
729: TAILQ_FOREACH_SAFE(task, &r->root_user, task_node, tmp) {
730: if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
731: continue;
732: else {
733: TASK_DATA(task) = (void*) res[i].data;
734: TASK_DATLEN(task) = res[i].fflags;
735: }
736: /* remove user handle */
737: #ifdef HAVE_LIBPTHREAD
738: pthread_mutex_lock(&r->root_mtx[taskUSER]);
739: #endif
740: TAILQ_REMOVE(&r->root_user, task, task_node);
741: #ifdef HAVE_LIBPTHREAD
742: pthread_mutex_unlock(&r->root_mtx[taskUSER]);
743: #endif
744: task->task_type = taskREADY;
745: #ifdef HAVE_LIBPTHREAD
746: pthread_mutex_lock(&r->root_mtx[taskREADY]);
747: #endif
748: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
749: #ifdef HAVE_LIBPTHREAD
750: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
751: #endif
752: break;
753: }
754: break;
755: #endif
756: }
757: if (kevent(r->root_kq, evt, 1, NULL, 0, &now) == -1) {
758: if (r->root_hooks.hook_exec.exception) {
759: if (r->root_hooks.hook_exec.exception(r, NULL))
760: return NULL;
761: } else
762: LOGERR;
763: }
764: }
765:
766: /* timer update & put in ready queue */
767: clock_gettime(CLOCK_MONOTONIC, &now);
768:
769: TAILQ_FOREACH_SAFE(task, &r->root_timer, task_node, tmp)
770: if (sched_timespeccmp(&now, &TASK_TS(task), -) >= 0) {
771: #ifdef HAVE_LIBPTHREAD
772: pthread_mutex_lock(&r->root_mtx[taskTIMER]);
773: #endif
774: TAILQ_REMOVE(&r->root_timer, task, task_node);
775: #ifdef HAVE_LIBPTHREAD
776: pthread_mutex_unlock(&r->root_mtx[taskTIMER]);
777: #endif
778: task->task_type = taskREADY;
779: #ifdef HAVE_LIBPTHREAD
780: pthread_mutex_lock(&r->root_mtx[taskREADY]);
781: #endif
782: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
783: #ifdef HAVE_LIBPTHREAD
784: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
785: #endif
786: }
787:
788: /* put eventlo priority task to ready queue, if there is no ready task or
789: reach max missed fetch-rotate */
790: if ((task = TAILQ_FIRST(&r->root_eventlo))) {
791: if (!TAILQ_FIRST(&r->root_ready) || r->root_eventlo_miss > MAX_EVENTLO_MISS) {
792: r->root_eventlo_miss = 0;
793:
794: #ifdef HAVE_LIBPTHREAD
795: pthread_mutex_lock(&r->root_mtx[taskEVENTLO]);
796: #endif
797: TAILQ_REMOVE(&r->root_eventlo, task, task_node);
798: #ifdef HAVE_LIBPTHREAD
799: pthread_mutex_unlock(&r->root_mtx[taskEVENTLO]);
800: #endif
801: task->task_type = taskREADY;
802: #ifdef HAVE_LIBPTHREAD
803: pthread_mutex_lock(&r->root_mtx[taskREADY]);
804: #endif
805: TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
806: #ifdef HAVE_LIBPTHREAD
807: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
808: #endif
809: } else
810: r->root_eventlo_miss++;
811: } else
812: r->root_eventlo_miss = 0;
813:
814: /* OK, lets get ready task !!! */
815: task = TAILQ_FIRST(&r->root_ready);
816: if (!(task))
817: return NULL;
818:
819: #ifdef HAVE_LIBPTHREAD
820: pthread_mutex_lock(&r->root_mtx[taskREADY]);
821: #endif
822: TAILQ_REMOVE(&r->root_ready, task, task_node);
823: #ifdef HAVE_LIBPTHREAD
824: pthread_mutex_unlock(&r->root_mtx[taskREADY]);
825: #endif
826: task->task_type = taskUNUSE;
827: #ifdef HAVE_LIBPTHREAD
828: pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
829: #endif
830: TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
831: #ifdef HAVE_LIBPTHREAD
832: pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
833: #endif
834: return task;
835: }
836:
837: /*
838: * sched_hook_exception() - Default EXCEPTION hook
839: *
840: * @root = root task
841: * @arg = custom handling: if arg == EV_EOF or other value; default: arg == NULL log errno
842: * return: <0 errors and 0 ok
843: */
844: void *
845: sched_hook_exception(void *root, void *arg)
846: {
847: sched_root_task_t *r = root;
848:
849: if (!r)
850: return NULL;
851:
852: /* custom exception handling ... */
853: if (arg) {
854: if (arg == (void*) EV_EOF)
855: return NULL;
856: return (void*) -1; /* raise scheduler error!!! */
857: }
858:
859: /* if error hook exists */
860: if (r->root_hooks.hook_root.error)
861: return (r->root_hooks.hook_root.error(root, (void*) ((intptr_t) errno)));
862:
863: /* default case! */
864: LOGERR;
865: return NULL;
866: }
867:
868: /*
869: * sched_hook_condition() - Default CONDITION hook
870: *
871: * @root = root task
872: * @arg = killState from schedRun()
873: * return: NULL kill scheduler loop or !=NULL ok
874: */
875: void *
876: sched_hook_condition(void *root, void *arg)
877: {
878: sched_root_task_t *r = root;
879:
880: if (!r)
881: return NULL;
882:
883: return (void*) (r->root_cond - *(intptr_t*) arg);
884: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>