File:  [ELWIX - Embedded LightWeight unIX -] / libaitsched / src / hooks.c
Revision 1.10: download - view: text, annotated - select for diffs - revision graph
Tue Jul 24 14:06:11 2012 UTC (11 years, 11 months ago) by misho
Branches: MAIN
CVS tags: sched2_6, SCHED2_5, HEAD
version 2.5

/*************************************************************************
* (C) 2011 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
*  by Michael Pounov <misho@openbsd-bg.org>
*
* $Author: misho $
* $Id: hooks.c,v 1.10 2012/07/24 14:06:11 misho Exp $
*
**************************************************************************
The ELWIX and AITNET software is distributed under the following
terms:

All of the documentation and software included in the ELWIX and AITNET
Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>

Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
	by Michael Pounov <misho@elwix.org>.  All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software
   must display the following acknowledgement:
This product includes software developed by Michael Pounov <misho@elwix.org>
ELWIX - Embedded LightWeight unIX and its contributors.
4. Neither the name of AITNET nor the names of its contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "global.h"
#include "hooks.h"


/*
 * sched_hook_init() - Default INIT hook
 *
 * @root = root task
 * @arg = unused
 * return: <0 errors and 0 ok
 */
void *
sched_hook_init(void *root, void *arg __unused)
{
	sched_root_task_t *r = root;

	if (!r)
		return (void*) -1;

	r->root_kq = kqueue();
	if (r->root_kq == -1) {
		LOGERR;
		return (void*) -1;
	}

	return NULL;
}

/*
 * sched_hook_fini() - Default FINI hook
 *
 * @root = root task
 * @arg = unused
 * return: <0 errors and 0 ok
 */
void *
sched_hook_fini(void *root, void *arg __unused)
{
	sched_root_task_t *r = root;

	if (!r)
		return (void*) -1;

	if (r->root_kq > 2) {
		close(r->root_kq);
		r->root_kq = 0;
	}

	return NULL;
}

/*
 * sched_hook_cancel() - Default CANCEL hook
 *
 * @task = current task
 * @arg = unused
 * return: <0 errors and 0 ok
 */
void *
sched_hook_cancel(void *task, void *arg __unused)
{
	sched_task_t *t = task;
	struct kevent chg[1];
	struct timespec timeout = { 0, 0 };

	if (!t || !TASK_ROOT(t))
		return (void*) -1;

	switch (TASK_TYPE(t)) {
		case taskREAD:
#ifdef __NetBSD__
			EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
#else
			EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_DELETE, 0, 0, (void*) TASK_FD(t));
#endif
			break;
		case taskWRITE:
#ifdef __NetBSD__
			EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
#else
			EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_DELETE, 0, 0, (void*) TASK_FD(t));
#endif
			break;
		case taskALARM:
#ifdef __NetBSD__
			EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_DELETE, 
					0, 0, (intptr_t) TASK_DATA(t));
#else
			EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_DELETE, 
					0, 0, (void*) TASK_DATA(t));
#endif
			break;
		case taskNODE:
#ifdef __NetBSD__
			EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (intptr_t) TASK_FD(t));
#else
			EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_DELETE, 0, 0, (void*) TASK_FD(t));
#endif
			break;
		case taskPROC:
#ifdef __NetBSD__
			EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
#else
			EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
#endif
			break;
		case taskSIGNAL:
#ifdef __NetBSD__
			EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
#else
			EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
#endif
			break;
#ifdef EVFILT_USER
		case taskUSER:
#ifdef __NetBSD__
			EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (intptr_t) TASK_VAL(t));
#else
			EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_DELETE, 0, 0, (void*) TASK_VAL(t));
#endif
			break;
#endif
		default:
			return NULL;
	}

	kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout);
	return NULL;
}

/*
 * sched_hook_read() - Default READ hook
 *
 * @task = current task
 * @arg = unused
 * return: <0 errors and 0 ok
 */
void *
sched_hook_read(void *task, void *arg __unused)
{
	sched_task_t *t = task;
	struct kevent chg[1];
	struct timespec timeout = { 0, 0 };

	if (!t || !TASK_ROOT(t))
		return (void*) -1;

#ifdef __NetBSD__
	EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t));
#else
	EV_SET(&chg[0], TASK_FD(t), EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t));
#endif
	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
		else
			LOGERR;
		return (void*) -1;
	}

	return NULL;
}

/*
 * sched_hook_write() - Default WRITE hook
 *
 * @task = current task
 * @arg = unused
 * return: <0 errors and 0 ok
 */
void *
sched_hook_write(void *task, void *arg __unused)
{
	sched_task_t *t = task;
	struct kevent chg[1];
	struct timespec timeout = { 0, 0 };

	if (!t || !TASK_ROOT(t))
		return (void*) -1;

#ifdef __NetBSD__
	EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (intptr_t) TASK_FD(t));
#else
	EV_SET(&chg[0], TASK_FD(t), EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, (void*) TASK_FD(t));
#endif
	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
		else
			LOGERR;
		return (void*) -1;
	}

	return NULL;
}

/*
 * sched_hook_alarm() - Default ALARM hook
 *
 * @task = current task
 * @arg = unused
 * return: <0 errors and 0 ok
 */
void *
sched_hook_alarm(void *task, void *arg __unused)
{
	sched_task_t *t = task;
	struct kevent chg[1];
	struct timespec timeout = { 0, 0 };

	if (!t || !TASK_ROOT(t))
		return (void*) -1;

#ifdef __NetBSD__
	EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 
			t->task_val.ts.tv_sec * 1000 + t->task_val.ts.tv_nsec / 1000000, 
			(intptr_t) TASK_DATA(t));
#else
	EV_SET(&chg[0], (uintptr_t) TASK_DATA(t), EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 
			t->task_val.ts.tv_sec * 1000 + t->task_val.ts.tv_nsec / 1000000, 
			(void*) TASK_DATA(t));
#endif
	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
		else
			LOGERR;
		return (void*) -1;
	}

	return NULL;
}

/*
 * sched_hook_node() - Default NODE hook
 *
 * @task = current task
 * @arg = unused
 * return: <0 errors and 0 ok
 */
void *
sched_hook_node(void *task, void *arg __unused)
{
	sched_task_t *t = task;
	struct kevent chg[1];
	struct timespec timeout = { 0, 0 };

	if (!t || !TASK_ROOT(t))
		return (void*) -1;

#ifdef __NetBSD__
	EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_ADD | EV_CLEAR, 
			NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | 
			NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, (intptr_t) TASK_FD(t));
#else
	EV_SET(&chg[0], TASK_FD(t), EVFILT_VNODE, EV_ADD | EV_CLEAR, 
			NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND | NOTE_ATTRIB | 
			NOTE_LINK | NOTE_RENAME | NOTE_REVOKE, 0, (void*) TASK_FD(t));
#endif
	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
		else
			LOGERR;
		return (void*) -1;
	}

	return NULL;
}

/*
 * sched_hook_proc() - Default PROC hook
 *
 * @task = current task
 * @arg = unused
 * return: <0 errors and 0 ok
 */
void *
sched_hook_proc(void *task, void *arg __unused)
{
	sched_task_t *t = task;
	struct kevent chg[1];
	struct timespec timeout = { 0, 0 };

	if (!t || !TASK_ROOT(t))
		return (void*) -1;

#ifdef __NetBSD__
	EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_ADD | EV_CLEAR, 
			NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, (intptr_t) TASK_VAL(t));
#else
	EV_SET(&chg[0], TASK_VAL(t), EVFILT_PROC, EV_ADD | EV_CLEAR, 
			NOTE_EXIT | NOTE_FORK | NOTE_EXEC | NOTE_TRACK, 0, (void*) TASK_VAL(t));
#endif
	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
		else
			LOGERR;
		return (void*) -1;
	}

	return NULL;
}

/*
 * sched_hook_signal() - Default SIGNAL hook
 *
 * @task = current task
 * @arg = unused
 * return: <0 errors and 0 ok
 */
void *
sched_hook_signal(void *task, void *arg __unused)
{
	sched_task_t *t = task;
	struct kevent chg[1];
	struct timespec timeout = { 0, 0 };

	if (!t || !TASK_ROOT(t))
		return (void*) -1;

#ifdef __NetBSD__
	EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD, 0, 0, (intptr_t) TASK_VAL(t));
#else
	EV_SET(&chg[0], TASK_VAL(t), EVFILT_SIGNAL, EV_ADD, 0, 0, (void*) TASK_VAL(t));
#endif
	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
		else
			LOGERR;
		return (void*) -1;
	}

	return NULL;
}

/*
 * sched_hook_user() - Default USER hook
 *
 * @task = current task
 * @arg = unused
 * return: <0 errors and 0 ok
 */
#ifdef EVFILT_USER
void *
sched_hook_user(void *task, void *arg __unused)
{
	sched_task_t *t = task;
	struct kevent chg[1];
	struct timespec timeout = { 0, 0 };

	if (!t || !TASK_ROOT(t))
		return (void*) -1;

#ifdef __NetBSD__
	EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_ADD | EV_CLEAR, TASK_DATLEN(t), 
			0, (intptr_t) TASK_VAL(t));
#else
	EV_SET(&chg[0], TASK_VAL(t), EVFILT_USER, EV_ADD | EV_CLEAR, TASK_DATLEN(t), 
			0, (void*) TASK_VAL(t));
#endif
	if (kevent(TASK_ROOT(t)->root_kq, chg, 1, NULL, 0, &timeout) == -1) {
		if (TASK_ROOT(t)->root_hooks.hook_exec.exception)
			TASK_ROOT(t)->root_hooks.hook_exec.exception(TASK_ROOT(t), NULL);
		else
			LOGERR;
		return (void*) -1;
	}

	return NULL;
}
#endif

/*
 * sched_hook_fetch() - Default FETCH hook
 *
 * @root = root task
 * @arg = unused
 * return: NULL error or !=NULL fetched task
 */
void *
sched_hook_fetch(void *root, void *arg __unused)
{
	sched_root_task_t *r = root;
	sched_task_t *task, *tmp;
	struct timespec now, m, mtmp;
	struct timespec *timeout;
	struct kevent evt[1], res[KQ_EVENTS];
	register int i, flg;
	int en;

	if (!r)
		return NULL;

	/* get new task by queue priority */
	while ((task = TAILQ_FIRST(&r->root_event))) {
#ifdef HAVE_LIBPTHREAD
		pthread_mutex_lock(&r->root_mtx[taskEVENT]);
#endif
		TAILQ_REMOVE(&r->root_event, task, task_node);
#ifdef HAVE_LIBPTHREAD
		pthread_mutex_unlock(&r->root_mtx[taskEVENT]);
#endif
		task->task_type = taskUNUSE;
#ifdef HAVE_LIBPTHREAD
		pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
#endif
		TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
#ifdef HAVE_LIBPTHREAD
		pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
#endif
		return task;
	}
	while ((task = TAILQ_FIRST(&r->root_ready))) {
#ifdef HAVE_LIBPTHREAD
		pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
		TAILQ_REMOVE(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
		pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
		task->task_type = taskUNUSE;
#ifdef HAVE_LIBPTHREAD
		pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
#endif
		TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
#ifdef HAVE_LIBPTHREAD
		pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
#endif
		return task;
	}

#ifdef TIMER_WITHOUT_SORT
	clock_gettime(CLOCK_MONOTONIC, &now);

	sched_timespecclear(&r->root_wait);
	TAILQ_FOREACH(task, &r->root_timer, task_node) {
		if (!sched_timespecisset(&r->root_wait))
			r->root_wait = TASK_TS(task);
		else if (sched_timespeccmp(&TASK_TS(task), &r->root_wait, -) < 0)
			r->root_wait = TASK_TS(task);
	}

	if (TAILQ_FIRST(&r->root_timer)) {
		m = r->root_wait;
		sched_timespecsub(&m, &now, &mtmp);
		r->root_wait = mtmp;
	} else {
		/* set wait INFTIM */
		sched_timespecinf(&r->root_wait);
	}
#else
	if (!TAILQ_FIRST(&r->root_eventlo) && (task = TAILQ_FIRST(&r->root_timer))) {
		clock_gettime(CLOCK_MONOTONIC, &now);

		m = TASK_TS(task);
		sched_timespecsub(&m, &now, &mtmp);
		r->root_wait = mtmp;
	} else {
		/* set wait INFTIM */
		sched_timespecinf(&r->root_wait);
	}
#endif
	/* if present member of eventLo, set NOWAIT */
	if (TAILQ_FIRST(&r->root_eventlo))
		sched_timespecclear(&r->root_wait);

	if (r->root_wait.tv_sec != -1 && r->root_wait.tv_nsec != -1)
		timeout = &r->root_wait;
	else if (sched_timespecisinf(&r->root_poll))
		timeout = NULL;
	else
		timeout = &r->root_poll;
	if ((en = kevent(r->root_kq, NULL, 0, res, KQ_EVENTS, timeout)) == -1) {
		if (r->root_hooks.hook_exec.exception) {
			if (r->root_hooks.hook_exec.exception(r, NULL))
				return NULL;
		} else if (errno != EINTR)
			LOGERR;
		return NULL;
	}

	now.tv_sec = now.tv_nsec = 0;
	/* Go and catch the cat into pipes ... */
	for (i = 0; i < en; i++) {
		memcpy(evt, &res[i], sizeof evt);
		evt->flags = EV_DELETE;
		/* Put read/write task to ready queue */
		switch (res[i].filter) {
			case EVFILT_READ:
				flg = 0;
				TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) {
					if (TASK_FD(task) != ((intptr_t) res[i].udata))
						continue;
					else
						flg++;
					/* remove read handle */
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskREAD]);
#endif
					TAILQ_REMOVE(&r->root_read, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskREAD]);
#endif
					if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) {
 						if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) {
							task->task_type = taskUNUSE;
#ifdef HAVE_LIBPTHREAD
							pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
#endif
							TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
#ifdef HAVE_LIBPTHREAD
							pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
#endif
						} else {
							task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
							pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
							TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
							pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
						}
					} else {
						task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
						pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
						TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
						pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
					}
				}
				/* if match at least 2, don't remove resouce of event */
				if (flg > 1)
					evt->flags ^= evt->flags;
				break;
			case EVFILT_WRITE:
				flg = 0;
				TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) {
					if (TASK_FD(task) != ((intptr_t) res[i].udata))
						continue;
					else
						flg++;
					/* remove write handle */
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskWRITE]);
#endif
					TAILQ_REMOVE(&r->root_write, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskWRITE]);
#endif
					if (r->root_hooks.hook_exec.exception && res[i].flags & EV_EOF) {
 						if (r->root_hooks.hook_exec.exception(r, (void*) EV_EOF)) {
							task->task_type = taskUNUSE;
#ifdef HAVE_LIBPTHREAD
							pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
#endif
							TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
#ifdef HAVE_LIBPTHREAD
							pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
#endif
						} else {
							task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
							pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
							TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
							pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
						}
					} else {
						task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
						pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
						TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
						pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
					}
				}
				/* if match at least 2, don't remove resouce of event */
				if (flg > 1)
					evt->flags ^= evt->flags;
				break;
			case EVFILT_TIMER:
				flg = 0;
				TAILQ_FOREACH_SAFE(task, &r->root_alarm, task_node, tmp) {
					if ((uintptr_t) TASK_DATA(task) != ((uintptr_t) res[i].udata))
						continue;
					else
						flg++;
					/* remove alarm handle */
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskALARM]);
#endif
					TAILQ_REMOVE(&r->root_alarm, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskALARM]);
#endif
					task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
				}
				/* if match at least 2, don't remove resouce of event */
				if (flg > 1)
					evt->flags ^= evt->flags;
				break;
			case EVFILT_VNODE:
				flg = 0;
				TAILQ_FOREACH_SAFE(task, &r->root_node, task_node, tmp) {
					if (TASK_FD(task) != ((intptr_t) res[i].udata))
						continue;
					else {
						flg++;
						TASK_DATA(task) = (void*) (uintptr_t) res[i].data;
						TASK_DATLEN(task) = res[i].fflags;
					}
					/* remove node handle */
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskNODE]);
#endif
					TAILQ_REMOVE(&r->root_node, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskNODE]);
#endif
					task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
				}
				/* if match at least 2, don't remove resouce of event */
				if (flg > 1)
					evt->flags ^= evt->flags;
				break;
			case EVFILT_PROC:
				flg = 0;
				TAILQ_FOREACH_SAFE(task, &r->root_proc, task_node, tmp) {
					if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
						continue;
					else {
						flg++;
						TASK_DATA(task) = (void*) (uintptr_t) res[i].data;
						TASK_DATLEN(task) = res[i].fflags;
					}
					/* remove proc handle */
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskPROC]);
#endif
					TAILQ_REMOVE(&r->root_proc, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskPROC]);
#endif
					task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
				}
				/* if match at least 2, don't remove resouce of event */
				if (flg > 1)
					evt->flags ^= evt->flags;
				break;
			case EVFILT_SIGNAL:
				flg = 0;
				TAILQ_FOREACH_SAFE(task, &r->root_signal, task_node, tmp) {
					if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
						continue;
					else
						flg++;
					/* remove signal handle */
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskSIGNAL]);
#endif
					TAILQ_REMOVE(&r->root_signal, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskSIGNAL]);
#endif
					task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
				}
				/* if match at least 2, don't remove resouce of event */
				if (flg > 1)
					evt->flags ^= evt->flags;
				break;
#ifdef EVFILT_USER
			case EVFILT_USER:
				flg = 0;
				TAILQ_FOREACH_SAFE(task, &r->root_user, task_node, tmp) {
					if (TASK_VAL(task) != ((uintptr_t) res[i].udata))
						continue;
					else {
						flg++;
						TASK_DATA(task) = (void*) res[i].data;
						TASK_DATLEN(task) = res[i].fflags;
					}
					/* remove user handle */
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskUSER]);
#endif
					TAILQ_REMOVE(&r->root_user, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskUSER]);
#endif
					task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
					TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
					pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
				}
				/* if match at least 2, don't remove resouce of event */
				if (flg > 1)
					evt->flags ^= evt->flags;
				break;
#endif
		}
		if (kevent(r->root_kq, evt, 1, NULL, 0, &now) == -1) {
			if (r->root_hooks.hook_exec.exception) {
				if (r->root_hooks.hook_exec.exception(r, NULL))
					return NULL;
			} else
				LOGERR;
		}
	}

	/* timer update & put in ready queue */
	clock_gettime(CLOCK_MONOTONIC, &now);

	TAILQ_FOREACH_SAFE(task, &r->root_timer, task_node, tmp)
		if (sched_timespeccmp(&now, &TASK_TS(task), -) >= 0) {
#ifdef HAVE_LIBPTHREAD
			pthread_mutex_lock(&r->root_mtx[taskTIMER]);
#endif
			TAILQ_REMOVE(&r->root_timer, task, task_node);
#ifdef HAVE_LIBPTHREAD
			pthread_mutex_unlock(&r->root_mtx[taskTIMER]);
#endif
			task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
			pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
			TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
			pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
		}

	/* put eventlo priority task to ready queue, if there is no ready task or 
	   	reach max missed fetch-rotate */
	if ((task = TAILQ_FIRST(&r->root_eventlo))) {
		if (!TAILQ_FIRST(&r->root_ready) || r->root_eventlo_miss > MAX_EVENTLO_MISS) {
			r->root_eventlo_miss = 0;

#ifdef HAVE_LIBPTHREAD
			pthread_mutex_lock(&r->root_mtx[taskEVENTLO]);
#endif
			TAILQ_REMOVE(&r->root_eventlo, task, task_node);
#ifdef HAVE_LIBPTHREAD
			pthread_mutex_unlock(&r->root_mtx[taskEVENTLO]);
#endif
			task->task_type = taskREADY;
#ifdef HAVE_LIBPTHREAD
			pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
			TAILQ_INSERT_TAIL(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
			pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
		} else
			r->root_eventlo_miss++;
	} else
		r->root_eventlo_miss = 0;

	/* OK, lets get ready task !!! */
	task = TAILQ_FIRST(&r->root_ready);
	if (!(task))
		return NULL;

#ifdef HAVE_LIBPTHREAD
	pthread_mutex_lock(&r->root_mtx[taskREADY]);
#endif
	TAILQ_REMOVE(&r->root_ready, task, task_node);
#ifdef HAVE_LIBPTHREAD
	pthread_mutex_unlock(&r->root_mtx[taskREADY]);
#endif
	task->task_type = taskUNUSE;
#ifdef HAVE_LIBPTHREAD
	pthread_mutex_lock(&r->root_mtx[taskUNUSE]);
#endif
	TAILQ_INSERT_TAIL(&r->root_unuse, task, task_node);
#ifdef HAVE_LIBPTHREAD
	pthread_mutex_unlock(&r->root_mtx[taskUNUSE]);
#endif
	return task;
}

/*
 * sched_hook_exception() - Default EXCEPTION hook
 *
 * @root = root task
 * @arg = custom handling: if arg == EV_EOF or other value; default: arg == NULL log errno
 * return: <0 errors and 0 ok
 */
void *
sched_hook_exception(void *root, void *arg)
{
	sched_root_task_t *r = root;

	if (!r)
		return NULL;

	/* custom exception handling ... */
	if (arg) {
		if (arg == (void*) EV_EOF)
			return NULL;
		return (void*) -1;	/* raise scheduler error!!! */
	}

	/* if error hook exists */
	if (r->root_hooks.hook_root.error)
		return (r->root_hooks.hook_root.error(root, (void*) ((intptr_t) errno)));

	/* default case! */
	LOGERR;
	return NULL;
}

/*
 * sched_hook_condition() - Default CONDITION hook
 *
 * @root = root task
 * @arg = killState from schedRun()
 * return: NULL kill scheduler loop or !=NULL ok
 */
void *
sched_hook_condition(void *root, void *arg)
{
	sched_root_task_t *r = root;

	if (!r)
		return NULL;

	return (void*) (r->root_cond - *(intptr_t*) arg);
}

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>