1.completion 是什么
completion 直接翻譯過(guò)來(lái)是完成,所以我更愿意稱 rt_completion 為 完成量。在 RT-Thread 的文檔中心 中講線程間通訊(IPC)時(shí),只介紹了,信號(hào)量, 互斥量, 事件集,其實(shí) rt_completion 可以認(rèn)為是輕量級(jí)的二值信號(hào)量。
2.completion 怎么使用
completion 的使用非常簡(jiǎn)單
定義一個(gè)完成量
struct rt_completion completion;
初始化完成量
rt_completion_init(&completion);
等待完成量
rt_completion_wait(&completion);
釋放完成量
rt_completion_done(&completion);
3.completion 的實(shí)現(xiàn)
completion 的 API 非常少,可以通過(guò)簡(jiǎn)單的代碼去分析
初始化完成量
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);
}
干了兩件事:
設(shè)置 flag 為 RT_UNCOMPLETED
初始化完成量的鏈表
等待完成量(以下代碼有刪減)
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;
}
主要做了以下工作:
關(guān)中斷:rt_hw_interrupt_disable();
掛起當(dāng)前線程:rt_thread_suspend(thread);
把掛起狀態(tài)插入到線程的鏈表中:rt_list_insert_before
確保當(dāng)前函數(shù)執(zhí)行不是在中斷中:RT_DEBUG_NOT_IN_INTERRUPT;
設(shè)置并啟動(dòng)定時(shí)器:rt_timer_start(&(thread->thread_timer));
開中斷:rt_hw_interrupt_enable(level);
開調(diào)度器:rt_schedule();
獲取當(dāng)前線程狀態(tài):result = thread->error;
設(shè)置完成量的標(biāo)志位:completion->flag = RT_UNCOMPLETED;
返回線程狀態(tài)
這樣就完成了線程的掛起。
完成完成量(以下代碼有刪減)
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();
}
}
主要做了以下工作:
關(guān)中斷:rt_hw_interrupt_disable();
設(shè)置 flag 為 RT_COMPLETED
檢查鏈表不為空:rt_list_isempty
獲取到當(dāng)前等待完成量的句柄:rt_list_entry
啟動(dòng)被掛起的線程:rt_thread_resume(thread);
開中斷:rt_hw_interrupt_enable(level);
開調(diào)度:rt_schedule();
4.completion 與信號(hào)量的對(duì)比
completion API 個(gè)數(shù)少,資源占用少,只能釋放獲取,不支持多次釋放
semaphore API 個(gè)數(shù)多,資源占用較多,使用靈活,可以嘗試獲取,可以多次釋放,
5.completion 如何加入工程
標(biāo)準(zhǔn)版 RT-Thread 中的 completion 源碼在 "\\rt-thread\\components\\drivers\\src\\completion.c"在你要使用的文件中#include completion.h直接就可以使用。
Nano 版 RT-Thread 直接拷貝completion.c 和 completion.h 添加到工程就可以使用。
-
定時(shí)器
+關(guān)注
關(guān)注
23文章
3255瀏覽量
115375 -
IPC
+關(guān)注
關(guān)注
3文章
352瀏覽量
52073 -
串口中斷
+關(guān)注
關(guān)注
0文章
67瀏覽量
14009 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1305瀏覽量
40387 -
調(diào)度器
+關(guān)注
關(guān)注
0文章
98瀏覽量
5298
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
completion CAN驅(qū)動(dòng)的completion會(huì)失效的原因?
用DIRECTIO函數(shù)發(fā)送數(shù)據(jù)有的時(shí)候會(huì)在KeyStone_SRIO_wait_LSU_completion里面的do while循環(huán)里,不出來(lái)這是為什么?
6670系列SRIO 用戶手冊(cè)中關(guān)于lsu_reg6中LTID,LCB配置問(wèn)題,completion code產(chǎn)生過(guò)程
6616 QMSS tx completion queue 如何產(chǎn)生中斷問(wèn)題?
請(qǐng)問(wèn)為什么EDMA3無(wú)法進(jìn)入completion code function?
兩個(gè)6655的DSP通過(guò)srio接口通訊時(shí),主機(jī)循環(huán)向從機(jī)發(fā)送數(shù)據(jù),第一組數(shù)據(jù)completion code=0,其后一直completion code=1
去掉can驅(qū)動(dòng)芯片程序卡死在rt_completion_wait()怎么辦
為什么rt_completion_wait不能用在ISR里呢
為什么rt_completion_wait不能用在ISR里呢
Virtex-7 FPGA Gen3 Integrated Block Completion timeout 機(jī)制詳解
![Virtex-7 FPGA Gen3 Integrated Block <b class='flag-5'>Completion</b> timeout 機(jī)制詳解](https://file1.elecfans.com//web2/M00/A6/EC/wKgZomUMQUiABfd-AAAN33y5bJo396.jpg)
你知道linux 同步機(jī)制的complete?
completion是什么?怎么使用?
RT-Thread隱藏的寶藏之completion
![RT-Thread隱藏的寶藏之<b class='flag-5'>completion</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
RT-Thread隱藏的寶藏之completion
![RT-Thread隱藏的寶藏之<b class='flag-5'>completion</b>](https://file.elecfans.com/web1/M00/D9/4E/pIYBAF_1ac2Ac0EEAABDkS1IP1s689.png)
評(píng)論