--- libaitsched/src/hooks.c 2014/01/27 17:08:02 1.24.4.2 +++ libaitsched/src/hooks.c 2014/01/28 12:57:52 1.24.4.7 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: hooks.c,v 1.24.4.2 2014/01/27 17:08:02 misho Exp $ +* $Id: hooks.c,v 1.24.4.7 2014/01/28 12:57:52 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 +Copyright 2004 - 2014 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -562,7 +562,24 @@ sched_hook_signal(void *task, void *arg __unused) LOGERR; return (void*) -1; } +#else +#if 0 + sched_task_t *t = task; + struct sigaction sa; + memset(&sa, 0, sizeof sa); + sigemptyset(&sa.sa_mask); + sa.sa_handler = _sched_sigHandler; + sa.sa_flags = SA_RESETHAND | SA_RESTART; + + if (sigaction(TASK_VAL(t), &sa, NULL) == -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; + } +#endif /* 0 */ #endif return NULL; } @@ -624,6 +641,7 @@ sched_hook_fetch(void *root, void *arg __unused) struct timespec *timeout, m, mtmp; #else struct timeval *timeout, tv; + fd_set rfd, wfd, xfd; #endif register int i, flg; int en; @@ -736,8 +754,9 @@ sched_hook_fetch(void *root, void *arg __unused) #ifndef KQ_DISABLE if ((en = kevent(r->root_kq, NULL, 0, res, KQ_EVENTS, timeout)) == -1) { #else - if ((en = select(r->root_kq, &r->root_fds[0], &r->root_fds[1], - &r->root_fds[0], timeout)) == -1) { + rfd = xfd = r->root_fds[0]; + wfd = r->root_fds[1]; + if ((en = select(r->root_kq, &rfd, &wfd, &xfd, timeout)) == -1) { #endif /* KQ_DISABLE */ if (r->root_hooks.hook_exec.exception) { if (r->root_hooks.hook_exec.exception(r, NULL)) @@ -750,8 +769,8 @@ sched_hook_fetch(void *root, void *arg __unused) /* kevent dispatcher */ now.tv_sec = now.tv_nsec = 0; /* Go and catch the cat into pipes ... */ - for (i = 0; i < en; i++) { #ifndef KQ_DISABLE + for (i = 0; i < en; i++) { memcpy(evt, &res[i], sizeof evt); evt->flags = EV_DELETE; /* Put read/write task to ready queue */ @@ -1120,9 +1139,127 @@ sched_hook_fetch(void *root, void *arg __unused) } else LOGERR; } + } #else /* end of kevent dispatcher */ -#endif /* KQ_DISABLE */ + for (i = 0; i < r->root_kq; i++) { + if (FD_ISSET(i, &r->root_fds[0])) { + flg = 0; + TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) { + if (TASK_FD(task) != i) + continue; + else { + printf("Suck the milk %d !!!\n", i); + flg++; + TASK_RET(task) ^= TASK_RET(task); + TASK_FLAG(task) ^= TASK_FLAG(task); + } + /* 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) { + if (r->root_hooks.hook_exec.exception(r, NULL)) { + 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 equal to 1, remove resouce */ + if (flg == 1) + FD_CLR(i, &r->root_fds[0]); + } + + if (FD_ISSET(i, &r->root_fds[1])) { + flg = 0; + TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) { + if (TASK_FD(task) != i) + continue; + else { + printf("HIT the pig %d !!!\n", i); + flg++; + TASK_RET(task) ^= TASK_RET(task); + TASK_FLAG(task) ^= TASK_FLAG(task); + } + /* 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) { + if (r->root_hooks.hook_exec.exception(r, NULL)) { + 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 equal to 1, remove resouce */ + if (flg == 1) + FD_CLR(i, &r->root_fds[1]); + } } + + /* optimize select */ + for (i = r->root_kq - 1; i > 2; i--) + if (FD_ISSET(i, &r->root_fds[0]) || FD_ISSET(i, &r->root_fds[1])) + break; + if (i > 2) + r->root_kq = i + 1; +#endif /* KQ_DISABLE */ skip_event: /* timer update & put in ready queue */