1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2022 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
OsSortLinkInit(SortLinkAttribute * sortLinkHeader)34 VOID OsSortLinkInit(SortLinkAttribute *sortLinkHeader)
35 {
36 LOS_ListInit(&sortLinkHeader->sortLink);
37 LOS_SpinInit(&sortLinkHeader->spinLock);
38 sortLinkHeader->nodeNum = 0;
39 }
40
AddNode2SortLink(SortLinkAttribute * sortLinkHeader,SortLinkList * sortList)41 STATIC INLINE VOID AddNode2SortLink(SortLinkAttribute *sortLinkHeader, SortLinkList *sortList)
42 {
43 LOS_DL_LIST *head = (LOS_DL_LIST *)&sortLinkHeader->sortLink;
44
45 if (LOS_ListEmpty(head)) {
46 LOS_ListHeadInsert(head, &sortList->sortLinkNode);
47 sortLinkHeader->nodeNum++;
48 return;
49 }
50
51 SortLinkList *listSorted = LOS_DL_LIST_ENTRY(head->pstNext, SortLinkList, sortLinkNode);
52 if (listSorted->responseTime > sortList->responseTime) {
53 LOS_ListAdd(head, &sortList->sortLinkNode);
54 sortLinkHeader->nodeNum++;
55 return;
56 } else if (listSorted->responseTime == sortList->responseTime) {
57 LOS_ListAdd(head->pstNext, &sortList->sortLinkNode);
58 sortLinkHeader->nodeNum++;
59 return;
60 }
61
62 LOS_DL_LIST *prevNode = head->pstPrev;
63 do {
64 listSorted = LOS_DL_LIST_ENTRY(prevNode, SortLinkList, sortLinkNode);
65 if (listSorted->responseTime <= sortList->responseTime) {
66 LOS_ListAdd(prevNode, &sortList->sortLinkNode);
67 sortLinkHeader->nodeNum++;
68 break;
69 }
70
71 prevNode = prevNode->pstPrev;
72 } while (1);
73 }
74
OsAdd2SortLink(SortLinkAttribute * head,SortLinkList * node,UINT64 responseTime,UINT16 idleCpu)75 VOID OsAdd2SortLink(SortLinkAttribute *head, SortLinkList *node, UINT64 responseTime, UINT16 idleCpu)
76 {
77 LOS_SpinLock(&head->spinLock);
78 SET_SORTLIST_VALUE(node, responseTime);
79 AddNode2SortLink(head, node);
80 #ifdef LOSCFG_KERNEL_SMP
81 node->cpuid = idleCpu;
82 #endif
83 LOS_SpinUnlock(&head->spinLock);
84 }
85
OsDeleteFromSortLink(SortLinkAttribute * head,SortLinkList * node)86 VOID OsDeleteFromSortLink(SortLinkAttribute *head, SortLinkList *node)
87 {
88 LOS_SpinLock(&head->spinLock);
89 if (node->responseTime != OS_SORT_LINK_INVALID_TIME) {
90 OsDeleteNodeSortLink(head, node);
91 }
92 LOS_SpinUnlock(&head->spinLock);
93 }
94
OsSortLinkAdjustNodeResponseTime(SortLinkAttribute * head,SortLinkList * node,UINT64 responseTime)95 UINT32 OsSortLinkAdjustNodeResponseTime(SortLinkAttribute *head, SortLinkList *node, UINT64 responseTime)
96 {
97 UINT32 ret = LOS_NOK;
98
99 LOS_SpinLock(&head->spinLock);
100 if (node->responseTime != OS_SORT_LINK_INVALID_TIME) {
101 OsDeleteNodeSortLink(head, node);
102 SET_SORTLIST_VALUE(node, responseTime);
103 AddNode2SortLink(head, node);
104 ret = LOS_OK;
105 }
106 LOS_SpinUnlock(&head->spinLock);
107 return ret;
108 }
109
OsSortLinkGetTargetExpireTime(UINT64 currTime,const SortLinkList * targetSortList)110 UINT64 OsSortLinkGetTargetExpireTime(UINT64 currTime, const SortLinkList *targetSortList)
111 {
112 if (currTime >= targetSortList->responseTime) {
113 return 0;
114 }
115
116 return (UINT32)(targetSortList->responseTime - currTime);
117 }
118
OsSortLinkGetNextExpireTime(UINT64 currTime,const SortLinkAttribute * sortLinkHeader)119 UINT64 OsSortLinkGetNextExpireTime(UINT64 currTime, const SortLinkAttribute *sortLinkHeader)
120 {
121 LOS_DL_LIST *head = (LOS_DL_LIST *)&sortLinkHeader->sortLink;
122
123 if (LOS_ListEmpty(head)) {
124 return OS_SORT_LINK_INVALID_TIME;
125 }
126
127 SortLinkList *listSorted = LOS_DL_LIST_ENTRY(head->pstNext, SortLinkList, sortLinkNode);
128 return OsSortLinkGetTargetExpireTime(currTime, listSorted);
129 }
130
131