vxWorks系统任务tJobTask的实现及使用

来源:互联网 发布:js 什么时候使用链表 编辑:程序博客网 时间:2024/06/06 01:44
前面说了tIsr以及tExcTask函数,这两个任务都是关于中断的,这里说的tJobTask是正常任务,跟中断没有关系。实现的就是简单的把一个函数延后执行。
/*记录job的结构体*/typedef struct    {    FUNCPTR func;                /* job function */    int     arg [JOB_MAX_ARGS];  /* args for function */    int     clientTid;           /* client task Id */    int     clientPrio;          /* client task Priority */    int     * pResult;           /* pointer to job result, supplied by client */#ifdef _WRS_CONFIG_SMP    cpuset_t affinity;#endif    } TASK_LEVEL_JOB;

/*job队列*/LOCAL TASK_LEVEL_JOB workerJob;
初始化过程:usrRoot--->usrIosExtraInit--->jobLibInit


/*初始化*/STATUS jobLibInit    (    int jobTaskStackSize    /* job task stack size */    )    {/*初始化clientMutex信号量*/    if (semMInit (&clientMutex, (SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE | SEM_INTERRUPTIBLE)) != OK)return (ERROR); /*初始化jobMutex信号量*/    if (semMInit (&jobMutex, (SEM_Q_PRIORITY | SEM_INVERSION_SAFE |       SEM_INTERRUPTIBLE)) != OK)return (ERROR);/*初始化jobSyncSem信号量,用于同步任务*/    if (semBInit (&jobSyncSem, (SEM_Q_PRIORITY | SEM_INTERRUPTIBLE),  SEM_EMPTY) != OK)return (ERROR);     /*创建系统函数tJobTask*/    if ((jobTaskId = taskSpawn ("tJobTask",JOB_TASK_PRIORITY, JOB_TASK_OPTIONS,jobTaskStackSize, (FUNCPTR) jobTask,0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR)return (ERROR);/*设定增加任务的函数的指针*/    _func_jobTaskWorkAdd = (FUNCPTR) jobTaskWorkAdd;    jobLibTaskInstalled = TRUE;    return (OK);    }

/*增加任务到任务队列中*/LOCAL STATUS jobTaskWorkAdd    (    FUNCPTR func,         /* job function */    int     arg1,         /* job argument */    int     arg2,         /* job argument */    int     arg3,         /* job argument */    int     arg4,         /* job argument */    int     arg5,         /* job argument */    int     arg6,         /* job argument */    int     * pResult     /* pointer to job result, supplied by client */    )    {    char text [100];/*如果在中断上下文中,就出错*/    if (INT_CONTEXT ())        {errnoSet (S_intLib_NOT_ISR_CALLABLE);return (ERROR);        }/*没有初始化,也出错返回*/    if (!jobLibTaskInstalled)        {        errnoSet (EINVAL);        return (ERROR);        }/*获取信号clientMutex,也就是控制了一次只能添加一个任务进来*/    if (semTake (&clientMutex, WAIT_FOREVER) == ERROR)        {        return (ERROR);        }/*这里的jobMutex信号*/    if (semTake (&jobMutex, WAIT_FOREVER) == ERROR)        {        semGive (&clientMutex);        return (ERROR);        }/*存储任务*/    workerJob.func      = func;    workerJob.arg[0]    = arg1;    workerJob.arg[1]    = arg2;    workerJob.arg[2]    = arg3;    workerJob.arg[3]    = arg4;    workerJob.arg[4]    = arg5;    workerJob.arg[5]    = arg6;    workerJob.pResult   = pResult;    workerJob.clientTid = (int) taskIdCurrent;/*设置当前任务的的优先级*/    taskPriorityGet ((int) taskIdCurrent, &workerJob.clientPrio);/*设置jobTask的优先级*/    taskPrioritySet ((int) jobTaskId, workerJob.clientPrio);#ifdef _WRS_CONFIG_SMP/*设置任务对应的affinity*/    if (taskCpuAffinityGet (0, &workerJob.affinity) == OK){if (!CPUSET_ISZERO(workerJob.affinity))    {    if (taskCpuAffinityGet ((int) jobTaskId, &jobTaskAffinity) == OK){taskCpuAffinitySet ((int) jobTaskId, workerJob.affinity);}    else{CPUSET_ZERO (workerJob.affinity);}    }}#endif/*防止任务被删除*/    TASK_SAFE ();    semGive (&jobMutex);     if (semMTakeByProxy (&jobMutex, jobTaskId) != OK){           text[0] = '\0';            EDR_KERNEL_FATAL_INJECT (NULL, NULL, NULL, NULL, text);}    semGive (&clientMutex); /* Allow subsequent jobAdd() calls to progress *//*告诉tJobTask有任务来了*/    semGive (&jobSyncSem);      TASK_UNSAFE ();    return (OK);    }

/*系统任务函数*/LOCAL void jobTask (void)    {    int result;/*防止任务被删除*/    TASK_SAFE();    FOREVER{semTake (&jobSyncSem, WAIT_FOREVER);/*执行任务*/result = (*workerJob.func) (workerJob.arg[0], workerJob.arg[1],    workerJob.arg[2], workerJob.arg[3],    workerJob.arg[4], workerJob.arg[5]);errno = 0;taskPrioritySet ((int) taskIdCurrent, JOB_TASK_PRIORITY); #ifdef _WRS_CONFIG_SMPif (!CPUSET_ISZERO(workerJob.affinity))    {    taskCpuAffinitySet ((int) jobTaskId, jobTaskAffinity);    }#endif    }/*允许处理下一个*/semGive (&jobMutex);}    }


原创粉丝点击