1 /*
2 * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
3 *
4 * UniProton is licensed under Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 * http://license.coscl.org.cn/MulanPSL2
8 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11 * See the Mulan PSL v2 for more details.
12 * Create: 2009-12-22
13 * Description: Task schedule implementation
14 */
15 #include "prt_task_external.h"
16
17 /*
18 * 描述:获取指定任务的优先级
19 */
PRT_TaskGetPriority(TskHandle taskPid,TskPrior * taskPrio)20 OS_SEC_L4_TEXT U32 PRT_TaskGetPriority(TskHandle taskPid, TskPrior *taskPrio)
21 {
22 uintptr_t intSave;
23 struct TagTskCb *taskCb = NULL;
24
25 if (CHECK_TSK_PID_OVERFLOW(taskPid)) {
26 return OS_ERRNO_TSK_ID_INVALID;
27 }
28
29 if (taskPrio == NULL) {
30 return OS_ERRNO_TSK_PTR_NULL;
31 }
32
33 taskCb = GET_TCB_HANDLE(taskPid);
34
35 intSave = OsIntLock();
36
37 if (TSK_IS_UNUSED(taskCb)) {
38 OsIntRestore(intSave);
39
40 return OS_ERRNO_TSK_NOT_CREATED;
41 }
42
43 *taskPrio = taskCb->priority;
44
45 OsIntRestore(intSave);
46
47 return OS_OK;
48 }
49
OsTaskPrioritySetCheck(TskHandle taskPid,TskPrior taskPrio)50 OS_SEC_ALW_INLINE INLINE U32 OsTaskPrioritySetCheck(TskHandle taskPid, TskPrior taskPrio)
51 {
52 if (taskPrio > OS_TSK_PRIORITY_LOWEST) {
53 return OS_ERRNO_TSK_PRIOR_ERROR;
54 }
55
56 if (CHECK_TSK_PID_OVERFLOW(taskPid)) {
57 return OS_ERRNO_TSK_ID_INVALID;
58 }
59
60 if (taskPid == IDLE_TASK_ID) {
61 return OS_ERRNO_TSK_OPERATE_IDLE;
62 }
63 return OS_OK;
64 }
65
66 /*
67 * 描述:设置指定任务的优先级
68 */
PRT_TaskSetPriority(TskHandle taskPid,TskPrior taskPrio)69 OS_SEC_L4_TEXT U32 PRT_TaskSetPriority(TskHandle taskPid, TskPrior taskPrio)
70 {
71 U32 ret;
72 bool isReady = FALSE;
73 uintptr_t intSave;
74 struct TagTskCb *taskCb = NULL;
75
76 ret = OsTaskPrioritySetCheck(taskPid, taskPrio);
77 if (ret != OS_OK) {
78 return ret;
79 }
80
81 taskCb = GET_TCB_HANDLE(taskPid);
82 intSave = OsIntLock();
83 if (TSK_IS_UNUSED(taskCb)) {
84 OsIntRestore(intSave);
85 return OS_ERRNO_TSK_NOT_CREATED;
86 }
87
88 isReady = (OS_TSK_READY & taskCb->taskStatus);
89
90 /* delete the task & insert with right priority into ready queue */
91 if (isReady) {
92 OsTskReadyDel(taskCb);
93 taskCb->priority = taskPrio;
94 OsTskReadyAdd(taskCb);
95 } else {
96 taskCb->priority = taskPrio;
97 }
98
99 /* reschedule if ready changed */
100 if (isReady) {
101 OsTskSchedule();
102 }
103
104 OsIntRestore(intSave);
105 return OS_OK;
106 }
107