鴻蒙輕內核M核源碼分析:數據結構之任務就緒隊列

{"type":"doc","content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"​​​​​​​​​​摘要:本文會給讀者介紹鴻蒙輕內核M核源碼中重要的數據結構,任務基於優先級的就緒隊列Priority Queue。","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文分享自華爲雲社區","attrs":{}},{"type":"link","attrs":{"href":"https://bbs.huaweicloud.com/blogs/265258?utm_source=infoq&utm_medium=bbs-ex&utm_campaign=iot&utm_content=content","title":"","type":null},"content":[{"type":"text","text":"《鴻蒙輕內核M核源碼分析系列三 數據結構-任務就緒隊列》","attrs":{}}]},{"type":"text","text":",原文作者:zhushy。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文會給讀者介紹鴻蒙輕內核M核源碼中重要的數據結構,任務基於優先級的就緒隊列Priority Queue。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在講解時,會結合數據結構相關繪圖,培養讀者們的數據結構的平面想象能力,幫助更好的學習和理解這些數據結構的用法。本文中所涉及的源碼,以OpenHarmonyLiteOS-M內核爲例,均可以在開源站點https://gitee.com/openharmony/kernel_liteos_m 獲取。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"1、任務就緒隊列","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在任務調度模塊,就緒隊列是個重要的數據結構。任務創建後即進入就緒態,並放入就緒隊列。在鴻蒙輕內核中,就緒隊列是一個雙向循環鏈表數組,每個數組元素就是一個鏈表,相同優先級的任務放入同一個鏈表。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"任務就緒隊列PriorityQueue主要供內部使用,用戶進行業務開發時不涉及,所以並未對外提供接口。雙向循環鏈表數組能夠更加方便的支持任務基於優先級進行調度。任務就緒隊列的核心代碼在kernel\\src\\los_task.c文件中。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"1.1 任務就緒隊列的定義","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在kernel\\src\\los_task.c文件中定義了和任務就緒隊列相關的主要變量。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"源碼如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"⑴ LITE_OS_SEC_BSS LOS_DL_LIST *g_losPriorityQueueList = NULL;\n\n⑵ static LITE_OS_SEC_BSS UINT32 g_priqueueBitmap = 0;\n\n⑶ #define PRIQUEUE_PRIOR0_BIT (UINT32)0x80000000\n\n⑷ #define OS_PRIORITY_QUEUE_PRIORITYNUM 32","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"​","attrs":{}},{"type":"text","text":"其中⑴表示任務就緒隊列,是一個雙向鏈表數組,後文初始化該數組時會將數組長度設置爲⑷處定義的OS_PRIORITY_QUEUE_PRIORITYNUM;⑵表示優先級位圖,標識了任務就緒隊列中已掛載的就緒任務所在的優先級;⑶表示優先級爲0的比特位;⑷表示任務就緒隊列支持的優先級個數32,所以鴻蒙輕內核優先級的取值範圍爲0-31,數值越小優先級越大。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"優先級位圖g_priqueueBitmap的bit位和優先級的關係爲bit=31-priority,優先級數組g_losPriorityQueueList[priority]包含了OS_PRIORITY_QUEUE_PRIORITYNUM個數組元素,每個數組元素都是一個雙向鏈表,同一優先級的處於就緒狀態的所有任務都會掛載到對應優先級的雙向鏈表中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"示意圖如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/ad/adf148644052edb9c0d5bbe5048234f2.jpeg","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"2、任務就緒隊列操作","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.1 初始化任務就緒隊列","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"任務就緒隊列初始化函數爲OsPriQueueInit(),系統初始化階段被調用,調用路徑爲:main.c:main()--> kernel\\src\\los_init.c:LOS_KernelInit() --> kernel\\src\\los_task.c:OsTaskInit()--> OsPriqueueInit()。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"源碼如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"STATIC UINT32 OsPriqueueInit(VOID)\n{\n UINT32 priority;\n⑴ UINT32 size = OS_PRIORITY_QUEUE_PRIORITYNUM * sizeof(LOS_DL_LIST);\n\n g_losPriorityQueueList = (LOS_DL_LIST *)LOS_MemAlloc(m_aucSysMem0, size);\n if (g_losPriorityQueueList == NULL) {\n return LOS_NOK;\n }\n\n for (priority = 0; priority < OS_PRIORITY_QUEUE_PRIORITYNUM; ++priority) {\n⑵ LOS_ListInit(&g_losPriorityQueueList[priority]);\n }\n return LOS_OK;\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"​","attrs":{}},{"type":"text","text":"⑴處計算就緒隊列數組需要的內存大小,然後爲任務就緒隊列申請內存,佔用內存爲OS_PRIORITY_QUEUE_PRIORITYNUM個雙向鏈表所需要的內存大小,運行期間該內存不會釋放,爲系統常駐內存。⑵處代碼將每一個數組元素都初始化爲雙向循環鏈表。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.2 任務就緒隊列插入","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"任務就緒隊列插入函數爲OsPriqueueEnqueue(),該函數把就緒狀態的任務插入任務就緒隊列的尾部。在任務就緒隊列中,先調用隊列頭部的任務,最後調用隊列尾部的任務。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"源碼如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"STATIC VOID OsPriqueueEnqueue(LOS_DL_LIST *priqueueItem, UINT32 priority)\n{\n⑴ if (LOS_ListEmpty(&g_losPriorityQueueList[priority])) {\n⑵ g_priqueueBitmap |= (PRIQUEUE_PRIOR0_BIT >> priority);\n }\n\n⑶ LOS_ListTailInsert(&g_losPriorityQueueList[priority], priqueueItem);\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"⑴處先判斷指定優先級priority的任務就緒隊列是否爲空,如果爲空,則在⑵處更新優先級位圖,將第31-prioritybit位設置爲1。⑶處把就緒狀態的任務插入任務就緒隊列的尾部,進行排隊。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.3 從任務就緒隊列中刪除","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從任務就緒隊列中刪除的函數爲OsPriqueueDequeue()。任務被刪除、進入suspend阻塞狀態、優先級調整等場景中,都需要調用該函數把任務從任務就緒隊列中刪除。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"源碼如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"STATIC VOID OsPriqueueDequeue(LOS_DL_LIST *priqueueItem)\n{\n LosTaskCB *runningTask = NULL;\n⑴ LOS_ListDelete(priqueueItem);\n\n⑵ runningTask = LOS_DL_LIST_ENTRY(priqueueItem, LosTaskCB, pendList);\n⑶ if (LOS_ListEmpty(&g_losPriorityQueueList[runningTask->priority])) {\n⑷ g_priqueueBitmap &= ~(PRIQUEUE_PRIOR0_BIT >> runningTask->priority);\n }\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"⑴把任務從任務就緒隊列中刪除。⑵獲取被刪除任務的任務控制塊信息,以獲取任務的優先級。刪除完任務後隊列可能成爲空隊列,所以⑶處代碼判斷任務就緒隊列是否爲空,如果爲空,則需要執行⑷處代碼,更新優先級位圖,將第31-prioritybit位設置爲0。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.4 獲取隊列中的最高優先級節點","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"獲取任務就緒隊列中優先級最高的鏈表節點的函數爲OsPriQueueTop()。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"源碼如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"STATIC LOS_DL_LIST *OsPriqueueTop(VOID)\n{\n UINT32 priority;\n\n⑴ if (g_priqueueBitmap != 0) {\n⑵ priority = CLZ(g_priqueueBitmap);\n⑶ return LOS_DL_LIST_FIRST(&g_losPriorityQueueList[priority]);\n }\n\n return (LOS_DL_LIST *)NULL;\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"⑴處判斷優先級位圖g_priqueueBitmap是否爲0,如果爲0則直接返回NULL,說明任務就緒隊列中沒有任何就緒狀態的任務。 ⑵處計算g_priqueueBitmap以二進制表示時高位爲0的位數,其值就是任務的優先級priority,以此方法得到的優先級就是任務就緒隊列中所有優先級裏最高的。然後⑶處從該優先級的隊列&g_losPriorityQueueList[priority]中獲取第一個鏈表節點,獲取的就是任務就緒隊列中優先級最高的任務。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"2.5 獲取指定優先級的就緒任務的數量","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"獲取任務就緒隊列中指定優先級的任務數量的函數爲OsPriqueueSize()。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"源碼如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"STATIC UINT32 OsPriqueueSize(UINT32 priority)\n{\n UINT32 itemCnt = 0;\n LOS_DL_LIST *curPQNode = (LOS_DL_LIST *)NULL;\n\n⑴ LOS_DL_LIST_FOR_EACH(curPQNode, &g_losPriorityQueueList[priority]) {\n⑵ ++itemCnt;\n }\n\n return itemCnt;\n}","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"​","attrs":{}},{"type":"text","text":"⑴處代碼使用宏LOS_DL_LIST_FOR_EACH定義的for循環遍歷指定優先級priority的雙向鏈表,如果獲取到新節點則表示該優先級下有一個就緒任務,然後執行⑵處代碼,對計數進行加1操作,返回的結果就是指定優先級下有多少個就緒任務。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"小結","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"掌握鴻蒙輕內核的優先級就緒隊列Priority Queue這一重要的數據結構,會給進一步學習、分析鴻蒙輕內核源代碼打下了基礎,讓後續的學習更加容易。後續也會陸續推出更多的分享文章,敬請期待,也歡迎大家分享學習、使用鴻蒙輕內核的心得,有任何問題、建議,都可以留言給我們: ","attrs":{}},{"type":"link","attrs":{"href":"https://gitee.com/openharmony/kernel_liteos_m/issues","title":"","type":null},"content":[{"type":"text","text":"https://gitee.com/openharmony/kernel_liteos_m/issues","attrs":{}}]},{"type":"text","text":" 。爲了更容易找到鴻蒙輕內核代碼倉,建議訪問 ","attrs":{}},{"type":"link","attrs":{"href":"https://gitee.com/openharmony/kernel_liteos_m","title":"","type":null},"content":[{"type":"text","text":"https://gitee.com/openharmony/kernel_liteos_m","attrs":{}}]},{"type":"text","text":" 。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https://bbs.huaweicloud.com/blogs?utm_source=infoq&utm_medium=bbs-ex&utm_campaign=iot&utm_content=content","title":"","type":null},"content":[{"type":"text","text":"點擊關注,第一時間瞭解華爲雲新鮮技術~","attrs":{}}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章