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.h"
33 #include "los_sched.h"
34 #include "los_debug.h"
35
36 #ifdef __cplusplus
37 #if __cplusplus
38 extern "C" {
39 #endif
40 #endif /* __cplusplus */
41
42 SortLinkAttribute g_taskSortLink;
43 SortLinkAttribute g_swtmrSortLink;
44
OsSortLinkInit(SortLinkAttribute * sortLinkHeader)45 UINT32 OsSortLinkInit(SortLinkAttribute *sortLinkHeader)
46 {
47 LOS_ListInit(&sortLinkHeader->sortLink);
48 return LOS_OK;
49 }
50
OsAddNode2SortLink(SortLinkAttribute * sortLinkHeader,SortLinkList * sortList)51 STATIC INLINE VOID OsAddNode2SortLink(SortLinkAttribute *sortLinkHeader, SortLinkList *sortList)
52 {
53 LOS_DL_LIST *head = (LOS_DL_LIST *)&sortLinkHeader->sortLink;
54
55 if (LOS_ListEmpty(head)) {
56 LOS_ListAdd(head, &sortList->sortLinkNode);
57 return;
58 }
59
60 SortLinkList *listSorted = LOS_DL_LIST_ENTRY(head->pstNext, SortLinkList, sortLinkNode);
61 if (listSorted->responseTime > sortList->responseTime) {
62 LOS_ListAdd(head, &sortList->sortLinkNode);
63 return;
64 } else if (listSorted->responseTime == sortList->responseTime) {
65 LOS_ListAdd(head->pstNext, &sortList->sortLinkNode);
66 return;
67 }
68
69 LOS_DL_LIST *prevNode = head->pstPrev;
70 do {
71 listSorted = LOS_DL_LIST_ENTRY(prevNode, SortLinkList, sortLinkNode);
72 if (listSorted->responseTime <= sortList->responseTime) {
73 LOS_ListAdd(prevNode, &sortList->sortLinkNode);
74 break;
75 }
76
77 prevNode = prevNode->pstPrev;
78 } while (1);
79 }
80
OsAdd2SortLink(SortLinkList * node,UINT64 startTime,UINT32 waitTicks,SortLinkType type)81 VOID OsAdd2SortLink(SortLinkList *node, UINT64 startTime, UINT32 waitTicks, SortLinkType type)
82 {
83 UINT32 intSave;
84 SortLinkAttribute *sortLinkHeader = NULL;
85
86 if (type == OS_SORT_LINK_TASK) {
87 sortLinkHeader = &g_taskSortLink;
88 } else if (type == OS_SORT_LINK_SWTMR) {
89 sortLinkHeader = &g_swtmrSortLink;
90 } else {
91 LOS_Panic("Sort link type error : %u\n", type);
92 }
93
94 intSave = LOS_IntLock();
95 SET_SORTLIST_VALUE(node, startTime + OS_SYS_TICK_TO_CYCLE(waitTicks));
96 OsAddNode2SortLink(sortLinkHeader, node);
97 LOS_IntRestore(intSave);
98 }
99
OsDeleteSortLink(SortLinkList * node)100 VOID OsDeleteSortLink(SortLinkList *node)
101 {
102 UINT32 intSave;
103
104 intSave = LOS_IntLock();
105 if (node->responseTime != OS_SORT_LINK_INVALID_TIME) {
106 OsSchedResetSchedResponseTime(node->responseTime);
107 OsDeleteNodeSortLink(node);
108 }
109 LOS_IntRestore(intSave);
110 }
111
OsGetSortLinkAttribute(SortLinkType type)112 SortLinkAttribute *OsGetSortLinkAttribute(SortLinkType type)
113 {
114 if (type == OS_SORT_LINK_TASK) {
115 return &g_taskSortLink;
116 } else if (type == OS_SORT_LINK_SWTMR) {
117 return &g_swtmrSortLink;
118 }
119
120 PRINT_ERR("Invalid sort link type!\n");
121 return NULL;
122 }
123
OsSortLinkGetTargetExpireTime(UINT64 currTime,const SortLinkList * targetSortList)124 UINT64 OsSortLinkGetTargetExpireTime(UINT64 currTime, const SortLinkList *targetSortList)
125 {
126 if (currTime >= targetSortList->responseTime) {
127 return 0;
128 }
129
130 return (targetSortList->responseTime - currTime);
131 }
132
OsSortLinkGetNextExpireTime(const SortLinkAttribute * sortLinkHeader)133 UINT64 OsSortLinkGetNextExpireTime(const SortLinkAttribute *sortLinkHeader)
134 {
135 LOS_DL_LIST *head = (LOS_DL_LIST *)&sortLinkHeader->sortLink;
136
137 if (LOS_ListEmpty(head)) {
138 return 0;
139 }
140
141 SortLinkList *listSorted = LOS_DL_LIST_ENTRY(head->pstNext, SortLinkList, sortLinkNode);
142 return OsSortLinkGetTargetExpireTime(OsGetCurrSchedTimeCycle(), listSorted);
143 }
144
145 #ifdef __cplusplus
146 #if __cplusplus
147 }
148 #endif
149 #endif /* __cplusplus */
150