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 "sched.h"
33 #include "map_error.h"
34 #include "sys/types.h"
35 #include "unistd.h"
36 #include "los_task_pri.h"
37
38
sched_get_priority_min(int policy)39 int sched_get_priority_min(int policy)
40 {
41 if (policy != SCHED_RR) {
42 errno = EINVAL;
43 return -1;
44 }
45
46 return OS_TASK_PRIORITY_HIGHEST;
47 }
48
sched_get_priority_max(int policy)49 int sched_get_priority_max(int policy)
50 {
51 if (policy != SCHED_RR) {
52 errno = EINVAL;
53 return -1;
54 }
55
56 return OS_TASK_PRIORITY_LOWEST;
57 }
58
59 /*
60 * This API is Linux-specific, not conforming to POSIX.
61 */
sched_setaffinity(pid_t pid,size_t set_size,const cpu_set_t * set)62 int sched_setaffinity(pid_t pid, size_t set_size, const cpu_set_t* set)
63 {
64 #ifdef LOSCFG_KERNEL_SMP
65 UINT32 taskID = (UINT32)pid;
66 UINT32 ret;
67
68 if ((set == NULL) || (set_size != sizeof(cpu_set_t)) || (set->__bits[0] > LOSCFG_KERNEL_CPU_MASK)) {
69 errno = EINVAL;
70 return -1;
71 }
72
73 if (taskID == 0) {
74 taskID = LOS_CurTaskIDGet();
75 if (taskID == LOS_ERRNO_TSK_ID_INVALID) {
76 errno = EINVAL;
77 return -1;
78 }
79 }
80
81 ret = LOS_TaskCpuAffiSet(taskID, (UINT16)set->__bits[0]);
82 if (ret != LOS_OK) {
83 errno = map_errno(ret);
84 return -1;
85 }
86 #endif
87
88 return 0;
89 }
90
91 /*
92 * This API is Linux-specific, not conforming to POSIX.
93 */
sched_getaffinity(pid_t pid,size_t set_size,cpu_set_t * set)94 int sched_getaffinity(pid_t pid, size_t set_size, cpu_set_t* set)
95 {
96 #ifdef LOSCFG_KERNEL_SMP
97 UINT32 taskID = (UINT32)pid;
98 UINT16 cpuAffiMask;
99
100 if ((set == NULL) || (set_size != sizeof(cpu_set_t))) {
101 errno = EINVAL;
102 return -1;
103 }
104
105 if (taskID == 0) {
106 taskID = LOS_CurTaskIDGet();
107 if (taskID == LOS_ERRNO_TSK_ID_INVALID) {
108 errno = EINVAL;
109 return -1;
110 }
111 }
112
113 cpuAffiMask = LOS_TaskCpuAffiGet(taskID);
114 if (cpuAffiMask == 0) {
115 errno = EINVAL;
116 return -1;
117 }
118
119 set->__bits[0] = cpuAffiMask;
120 #endif
121
122 return 0;
123 }
124
__sched_cpucount(size_t set_size,const cpu_set_t * set)125 int __sched_cpucount(size_t set_size, const cpu_set_t* set)
126 {
127 INT32 count = 0;
128 UINT32 i;
129
130 if ((set_size != sizeof(cpu_set_t)) || (set == NULL)) {
131 return 0;
132 }
133
134 for (i = 0; i < set_size / sizeof(__CPU_BITTYPE); i++) {
135 count += __builtin_popcountl(set->__bits[i]);
136 }
137
138 return count;
139 }
140
sched_yield()141 int sched_yield()
142 {
143 (void)LOS_TaskYield();
144 return 0;
145 }
146