rt-thread的IPC机制之邮箱源码分析(二)

2014-11-24 07:52:27 · 作者: · 浏览: 1
T_NULL)
{
/* delete mailbox object */
rt_object_delete(&(mb->parent.parent));
return RT_NULL;
}
mb->entry = 0;//默认邮箱内的消息条数为0
mb->in_offset = 0;//入口偏移位置为0
mb->out_offset = 0;//出口偏移位置为0
/* init an additional list of sender suspend thread */
rt_list_init(&(mb->suspend_sender_thread));//初始化邮箱的发送线程挂起链表
return mb;
}
2.3 脱离邮箱
[cpp]
/**
* This function will detach a mailbox from resource management
*
* @param mb the mailbox object
*
* @return the operation status, RT_EOK on successful
*/
rt_err_t rt_mb_detach(rt_mailbox_t mb)
{
/* parameter check */
RT_ASSERT(mb != RT_NULL);
/* resume all suspended thread */
rt_ipc_list_resume_all(&(mb->parent.suspend_thread));//唤醒所有挂起的接收线程
/* also resume all mailbox private suspended thread */
rt_ipc_list_resume_all(&(mb->suspend_sender_thread));//唤醒所有挂起的发送线程
/* detach mailbox object */
rt_object_detach(&(mb->parent.parent));//脱离邮箱对应的内核对象
return RT_EOK;
}
2.4 删除邮箱
[cpp]
/**
* This function will delete a mailbox object and release the memory
*
* @param mb the mailbox object
*
* @return the error code
*/
rt_err_t rt_mb_delete(rt_mailbox_t mb)
{
RT_DEBUG_NOT_IN_INTERRUPT;
/* parameter check */
RT_ASSERT(mb != RT_NULL);
/* resume all suspended thread */
rt_ipc_list_resume_all(&(mb->parent.suspend_thread));//唤醒所有挂起的接收线程
/* also resume all mailbox private suspended thread */
rt_ipc_list_resume_all(&(mb->suspend_sender_thread));//唤醒所有挂起的发送线程
#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
/* the mb object belongs to an application module */
if (mb->parent.parent.flag & RT_OBJECT_FLAG_MODULE)
rt_module_free(mb->parent.parent.module_id, mb->msg_pool);//如果有模块,则需要卸载模块
else
#endif
/* free mailbox pool */
rt_free(mb->msg_pool);//删除接收缓冲
/* delete mailbox object */
rt_object_delete(&(mb->parent.parent));//删除邮箱对应的内核对象
return RT_EOK;
}
2.5 接收邮件
[cpp]
/**
* This function will receive a mail from mailbox object, if there is no mail
* in mailbox object, the thread shall wait for a specified time.
*
* @param mb the mailbox object
* @param value the received mail will be saved in
* @param timeout the waiting time
*
* @return the error code
*/
rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout)
{
struct rt_thread *thread;
register rt_ubase_t temp;
rt_uint32_t tick_delta;
/* parameter check */
RT_ASSERT(mb != RT_NULL);
/* initialize delta tick */
tick_delta = 0;
/* get current thread */
thread = rt_thread_self();//获取当前线程
RT_OBJECT_HOOK_CALL(rt_object_trytake_hook, (&(mb->parent.parent)));
/* disable interrupt */
temp = rt_hw_interrupt_disable();//关中断
/* for non-blocking call */
if (mb->entry == 0 && timeout == 0)//如果当前邮箱中无信件且当前线程等待时间为0,则立即返回超时错误
{
rt_hw_interrupt_enable(temp);//开中断
return -RT_ETIMEOUT;
}
/*