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