completion 直接翻译过来是完成,所以我更愿意称 rt_completion 为 完成量。在 RT-Thread 的文档中心 中讲线程间通讯(IPC)时,只介绍了,信号量, 互斥量, 事件集,其实 rt_completion 可以认为是轻量级的二值信号量。
2. completion 怎么使用completion 的使用非常简单
定义一个完成量
struct rt_completion completion;初始化完成量
rt_completion_init(&completion);等待完成量
rt_completion_wait(&completion);释放完成量
rt_completion_done(&completion);completion 的 API 非常少,可以通过简单的代码去分析
初始化完成量
void rt_completion_init(struct rt_completion *completion) { rt_base_t level; RT_ASSERT(completion != RT_NULL); level = rt_hw_interrupt_disable(); completion->flag = RT_UNCOMPLETED; rt_list_init(&completion->suspended_list); rt_hw_interrupt_enable(level); }干了两件事:
等待完成量(以下代码有删减)
rt_err_t rt_completion_wait(struct rt_completion *completion, rt_int32_t timeout) { result = RT_EOK; thread = rt_thread_self(); level = rt_hw_interrupt_disable(); if (completion->flag != RT_COMPLETED) { if (timeout == 0) { } else { /* reset thread error number */ thread->error = RT_EOK; /* suspend thread */ rt_thread_suspend(thread); /* add to suspended list */ rt_list_insert_before(&(completion->suspended_list), &(thread->tlist)); /* current context checking */ RT_DEBUG_NOT_IN_INTERRUPT; /* start timer */ if (timeout > 0) { /* reset the timeout of thread timer and start it */ rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout); rt_timer_start(&(thread->thread_timer)); } /* enable interrupt */ rt_hw_interrupt_enable(level); /* do schedule */ rt_schedule(); /* thread is waked up */ result = thread->error; level = rt_hw_interrupt_disable(); } } /* clean completed flag */ completion->flag = RT_UNCOMPLETED; return result; }主要做了以下工作:
这样就完成了线程的挂起。
完成完成量(以下代码有删减)
void rt_completion_done(struct rt_completion *completion) { level = rt_hw_interrupt_disable(); completion->flag = RT_COMPLETED; if (!rt_list_isempty(&(completion->suspended_list))) { /* there is one thread in suspended list */ struct rt_thread *thread; /* get thread entry */ thread = rt_list_entry(completion->suspended_list.next, struct rt_thread, tlist); /* resume it */ rt_thread_resume(thread); rt_hw_interrupt_enable(level); /* perform a schedule */ rt_schedule(); } }主要做了以下工作:
作者: rtthread小师弟, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-3941260.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
curton 2021-1-20 20:42
欢迎点击
论坛> >机器人/工业电子> >工业电子与自动化
https://mbb.eet-china.com/forum/topic/85437_1_1.html