1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include <unistd.h>
17f6603c60Sopenharmony_ci#include <pthread.h>
18f6603c60Sopenharmony_ci#include <semaphore.h>
19f6603c60Sopenharmony_ci#include <sys/resource.h>
20f6603c60Sopenharmony_ci#include <gtest/gtest.h>
21f6603c60Sopenharmony_ci#include "log.h"
22f6603c60Sopenharmony_ci#include "utils.h"
23f6603c60Sopenharmony_ci#include "mt_utils.h"
24f6603c60Sopenharmony_ci#include "KernelConstants.h"
25f6603c60Sopenharmony_ci
26f6603c60Sopenharmony_ciusing namespace testing::ext;
27f6603c60Sopenharmony_ci// global variables used to communicate between threads
28f6603c60Sopenharmony_cistatic int g_policy = 0;
29f6603c60Sopenharmony_cistatic int g_prioriy = 0;
30f6603c60Sopenharmony_ci
31f6603c60Sopenharmony_ciclass PthreadSchedApiTest : public testing::Test {
32f6603c60Sopenharmony_ci};
33f6603c60Sopenharmony_ciclass PthreadAllPrioTest : public testing::TestWithParam<int> {
34f6603c60Sopenharmony_ci};
35f6603c60Sopenharmony_ci
36f6603c60Sopenharmony_ci/**
37f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_INHERIT_0100
38f6603c60Sopenharmony_ci * @tc.name     test the default value of inheritsched.
39f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
40f6603c60Sopenharmony_ci */
41f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrGetInheritsched, Function | MediumTest | Level1)
42f6603c60Sopenharmony_ci{
43f6603c60Sopenharmony_ci    pthread_attr_t attr;
44f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
45f6603c60Sopenharmony_ci
46f6603c60Sopenharmony_ci    int inheritsched = -1;
47f6603c60Sopenharmony_ci    int rt = pthread_attr_getinheritsched(&attr, &inheritsched);
48f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getinheritsched failed. errno=" << errno;
49f6603c60Sopenharmony_ci    EXPECT_EQ(inheritsched, PTHREAD_INHERIT_SCHED)
50f6603c60Sopenharmony_ci        << "check default inheritsched failed. expect PTHREAD_INHERIT_SCHED";
51f6603c60Sopenharmony_ci}
52f6603c60Sopenharmony_ci
53f6603c60Sopenharmony_ci/**
54f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_INHERIT_0200
55f6603c60Sopenharmony_ci * @tc.name     test set and get inheritsched.
56f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
57f6603c60Sopenharmony_ci */
58f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrSetInheritsched, Function | MediumTest | Level1)
59f6603c60Sopenharmony_ci{
60f6603c60Sopenharmony_ci    pthread_attr_t attr;
61f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
62f6603c60Sopenharmony_ci
63f6603c60Sopenharmony_ci    int rt = pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
64f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_setinheritsched failed. errno=" << errno;
65f6603c60Sopenharmony_ci    int inheritsched = -1;
66f6603c60Sopenharmony_ci    rt = pthread_attr_getinheritsched(&attr, &inheritsched);
67f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getinheritsched failed. errno=" << errno;
68f6603c60Sopenharmony_ci    EXPECT_EQ(inheritsched, PTHREAD_INHERIT_SCHED) << "check inheritsched failed";
69f6603c60Sopenharmony_ci
70f6603c60Sopenharmony_ci    rt = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
71f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_setinheritsched failed. errno=" << errno;
72f6603c60Sopenharmony_ci    inheritsched = -1;
73f6603c60Sopenharmony_ci    rt = pthread_attr_getinheritsched(&attr, &inheritsched);
74f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getinheritsched failed. errno=" << errno;
75f6603c60Sopenharmony_ci    EXPECT_EQ(inheritsched, PTHREAD_EXPLICIT_SCHED) << "check inheritsched failed";
76f6603c60Sopenharmony_ci}
77f6603c60Sopenharmony_ci
78f6603c60Sopenharmony_ci/**
79f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_INHERIT_0300
80f6603c60Sopenharmony_ci * @tc.name     pthread_attr_setinheritsched error test.
81f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
82f6603c60Sopenharmony_ci */
83f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrSetInheritschedError, Function | MediumTest | Level1)
84f6603c60Sopenharmony_ci{
85f6603c60Sopenharmony_ci    pthread_attr_t attr;
86f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
87f6603c60Sopenharmony_ci
88f6603c60Sopenharmony_ci    int n = -GetRandom(100);
89f6603c60Sopenharmony_ci    int rt = pthread_attr_setinheritsched(&attr, n);
90f6603c60Sopenharmony_ci    EXPECT_EQ(rt, EINVAL) << "pthread_attr_setinheritsched(" << n << ") should fail";
91f6603c60Sopenharmony_ci    n = 2 + GetRandom(100);
92f6603c60Sopenharmony_ci    rt = pthread_attr_setinheritsched(&attr, n);
93f6603c60Sopenharmony_ci    EXPECT_EQ(rt, EINVAL) << "pthread_attr_setinheritsched(" << n << ") should fail";
94f6603c60Sopenharmony_ci}
95f6603c60Sopenharmony_ci
96f6603c60Sopenharmony_ci/**
97f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCHEDPARAM_0100
98f6603c60Sopenharmony_ci * @tc.name     test the default value of sched_param.
99f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
100f6603c60Sopenharmony_ci */
101f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrGetSchedParam, Function | MediumTest | Level1)
102f6603c60Sopenharmony_ci{
103f6603c60Sopenharmony_ci    pthread_attr_t attr;
104f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
105f6603c60Sopenharmony_ci
106f6603c60Sopenharmony_ci    struct sched_param param = {0};
107f6603c60Sopenharmony_ci    int rt = pthread_attr_getschedparam(&attr, &param);
108f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getschedparam failed. errno=" << errno;
109f6603c60Sopenharmony_ci    EXPECT_EQ(param.sched_priority, DEFAULT_THREAD_PRIORITY) << "check default pthread priority failed";
110f6603c60Sopenharmony_ci}
111f6603c60Sopenharmony_ci
112f6603c60Sopenharmony_ci/**
113f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCHEDPARAM_0200
114f6603c60Sopenharmony_ci * @tc.name     test set and get sched param.
115f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
116f6603c60Sopenharmony_ci */
117f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrSetSchedParam, Function | MediumTest | Level1)
118f6603c60Sopenharmony_ci{
119f6603c60Sopenharmony_ci    pthread_attr_t attr;
120f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
121f6603c60Sopenharmony_ci
122f6603c60Sopenharmony_ci    struct sched_param param = {0};
123f6603c60Sopenharmony_ci    int rt = pthread_attr_getschedparam(&attr, &param);
124f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getschedparam failed. errno=" << errno;
125f6603c60Sopenharmony_ci
126f6603c60Sopenharmony_ci    param.sched_priority = 22;
127f6603c60Sopenharmony_ci    rt = pthread_attr_setschedparam(&attr, &param);
128f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_setschedparam failed. errno=" << errno;
129f6603c60Sopenharmony_ci
130f6603c60Sopenharmony_ci    rt = pthread_attr_getschedparam(&attr, &param);
131f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getschedparam failed. errno=" << errno;
132f6603c60Sopenharmony_ci    EXPECT_EQ(param.sched_priority, 22) << "check pthread priority failed";
133f6603c60Sopenharmony_ci}
134f6603c60Sopenharmony_ci
135f6603c60Sopenharmony_ci/**
136f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCHEDPARAM_0300
137f6603c60Sopenharmony_ci * @tc.name     pthread_attr_setschedparam error test.
138f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
139f6603c60Sopenharmony_ci */
140f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrSetSchedParamError, Function | MediumTest | Level1)
141f6603c60Sopenharmony_ci{
142f6603c60Sopenharmony_ci    pthread_attr_t attr;
143f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
144f6603c60Sopenharmony_ci
145f6603c60Sopenharmony_ci    struct sched_param param = {0};
146f6603c60Sopenharmony_ci    int rt = pthread_attr_getschedparam(&attr, &param);
147f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getschedparam failed. errno=" << errno;
148f6603c60Sopenharmony_ci
149f6603c60Sopenharmony_ci    param.sched_priority = LOWEST_USER_THREAD_PRIORITY + 1;
150f6603c60Sopenharmony_ci    rt = pthread_attr_setschedparam(&attr, &param);
151f6603c60Sopenharmony_ci    EXPECT_EQ(rt, EINVAL);
152f6603c60Sopenharmony_ci}
153f6603c60Sopenharmony_ci
154f6603c60Sopenharmony_ci/**
155f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCHEDPOLICY_0100
156f6603c60Sopenharmony_ci * @tc.name     test the default value of sched policy.
157f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
158f6603c60Sopenharmony_ci */
159f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrGetSchedPolicy, Function | MediumTest | Level1)
160f6603c60Sopenharmony_ci{
161f6603c60Sopenharmony_ci    pthread_attr_t attr;
162f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
163f6603c60Sopenharmony_ci
164f6603c60Sopenharmony_ci    int policy = -1;
165f6603c60Sopenharmony_ci    int rt = pthread_attr_getschedpolicy(&attr, &policy);
166f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getschedpolicy failed. errno=" << errno;
167f6603c60Sopenharmony_ci    EXPECT_EQ(policy, SCHED_RR) << "check default policy failed, expect SCHED_RR";
168f6603c60Sopenharmony_ci}
169f6603c60Sopenharmony_ci
170f6603c60Sopenharmony_ci/**
171f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCHEDPOLICY_0200
172f6603c60Sopenharmony_ci * @tc.name     test set and get sched policy.
173f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
174f6603c60Sopenharmony_ci */
175f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrSetSchedPolicy, Function | MediumTest | Level1)
176f6603c60Sopenharmony_ci{
177f6603c60Sopenharmony_ci    pthread_attr_t attr;
178f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
179f6603c60Sopenharmony_ci
180f6603c60Sopenharmony_ci    int rt = pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
181f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_setschedparam failed. errno=" << errno;
182f6603c60Sopenharmony_ci    int policy = -1;
183f6603c60Sopenharmony_ci    rt = pthread_attr_getschedpolicy(&attr, &policy);
184f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getschedpolicy failed. errno=" << errno;
185f6603c60Sopenharmony_ci    EXPECT_EQ(policy, SCHED_FIFO);
186f6603c60Sopenharmony_ci
187f6603c60Sopenharmony_ci    rt = pthread_attr_setschedpolicy(&attr, SCHED_RR);
188f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_setschedparam failed. errno=" << errno;
189f6603c60Sopenharmony_ci    rt = pthread_attr_getschedpolicy(&attr, &policy);
190f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getschedpolicy failed. errno=" << errno;
191f6603c60Sopenharmony_ci    EXPECT_EQ(policy, SCHED_RR);
192f6603c60Sopenharmony_ci}
193f6603c60Sopenharmony_ci
194f6603c60Sopenharmony_ci/**
195f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCHEDPOLICY_0300
196f6603c60Sopenharmony_ci * @tc.name     pthread_attr_setschedpolicy error test.
197f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
198f6603c60Sopenharmony_ci */
199f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrSetSchedPolicyError, Function | MediumTest | Level1)
200f6603c60Sopenharmony_ci{
201f6603c60Sopenharmony_ci    pthread_attr_t attr;
202f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
203f6603c60Sopenharmony_ci    int rt;
204f6603c60Sopenharmony_ci
205f6603c60Sopenharmony_ci    // SCHED_FIFO is 1, and SCHED_RR is 2
206f6603c60Sopenharmony_ci    const int testLoop = 7;
207f6603c60Sopenharmony_ci    int invalidPolicy[testLoop] = {SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_RESET_ON_FORK};
208f6603c60Sopenharmony_ci    invalidPolicy[4] = GetRandom(10000);
209f6603c60Sopenharmony_ci    invalidPolicy[5] = -GetRandom(10000) + 6;
210f6603c60Sopenharmony_ci    invalidPolicy[6] = GetRandom(10000) + 6;
211f6603c60Sopenharmony_ci    for (int i = 0; i < testLoop; i++) {
212f6603c60Sopenharmony_ci        rt = pthread_attr_setschedpolicy(&attr, invalidPolicy[i]);
213f6603c60Sopenharmony_ci        EXPECT_EQ(rt, EINVAL) << "errno check fail for policy=" << invalidPolicy[i];
214f6603c60Sopenharmony_ci    }
215f6603c60Sopenharmony_ci}
216f6603c60Sopenharmony_ci
217f6603c60Sopenharmony_ci/**
218f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCOPE_0100
219f6603c60Sopenharmony_ci * @tc.name     test the default value of sched scope.
220f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
221f6603c60Sopenharmony_ci */
222f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrGetScope, Function | MediumTest | Level1)
223f6603c60Sopenharmony_ci{
224f6603c60Sopenharmony_ci    pthread_attr_t attr;
225f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
226f6603c60Sopenharmony_ci
227f6603c60Sopenharmony_ci    int scope = -1;
228f6603c60Sopenharmony_ci    int rt = pthread_attr_getscope(&attr, &scope);
229f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getscope failed. errno=" << errno;
230f6603c60Sopenharmony_ci    EXPECT_EQ(scope, PTHREAD_SCOPE_PROCESS) << "check default scope failed. expect PTHREAD_SCOPE_PROCESS";
231f6603c60Sopenharmony_ci}
232f6603c60Sopenharmony_ci
233f6603c60Sopenharmony_ci/**
234f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCOPE_0200
235f6603c60Sopenharmony_ci * @tc.name     test set and get scope.
236f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
237f6603c60Sopenharmony_ci */
238f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testAttrSetScope, Function | MediumTest | Level1)
239f6603c60Sopenharmony_ci{
240f6603c60Sopenharmony_ci    pthread_attr_t attr;
241f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
242f6603c60Sopenharmony_ci
243f6603c60Sopenharmony_ci    int rt = pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM);
244f6603c60Sopenharmony_ci    EXPECT_EQ(rt, ENOTSUP); // liteos support PTHREAD_SCOPE_PROCESS only
245f6603c60Sopenharmony_ci
246f6603c60Sopenharmony_ci    rt = pthread_attr_setscope(&attr, -GetRandom(10000));
247f6603c60Sopenharmony_ci    EXPECT_EQ(rt, EINVAL);
248f6603c60Sopenharmony_ci
249f6603c60Sopenharmony_ci    rt = pthread_attr_setscope(&attr, GetRandom(10000) + 2);
250f6603c60Sopenharmony_ci    EXPECT_EQ(rt, EINVAL);
251f6603c60Sopenharmony_ci
252f6603c60Sopenharmony_ci    int scope = -1;
253f6603c60Sopenharmony_ci    rt = pthread_attr_getscope(&attr, &scope);
254f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0) << "pthread_attr_getscope failed. errno=" << errno;
255f6603c60Sopenharmony_ci    EXPECT_EQ(scope, PTHREAD_SCOPE_PROCESS) << "check scope failed. expect PTHREAD_SCOPE_PROCESS";
256f6603c60Sopenharmony_ci}
257f6603c60Sopenharmony_ci
258f6603c60Sopenharmony_civoid* ThreadFunc1(void* arg)
259f6603c60Sopenharmony_ci{
260f6603c60Sopenharmony_ci    sem_t *sem = (sem_t*)arg;
261f6603c60Sopenharmony_ci
262f6603c60Sopenharmony_ci    int policy;
263f6603c60Sopenharmony_ci    struct sched_param param = {0};
264f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_getschedparam(pthread_self(), &policy, &param), 0);
265f6603c60Sopenharmony_ci    EXPECT_EQ(policy, g_policy);
266f6603c60Sopenharmony_ci    EXPECT_EQ(param.sched_priority, g_prioriy);
267f6603c60Sopenharmony_ci
268f6603c60Sopenharmony_ci    LOG("wait main thread check this thread para...");
269f6603c60Sopenharmony_ci    sem_wait(sem);
270f6603c60Sopenharmony_ci    return nullptr;
271f6603c60Sopenharmony_ci}
272f6603c60Sopenharmony_ci/**
273f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PTHREAD_GETSCHEDPARAM_0100
274f6603c60Sopenharmony_ci * @tc.name     pthread_getschedparam basic test.
275f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
276f6603c60Sopenharmony_ci */
277f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testGetSchedParam, Function | MediumTest | Level1)
278f6603c60Sopenharmony_ci{
279f6603c60Sopenharmony_ci    sem_t sem;
280f6603c60Sopenharmony_ci    ASSERT_EQ(sem_init(&sem, 0, 0), 0) << "sem_init errno = " << errno;
281f6603c60Sopenharmony_ci
282f6603c60Sopenharmony_ci    pthread_t ptSub;
283f6603c60Sopenharmony_ci    pthread_attr_t attr;
284f6603c60Sopenharmony_ci    struct sched_param param = {0};
285f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
286f6603c60Sopenharmony_ci    g_policy = SCHED_FIFO;
287f6603c60Sopenharmony_ci    param.sched_priority = g_prioriy = GetRandom(LOWEST_USER_THREAD_PRIORITY);
288f6603c60Sopenharmony_ci
289f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED), 0);
290f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setschedpolicy(&attr, g_policy), 0);
291f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setschedparam(&attr, &param), 0);
292f6603c60Sopenharmony_ci    int rt = pthread_create(&ptSub, &attr, ThreadFunc1, (void*)&sem);
293f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0) << "pthread_create failed!";
294f6603c60Sopenharmony_ci
295f6603c60Sopenharmony_ci    LOG("check sub thread's para");
296f6603c60Sopenharmony_ci    int policy;
297f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_getschedparam(ptSub, &policy, &param), 0);
298f6603c60Sopenharmony_ci    EXPECT_EQ(policy, g_policy);
299f6603c60Sopenharmony_ci    EXPECT_EQ(param.sched_priority, g_prioriy);
300f6603c60Sopenharmony_ci
301f6603c60Sopenharmony_ci    LOGD("main:before post sem...");
302f6603c60Sopenharmony_ci    sem_post(&sem);
303f6603c60Sopenharmony_ci    LOGD("main:after post sem...");
304f6603c60Sopenharmony_ci    pthread_join(ptSub, nullptr);
305f6603c60Sopenharmony_ci}
306f6603c60Sopenharmony_ci
307f6603c60Sopenharmony_ci/**
308f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PTHREAD_SETSCHEDPARAM_0100
309f6603c60Sopenharmony_ci * @tc.name     pthread_setschedparam basic test.
310f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
311f6603c60Sopenharmony_ci */
312f6603c60Sopenharmony_ciHWTEST_P(PthreadAllPrioTest, testSetSchedParam, Function | MediumTest | Level1)
313f6603c60Sopenharmony_ci{
314f6603c60Sopenharmony_ci    pthread_t ptSub;
315f6603c60Sopenharmony_ci    pthread_t ptSelf = pthread_self();
316f6603c60Sopenharmony_ci    sem_t sem;
317f6603c60Sopenharmony_ci    ASSERT_EQ(sem_init(&sem, 0, 0), 0) << "sem_init errno = " << errno;
318f6603c60Sopenharmony_ci
319f6603c60Sopenharmony_ci    // change self sched para
320f6603c60Sopenharmony_ci    struct sched_param param = {0};
321f6603c60Sopenharmony_ci    int policy;
322f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_getschedparam(ptSelf, &policy, &param), 0);
323f6603c60Sopenharmony_ci    param.sched_priority = g_prioriy = GetParam();
324f6603c60Sopenharmony_ci    if (GetRandom(100)%2) {
325f6603c60Sopenharmony_ci        policy = g_policy = SCHED_RR;
326f6603c60Sopenharmony_ci    } else {
327f6603c60Sopenharmony_ci        policy = g_policy = SCHED_FIFO;
328f6603c60Sopenharmony_ci    }
329f6603c60Sopenharmony_ci    LOG("========= Test policy(%d) and prioriy(%d) =========", g_policy, g_prioriy);
330f6603c60Sopenharmony_ci
331f6603c60Sopenharmony_ci    int rt = pthread_setschedparam(ptSelf, policy, &param);
332f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0) << "pthread_setschedparam failed!";
333f6603c60Sopenharmony_ci
334f6603c60Sopenharmony_ci    LOG("create sub thread");
335f6603c60Sopenharmony_ci    pthread_attr_t attr;
336f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
337f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED), 0);
338f6603c60Sopenharmony_ci    rt = pthread_create(&ptSub, &attr, ThreadFunc1, (void*)&sem);
339f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0) << "pthread_create failed!";
340f6603c60Sopenharmony_ci
341f6603c60Sopenharmony_ci    LOG("check self and sub thread's para");
342f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_getschedparam(ptSelf, &policy, &param), 0);
343f6603c60Sopenharmony_ci    EXPECT_EQ(policy, g_policy);
344f6603c60Sopenharmony_ci    EXPECT_EQ(param.sched_priority, g_prioriy);
345f6603c60Sopenharmony_ci
346f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_getschedparam(ptSub, &policy, &param), 0);
347f6603c60Sopenharmony_ci    EXPECT_EQ(policy, g_policy);
348f6603c60Sopenharmony_ci    EXPECT_EQ(param.sched_priority, g_prioriy);
349f6603c60Sopenharmony_ci
350f6603c60Sopenharmony_ci    LOGD("main:before post sem...");
351f6603c60Sopenharmony_ci    sem_post(&sem);
352f6603c60Sopenharmony_ci    LOGD("main:after post sem...");
353f6603c60Sopenharmony_ci    pthread_join(ptSub, nullptr);
354f6603c60Sopenharmony_ci
355f6603c60Sopenharmony_ci
356f6603c60Sopenharmony_ci    LOG("restore...");
357f6603c60Sopenharmony_ci    policy = SCHED_RR;
358f6603c60Sopenharmony_ci    param.sched_priority = DEFAULT_THREAD_PRIORITY;
359f6603c60Sopenharmony_ci    rt = pthread_setschedparam(ptSelf, policy, &param);
360f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0) << "pthread_setschedparam failed!";
361f6603c60Sopenharmony_ci}
362f6603c60Sopenharmony_ciINSTANTIATE_TEST_CASE_P(PthreadSchedApiTest, PthreadAllPrioTest,
363f6603c60Sopenharmony_ci    testing::Range(HIGHEST_USER_THREAD_PRIORITY, LOWEST_USER_THREAD_PRIORITY + 1));
364f6603c60Sopenharmony_ci
365f6603c60Sopenharmony_civoid* ThrdFuncForSetSchedParamTest(void* arg)
366f6603c60Sopenharmony_ci{
367f6603c60Sopenharmony_ci    sem_t *sem = (sem_t*)arg;
368f6603c60Sopenharmony_ci    pthread_t ptSelf = pthread_self();
369f6603c60Sopenharmony_ci    int rt, policy;
370f6603c60Sopenharmony_ci    struct sched_param param = {0};
371f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_getschedparam(ptSelf, &policy, &param), 0);
372f6603c60Sopenharmony_ci
373f6603c60Sopenharmony_ci    LOG("invalid policy test:");
374f6603c60Sopenharmony_ci    const int testLoop = 7;
375f6603c60Sopenharmony_ci    int invalidPolicy[testLoop] = {
376f6603c60Sopenharmony_ci        SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_DEADLINE, SCHED_RESET_ON_FORK,
377f6603c60Sopenharmony_ci        -(int)GetRandom(10000), (int)GetRandom(10000) + SCHED_DEADLINE
378f6603c60Sopenharmony_ci    };
379f6603c60Sopenharmony_ci    for (int i = 0; i < testLoop; i++) {
380f6603c60Sopenharmony_ci        rt = pthread_setschedparam(ptSelf, invalidPolicy[i], &param);
381f6603c60Sopenharmony_ci        EXPECT_EQ(rt, EINVAL) << "pthread_setschedparam should fail for policy=" << invalidPolicy[i];
382f6603c60Sopenharmony_ci    }
383f6603c60Sopenharmony_ci
384f6603c60Sopenharmony_ci    LOG("invalid 'priority' test:");
385f6603c60Sopenharmony_ci    param.sched_priority = HIGHEST_USER_THREAD_PRIORITY - 1;
386f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_setschedparam(ptSelf, SCHED_RR, &param), EINVAL);
387f6603c60Sopenharmony_ci    param.sched_priority = LOWEST_USER_THREAD_PRIORITY + 1;
388f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_setschedparam(ptSelf, SCHED_RR, &param), EINVAL);
389f6603c60Sopenharmony_ci
390f6603c60Sopenharmony_ci    LOG("tell main thread check this thread para...");
391f6603c60Sopenharmony_ci    sem_post(sem);
392f6603c60Sopenharmony_ci    Msleep(100);
393f6603c60Sopenharmony_ci    LOG("thread exit...");
394f6603c60Sopenharmony_ci    return nullptr;
395f6603c60Sopenharmony_ci}
396f6603c60Sopenharmony_ci/**
397f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PTHREAD_SETSCHEDPARAM_0200
398f6603c60Sopenharmony_ci * @tc.name     pthread_setschedparam api error test.
399f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
400f6603c60Sopenharmony_ci */
401f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testSetSchedParamError, Function | MediumTest | Level3)
402f6603c60Sopenharmony_ci{
403f6603c60Sopenharmony_ci    sem_t sem;
404f6603c60Sopenharmony_ci    ASSERT_EQ(sem_init(&sem, 0, 0), 0) << "sem_init errno = " << errno;
405f6603c60Sopenharmony_ci
406f6603c60Sopenharmony_ci    pthread_t ptSub;
407f6603c60Sopenharmony_ci    pthread_attr_t attr;
408f6603c60Sopenharmony_ci    struct sched_param param = {0};
409f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
410f6603c60Sopenharmony_ci    g_policy = SCHED_FIFO;
411f6603c60Sopenharmony_ci    param.sched_priority = g_prioriy = GetRandom(LOWEST_USER_THREAD_PRIORITY);
412f6603c60Sopenharmony_ci
413f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED), 0);
414f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setschedpolicy(&attr, g_policy), 0);
415f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setschedparam(&attr, &param), 0);
416f6603c60Sopenharmony_ci    int rt = pthread_create(&ptSub, &attr, ThrdFuncForSetSchedParamTest, (void*)&sem);
417f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0) << "pthread_create failed!";
418f6603c60Sopenharmony_ci
419f6603c60Sopenharmony_ci    LOGD("main:wait sub-thread...");
420f6603c60Sopenharmony_ci    sem_wait(&sem);
421f6603c60Sopenharmony_ci    LOG("check sub thread's para doesn't changed");
422f6603c60Sopenharmony_ci    int policy;
423f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_getschedparam(ptSub, &policy, &param), 0);
424f6603c60Sopenharmony_ci    EXPECT_EQ(policy, g_policy);
425f6603c60Sopenharmony_ci    EXPECT_EQ(param.sched_priority, g_prioriy);
426f6603c60Sopenharmony_ci
427f6603c60Sopenharmony_ci    pthread_join(ptSub, nullptr);
428f6603c60Sopenharmony_ci}
429f6603c60Sopenharmony_ci
430f6603c60Sopenharmony_civoid* ThrdFuncForSetSchedPrioTest(void* arg)
431f6603c60Sopenharmony_ci{
432f6603c60Sopenharmony_ci    sem_t *sem = (sem_t*)arg;
433f6603c60Sopenharmony_ci    pthread_t ptSelf = pthread_self();
434f6603c60Sopenharmony_ci
435f6603c60Sopenharmony_ci    LOG("invalid 'priority' test:");
436f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_setschedprio(ptSelf, HIGHEST_USER_THREAD_PRIORITY - 1), EINVAL);
437f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_setschedprio(ptSelf, LOWEST_USER_THREAD_PRIORITY + 1), EINVAL);
438f6603c60Sopenharmony_ci
439f6603c60Sopenharmony_ci    LOG("valid 'priority' test:");
440f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_setschedprio(ptSelf, LOWEST_USER_THREAD_PRIORITY), 0);
441f6603c60Sopenharmony_ci
442f6603c60Sopenharmony_ci    LOG("tell main thread check this thread para...");
443f6603c60Sopenharmony_ci    sem_post(sem);
444f6603c60Sopenharmony_ci    Msleep(100);
445f6603c60Sopenharmony_ci    LOG("thread exit...");
446f6603c60Sopenharmony_ci    return nullptr;
447f6603c60Sopenharmony_ci}
448f6603c60Sopenharmony_ci/**
449f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PTHREAD_SETSCHEDPRIO_0100
450f6603c60Sopenharmony_ci * @tc.name     pthread_setschedprio api basic test.
451f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
452f6603c60Sopenharmony_ci */
453f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testSetSchedPrio, Function | MediumTest | Level1)
454f6603c60Sopenharmony_ci{
455f6603c60Sopenharmony_ci    sem_t sem;
456f6603c60Sopenharmony_ci    ASSERT_EQ(sem_init(&sem, 0, 0), 0) << "sem_init errno = " << errno;
457f6603c60Sopenharmony_ci
458f6603c60Sopenharmony_ci    pthread_t ptSub;
459f6603c60Sopenharmony_ci    pthread_attr_t attr;
460f6603c60Sopenharmony_ci    struct sched_param param = {0};
461f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_attr_init(&attr), 0);
462f6603c60Sopenharmony_ci    g_policy = SCHED_RR;
463f6603c60Sopenharmony_ci    param.sched_priority = g_prioriy = HIGHEST_USER_THREAD_PRIORITY;
464f6603c60Sopenharmony_ci
465f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED), 0);
466f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setschedpolicy(&attr, g_policy), 0);
467f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setschedparam(&attr, &param), 0);
468f6603c60Sopenharmony_ci    int rt = pthread_create(&ptSub, &attr, ThrdFuncForSetSchedPrioTest, (void*)&sem);
469f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0) << "pthread_create failed!";
470f6603c60Sopenharmony_ci
471f6603c60Sopenharmony_ci    LOGD("main:wait sub-thread...");
472f6603c60Sopenharmony_ci    sem_wait(&sem);
473f6603c60Sopenharmony_ci    LOG("check sub thread's priority has changed");
474f6603c60Sopenharmony_ci    int policy;
475f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_getschedparam(ptSub, &policy, &param), 0);
476f6603c60Sopenharmony_ci    EXPECT_EQ(policy, g_policy);
477f6603c60Sopenharmony_ci    EXPECT_EQ(param.sched_priority, LOWEST_USER_THREAD_PRIORITY);
478f6603c60Sopenharmony_ci    pthread_join(ptSub, nullptr);
479f6603c60Sopenharmony_ci}
480f6603c60Sopenharmony_ci
481f6603c60Sopenharmony_ci// second
482f6603c60Sopenharmony_civoid *ThreadTestFifoSched1(void *arg)
483f6603c60Sopenharmony_ci{
484f6603c60Sopenharmony_ci    sem_t *sem = (sem_t*)arg;
485f6603c60Sopenharmony_ci    EXPECT_EQ(sem_wait(sem), 0) << "sem_wait errno = " << errno;
486f6603c60Sopenharmony_ci    CheckStep(3);
487f6603c60Sopenharmony_ci    LOG("> This is ThreadSched1");
488f6603c60Sopenharmony_ci    return arg;
489f6603c60Sopenharmony_ci}
490f6603c60Sopenharmony_ci
491f6603c60Sopenharmony_ci// first
492f6603c60Sopenharmony_civoid *ThreadTestFifoSched2(void *arg)
493f6603c60Sopenharmony_ci{
494f6603c60Sopenharmony_ci    sem_t *sem = (sem_t*)arg;
495f6603c60Sopenharmony_ci    EXPECT_EQ(sem_wait(sem), 0) << "sem_wait errno = " << errno;
496f6603c60Sopenharmony_ci    CheckStep(2);
497f6603c60Sopenharmony_ci    LOG("> This is ThreadSched2");
498f6603c60Sopenharmony_ci    return arg;
499f6603c60Sopenharmony_ci}
500f6603c60Sopenharmony_ci
501f6603c60Sopenharmony_ci/**
502f6603c60Sopenharmony_ci * @tc.number   SUB_KERNEL_SCHED_API_PTHREAD_SCHED_ALL_0100
503f6603c60Sopenharmony_ci * @tc.name     test thread high ang low priority function
504f6603c60Sopenharmony_ci * @tc.desc     [C- SOFTWARE -0200]
505f6603c60Sopenharmony_ci */
506f6603c60Sopenharmony_ciHWTEST_F(PthreadSchedApiTest, testSchedFifo, Function | MediumTest | Level3)
507f6603c60Sopenharmony_ci{
508f6603c60Sopenharmony_ci    pthread_t tid1;
509f6603c60Sopenharmony_ci    pthread_t tid2;
510f6603c60Sopenharmony_ci    int param;
511f6603c60Sopenharmony_ci    pthread_attr_t attr;
512f6603c60Sopenharmony_ci    struct sched_param schedParam = {0};
513f6603c60Sopenharmony_ci    sem_t sem;
514f6603c60Sopenharmony_ci    CheckStep(1);
515f6603c60Sopenharmony_ci
516f6603c60Sopenharmony_ci    ASSERT_EQ(sem_init(&sem, 0, 0), 0) << "> sem_init errno = " << errno;
517f6603c60Sopenharmony_ci
518f6603c60Sopenharmony_ci    // low
519f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_init(&attr), 0);
520f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED), 0) << "> return errno";
521f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setschedpolicy(&attr, SCHED_FIFO), 0) << "> return errno";
522f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_getschedpolicy(&attr, &param), 0);
523f6603c60Sopenharmony_ci    EXPECT_EQ(param, SCHED_FIFO);
524f6603c60Sopenharmony_ci
525f6603c60Sopenharmony_ci    schedParam.sched_priority = 22;
526f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setschedparam(&attr, &schedParam), 0) << "> return errno";
527f6603c60Sopenharmony_ci
528f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_create(&tid1, &attr, ThreadTestFifoSched1, (void*)&sem), 0) << "> return errno";
529f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_destroy(&attr), 0);
530f6603c60Sopenharmony_ci
531f6603c60Sopenharmony_ci    // high
532f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_init(&attr), 0);
533f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED), 0) << "> return errno";
534f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setschedpolicy(&attr, SCHED_FIFO), 0) << "> return errno";
535f6603c60Sopenharmony_ci
536f6603c60Sopenharmony_ci    schedParam.sched_priority = 21;
537f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_setschedparam(&attr, &schedParam), 0) << "> return errno";
538f6603c60Sopenharmony_ci
539f6603c60Sopenharmony_ci    ASSERT_EQ(pthread_create(&tid2, &attr, ThreadTestFifoSched2, (void*)&sem), 0) << "> return errno";
540f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_attr_destroy(&attr), 0);
541f6603c60Sopenharmony_ci
542f6603c60Sopenharmony_ci    Msleep(10);
543f6603c60Sopenharmony_ci    EXPECT_EQ(sem_post(&sem), 0) << "sem_post errno = " << errno;
544f6603c60Sopenharmony_ci    Msleep(10);
545f6603c60Sopenharmony_ci    EXPECT_EQ(sem_post(&sem), 0) << "sem_post errno = " << errno;
546f6603c60Sopenharmony_ci
547f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_join(tid1, nullptr), 0) << "> return errno";
548f6603c60Sopenharmony_ci    EXPECT_EQ(pthread_join(tid2, nullptr), 0) << "> return errno";
549f6603c60Sopenharmony_ci
550f6603c60Sopenharmony_ci    EXPECT_EQ(CheckStep(4), 0x1234Ull);    // make sure ThreadTestFifoSched2 ahead
551f6603c60Sopenharmony_ci}