• 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_rwlock_test.h"
32 #include "string.h"
33 
34 static const int WRITE_THREAD_COUNT = 5; // 5, set w thread count.
35 static const int READ_THREAD_COUNT = 5; // 5, set read thread count.
36 
37 static const int READ_LOOP_COUNT = 10; // 10, set read loop count.
38 static pthread_rwlock_t g_rwlockLock;
39 static const int TEST_DATA_SIZE = 100000; // 100000, set test data size.
40 static int g_rwlockData[TEST_DATA_SIZE];
41 static int g_rwlockMask;
42 static volatile int g_isWriting[WRITE_THREAD_COUNT];
43 static volatile int g_isReading[READ_THREAD_COUNT];
44 static volatile int g_isReadExit[READ_THREAD_COUNT];
45 static volatile int g_isWriteExit[WRITE_THREAD_COUNT];
46 static int g_writePar[WRITE_THREAD_COUNT];
47 static int g_readPar[READ_THREAD_COUNT];
48 
RwlockWait()49 static void RwlockWait()
50 {
51     int count;
52     int count1;
53     for (count = 0xFFFFFFF; count != 0; count--) {
54     }
55 }
56 
CheckReadThreadExit(void)57 static int CheckReadThreadExit(void)
58 {
59     int count = 0;
60 
61     for (int i = 0; i < READ_THREAD_COUNT; i++) {
62         if (g_isReadExit[i] == 5) { // 5, set read exit data.
63             count++;
64         }
65     }
66 
67     if (count == READ_THREAD_COUNT) {
68         return 0;
69     }
70 
71     return 1;
72 }
73 
CheckWriteThreadExit(void)74 static int CheckWriteThreadExit(void)
75 {
76     int count = 0;
77 
78     for (int i = 1; i < WRITE_THREAD_COUNT; i++) {
79         if (g_isWriteExit[i] == 6) { // 6, The current possible value of the variable.
80             count++;
81         }
82     }
83 
84     if (count == (WRITE_THREAD_COUNT - 1)) {
85         return 0;
86     }
87 
88     return 1;
89 }
90 
ThreadReadFunc(void * a)91 static void *ThreadReadFunc(void *a)
92 {
93     int ret;
94     int count = 0;
95     pthread_t thread = pthread_self();
96     int loop = READ_LOOP_COUNT;
97     int threadCount = *((int *)a);
98 
99     ret = pthread_detach(thread);
100     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
101 
102     while (loop > 0) {
103         SLEEP_AND_YIELD(1);
104         ret = pthread_rwlock_rdlock(&g_rwlockLock);
105         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
106         g_isReading[threadCount] = 1;
107 
108         for (count = 0; count < TEST_DATA_SIZE; count++) {
109             ICUNIT_GOTO_EQUAL(g_rwlockData[count], g_rwlockMask, g_rwlockData[count], EXIT);
110             for (int index = 0; index < WRITE_THREAD_COUNT; index++) {
111                 ICUNIT_GOTO_EQUAL(g_isWriting[index], 0, g_isWriting[index], EXIT);
112             }
113         }
114 
115         SLEEP_AND_YIELD(1);
116         g_isReading[threadCount] = 0;
117 
118         ret = pthread_rwlock_unlock(&g_rwlockLock);
119         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
120 
121         RwlockWait();
122         loop--;
123     }
124 
125     g_isReadExit[threadCount] = 5; // 5, set read exit data.
126 
127 EXIT:
128     return nullptr;
129 }
130 
ThreadWriteFunc1(void * a)131 static void *ThreadWriteFunc1(void *a)
132 {
133     int ret;
134     int count = 0;
135     int oldRwlockMask;
136     pthread_t thread = pthread_self();
137     int threadCount = *((int *)a);
138 
139     ret = pthread_detach(thread);
140     ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
141 
142     while (CheckReadThreadExit()) {
143         SLEEP_AND_YIELD(1);
144         ret = pthread_rwlock_wrlock(&g_rwlockLock);
145         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
146 
147         g_isWriting[threadCount] = 1;
148 
149         oldRwlockMask = g_rwlockMask;
150 
151         g_rwlockMask++;
152 
153         for (count = 0; count < TEST_DATA_SIZE; count++) {
154             ICUNIT_GOTO_EQUAL(g_rwlockData[count], oldRwlockMask, g_rwlockData[count], EXIT);
155             for (int i = 0; i < READ_THREAD_COUNT; i++) {
156                 ICUNIT_GOTO_EQUAL(g_isReading[i], 0, g_isReading[i], EXIT);
157             }
158             g_rwlockData[count] = g_rwlockMask;
159         }
160 
161         SLEEP_AND_YIELD(1);
162 
163         g_isWriting[threadCount] = 0;
164 
165         ret = pthread_rwlock_unlock(&g_rwlockLock);
166         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
167         SLEEP_AND_YIELD(1);
168     }
169 
170     g_isWriteExit[threadCount] = 6; // 6, The current possible value of the variable.
171 
172 EXIT:
173     return nullptr;
174 }
175 
ThreadWriteFunc(void * a)176 static void *ThreadWriteFunc(void *a)
177 {
178     int ret;
179     int count = 0;
180     int oldRwlockMask;
181     pthread_t thread = pthread_self();
182     int threadCount = *((int *)a);
183 
184     while (CheckReadThreadExit() || CheckWriteThreadExit()) {
185         SLEEP_AND_YIELD(1);
186         ret = pthread_rwlock_wrlock(&g_rwlockLock);
187         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
188 
189         g_isWriting[threadCount] = 1;
190 
191         oldRwlockMask = g_rwlockMask;
192 
193         g_rwlockMask++;
194 
195         for (count = 0; count < TEST_DATA_SIZE; count++) {
196             ICUNIT_GOTO_EQUAL(g_rwlockData[count], oldRwlockMask, g_rwlockData[count], EXIT);
197             for (int i = 0; i < READ_THREAD_COUNT; i++) {
198                 ICUNIT_GOTO_EQUAL(g_isReading[i], 0, g_isReading[i], EXIT);
199             }
200             g_rwlockData[count] = g_rwlockMask;
201         }
202 
203         SLEEP_AND_YIELD(1);
204 
205         g_isWriting[threadCount] = 0;
206 
207         ret = pthread_rwlock_unlock(&g_rwlockLock);
208         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
209 
210         SLEEP_AND_YIELD(1);
211     }
212 
213 EXIT:
214     return nullptr;
215 }
216 
PthreadRwlockTest(void)217 static int PthreadRwlockTest(void)
218 {
219     struct sched_param param = { 0 };
220     int ret;
221     void *res = nullptr;
222     int count = 0;
223     pthread_attr_t a = { 0 };
224     pthread_t thread = pthread_self();
225     pthread_t newPthread, newPthread1;
226     pthread_rwlockattr_t rwlock;
227     int index = 0;
228     int curThreadPri, curThreadPolicy;
229 
230     (void)memset_s((void *)g_rwlockData, sizeof(int) * TEST_DATA_SIZE, 0, sizeof(int) * TEST_DATA_SIZE);
231     g_rwlockMask = 0;
232 
233     pthread_rwlock_init(&g_rwlockLock, NULL);
234 
235     ret = pthread_getschedparam(pthread_self(), &curThreadPolicy, &param);
236     ICUNIT_ASSERT_EQUAL(ret, 0, -ret);
237 
238     curThreadPri = param.sched_priority;
239 
240     while (index < 2) { // 2, Number of cycles.
241         ret = pthread_attr_init(&a);
242         pthread_attr_setinheritsched(&a, PTHREAD_EXPLICIT_SCHED);
243         param.sched_priority = curThreadPri + 1;
244         pthread_attr_setschedparam(&a, &param);
245 
246         g_writePar[0] = 0;
247         ret = pthread_create(&newPthread1, &a, ThreadWriteFunc, &g_writePar[0]);
248         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
249 
250         count = 1;
251         while (count < WRITE_THREAD_COUNT) {
252             g_writePar[count] = count;
253             g_isWriting[count] = 0;
254             g_isWriteExit[count] = 0;
255             ret = pthread_create(&newPthread, &a, ThreadWriteFunc1, &g_writePar[count]);
256             ICUNIT_ASSERT_EQUAL(ret, 0, ret);
257             count++;
258         }
259 
260         count = 0;
261         while (count < READ_THREAD_COUNT) {
262             g_readPar[count] = count;
263             g_isReading[count] = 0;
264             g_isReadExit[count] = 0;
265             ret = pthread_create(&newPthread, &a, ThreadReadFunc, &g_readPar[count]);
266             ICUNIT_ASSERT_EQUAL(ret, 0, ret);
267             count++;
268         }
269 
270         ret = pthread_join(newPthread1, &res);
271         ICUNIT_ASSERT_EQUAL(ret, 0, ret);
272         index++;
273     }
274 
275     pthread_rwlock_destroy(&g_rwlockLock);
276     return 0;
277 }
278 
PthreadRwlockTest1(void)279 static int PthreadRwlockTest1(void)
280 {
281     PthreadRwlockTest();
282     exit(255); // 255, exit args.
283 }
284 
Testcase(void)285 static int Testcase(void)
286 {
287     int ret;
288     int status = 0;
289     pid_t pid = fork();
290     ICUNIT_ASSERT_WITHIN_EQUAL(pid, 0, 100000, pid); // 100000, assert that function Result is equal to this.
291     if (pid == 0) {
292         PthreadRwlockTest1();
293         exit(__LINE__);
294     }
295 
296     PthreadRwlockTest();
297 
298     ret = wait(&status);
299     status = WEXITSTATUS(status);
300     ICUNIT_ASSERT_EQUAL(ret, pid, ret);
301     ICUNIT_ASSERT_EQUAL(status, 255, status); // 255, assert that function Result is equal to this.
302     return 0;
303 }
304 
ItTestPthreadRwlock002(void)305 void ItTestPthreadRwlock002(void)
306 {
307     TEST_ADD_CASE("IT_POSIX_PTHREAD_RWLOCK_002", Testcase, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
308 }
309