• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_pthreadTestCount = 0;
35 static int g_threadTestCount1 = 0;
36 
ThreadFunc5(void * ptr)37 static void *ThreadFunc5(void *ptr)
38 {
39     pid_t pid = getpid();
40     int ret;
41 
42     ICUNIT_GOTO_EQUAL(g_threadTestCount1, 2, g_threadTestCount1, EXIT); // 2, here assert the result.
43     g_threadTestCount1++;
44 
45     ret = pthread_detach(pthread_self());
46     ICUNIT_GOTO_EQUAL(ret, EINVAL, ret, EXIT);
47 
48 EXIT:
49     return NULL;
50 }
51 
ThreadFunc4(void * arg)52 static void *ThreadFunc4(void *arg)
53 {
54     pthread_t pthread = pthread_self();
55     int i = 0;
56     unsigned int ret;
57     pid_t pid = getpid();
58 
59     ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 9, g_pthreadTestCount, EXIT); // 9, here assert the result.
60     g_pthreadTestCount++;                                               // 10
61 
62     pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
63 
64     while (1) {
65         pthread_testcancel();
66         if (++i == 5) { // 5, in loop 5, set cancel state.
67             pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
68         }
69 
70         if (i == 10) { // 10, in loop 10, cancel pthread.
71             ret = pthread_cancel(pthread);
72         }
73     }
74     return reinterpret_cast<void *>(i);
75 
76 EXIT:
77     return NULL;
78 }
79 
ThreadFunc3(void * arg)80 static void *ThreadFunc3(void *arg)
81 {
82     pthread_t pthread = pthread_self();
83     int i = 0;
84     unsigned int ret;
85     pid_t pid = getpid();
86 
87     g_pthreadTestCount++; // 7
88 
89     pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
90 
91     while (1) {
92         pthread_testcancel();
93         if (++i == 5) { // 5, in loop 5, set cancel state.
94             pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
95         }
96 
97         if (i == 10) { // 10, in loop 10, cancel pthread.
98             ret = pthread_cancel(pthread);
99         }
100     }
101 
102     ICUNIT_GOTO_EQUAL(i, 10, i, EXIT); // 10, here assert the result.
103     return reinterpret_cast<void *>(i);
104 EXIT:
105     return NULL;
106 }
107 
ThreadFunc2(void * arg)108 static void *ThreadFunc2(void *arg)
109 {
110     pthread_t pthread = pthread_self();
111     pid_t pid = getpid();
112     unsigned int ret;
113 
114     ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 3, g_pthreadTestCount, EXIT); // 3, here assert the result.
115 
116     g_pthreadTestCount++; // 4
117 
118     ret = pthread_join(pthread_self(), 0);
119     ICUNIT_GOTO_EQUAL(ret, EINVAL, ret, EXIT);
120 
121     ret = pthread_detach(pthread_self());
122     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
123 
124 EXIT:
125     return NULL;
126 }
127 
ThreadFunc6(void * arg)128 static void *ThreadFunc6(void *arg)
129 {
130     pid_t pid = getpid();
131 
132     ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 12, g_pthreadTestCount, EXIT); // 12, here assert the result.
133     g_pthreadTestCount++;                                                // 13
134 
135     return reinterpret_cast<void *>(pthread_self());
136 
137 EXIT:
138     return NULL;
139 }
140 
ThreadFunc(void * arg)141 static void *ThreadFunc(void *arg)
142 {
143     unsigned int ret;
144     void *res = NULL;
145     pthread_attr_t a = { 0 };
146     pthread_t newPthread;
147     struct sched_param param = { 0 };
148     pid_t pid = getpid();
149     int curThreadPri, curThreadPolicy;
150 
151     ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 2, g_pthreadTestCount, EXIT); // 2, here assert the result.
152 
153     g_pthreadTestCount++; // 3
154 
155     ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, &param);
156     ICUNIT_GOTO_EQUAL(ret, 0, -ret, EXIT);
157 
158     curThreadPri = param.sched_priority;
159 
160     ret = pthread_attr_init(&a);
161     pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
162     param.sched_priority = curThreadPri - 1;
163     pthread_attr_setschedparam(&a, &param);
164     ret = pthread_create(&newPthread, &a, ThreadFunc2, 0);
165     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
166 
167 #ifdef LOSCFG_USER_TEST_SMP
168     usleep(1000 * 10 * 2); // 1000 * 10 * 2, for timing control.
169 #endif
170     ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 4, g_pthreadTestCount, EXIT); // 4, here assert the result.
171 
172     g_pthreadTestCount++; // 5
173 
174     ret = pthread_attr_init(&a);
175     pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
176     param.sched_priority = curThreadPri + 1;
177     pthread_attr_setschedparam(&a, &param);
178     ret = pthread_create(&newPthread, &a, ThreadFunc3, 0);
179     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
180 
181     g_pthreadTestCount++; // 6
182 
183     ret = pthread_join(newPthread, &res);
184     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
185 
186     ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 7, g_pthreadTestCount, EXIT); // 7, here assert the result.
187     g_pthreadTestCount++;                                               // 8
188 
189     ret = pthread_attr_init(&a);
190     pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
191     param.sched_priority = curThreadPri + 2; // 2, adjust the priority.
192     pthread_attr_setschedparam(&a, &param);
193     ret = pthread_create(&newPthread, &a, ThreadFunc4, 0);
194     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
195 
196     ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 8, g_pthreadTestCount, EXIT); // 8, here assert the result.
197     g_pthreadTestCount++;                                               // 9
198 
199     ret = pthread_join(newPthread, &res);
200     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
201 
202     ICUNIT_GOTO_EQUAL(g_pthreadTestCount, 10, g_pthreadTestCount, EXIT); // 10, here assert the result.
203     g_pthreadTestCount++;                                                // 11
204 
205 EXIT:
206     return NULL;
207 }
208 
PthreadTest001()209 int PthreadTest001()
210 {
211     struct sched_param param = { 0 };
212     int ret;
213     void *res = NULL;
214     pthread_attr_t a = { 0 };
215     pthread_t newPthread, newPthread1;
216     int count = 0xf0000;
217     g_threadTestCount1 = 0;
218     g_pthreadTestCount = 0;
219 
220     ret = pthread_getschedparam(pthread_self(), &g_currThreadPolicy, &param);
221     ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
222 
223     g_currThreadPri = param.sched_priority;
224 
225     g_pthreadTestCount++;
226 
227     ret = pthread_attr_init(&a);
228     pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
229     param.sched_priority = g_currThreadPri + 1;
230     pthread_attr_setschedparam(&a, &param);
231     pthread_attr_setschedpolicy(&a, g_currThreadPolicy);
232     ret = pthread_create(&newPthread, &a, ThreadFunc, 0);
233     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
234 
235     ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 1, g_pthreadTestCount);
236     g_pthreadTestCount++; // 2
237 
238     ret = pthread_create(&newPthread1, &a, ThreadFunc5, 0);
239     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
240 
241     g_threadTestCount1++;
242     ICUNIT_ASSERT_EQUAL(g_threadTestCount1, 1, g_threadTestCount1);
243 
244     ret = pthread_detach(newPthread1);
245     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
246 
247     g_threadTestCount1++;
248     ICUNIT_ASSERT_EQUAL(g_threadTestCount1, 2, g_threadTestCount1); // 2, here assert the result.
249     ret = pthread_join(newPthread, &res);
250     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
251 
252     ICUNIT_ASSERT_EQUAL(g_threadTestCount1, 3, g_threadTestCount1);  // 3, here assert the result.
253     ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 11, g_pthreadTestCount); // 11, here assert the result.
254     g_pthreadTestCount++;                                            // 12
255 
256     param.sched_priority = g_currThreadPri - 1;
257     pthread_attr_setschedparam(&a, &param);
258     ret = pthread_create(&newPthread1, &a, ThreadFunc6, 0);
259     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
260 
261 #ifdef LOSCFG_USER_TEST_SMP
262     usleep(1000 * 10 * 10); // 1000 * 10 * 10, for timing control.
263 #endif
264 
265     ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 13, g_pthreadTestCount); // 13, here assert the result.
266     g_pthreadTestCount++;                                            // 14
267 
268     ret = pthread_detach(newPthread1);
269     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
270 
271     ICUNIT_ASSERT_EQUAL(g_pthreadTestCount, 14, g_pthreadTestCount); // 14, here assert the result.
272 
273     return 0;
274 }
275 
ItTestPthread001(void)276 void ItTestPthread001(void)
277 {
278     TEST_ADD_CASE("IT_POSIX_PTHREAD_001", PthreadTest001, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
279 }
280