rt-thread的IPC机制之信号量源码分析(四)
if (!rt_list_isempty(&sem->parent.suspend_thread))//挂起线程不为空
{
/* resume the suspended thread */
rt_ipc_list_resume(&(sem->parent.suspend_thread));//唤醒第一个挂起的线程
need_schedule = RT_TRUE;//需要重新调度
}
else
sem->value ++; /* increase value *///信号量计数器加1
/* enable interrupt */
rt_hw_interrupt_enable(temp);//开中断
/* resume a thread, re-schedule */
if (need_schedule == RT_TRUE)//如果需要重新调度线程,则重新调度
rt_schedule();
return RT_EOK;
}
其中函数rt_ipc_list_resume只会唤醒信号量中第一个挂起的线程,其
源码如下:
[cpp]
/**
* This function will resume the first thread in the list of a IPC object:
* - remove the thread from suspend queue of IPC object
* - put the thread into system ready queue
*
* @param list the thread list
*
* @return the operation status, RT_EOK on successful
*/
rt_inline rt_err_t rt_ipc_list_resume(rt_list_t *list)
{
struct rt_thread *thread;
/* get thread entry */
thread = rt_list_entry(list->next, struct rt_thread, tlist);//获取线程
RT_DEBUG_LOG(RT_DEBUG_IPC, ("resume thread:%s\n", thread->name));
/* resume it */
rt_thread_resume(thread);//唤醒此线程
return RT_EOK;
}
6 信号量控制
[cpp]
/**
* This function can get or set some extra attributions of a semaphore object.
*
* @param sem the semaphore object
* @param cmd the execution command
* @param arg the execution argument
*
* @return the error code
*/
rt_err_t rt_sem_control(rt_sem_t sem, rt_uint8_t cmd, void *arg)
{
rt_ubase_t level;
RT_ASSERT(sem != RT_NULL);
if (cmd == RT_IPC_CMD_RESET)//重置信号量的计数器值
{
rt_uint32_t value;
/* get value */
value = (rt_uint32_t)arg;
/* disable interrupt */
level = rt_hw_interrupt_disable();//关中断
/* resume all waiting thread */ www.2cto.com
rt_ipc_list_resume_all(&sem->parent.suspend_thread);//唤醒信号量上所有挂起的线程
/* set new value */
sem->value = (rt_uint16_t)value;//设置信号时的计数器值
/* enable interrupt */
rt_hw_interrupt_enable(level);//开中断
rt_schedule();//立即重新调试
return RT_EOK;
}
return -RT_ERROR;
}