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 #include "it_pthread_test.h"
32
33 static int g_currThreadPri, g_currThreadPolicy;
34 static volatile int g_testCnt1 = 0;
35 static volatile int g_testCnt2 = 0;
36 static volatile int g_testPthredCount = 0;
37
ThreadWaitCount(int scount,volatile int * ent)38 static void ThreadWaitCount(int scount, volatile int *ent)
39 {
40 int count = 0xf0000;
41 while (*ent < scount) {
42 while (count > 0) {
43 count--;
44 }
45 count = 0xf0000;
46 (*ent)++;
47 }
48 }
49
ThreadFuncTest2(void * arg)50 static void *ThreadFuncTest2(void *arg)
51 {
52 (void)arg;
53 int ret;
54 struct sched_param param = { 0 };
55 int threadPolicy, threadPri;
56 int old;
57 const int waitCount1 = 20;
58 const int waitCount2 = 50;
59 g_testCnt2 = 0;
60
61 ThreadWaitCount(waitCount1 + g_testCnt2, &g_testCnt2);
62
63 while (g_testCnt1 < 10) { // 10, wait until g_testCnt1 >= 10.
64 printf("\r");
65 }
66
67 g_testPthredCount++;
68
69 param.sched_priority = g_currThreadPri + 1;
70 ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
71 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
72
73 old = g_testCnt1;
74 param.sched_priority = 0;
75 ret = pthread_getschedparam(pthread_self(), &threadPolicy, ¶m);
76 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
77
78 ICUNIT_GOTO_EQUAL(threadPolicy, SCHED_FIFO, threadPolicy, EXIT);
79 ICUNIT_GOTO_EQUAL(param.sched_priority, (g_currThreadPri + 1), param.sched_priority, EXIT);
80
81 ThreadWaitCount(waitCount2 + g_testCnt2, &g_testCnt2);
82
83 ICUNIT_GOTO_EQUAL(g_testCnt1, old, g_testCnt1, EXIT);
84
85 param.sched_priority = g_currThreadPri;
86 ret = pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
87 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
88
89 old = g_testCnt1;
90 param.sched_priority = 0;
91 ret = pthread_getschedparam(pthread_self(), &threadPolicy, ¶m);
92 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
93
94 ICUNIT_GOTO_EQUAL(threadPolicy, SCHED_RR, threadPolicy, EXIT);
95 ICUNIT_GOTO_EQUAL(param.sched_priority, g_currThreadPri, param.sched_priority, EXIT);
96
97 ThreadWaitCount(waitCount2 + g_testCnt2, &g_testCnt2);
98
99 ICUNIT_GOTO_EQUAL(g_testCnt1, old, g_testCnt1, EXIT);
100
101 ret = pthread_setschedprio(pthread_self(), g_currThreadPri + 1);
102 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
103
104 ThreadWaitCount(waitCount2 + g_testCnt2, &g_testCnt2);
105 ICUNIT_GOTO_EQUAL(g_testCnt2, 170, g_testCnt2, EXIT); // 170, here assert the result.
106 g_testPthredCount++;
107
108 EXIT:
109 return NULL;
110 }
111
ThreadFuncTest3(void * arg)112 static void *ThreadFuncTest3(void *arg)
113 {
114 (void)arg;
115 struct sched_param param = { 0 };
116 int threadPolicy, threadPri;
117 int ret;
118 int old = 0;
119 const int waitCount1 = 20;
120 const int waitCount2 = 50;
121
122 g_testCnt1 = 0;
123
124 ThreadWaitCount(waitCount1 + g_testCnt1, &g_testCnt1);
125
126 while (g_testCnt2 < 10) { // 10, wait until g_testCnt2 >= 10.
127 printf("\r");
128 }
129
130 g_testPthredCount++;
131 param.sched_priority = g_currThreadPri + 1;
132 ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
133 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
134
135 old = g_testCnt2;
136 param.sched_priority = 0;
137 ret = pthread_getschedparam(pthread_self(), &threadPolicy, ¶m);
138 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
139
140 ICUNIT_GOTO_EQUAL(threadPolicy, SCHED_FIFO, threadPolicy, EXIT);
141 ICUNIT_GOTO_EQUAL(param.sched_priority, (g_currThreadPri + 1), param.sched_priority, EXIT);
142
143 ThreadWaitCount(waitCount2 + g_testCnt1, &g_testCnt1);
144
145 ICUNIT_GOTO_EQUAL(g_testCnt2, old, g_testCnt2, EXIT);
146
147 param.sched_priority = g_currThreadPri;
148 ret = pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
149 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
150
151 old = g_testCnt2;
152 param.sched_priority = 0;
153 ret = pthread_getschedparam(pthread_self(), &threadPolicy, ¶m);
154 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
155
156 ICUNIT_GOTO_EQUAL(threadPolicy, SCHED_RR, threadPolicy, EXIT);
157 ICUNIT_GOTO_EQUAL(param.sched_priority, g_currThreadPri, param.sched_priority, EXIT);
158
159 ThreadWaitCount(waitCount2 + g_testCnt1, &g_testCnt1);
160
161 ICUNIT_GOTO_EQUAL(g_testCnt2, old, g_testCnt2, EXIT);
162 ICUNIT_GOTO_EQUAL(g_testCnt1, 120, g_testCnt1, EXIT); // 120, here assert the result.
163 g_testPthredCount++;
164
165 EXIT:
166 return NULL;
167 }
168
Testcase()169 static int Testcase()
170 {
171 struct sched_param param = { 0 };
172 int ret;
173 void *res = NULL;
174 pthread_attr_t a = { 0 };
175 pthread_t newPthread, newPthread1;
176
177 g_testPthredCount = 0;
178
179 ret = pthread_getschedparam(pthread_self(), &g_currThreadPolicy, ¶m);
180 ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
181
182 g_currThreadPri = param.sched_priority;
183
184 g_testPthredCount++;
185
186 ret = pthread_attr_init(&a);
187 param.sched_priority = g_currThreadPri + 1;
188 pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
189 pthread_attr_setschedparam(&a, ¶m);
190 ret = pthread_create(&newPthread, &a, ThreadFuncTest2, 0);
191 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
192
193 ICUNIT_ASSERT_EQUAL(g_testPthredCount, 1, g_testPthredCount);
194 g_testPthredCount++; // 2
195
196 ret = pthread_create(&newPthread1, &a, ThreadFuncTest3, 0);
197 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
198
199 ICUNIT_ASSERT_EQUAL(g_testPthredCount, 2, g_testPthredCount); // 2, here assert the result.
200 g_testPthredCount++; // 3
201
202 ret = pthread_join(newPthread, &res);
203 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
204
205 ret = pthread_join(newPthread1, &res);
206 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
207
208 ICUNIT_ASSERT_EQUAL(g_testPthredCount, 7, g_testPthredCount); // 7, here assert the result.
209
210 return 0;
211 }
212
ItTestPthread002(void)213 void ItTestPthread002(void)
214 {
215 TEST_ADD_CASE("IT_POSIX_PTHREAD_002", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
216 }
217