1. completion 是什么
completion 直接翻譯過(guò)來(lái)是完成,所以我們可以稱(chēng) rt_completion 為 完成量。在 RT-Thread 的文檔中心 中講線(xiàn)程間同步時(shí),介紹了 信號(hào)量, 互斥量, 事件集 。 rt_completion 是一個(gè) 輕量級(jí)的二值信號(hào)量。
2. completion 怎么使用
completion 的使用非常簡(jiǎn)單
定義一個(gè)完成量
1struct rt_completion completion;
初始化完成量
1rt_completion_init(&completion);
等待完成量
1rt_completion_wait(&completion);
釋放完成量
《br /》rt_completion_done(&completion);《br /》
3. completion 的實(shí)現(xiàn)
completion 的 API 非常少,可以通過(guò)簡(jiǎn)單的代碼去分析
初始化完成量
1void rt_completion_init(struct rt_completion *completion)
2{
3 rt_base_t level;
4 RT_ASSERT(completion != RT_NULL);
5
6 level = rt_hw_interrupt_disable();
7 completion-》flag = RT_UNCOMPLETED;
8 rt_list_init(&completion-》suspended_list);
9 rt_hw_interrupt_enable(level);
10}
干了兩件事:
設(shè)置 flag 為 RT_UNCOMPLETED
初始化完成量的鏈表
2.等待完成量(以下代碼有刪減)
1rt_err_t rt_completion_wait(struct rt_completion *completion,
2 rt_int32_t timeout)
3{
4 result = RT_EOK;
5 thread = rt_thread_self();
6
7 level = rt_hw_interrupt_disable();
8 if (completion-》flag != RT_COMPLETED)
9 {
10 if (timeout == 0)
11 {
12
13 }
14 else
15 {
16 /* reset thread error number */
17 thread-》error = RT_EOK;
18
19 /* suspend thread */
20 rt_thread_suspend(thread);
21 /* add to suspended list */
22 rt_list_insert_before(&(completion-》suspended_list),
23 &(thread-》tlist));
24
25 /* current context checking */
26 RT_DEBUG_NOT_IN_INTERRUPT;
27
28 /* start timer */
29 if (timeout 》 0)
30 {
31 /* reset the timeout of thread timer and start it */
32 rt_timer_control(&(thread-》thread_timer),
33 RT_TIMER_CTRL_SET_TIME,
34 &timeout);
35 rt_timer_start(&(thread-》thread_timer));
36 }
37 /* enable interrupt */
38 rt_hw_interrupt_enable(level);
39
40 /* do schedule */
41 rt_schedule();
42
43 /* thread is waked up */
44 result = thread-》error;
45
46 level = rt_hw_interrupt_disable();
47 }
48 }
49 /* clean completed flag */
50 completion-》flag = RT_UNCOMPLETED;
51
52 return result;
53}
主要做了以下工作:
關(guān)中斷:rt_hw_interrupt_disable();
掛起當(dāng)前線(xiàn)程:rt_thread_suspend(thread);
把掛起狀態(tài)插入到線(xiàn)程的鏈表中:rt_list_insert_before
確保當(dāng)前函數(shù)執(zhí)行不是在中斷中:RT_DEBUG_NOT_IN_INTERRUPT;
設(shè)置并啟動(dòng)定時(shí)器:rt_timer_start(&(thread-》thread_timer));
開(kāi)中斷:rt_hw_interrupt_enable(level);
開(kāi)調(diào)度器:rt_schedule();
獲取當(dāng)前線(xiàn)程狀態(tài):result = thread-》error;
設(shè)置完成量的標(biāo)志位:completion-》flag = RT_UNCOMPLETED;
返回線(xiàn)程狀態(tài)
這樣就完成了線(xiàn)程的掛起。
3.完成完成量(以下代碼有刪減)
1 void rt_completion_done(struct rt_completion *completion)
2 {
3 level = rt_hw_interrupt_disable();
4 completion-》flag = RT_COMPLETED;
5
6 if (!rt_list_isempty(&(completion-》suspended_list)))
7 {
8 /* there is one thread in suspended list */
9 struct rt_thread *thread;
10
11 /* get thread entry */
12 thread = rt_list_entry(completion-》suspended_list.next,
13 struct rt_thread,
14 tlist);
15
16 /* resume it */
17 rt_thread_resume(thread);
18 rt_hw_interrupt_enable(level);
19
20 /* perform a schedule */
21 rt_schedule();
22 }
23 }
主要做了以下工作:
關(guān)中斷:rt_hw_interrupt_disable();
設(shè)置 flag 為 RT_COMPLETED
檢查鏈表不為空:rt_list_isempty
獲取到當(dāng)前等待完成量的句柄:rt_list_entry
啟動(dòng)被掛起的線(xiàn)程:rt_thread_resume(thread);
開(kāi)中斷:rt_hw_interrupt_enable(level);
開(kāi)調(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 源碼在 “ t-threadcomponentsdriverssrccompletion.c”在你要使用的文件中#include completion.h直接就可以使用。
Nano 版 RT-Thread 直接拷貝completion.c 和 completion.h 添加到工程就可以使用
編輯:lyn
-
代碼
+關(guān)注
關(guān)注
30文章
4899瀏覽量
70594 -
信號(hào)量
+關(guān)注
關(guān)注
0文章
53瀏覽量
8557
原文標(biāo)題:RT-Thread隱藏的寶藏之completion
文章出處:【微信號(hào):RTThread,微信公眾號(hào):RTThread物聯(lián)網(wǎng)操作系統(tǒng)】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
中科創(chuàng)達(dá)推出基于高通Wear 5100+MCU打造的TurboX AI眼鏡

施耐德電氣與奇安信共建技術(shù)本地化創(chuàng)新中心

A股國(guó)產(chǎn)力傳感器龍頭柯力傳感投資北京一家觸覺(jué)傳感器公司“他山科技”

Molex推出家庭能源存儲(chǔ)解決方案

在 Film Frame 和 Waffle Pack 上提供的 Hypersudden 調(diào)諧變?nèi)萜?skyworksinc

Hyperabrupt Junction Tuning 變?nèi)荻O管 skyworksinc

塑料封裝的 Abrupt Junction 調(diào)諧變?nèi)荻O管 skyworksinc

突變結(jié)變?nèi)荻O管陶瓷封裝 skyworksinc

輸電線(xiàn)路三跨線(xiàn)路圖像視頻監(jiān)測(cè)裝置:“全景攝像頭”
NVMe協(xié)議簡(jiǎn)要分析
使用Dockerfile搭建潤(rùn)和WS63E的開(kāi)發(fā)環(huán)境
【米爾-Xilinx XC7A100T FPGA開(kāi)發(fā)板試用】+02.PCIE接口測(cè)試(zmj)
PCI-E TLP學(xué)習(xí)筆記(3)

PCI-E TLP學(xué)習(xí)筆記(2)

評(píng)論