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