• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <pthread.h>
17 #include "functionalext.h"
18 
Thread1(void)19 static void Thread1(void)
20 {
21     sleep(1);
22     int ret = pthread_setschedprio(pthread_self(), 10);
23     EXPECT_EQ("pthread_setschedprio_0100", ret, 0);
24 }
25 
Thread2(void)26 static void Thread2(void)
27 {
28     sleep(1);
29 
30     int ret = pthread_setschedprio(pthread_self(), 1000);
31     EXPECT_EQ("pthread_setschedprio_0100", ret, EINVAL);
32 }
33 
setthreadproperty(pthread_attr_t * attr,struct sched_param * param)34 static void setthreadproperty(pthread_attr_t *attr, struct sched_param *param)
35 {
36     int ret = pthread_attr_init(attr);
37     EXPECT_EQ("pthread_setschedprio_0100", ret, CMPFLAG);
38 
39     param->sched_priority = 51;
40     ret = pthread_attr_setschedpolicy(attr, SCHED_RR);
41     EXPECT_EQ("pthread_setschedprio_0100", ret, CMPFLAG);
42     ret = pthread_attr_setschedparam(attr, param);
43     EXPECT_EQ("pthread_setschedprio_0100", ret, CMPFLAG);
44 
45     ret = pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED);
46     EXPECT_EQ("pthread_setschedprio_0100", ret, CMPFLAG);
47 }
48 
49 /**
50  * @tc.name      : pthread_setschedprio_0100
51  * @tc.desc      : Set the thread properties, and judge the normal system of the function in the child thread
52  * @tc.level     : Level 0
53  */
pthread_setschedprio_0100(void)54 void pthread_setschedprio_0100(void)
55 {
56     pthread_t thread;
57     pthread_attr_t attr;
58     struct sched_param param;
59 
60     setthreadproperty(&attr, &param);
61     int ret = pthread_create(&thread, &attr, (void *)Thread1, NULL);
62     if (ret != 0) {
63         EXPECT_EQ("pthread_setschedprio_0100", ret, CMPFLAG);
64         return;
65     }
66 
67     pthread_join(thread, NULL);
68     pthread_attr_destroy(&attr);
69 }
70 
71 /**
72  * @tc.name      : pthread_setschedprio_0200
73  * @tc.desc      : Set the thread properties, and judge abnormal system of the function in the child thread
74  * @tc.level     : Level 2
75  */
pthread_setschedprio_0200(void)76 void pthread_setschedprio_0200(void)
77 {
78     pthread_t thread;
79     pthread_attr_t attr;
80     struct sched_param param;
81 
82     setthreadproperty(&attr, &param);
83     int ret = pthread_create(&thread, &attr, (void *)Thread2, NULL);
84     if (ret != 0) {
85         EXPECT_EQ("pthread_setschedprio_0200", ret, CMPFLAG);
86         return;
87     }
88 
89     pthread_join(thread, NULL);
90     pthread_attr_destroy(&attr);
91 }
92 
main(void)93 int main(void)
94 {
95     pthread_setschedprio_0100();
96     pthread_setschedprio_0200();
97 
98     return 0;
99 }
100