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 #include "threads.h"
33 static const int TEST_LOOP = 5000;
34
35 static int g_testCnt = 0;
ThreadFunc(void * arg)36 static void *ThreadFunc(void *arg)
37 {
38 int count = 1;
39 while (g_testCnt < TEST_LOOP) {
40 if (g_testCnt != count) {
41 printf("%s %d g_testCnt should : %d but is : %d\n", __FUNCTION__, __LINE__, count, g_testCnt);
42 return NULL;
43 }
44 g_testCnt++;
45 count += 5; // total 5 pthreads.
46 thrd_yield();
47 }
48
49 g_testCnt = TEST_LOOP + 1000; // 1000, here set a special num.
50 return NULL;
51 }
52
ThreadFunc1(void * arg)53 static void *ThreadFunc1(void *arg)
54 {
55 int count = 2;
56 while (g_testCnt < TEST_LOOP) {
57 if (g_testCnt != count) {
58 printf("%s %d g_testCnt should : %d but is : %d\n", __FUNCTION__, __LINE__, count, g_testCnt);
59 return NULL;
60 }
61 g_testCnt++;
62 count += 5; // total 5 pthreads.
63 thrd_yield();
64 }
65
66 return NULL;
67 }
68
ThreadFunc2(void * arg)69 static void *ThreadFunc2(void *arg)
70 {
71 int count = 3;
72 while (g_testCnt < TEST_LOOP) {
73 if (g_testCnt != count) {
74 printf("%s %d g_testCnt should : %d but is : %d\n", __FUNCTION__, __LINE__, count, g_testCnt);
75 return NULL;
76 }
77 g_testCnt++;
78 count += 5; // total 5 pthreads.
79 thrd_yield();
80 }
81
82 return NULL;
83 }
84
ThreadFunc3(void * arg)85 static void *ThreadFunc3(void *arg)
86 {
87 int count = 4;
88 while (g_testCnt < TEST_LOOP) {
89 if (g_testCnt != count) {
90 printf("%s %d g_testCnt should : %d but is : %d\n", __FUNCTION__, __LINE__, count, g_testCnt);
91 return NULL;
92 }
93 g_testCnt++;
94 count += 5; // total 5 pthreads.
95 thrd_yield();
96 }
97
98 return NULL;
99 }
100
TestCase(void)101 static int TestCase(void)
102 {
103 int ret;
104 pthread_t tid, tid1, tid2, tid3;
105 pthread_attr_t attr = { 0 };
106 int scope = 0;
107 int threadPolicy = 0;
108 struct sched_param param = { 0 };
109
110 g_testCnt = 0;
111
112 ret = pthread_getschedparam(pthread_self(), &threadPolicy, ¶m);
113 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
114
115 ret = pthread_setschedparam(pthread_self(), SCHED_FIFO, ¶m);
116 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
117
118 ret = pthread_create(&tid, NULL, ThreadFunc, 0);
119 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
120
121 ret = pthread_create(&tid1, NULL, ThreadFunc1, 0);
122 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
123
124 ret = pthread_create(&tid2, NULL, ThreadFunc2, 0);
125 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
126
127 ret = pthread_create(&tid3, NULL, ThreadFunc3, 0);
128 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
129
130 while (g_testCnt < TEST_LOOP) {
131 g_testCnt++;
132 thrd_yield();
133 }
134
135 param.sched_priority -= 2; // 2, adjust the priority.
136 ret = pthread_setschedparam(tid, SCHED_FIFO, ¶m);
137 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
138
139 ICUNIT_GOTO_EQUAL(g_testCnt, TEST_LOOP + 1000, g_testCnt, EXIT); // 1000, here assert the special num.
140
141 ret = pthread_join(tid, NULL);
142 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
143 ret = pthread_join(tid1, NULL);
144 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
145 ret = pthread_join(tid2, NULL);
146 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
147 ret = pthread_join(tid3, NULL);
148 ICUNIT_ASSERT_EQUAL(ret, 0, ret);
149
150 param.sched_priority += 2; // 2, adjust the priority.
151 ret = pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
152 ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
153
154 EXIT:
155 return 0;
156 }
157
ItTestPthread014(void)158 void ItTestPthread014(void)
159 {
160 TEST_ADD_CASE("IT_POSIX_PTHREAD_014", TestCase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
161 }
162