--- libaitsched/src/hooks.c 2012/08/02 13:56:19 1.11 +++ libaitsched/src/hooks.c 2012/08/21 12:54:39 1.14 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: hooks.c,v 1.11 2012/08/02 13:56:19 misho Exp $ +* $Id: hooks.c,v 1.14 2012/08/21 12:54:39 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -207,6 +207,11 @@ sched_hook_cancel(void *task, void *arg __unused) #endif break; #endif + case taskTHREAD: +#ifdef HAVE_LIBPTHREAD + pthread_cancel((pthread_t) TASK_VAL(t)); +#endif + TASK_UNLOCK(t); default: return NULL; } @@ -215,7 +220,38 @@ sched_hook_cancel(void *task, void *arg __unused) return NULL; } +#ifdef HAVE_LIBPTHREAD /* + * sched_hook_thread() - Default THREAD hook + * + * @task = current task + * @arg = pthread attributes + * return: <0 errors and 0 ok + */ +void * +sched_hook_thread(void *task, void *arg) +{ + sched_task_t *t = task; + pthread_t tid; + + if (!t || !TASK_ROOT(t)) + return (void*) -1; + + if (pthread_create(&tid, (pthread_attr_t*) arg, + (void *(*)(void*)) TASK_FUNC(t), t)) { + LOGERR; + return (void*) -1; + } + + if (!TASK_ISLOCKED(t)) + TASK_LOCK(t); + + TASK_VAL(t) = (u_long) tid; + return NULL; +} +#endif + +/* * sched_hook_read() - Default READ hook * * @task = current task @@ -550,7 +586,7 @@ sched_hook_fetch(void *root, void *arg __unused) sched_timespecinf(&r->root_wait); } #else - if (!TAILQ_FIRST(&r->root_eventlo) && (task = TAILQ_FIRST(&r->root_timer))) { + if (!TAILQ_FIRST(&r->root_task) && (task = TAILQ_FIRST(&r->root_timer))) { clock_gettime(CLOCK_MONOTONIC, &now); m = TASK_TS(task); @@ -561,8 +597,8 @@ sched_hook_fetch(void *root, void *arg __unused) sched_timespecinf(&r->root_wait); } #endif - /* if present member of eventLo, set NOWAIT */ - if (TAILQ_FIRST(&r->root_eventlo)) + /* if present member of task, set NOWAIT */ + if (TAILQ_FIRST(&r->root_task)) sched_timespecclear(&r->root_wait); if (r->root_wait.tv_sec != -1 && r->root_wait.tv_nsec != -1) @@ -592,8 +628,11 @@ sched_hook_fetch(void *root, void *arg __unused) TAILQ_FOREACH_SAFE(task, &r->root_read, task_node, tmp) { if (TASK_FD(task) != ((intptr_t) res[i].udata)) continue; - else + else { flg++; + TASK_RET(task) = res[i].data; + TASK_FLAG(task) = res[i].fflags; + } /* remove read handle */ #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&r->root_mtx[taskREAD]); @@ -642,8 +681,11 @@ sched_hook_fetch(void *root, void *arg __unused) TAILQ_FOREACH_SAFE(task, &r->root_write, task_node, tmp) { if (TASK_FD(task) != ((intptr_t) res[i].udata)) continue; - else + else { flg++; + TASK_RET(task) = res[i].data; + TASK_FLAG(task) = res[i].fflags; + } /* remove write handle */ #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&r->root_mtx[taskWRITE]); @@ -692,8 +734,11 @@ sched_hook_fetch(void *root, void *arg __unused) TAILQ_FOREACH_SAFE(task, &r->root_alarm, task_node, tmp) { if ((uintptr_t) TASK_DATA(task) != ((uintptr_t) res[i].udata)) continue; - else + else { flg++; + TASK_RET(task) = res[i].data; + TASK_FLAG(task) = res[i].fflags; + } /* remove alarm handle */ #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&r->root_mtx[taskALARM]); @@ -722,8 +767,8 @@ sched_hook_fetch(void *root, void *arg __unused) continue; else { flg++; - TASK_DATA(task) = (void*) (uintptr_t) res[i].data; - TASK_DATLEN(task) = res[i].fflags; + TASK_RET(task) = res[i].data; + TASK_FLAG(task) = res[i].fflags; } /* remove node handle */ #ifdef HAVE_LIBPTHREAD @@ -753,8 +798,8 @@ sched_hook_fetch(void *root, void *arg __unused) continue; else { flg++; - TASK_DATA(task) = (void*) (uintptr_t) res[i].data; - TASK_DATLEN(task) = res[i].fflags; + TASK_RET(task) = res[i].data; + TASK_FLAG(task) = res[i].fflags; } /* remove proc handle */ #ifdef HAVE_LIBPTHREAD @@ -782,8 +827,11 @@ sched_hook_fetch(void *root, void *arg __unused) TAILQ_FOREACH_SAFE(task, &r->root_signal, task_node, tmp) { if (TASK_VAL(task) != ((uintptr_t) res[i].udata)) continue; - else + else { flg++; + TASK_RET(task) = res[i].data; + TASK_FLAG(task) = res[i].fflags; + } /* remove signal handle */ #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&r->root_mtx[taskSIGNAL]); @@ -812,8 +860,11 @@ sched_hook_fetch(void *root, void *arg __unused) acb = (struct aiocb*) TASK_VAL(task); if (acb != ((struct aiocb*) res[i].udata)) continue; - else + else { flg++; + TASK_RET(task) = res[i].data; + TASK_FLAG(task) = res[i].fflags; + } /* remove user handle */ #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&r->root_mtx[taskAIO]); @@ -851,8 +902,11 @@ sched_hook_fetch(void *root, void *arg __unused) acbs = (struct aiocb**) TASK_VAL(task); if (acbs != ((struct aiocb**) res[i].udata)) continue; - else + else { flg++; + TASK_RET(task) = res[i].data; + TASK_FLAG(task) = res[i].fflags; + } /* remove user handle */ #ifdef HAVE_LIBPTHREAD pthread_mutex_lock(&r->root_mtx[taskLIO]); @@ -900,8 +954,8 @@ sched_hook_fetch(void *root, void *arg __unused) continue; else { flg++; - TASK_DATA(task) = (void*) res[i].data; - TASK_DATLEN(task) = res[i].fflags; + TASK_RET(task) = res[i].data; + TASK_FLAG(task) = res[i].fflags; } /* remove user handle */ #ifdef HAVE_LIBPTHREAD @@ -957,18 +1011,18 @@ sched_hook_fetch(void *root, void *arg __unused) #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; + /* put regular task priority task to ready queue, + if there is no ready task or reach max missing hit for regular task */ + if ((task = TAILQ_FIRST(&r->root_task))) { + if (!TAILQ_FIRST(&r->root_ready) || r->root_miss >= TASK_VAL(task)) { + r->root_miss ^= r->root_miss; #ifdef HAVE_LIBPTHREAD - pthread_mutex_lock(&r->root_mtx[taskEVENTLO]); + pthread_mutex_lock(&r->root_mtx[taskTASK]); #endif - TAILQ_REMOVE(&r->root_eventlo, task, task_node); + TAILQ_REMOVE(&r->root_task, task, task_node); #ifdef HAVE_LIBPTHREAD - pthread_mutex_unlock(&r->root_mtx[taskEVENTLO]); + pthread_mutex_unlock(&r->root_mtx[taskTASK]); #endif task->task_type = taskREADY; #ifdef HAVE_LIBPTHREAD @@ -979,9 +1033,9 @@ sched_hook_fetch(void *root, void *arg __unused) pthread_mutex_unlock(&r->root_mtx[taskREADY]); #endif } else - r->root_eventlo_miss++; + r->root_miss++; } else - r->root_eventlo_miss = 0; + r->root_miss ^= r->root_miss; /* OK, lets get ready task !!! */ task = TAILQ_FIRST(&r->root_ready);