--- libaitsched/src/tasks.c 2012/08/21 11:07:16 1.12.4.1 +++ libaitsched/src/tasks.c 2012/08/22 23:43:36 1.13.2.3 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: tasks.c,v 1.12.4.1 2012/08/21 11:07:16 misho Exp $ +* $Id: tasks.c,v 1.13.2.3 2012/08/22 23:43:36 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -46,10 +46,14 @@ SUCH DAMAGE. #include "global.h" -#pragma GCC visibility push(hidden) - +/* + * sched_useTask() - Get and init new task + * + * @root = root task + * return: NULL error or !=NULL prepared task + */ inline sched_task_t * -_sched_useTask(sched_root_task_t * __restrict root) +sched_useTask(sched_root_task_t * __restrict root) { sched_task_t *task, *tmp; @@ -79,8 +83,14 @@ _sched_useTask(sched_root_task_t * __restrict root) return task; } +/* + * sched_unuseTask() - Unlock and put task to unuse queue + * + * @task = task + * return: always is NULL + */ inline sched_task_t * -_sched_unuseTask(sched_task_t * __restrict task) +sched_unuseTask(sched_task_t * __restrict task) { TASK_UNLOCK(task); TASK_TYPE(task) = taskUNUSE; @@ -96,9 +106,63 @@ _sched_unuseTask(sched_task_t * __restrict task) return task; } +#pragma GCC visibility push(hidden) + +void * +_sched_threadJoin(sched_task_t *task) +{ + void *ret = NULL; + + if (!task) + return NULL; + +#ifdef HAVE_LIBPTHREAD + if (pthread_kill((pthread_t) TASK_VAL(task), 0)) { + pthread_join((pthread_t) TASK_VAL(task), &ret); + TASK_ROOT(task)->root_ret = ret; + } else { + usleep(10000); + schedTaskSelf(task); + } +#endif + + return NULL; +} + #pragma GCC visibility pop +/* + * sched_taskExit() - Exit routine for scheduler task, explicit required for thread tasks + * + * @task = current task + * @retcode = return code + * return: return code + */ +inline void * +sched_taskExit(sched_task_t *task, intptr_t retcode) +{ + if (!task || !TASK_ROOT(task)) + return (void*) -1; + if (TASK_ROOT(task)->root_hooks.hook_exec.exit) + TASK_ROOT(task)->root_hooks.hook_exec.exit(task, (void*) retcode); + + TASK_ROOT(task)->root_ret = (void*) retcode; + +#ifdef HAVE_LIBPTHREAD + if (TASK_TYPE(task) == taskTHREAD) { + if (TASK_FLAG(task) == PTHREAD_CREATE_JOINABLE) /* joinable thread */ + schedTask(TASK_ROOT(task), _sched_threadJoin, TASK_ARG(task), + TASK_VAL(task), TASK_DATA(task), TASK_DATLEN(task)); + sched_unuseTask(task); + pthread_exit((void*) retcode); + } +#endif + + return (void*) retcode; +} + + /* * schedRead() - Add READ I/O task to scheduler queue * @@ -121,7 +185,7 @@ schedRead(sched_root_task_t * __restrict root, sched_t return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -148,7 +212,7 @@ schedRead(sched_root_task_t * __restrict root, sched_t pthread_mutex_unlock(&root->root_mtx[taskREAD]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -175,7 +239,7 @@ schedWrite(sched_root_task_t * __restrict root, sched_ return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -202,7 +266,7 @@ schedWrite(sched_root_task_t * __restrict root, sched_ pthread_mutex_unlock(&root->root_mtx[taskWRITE]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -229,7 +293,7 @@ schedNode(sched_root_task_t * __restrict root, sched_t return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -256,7 +320,7 @@ schedNode(sched_root_task_t * __restrict root, sched_t pthread_mutex_unlock(&root->root_mtx[taskNODE]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -283,7 +347,7 @@ schedProc(sched_root_task_t * __restrict root, sched_t return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -310,7 +374,7 @@ schedProc(sched_root_task_t * __restrict root, sched_t pthread_mutex_unlock(&root->root_mtx[taskPROC]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -341,7 +405,7 @@ schedUser(sched_root_task_t * __restrict root, sched_t return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -368,7 +432,7 @@ schedUser(sched_root_task_t * __restrict root, sched_t pthread_mutex_unlock(&root->root_mtx[taskUSER]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; #endif @@ -396,7 +460,7 @@ schedSignal(sched_root_task_t * __restrict root, sched return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -423,7 +487,7 @@ schedSignal(sched_root_task_t * __restrict root, sched pthread_mutex_unlock(&root->root_mtx[taskSIGNAL]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -450,7 +514,7 @@ schedAlarm(sched_root_task_t * __restrict root, sched_ return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -477,7 +541,7 @@ schedAlarm(sched_root_task_t * __restrict root, sched_ pthread_mutex_unlock(&root->root_mtx[taskALARM]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -505,7 +569,7 @@ schedAIO(sched_root_task_t * __restrict root, sched_ta return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -532,7 +596,7 @@ schedAIO(sched_root_task_t * __restrict root, sched_ta pthread_mutex_unlock(&root->root_mtx[taskAIO]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -668,7 +732,7 @@ schedLIO(sched_root_task_t * __restrict root, sched_ta return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -695,7 +759,7 @@ schedLIO(sched_root_task_t * __restrict root, sched_ta pthread_mutex_unlock(&root->root_mtx[taskLIO]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -869,7 +933,7 @@ schedTimer(sched_root_task_t * __restrict root, sched_ return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -918,7 +982,7 @@ schedTimer(sched_root_task_t * __restrict root, sched_ pthread_mutex_unlock(&root->root_mtx[taskTIMER]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -945,7 +1009,7 @@ schedEvent(sched_root_task_t * __restrict root, sched_ return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -972,7 +1036,7 @@ schedEvent(sched_root_task_t * __restrict root, sched_ pthread_mutex_unlock(&root->root_mtx[taskEVENT]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -1000,7 +1064,7 @@ schedTask(sched_root_task_t * __restrict root, sched_t return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -1033,7 +1097,7 @@ schedTask(sched_root_task_t * __restrict root, sched_t pthread_mutex_unlock(&root->root_mtx[taskTASK]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -1060,7 +1124,7 @@ schedSuspend(sched_root_task_t * __restrict root, sche return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -1087,7 +1151,7 @@ schedSuspend(sched_root_task_t * __restrict root, sche pthread_mutex_unlock(&root->root_mtx[taskSUSPEND]); #endif } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; } @@ -1114,7 +1178,7 @@ schedCallOnce(sched_root_task_t * __restrict root, sch return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -1129,7 +1193,7 @@ schedCallOnce(sched_root_task_t * __restrict root, sch ret = schedCall(task); - _sched_unuseTask(task); + sched_unuseTask(task); return ret; } @@ -1154,14 +1218,13 @@ schedThread(sched_root_task_t * __restrict root, sched #endif sched_task_t *task; void *ptr; - pthread_t tid; pthread_attr_t attr; if (!root || !func) return NULL; /* get new task */ - if (!(task = _sched_useTask(root))) + if (!(task = sched_useTask(root))) return NULL; task->task_func = func; @@ -1169,35 +1232,25 @@ schedThread(sched_root_task_t * __restrict root, sched TASK_ROOT(task) = root; TASK_ARG(task) = arg; + TASK_FLAG(task) = detach ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE; TASK_DATA(task) = opt_data; TASK_DATLEN(task) = opt_dlen; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, TASK_FLAG(task)); if (root->root_hooks.hook_add.thread) - ptr = root->root_hooks.hook_add.thread(task, NULL); + ptr = root->root_hooks.hook_add.thread(task, &attr); else ptr = NULL; + pthread_attr_destroy(&attr); if (!ptr) { pthread_mutex_lock(&root->root_mtx[taskTHREAD]); TAILQ_INSERT_TAIL(&root->root_thread, TASK_ID(task), task_node); pthread_mutex_unlock(&root->root_mtx[taskTHREAD]); - - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, detach ? - PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE); - if (pthread_create(&tid, &attr, - (void *(*)(void*)) task->task_func, task)) { - LOGERR; - pthread_mutex_lock(&root->root_mtx[taskTHREAD]); - TAILQ_REMOVE(&root->root_thread, TASK_ID(task), task_node); - pthread_mutex_unlock(&root->root_mtx[taskTHREAD]); - task = _sched_unuseTask(task); - } else - TASK_VAL(task) = (u_long) tid; - pthread_attr_destroy(&attr); } else - task = _sched_unuseTask(task); + task = sched_unuseTask(task); return task; }