• 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 #include <errno.h>
16 #include <pthread.h>
17 #include "pthread_util.h"
18 
19 static pthread_rwlock_t w_rwlock1;
20 static pthread_rwlock_t w_rwlock2;
21 static pthread_rwlock_t w_rwlock3;
22 static pthread_rwlock_t w_rwlock4;
23 static pthread_rwlock_t w_rwlock5;
24 static pthread_rwlock_t w_rwlock6;
25 
RwlockClockRealTimeOut1(void * arg)26 static void *RwlockClockRealTimeOut1(void *arg)
27 {
28     EXPECT_EQ(pthread_rwlock_wrlock(&w_rwlock1), 0);
29     Msleep(SLEEP_50_MS);
30     Msleep(SLEEP_100_MS);
31     EXPECT_EQ(pthread_rwlock_unlock(&w_rwlock1), 0);
32     return arg;
33 }
34 
RwlockClockRealTimeOut2(void * arg)35 static void *RwlockClockRealTimeOut2(void *arg)
36 {
37     struct timespec ts = {0};
38     struct timespec tsNow = {0};
39     Msleep(SLEEP_20_MS);
40     GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_REALTIME);
41     EXPECT_EQ(pthread_rwlock_clockwrlock(&w_rwlock1, CLOCK_REALTIME, &ts), ETIMEDOUT);
42     clock_gettime(CLOCK_REALTIME, &tsNow);
43     int timeDiff = GetTimeDiff(tsNow, ts);
44     EXPECT_GE(timeDiff, 0);
45     EXPECT_LE(timeDiff, SLEEP_20_MS);
46     return arg;
47 }
48 
RwlockClockRealTimeWait1(void * arg)49 static void *RwlockClockRealTimeWait1(void *arg)
50 {
51     EXPECT_EQ(pthread_rwlock_wrlock(&w_rwlock2), 0);
52     Msleep(SLEEP_50_MS);
53     EXPECT_EQ(pthread_rwlock_unlock(&w_rwlock2), 0);
54     return arg;
55 }
56 
RwlockClockRealTimeWait2(void * arg)57 static void *RwlockClockRealTimeWait2(void *arg)
58 {
59     struct timespec ts = {0};
60     clockid_t clock_id = CLOCK_REALTIME;
61     Msleep(SLEEP_20_MS);
62     GetDelayedTimeByClockid(&ts, SLEEP_100_MS, clock_id);
63     EXPECT_EQ(pthread_rwlock_clockwrlock(&w_rwlock2, clock_id, &ts), 0);
64     EXPECT_EQ(pthread_rwlock_unlock(&w_rwlock2), 0);
65     return arg;
66 }
67 
RwlockClockMonotonicTimeOut1(void * arg)68 static void *RwlockClockMonotonicTimeOut1(void *arg)
69 {
70     EXPECT_EQ(pthread_rwlock_wrlock(&w_rwlock5), 0);
71     Msleep(SLEEP_50_MS);
72     Msleep(SLEEP_100_MS);
73     EXPECT_EQ(pthread_rwlock_unlock(&w_rwlock5), 0);
74     return arg;
75 }
76 
RwlockClockMonotonicTimeOut2(void * arg)77 static void *RwlockClockMonotonicTimeOut2(void *arg)
78 {
79     struct timespec ts = {0};
80     struct timespec tsNow = {0};
81     Msleep(SLEEP_20_MS);
82     GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_MONOTONIC);
83     EXPECT_EQ(pthread_rwlock_clockwrlock(&w_rwlock5, CLOCK_MONOTONIC, &ts), ETIMEDOUT);
84     clock_gettime(CLOCK_MONOTONIC, &tsNow);
85     int timeDiff = GetTimeDiff(tsNow, ts);
86     EXPECT_GE(timeDiff, 0);
87     EXPECT_LE(timeDiff, SLEEP_20_MS);
88     return arg;
89 }
90 
RwlockClockMonotonicTimeWait1(void * arg)91 static void *RwlockClockMonotonicTimeWait1(void *arg)
92 {
93     EXPECT_EQ(pthread_rwlock_wrlock(&w_rwlock6), 0);
94     Msleep(SLEEP_50_MS);
95     EXPECT_EQ(pthread_rwlock_unlock(&w_rwlock6), 0);
96     return arg;
97 }
98 
RwlockClockMonotonicTimeWait2(void * arg)99 static void *RwlockClockMonotonicTimeWait2(void *arg)
100 {
101     struct timespec ts = {0};
102     Msleep(SLEEP_20_MS);
103     GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_MONOTONIC);
104     EXPECT_EQ(pthread_rwlock_clockwrlock(&w_rwlock6, CLOCK_MONOTONIC, &ts), 0);
105     EXPECT_EQ(pthread_rwlock_unlock(&w_rwlock6), 0);
106     return arg;
107 }
108 
RwlockMonotonicTime1(void * arg)109 static void *RwlockMonotonicTime1(void *arg)
110 {
111     EXPECT_EQ(pthread_rwlock_wrlock(&w_rwlock3), 0);
112     Msleep(SLEEP_50_MS);
113     Msleep(SLEEP_100_MS);
114     EXPECT_EQ(pthread_rwlock_unlock(&w_rwlock3), 0);
115     return arg;
116 }
117 
RwlockMonotonicTime2(void * arg)118 static void *RwlockMonotonicTime2(void *arg)
119 {
120     struct timespec ts = {0};
121     struct timespec tsNow = {0};
122     Msleep(SLEEP_20_MS);
123     GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_MONOTONIC);
124     EXPECT_EQ(pthread_rwlock_timedwrlock_monotonic_np(&w_rwlock3, &ts), ETIMEDOUT);
125     clock_gettime(CLOCK_MONOTONIC, &tsNow);
126     int timeDiff = GetTimeDiff(tsNow, ts);
127     EXPECT_GE(timeDiff, 0);
128     EXPECT_LE(timeDiff, SLEEP_20_MS);
129     return arg;
130 }
131 
RwlockMonotonicTime3(void * arg)132 static void *RwlockMonotonicTime3(void *arg)
133 {
134     EXPECT_EQ(pthread_rwlock_wrlock(&w_rwlock4), 0);
135     Msleep(SLEEP_50_MS);
136     EXPECT_EQ(pthread_rwlock_unlock(&w_rwlock4), 0);
137     return arg;
138 }
139 
RwlockMonotonicTime4(void * arg)140 static void *RwlockMonotonicTime4(void *arg)
141 {
142     struct timespec ts = {0};
143     Msleep(SLEEP_20_MS);
144     GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_MONOTONIC);
145     EXPECT_EQ(pthread_rwlock_timedwrlock_monotonic_np(&w_rwlock4, &ts), 0);
146     EXPECT_EQ(pthread_rwlock_unlock(&w_rwlock4), 0);
147     return arg;
148 }
149 
150 /**
151  * @tc.number    : pthread_rwlock_timedwrlock_0010
152  * @tc.desc      : Test whether all the interfaces to be used are normal
153  * @tc.level     : Level 0
154  */
155 
pthread_rwlock_timedwrlock_0010(void)156 void pthread_rwlock_timedwrlock_0010(void)
157 {
158     pthread_rwlock_t w;
159     struct timespec ts = {0};
160     EXPECT_EQ(clock_gettime(CLOCK_MONOTONIC, &ts), 0);
161     EXPECT_EQ(pthread_rwlock_init(&w, NULL), 0);
162     EXPECT_EQ(pthread_rwlock_wrlock(&w), 0);
163     EXPECT_EQ(pthread_rwlock_timedwrlock_monotonic_np(&w, &ts), ETIMEDOUT);
164     EXPECT_EQ(pthread_rwlock_unlock(&w), 0);
165     EXPECT_EQ(pthread_rwlock_destroy(&w), 0);
166 }
167 
168 /**
169  * @tc.number    : pthread_rwlock_timedwrlock_0020
170  * @tc.desc      : Test the case of pthread_cond_clockwait no timeout by CLOCK_REALTIME
171  * @tc.level     : Level 0
172  */
pthread_rwlock_timedwrlock_0020(void)173 void pthread_rwlock_timedwrlock_0020(void)
174 {
175     pthread_rwlock_t w;
176     struct timespec ts = {0};
177     clockid_t clock_id = CLOCK_REALTIME;
178     EXPECT_EQ(clock_gettime(CLOCK_REALTIME, &ts), 0);
179     EXPECT_EQ(pthread_rwlock_init(&w, NULL), 0);
180     EXPECT_EQ(pthread_rwlock_wrlock(&w), 0);
181     GetDelayedTimeByClockid(&ts, SLEEP_100_MS, clock_id);
182     EXPECT_EQ(pthread_rwlock_clockwrlock(&w, clock_id, &ts), ETIMEDOUT);
183     EXPECT_EQ(pthread_rwlock_unlock(&w), 0);
184     EXPECT_EQ(pthread_rwlock_destroy(&w), 0);
185 }
186 
187 /**
188  * @tc.number    : pthread_rwlock_timedwrlock_0030
189  * @tc.desc      : Test the case of pthread_cond_clockwait no timeout by CLOCK_REALTIME
190  * @tc.level     : Level 0
191  */
pthread_rwlock_timedwrlock_0030(void)192 void pthread_rwlock_timedwrlock_0030(void)
193 {
194     pthread_rwlock_t w;
195     struct timespec ts = {0};
196     clockid_t clock_id = CLOCK_MONOTONIC;
197     EXPECT_EQ(clock_gettime(CLOCK_MONOTONIC, &ts), 0);
198     EXPECT_EQ(pthread_rwlock_init(&w, NULL), 0);
199     EXPECT_EQ(pthread_rwlock_wrlock(&w), 0);
200     GetDelayedTimeByClockid(&ts, SLEEP_100_MS, clock_id);
201     EXPECT_EQ(pthread_rwlock_clockwrlock(&w, clock_id, &ts), ETIMEDOUT);
202     EXPECT_EQ(pthread_rwlock_unlock(&w), 0);
203     EXPECT_EQ(pthread_rwlock_destroy(&w), 0);
204 }
205 
206 /**
207  * @tc.number    : pthread_rwlock_timedwrlock_0040
208  * @tc.desc      : Test the case of pthread_cond_clockwait no timeout by CLOCK_REALTIME
209  * @tc.level     : Level 2
210  */
pthread_rwlock_timedwrlock_0040(void)211 void pthread_rwlock_timedwrlock_0040(void)
212 {
213     pthread_rwlock_t w;
214     struct timespec ts = {0};
215     clockid_t clock_id = CLOCK_PROCESS_CPUTIME_ID;
216     EXPECT_EQ(pthread_rwlock_init(&w, NULL), 0);
217     EXPECT_EQ(pthread_rwlock_wrlock(&w), 0);
218     EXPECT_EQ(pthread_rwlock_clockwrlock(&w, clock_id, &ts), EINVAL);
219     EXPECT_EQ(pthread_rwlock_unlock(&w), 0);
220     EXPECT_EQ(pthread_rwlock_destroy(&w), 0);
221 }
222 
223 /**
224  * @tc.number    : pthread_rwlock_timedwrlock_0050
225  * @tc.desc      : Test the case of pthread_cond_clockwait no timeout by CLOCK_REALTIME
226  * @tc.level     : Level 1
227  */
pthread_rwlock_timedwrlock_0050(void)228 void pthread_rwlock_timedwrlock_0050(void)
229 {
230     pthread_t tid[2];
231     EXPECT_EQ(pthread_rwlock_init(&w_rwlock2, NULL), 0);
232 
233     EXPECT_EQ(pthread_create(&tid[0], NULL, RwlockClockRealTimeWait1, NULL), 0);
234     EXPECT_EQ(pthread_create(&tid[1], NULL, RwlockClockRealTimeWait2, NULL), 0);
235 
236     EXPECT_EQ(pthread_join(tid[0], NULL), 0);
237     EXPECT_EQ(pthread_join(tid[1], NULL), 0);
238     EXPECT_EQ(pthread_rwlock_destroy(&w_rwlock2), 0);
239 }
240 
241 /**
242  * @tc.number    : pthread_rwlock_timedwrlock_0060
243  * @tc.desc      : Test the case of pthread_cond_clockwait timeout by CLOCK_REALTIME
244  * @tc.level     : Level 1
245  */
pthread_rwlock_timedwrlock_0060(void)246 void pthread_rwlock_timedwrlock_0060(void)
247 {
248     pthread_t tid[2];
249     EXPECT_EQ(pthread_rwlock_init(&w_rwlock1, NULL), 0);
250 
251     EXPECT_EQ(pthread_create(&tid[0], NULL, RwlockClockRealTimeOut1, NULL), 0);
252     EXPECT_EQ(pthread_create(&tid[1], NULL, RwlockClockRealTimeOut2, NULL), 0);
253 
254     EXPECT_EQ(pthread_join(tid[0], NULL), 0);
255     EXPECT_EQ(pthread_join(tid[1], NULL), 0);
256     EXPECT_EQ(pthread_rwlock_destroy(&w_rwlock1), 0);
257 }
258 
259 /**
260  * @tc.number    : pthread_rwlock_timedwrlock_0070
261  * @tc.desc      : Test the case of pthread_cond_clockwait timeout by CLOCK_MONOTONIC
262  * @tc.level     : Level 1
263  */
pthread_rwlock_timedwrlock_0070(void)264 void pthread_rwlock_timedwrlock_0070(void)
265 {
266     pthread_t tid[2];
267     EXPECT_EQ(pthread_rwlock_init(&w_rwlock5, NULL), 0);
268 
269     EXPECT_EQ(pthread_create(&tid[0], NULL, RwlockClockMonotonicTimeOut1, NULL), 0);
270     EXPECT_EQ(pthread_create(&tid[1], NULL, RwlockClockMonotonicTimeOut2, NULL), 0);
271 
272     EXPECT_EQ(pthread_join(tid[0], NULL), 0);
273     EXPECT_EQ(pthread_join(tid[1], NULL), 0);
274     EXPECT_EQ(pthread_rwlock_destroy(&w_rwlock5), 0);
275 }
276 
277 /**
278  * @tc.number    : pthread_rwlock_timedwrlock_0080
279  * @tc.desc      : Test the case of pthread_cond_clockwait timeout by CLOCK_MONOTONIC
280  * @tc.level     : Level 1
281  */
pthread_rwlock_timedwrlock_0080(void)282 void pthread_rwlock_timedwrlock_0080(void)
283 {
284     pthread_t tid[2];
285     EXPECT_EQ(pthread_rwlock_init(&w_rwlock6, NULL), 0);
286 
287     EXPECT_EQ(pthread_create(&tid[0], NULL, RwlockClockMonotonicTimeWait1, NULL), 0);
288     EXPECT_EQ(pthread_create(&tid[1], NULL, RwlockClockMonotonicTimeWait2, NULL), 0);
289 
290     EXPECT_EQ(pthread_join(tid[0], NULL), 0);
291     EXPECT_EQ(pthread_join(tid[1], NULL), 0);
292     EXPECT_EQ(pthread_rwlock_destroy(&w_rwlock6), 0);
293 }
294 
295 /**
296  * @tc.number    : pthread_rwlock_timedwrlock_0090
297  * @tc.desc      : Test the case of pthread_rwlock_timedwrlock_monotonic_np no timeout
298  * @tc.level     : Level 1
299  */
pthread_rwlock_timedwrlock_0090(void)300 void pthread_rwlock_timedwrlock_0090(void)
301 {
302     pthread_t tid[2];
303     EXPECT_EQ(pthread_rwlock_init(&w_rwlock3, NULL), 0);
304 
305     EXPECT_EQ(pthread_create(&tid[0], NULL, RwlockMonotonicTime1, NULL), 0);
306     EXPECT_EQ(pthread_create(&tid[1], NULL, RwlockMonotonicTime2, NULL), 0);
307 
308     EXPECT_EQ(pthread_join(tid[0], NULL), 0);
309     EXPECT_EQ(pthread_join(tid[1], NULL), 0);
310     EXPECT_EQ(pthread_rwlock_destroy(&w_rwlock3), 0);
311 }
312 
313 /**
314  * @tc.number    : pthread_rwlock_timedwrlock_0100
315  * @tc.desc      : Test the case of pthread_rwlock_timedwrlock_monotonic_np timeout
316  * @tc.level     : Level 1
317  */
pthread_rwlock_timedwrlock_0100(void)318 void pthread_rwlock_timedwrlock_0100(void)
319 {
320     pthread_t tid[2];
321     EXPECT_EQ(pthread_rwlock_init(&w_rwlock4, NULL), 0);
322 
323     EXPECT_EQ(pthread_create(&tid[0], NULL, RwlockMonotonicTime3, NULL), 0);
324     EXPECT_EQ(pthread_create(&tid[1], NULL, RwlockMonotonicTime4, NULL), 0);
325 
326     EXPECT_EQ(pthread_join(tid[0], NULL), 0);
327     EXPECT_EQ(pthread_join(tid[1], NULL), 0);
328     EXPECT_EQ(pthread_rwlock_destroy(&w_rwlock4), 0);
329 }
330 
331 TEST_FUN G_Fun_Array[] = {
332     pthread_rwlock_timedwrlock_0010,
333     pthread_rwlock_timedwrlock_0020,
334     pthread_rwlock_timedwrlock_0030,
335     pthread_rwlock_timedwrlock_0040,
336     pthread_rwlock_timedwrlock_0050,
337     pthread_rwlock_timedwrlock_0060,
338     pthread_rwlock_timedwrlock_0070,
339     pthread_rwlock_timedwrlock_0080,
340     pthread_rwlock_timedwrlock_0090,
341     pthread_rwlock_timedwrlock_0100,
342 };
343 
main(void)344 int main(void)
345 {
346     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
347     for (int pos = 0; pos < num; ++pos) {
348         G_Fun_Array[pos]();
349     }
350     return t_status;
351 }