|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2014-6-24 21:29:02
|
显示全部楼层
本帖最后由 myxiaonia 于 2014-6-24 21:30 编辑
这是最后修改的rt_mut_release函数,又修改了当释放互斥量的等待任务调度回来和当前任务运行的情况,原来的思路还是不清啊,只需要当前任务和此调度任务进行互相比较优先级即可,大家看看是不是简洁不少啊
- OS_RESULT rt_mut_release (OS_ID mutex) {
- /* Release a mutex object */
- P_MUCB p_MCB = mutex;
- P_TCB p_TCB;
- P_TCB prun = os_tsk.run;
- if (p_MCB->level == 0 || p_MCB->owner != prun) {
- /* Unbalanced mutex release or task is not the owner */
- return (OS_R_NOK);
- }
- if (--p_MCB->level != 0) {
- return (OS_R_OK);
- }
- /* Remove mutex from task mutex owner list. */
- {
- P_MUCB *p = &prun->p_mlnk;
- do
- {
- if (*p == p_MCB) {
- *p = (*p)->p_mlnk;break;
- }
- else
- {
- p = &(*p)->p_mlnk;
- }
- }while(*p);
- }
- if (p_MCB->p_lnk != NULL) {
- /* Restore owner task's priority. */
- if(p_MCB->p_lnk->prio >= prun->prio_base)
- {
- U8 prio = prun->prio_base;
- P_MUCB p_mlnk = prun->p_mlnk;
- while (p_mlnk != NULL) {
- if (p_mlnk->p_lnk != NULL && (p_mlnk->p_lnk->prio > prio)) {
- /* A task with higher priority is waiting for mutex. */
- prio = p_mlnk->p_lnk->prio;
- }
- p_mlnk = p_mlnk->p_mlnk;
- }
- prun->prio = prio;
- }
- /* A task is waiting for mutex. */
- p_TCB = rt_get_first ((P_XCB)p_MCB);
- rt_rmv_dly (p_TCB);
- /* A waiting task becomes the owner of this mutex. */
- p_MCB->level = 1;
- p_MCB->owner = p_TCB;
- p_MCB->p_mlnk = p_TCB->p_mlnk;
- p_TCB->p_mlnk = p_MCB;
- p_TCB->ret_val = OS_R_MUT;
- prun->ret_val = OS_R_OK;
- /* Priority inversion, check which task continues. */
- rt_dispatch (p_TCB);
- return (OS_R_OK);
- }
复制代码 |
|