更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
los_sortlink.c
浏览该文件的文档.
1/*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include "los_sortlink_pri.h"
33/// 排序链表初始化
34VOID OsSortLinkInit(SortLinkAttribute *sortLinkHeader)
35{
36 LOS_ListInit(&sortLinkHeader->sortLink);
37 LOS_SpinInit(&sortLinkHeader->spinLock);
38 sortLinkHeader->nodeNum = 0;
39}
40
41/*!
42 * @brief OsAddNode2SortLink 向链表中插入结点,并按时间顺序排列
43 *
44 * @param sortLinkHeader 被插入的链表
45 * @param sortList 要插入的结点
46 * @return
47 *
48 * @see
49 */
50STATIC INLINE VOID AddNode2SortLink(SortLinkAttribute *sortLinkHeader, SortLinkList *sortList)
51{
52 LOS_DL_LIST *head = (LOS_DL_LIST *)&sortLinkHeader->sortLink; //获取双向链表
53
54 if (LOS_ListEmpty(head)) { //空链表,直接插入
55 LOS_ListHeadInsert(head, &sortList->sortLinkNode);//插入结点
56 sortLinkHeader->nodeNum++;//CPU的工作量增加了
57 return;
58 }
59 //链表不为空时,插入分三种情况, responseTime 大于,等于,小于的处理
60 SortLinkList *listSorted = LOS_DL_LIST_ENTRY(head->pstNext, SortLinkList, sortLinkNode);
61 if (listSorted->responseTime > sortList->responseTime) {//如果要插入的节点 responseTime 最小
62 LOS_ListAdd(head, &sortList->sortLinkNode);//能跑进来说明是最小的,直接插入到第一的位置
63 sortLinkHeader->nodeNum++;//CPU的工作量增加了
64 return;//直接返回了
65 } else if (listSorted->responseTime == sortList->responseTime) {//相等的情况
66 LOS_ListAdd(head->pstNext, &sortList->sortLinkNode);//插到第二的位置
67 sortLinkHeader->nodeNum++;
68 return;
69 }
70 //处理大于链表中第一个responseTime的情况,需要遍历链表
71 LOS_DL_LIST *prevNode = head->pstPrev;//注意这里用的前一个结点,也就是说前一个结点中的responseTime 是最大的
72 do { // @note_good 这里写的有点妙,也是双向链表的魅力所在
73 listSorted = LOS_DL_LIST_ENTRY(prevNode, SortLinkList, sortLinkNode);//一个个遍历,先比大的再比小的
74 if (listSorted->responseTime <= sortList->responseTime) {//如果时间比你小,就插到后面
75 LOS_ListAdd(prevNode, &sortList->sortLinkNode);
76 sortLinkHeader->nodeNum++;
77 break;
78 }
79
80 prevNode = prevNode->pstPrev;//再拿上一个更小的responseTime进行比较
81 } while (1);//死循环
82}
83
84VOID OsAdd2SortLink(SortLinkAttribute *head, SortLinkList *node, UINT64 responseTime, UINT16 idleCpu)
85{
86 LOS_SpinLock(&head->spinLock);
87 SET_SORTLIST_VALUE(node, responseTime);
88 AddNode2SortLink(head, node);
89#ifdef LOSCFG_KERNEL_SMP
90 node->cpuid = idleCpu;
91#endif
93}
94
96{
97 LOS_SpinLock(&head->spinLock);
98 if (node->responseTime != OS_SORT_LINK_INVALID_TIME) {
99 OsDeleteNodeSortLink(head, node);
100 }
101 LOS_SpinUnlock(&head->spinLock);
102}
103
105{
106 UINT32 ret = LOS_NOK;
107
108 LOS_SpinLock(&head->spinLock);
109 if (node->responseTime != OS_SORT_LINK_INVALID_TIME) {
110 OsDeleteNodeSortLink(head, node);
111 SET_SORTLIST_VALUE(node, responseTime);
112 AddNode2SortLink(head, node);
113 ret = LOS_OK;
114 }
115 LOS_SpinUnlock(&head->spinLock);
116 return ret;
117}
118
120{
121 if (currTime >= targetSortList->responseTime) {
122 return 0;
123 }
124
125 return (UINT32)(targetSortList->responseTime - currTime);
126}
127
129{
130 LOS_DL_LIST *head = (LOS_DL_LIST *)&sortLinkHeader->sortLink;
131
132 if (LOS_ListEmpty(head)) {
133 return OS_SORT_LINK_INVALID_TIME;
134 }
135
136 SortLinkList *listSorted = LOS_DL_LIST_ENTRY(head->pstNext, SortLinkList, sortLinkNode);
137 return OsSortLinkGetTargetExpireTime(currTime, listSorted);
138}
139
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListInit(LOS_DL_LIST *list)
Definition: los_list.h:104
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListHeadInsert(LOS_DL_LIST *list, LOS_DL_LIST *node)
Insert a node to the head of a doubly linked list.
Definition: los_list.h:268
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListAdd(LOS_DL_LIST *list, LOS_DL_LIST *node)
Insert a new node to a doubly linked list.
Definition: los_list.h:217
LITE_OS_SEC_ALW_INLINE STATIC INLINE BOOL LOS_ListEmpty(LOS_DL_LIST *list)
Identify whether a specified doubly linked list is empty. | 判断链表是否为空
Definition: los_list.h:321
VOID LOS_SpinLock(SPIN_LOCK_S *lock)
Definition: los_spinlock.c:50
VOID LOS_SpinInit(SPIN_LOCK_S *lock)
Definition: los_spinlock.c:37
VOID LOS_SpinUnlock(SPIN_LOCK_S *lock)
Definition: los_spinlock.c:84
unsigned short UINT16
Definition: los_typedef.h:56
long unsigned int UINT64
Definition: los_typedef.h:66
unsigned int UINT32
Definition: los_typedef.h:57
struct LOS_DL_LIST * pstPrev
Definition: los_list.h:83
struct LOS_DL_LIST * pstNext
Definition: los_list.h:84
if(tv==NULL)
Definition: time.c:430