--- libaitsched/inc/aitsched.h 2012/08/08 23:04:41 1.14 +++ libaitsched/inc/aitsched.h 2012/08/21 11:45:34 1.14.2.2 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: aitsched.h,v 1.14 2012/08/08 23:04:41 misho Exp $ +* $Id: aitsched.h,v 1.14.2.2 2012/08/21 11:45:34 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -53,6 +53,7 @@ SUCH DAMAGE. #include #include #include +#include #ifdef EVFILT_LIO #include #endif @@ -89,6 +90,7 @@ typedef enum { taskSUSPEND, taskREADY, taskUNUSE, + taskTHREAD, taskMAX } sched_task_type_t; @@ -122,8 +124,12 @@ struct sched_HooksTask { sched_hook_func_t task; /* suspend(sched_task_t *task, NULL) -> int */ sched_hook_func_t suspend; + /* thread(sched_task_t *task, NULL) -> int */ + sched_hook_func_t thread; } hook_add; struct { + /* exit(sched_task_t *task, void *exitValue) -> int */ + sched_hook_func_t exit; /* cancel(sched_task_t *task, NULL) -> int */ sched_hook_func_t cancel; /* resume(sched_task_t *task, NULL) -> int */ @@ -158,16 +164,20 @@ typedef void *(*sched_task_func_t)(sched_task_t * /* c /* task & queue */ struct sched_Task { - volatile int task_lock; uintptr_t task_id; #define TASK_ID(x) ((struct sched_Task*) (x)->task_id) sched_task_type_t task_type; #define TASK_TYPE(x) (x)->task_type + volatile int task_lock; sched_root_task_t *task_root; #define TASK_ROOT(x) (x)->task_root sched_task_func_t task_func; #define TASK_FUNC(x) (x)->task_func + intptr_t task_ret; +#define TASK_RET(x) (x)->task_ret + unsigned int task_flag; +#define TASK_FLAG(x) (x)->task_flag void *task_arg; union { @@ -220,6 +230,7 @@ struct sched_RootTask { sched_queue_t root_suspend; sched_queue_t root_ready; sched_queue_t root_unuse; + sched_queue_t root_thread; hooks_task_t root_hooks; struct iovec root_data; @@ -605,6 +616,39 @@ sched_task_t *schedCallOnce(sched_root_task_t * __rest unsigned long val, void *opt_data, size_t opt_dlen); #define schedCallAgain(x) schedCallOnce(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \ TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x))) + +/* + * schedThread() - Add thread task to scheduler queue + * + * @root = root task + * @func = task execution function + * @arg = 1st func argument + * @detach = Detach thread from scheduler, if !=0 + * @opt_data = Optional data + * @opt_dlen = Optional data length + * return: NULL error or !=NULL new queued task + */ +sched_task_t *schedThread(sched_root_task_t * __restrict root, sched_task_func_t func, void *arg, + int detach, void *opt_data, size_t opt_dlen); +#define schedThreadSelf(x) schedThread(TASK_ROOT((x)), TASK_FUNC((x)), TASK_ARG((x)), \ + TASK_VAL((x)), TASK_DATA((x)), TASK_DATLEN((x))) +/* + * taskExit() - Exit routine for scheduler task + * + * @t = current executed task + * @x = exit value for task + * return: none + */ +#define taskExit(t, x) do { assert((t) && TASK_ROOT(t)); \ + if (TASK_ROOT(t)->root_hooks.hook_exec.exit) \ + TASK_ROOT(t)->root_hooks.hook_exec.exit((t), \ + (void*) (x)); \ + TASK_ROOT(t)->root_ret = (void*) (x); \ + if (TASK_TYPE(t) == taskTHREAD) \ + pthread_exit((void*) (x)); \ + else \ + return ((void*) (x)); \ +} while (0) #endif