这段代码其实开头都还好,关键是末尾要结束的时候有一段代码比较费解。我想,这是我前面说过的优先级反转问题。为了解决这一问题,在rawos版本中采取了优先级继承的方法。我们还是详细看一下逻辑本身是怎么样的,

  (1)判断参数合法性;

  (2)判断资源是否可取,如果可取,则在记录当前线程和优先级后返回;

  (3)如果资源被自己重复申请,返回;

  (4)如果线程不愿等待,返回;

  (5)如果此时禁止调度,返回;

  (6)如果此时优先级大于互斥量占有者的优先级,分情况处理

    a)占有者处于ready的状态,那么修改它的优先级,重新加入调度队列;

    b)占有者处于sleep的状态,直接修改优先级即可;

    c)占有者也被pend到别的资源上面了,那么修改那个资源的pend列表,可能设计到调度顺序问题。

  (7)线程把自己pend到互斥量等待队列上面;

  (8)线程调用系统调度函数,切换到其他线程运行;

  (9)线程再次得到运行的机会,从task获取结果后返回。

  基本上上面的介绍算得上是很详细了,那么互斥量的释放基本上是一个逆操作的过程,朋友也可以思考一下应该怎么解决才好,

RAW_U16 raw_mutex_put(RAW_MUTEX *mutex_ptr)
  {
 
   LIST *block_list_head;
  
   RAW_SR_ALLOC();
 
   #if (RAW_MUTEX_FUNCTION_CHECK > 0)
 
   if (mutex_ptr == 0) {
    return RAW_NULL_OBJECT;
   }
  
   #endif
 
   block_list_head = &mutex_ptr->common_block_obj.block_list;
  
   RAW_CRITICAL_ENTER();
 
   /*Must release the mutex by self*/
   if (raw_task_active != mutex_ptr->occupy) {         
    RAW_CRITICAL_EXIT();
    return RAW_MUTEX_NOT_RELEASE_BY_OCCYPY;
   }
 
   /*if no block task on this list just return*/
   if (is_list_empty(block_list_head)) {      
    mutex_ptr->count   = 1;                                  
    RAW_CRITICAL_EXIT();
    return RAW_SUCCESS;
   }
 
     /*if priority was changed, just change it back to original priority*/
  
   if (raw_task_active->priority != mutex_ptr->occupy_original_priority) {
   
    remove_ready_list(&raw_ready_queue, raw_task_active);
    raw_task_active->priority = mutex_ptr->occupy_original_priority; 
    add_ready_list_end(&raw_ready_queue, raw_task_active);
 
   }
 
   /* there must have task blocked on this mutex object*/                            
   mutex_ptr->occupy       =   list_entry(block_list_head->next, RAW_TASK_OBJ, task_list);
   /*the first blocked task became the occupy task*/
   mutex_ptr->occupy_original_priority = mutex_ptr->occupy->priority;
   /*mutex resource is occupied*/
   mutex_ptr->count   = 0;
 
   /*Wake up the occupy task, which is the highst priority task on the list*/                         
   raw_wake_object(mutex_ptr->occupy);
  
   RAW_CRITICAL_EXIT();
 
 
   raw_sched();                                    
 
   return RAW_SUCCESS;
  
  }