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 extern int __pthread_cond_timedwait_time64(pthread_cond_t *__restrict,
20 pthread_mutex_t *__restrict, const struct timespec *__restrict);
21
22 // pthread_cond_clockwait
23 static pthread_mutex_t c_mtx1 = PTHREAD_MUTEX_INITIALIZER;
24 static pthread_cond_t c_cond1 = PTHREAD_COND_INITIALIZER;
25 static pthread_mutex_t c_mtx2 = PTHREAD_MUTEX_INITIALIZER;
26 static pthread_mutex_t c_mtx10 = PTHREAD_MUTEX_INITIALIZER;
27 static pthread_cond_t c_cond2 = PTHREAD_COND_INITIALIZER;
28 static pthread_mutex_t c_mtx3 = PTHREAD_MUTEX_INITIALIZER;
29 static pthread_cond_t c_cond3 = PTHREAD_COND_INITIALIZER;
30 static pthread_mutex_t c_mtx4 = PTHREAD_MUTEX_INITIALIZER;
31 static pthread_cond_t c_cond4 = PTHREAD_COND_INITIALIZER;
32 static pthread_mutex_t c_mtx5 = PTHREAD_MUTEX_INITIALIZER;
33 static pthread_cond_t c_cond5 = PTHREAD_COND_INITIALIZER;
34 static pthread_mutex_t c_mtx6 = PTHREAD_MUTEX_INITIALIZER;
35 static pthread_cond_t c_cond6 = PTHREAD_COND_INITIALIZER;
36 // pthread_cond_timedwait_monotonic_np
37 static pthread_mutex_t m_mtx1 = PTHREAD_MUTEX_INITIALIZER;
38 static pthread_cond_t m_cond1 = PTHREAD_COND_INITIALIZER;
39 static pthread_mutex_t m_mtx2 = PTHREAD_MUTEX_INITIALIZER;
40 static pthread_cond_t m_cond2 = PTHREAD_COND_INITIALIZER;
41 static pthread_mutex_t m_mtx3 = PTHREAD_MUTEX_INITIALIZER;
42 static pthread_cond_t m_cond3 = PTHREAD_COND_INITIALIZER;
43 // pthread_cond_timeout_np
44 static pthread_mutex_t u_mtx1 = PTHREAD_MUTEX_INITIALIZER;
45 static pthread_cond_t u_cond1 = PTHREAD_COND_INITIALIZER;
46 static pthread_mutex_t u_mtx2 = PTHREAD_MUTEX_INITIALIZER;
47 static pthread_cond_t u_cond2 = PTHREAD_COND_INITIALIZER;
48
49 /**
50 * @tc.number : pthread_cond_timedwait_0010
51 * @tc.desc : Test whether the pthread_cond_timedwait is normal
52 * @tc.level : Level 0
53 */
pthread_cond_timedwait_0010(void)54 void pthread_cond_timedwait_0010(void)
55 {
56 pthread_condattr_t a;
57 EXPECT_EQ(pthread_condattr_init(&a), 0);
58 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
59 EXPECT_EQ(pthread_cond_init(&cond, &a), 0);
60 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
61 struct timespec ts = {0};
62 EXPECT_EQ(pthread_mutex_lock(&mutex), 0);
63 EXPECT_EQ(clock_gettime(CLOCK_REALTIME, &ts), 0);
64
65 ts.tv_nsec += SLEEP_10_MS*MS_PER_S*MS_PER_S;
66 if (ts.tv_nsec >= MS_PER_S*MS_PER_S*MS_PER_S) {
67 ts.tv_nsec -= MS_PER_S*MS_PER_S*MS_PER_S;
68 ts.tv_sec += 1;
69 }
70 EXPECT_EQ(pthread_cond_timedwait(&cond, &mutex, &ts), ETIMEDOUT);
71 EXPECT_EQ(pthread_mutex_unlock(&mutex), 0);
72 EXPECT_EQ(pthread_cond_destroy(&cond), 0);
73 EXPECT_EQ(pthread_mutex_destroy(&mutex), 0);
74 }
75
76 /**
77 * @tc.number : pthread_cond_timedwait_0020
78 * @tc.desc : Test for pthread_cond_timedwait exceptions
79 * @tc.level : Level 2
80 */
pthread_cond_timedwait_0020(void)81 void pthread_cond_timedwait_0020(void)
82 {
83 pthread_condattr_t a;
84 EXPECT_EQ(pthread_condattr_init(&a), 0);
85 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
86 EXPECT_EQ(pthread_cond_init(&cond, &a), 0);
87 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
88 struct timespec ts = {0};
89 EXPECT_EQ(pthread_mutex_lock(&mutex), 0);
90 EXPECT_EQ(clock_gettime(CLOCK_REALTIME, &ts), 0);
91
92 ts.tv_nsec = NSEC_PER_SEC;
93 EXPECT_EQ(pthread_cond_timedwait(&cond, &mutex, &ts), EINVAL);
94 EXPECT_EQ(pthread_mutex_unlock(&mutex), 0);
95 EXPECT_EQ(pthread_cond_destroy(&cond), 0);
96 EXPECT_EQ(pthread_mutex_destroy(&mutex), 0);
97 }
98
99 /**
100 * @tc.number : pthread_cond_timedwait_time64_0010
101 * @tc.desc : Test whether the pthread_cond_timedwait is normal
102 * @tc.level : Level 0
103 */
pthread_cond_timedwait_time64_0010(void)104 void pthread_cond_timedwait_time64_0010(void)
105 {
106 pthread_condattr_t a;
107 EXPECT_EQ(pthread_condattr_init(&a), 0);
108 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
109 EXPECT_EQ(pthread_cond_init(&cond, &a), 0);
110 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
111 struct timespec ts = {0};
112 EXPECT_EQ(pthread_mutex_lock(&mutex), 0);
113 EXPECT_EQ(clock_gettime(CLOCK_REALTIME, &ts), 0);
114
115 ts.tv_nsec += SLEEP_10_MS*MS_PER_S*MS_PER_S;
116 if (ts.tv_nsec >= MS_PER_S*MS_PER_S*MS_PER_S) {
117 ts.tv_nsec -= MS_PER_S*MS_PER_S*MS_PER_S;
118 ts.tv_sec += 1;
119 }
120 EXPECT_EQ(__pthread_cond_timedwait_time64(&cond, &mutex, &ts), ETIMEDOUT);
121 EXPECT_EQ(pthread_mutex_unlock(&mutex), 0);
122 EXPECT_EQ(pthread_cond_destroy(&cond), 0);
123 EXPECT_EQ(pthread_mutex_destroy(&mutex), 0);
124 }
125
126 /**
127 * @tc.number : pthread_cond_timedwait_monotonic_np_0010
128 * @tc.desc : Test whether the pthread_cond_timedwait_monotonic_np is normal
129 * @tc.level : Level 0
130 */
pthread_cond_timedwait_monotonic_np_0010(void)131 void pthread_cond_timedwait_monotonic_np_0010(void)
132 {
133 pthread_cond_t cond;
134 pthread_mutex_t mutex;
135 struct timespec ts = {0};
136 EXPECT_EQ(pthread_cond_init(&cond, 0), 0);
137 EXPECT_EQ(pthread_mutex_init(&mutex, 0), 0);
138
139 EXPECT_EQ(clock_gettime(CLOCK_MONOTONIC, &ts), 0);
140 ts.tv_nsec += SLEEP_10_MS*MS_PER_S*MS_PER_S;
141 if (ts.tv_nsec >= MS_PER_S*MS_PER_S*MS_PER_S) {
142 ts.tv_nsec -= MS_PER_S*MS_PER_S*MS_PER_S;
143 ts.tv_sec += 1;
144 }
145 EXPECT_EQ(pthread_cond_timedwait_monotonic_np(&cond, &mutex, &ts), ETIMEDOUT);
146 EXPECT_EQ(pthread_mutex_unlock(&mutex), 0);
147 EXPECT_EQ(pthread_cond_destroy(&cond), 0);
148 EXPECT_EQ(pthread_mutex_destroy(&mutex), 0);
149 }
150
151 /**
152 * @tc.number : pthread_cond_timedwait_monotonic_np_0020
153 * @tc.desc : Test whether the pthread_cond_timedwait_monotonic_np is invalid
154 * @tc.level : Level 2
155 */
pthread_cond_timedwait_monotonic_np_0020(void)156 void pthread_cond_timedwait_monotonic_np_0020(void)
157 {
158 pthread_cond_t *cond = (pthread_cond_t *)NULL;
159 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
160 struct timespec ts = {0};
161 EXPECT_EQ(pthread_cond_timedwait_monotonic_np(cond, &mutex, &ts), EINVAL);
162 }
163
164 /**
165 * @tc.number : pthread_cond_timeout_np_0010
166 * @tc.desc : Test whether the pthread_cond_timeout_np is normal
167 * @tc.level : Level 0
168 */
pthread_cond_timeout_np_0010(void)169 void pthread_cond_timeout_np_0010(void)
170 {
171 unsigned int ms = SLEEP_100_MS;
172 pthread_condattr_t a;
173 EXPECT_EQ(pthread_condattr_init(&a), 0);
174 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
175 EXPECT_EQ(pthread_cond_init(&cond, &a), 0);
176 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
177 EXPECT_EQ(pthread_cond_timeout_np(&cond, &mutex, ms), ETIMEDOUT);
178 EXPECT_EQ(pthread_mutex_unlock(&mutex), 0);
179 EXPECT_EQ(pthread_cond_destroy(&cond), 0);
180 EXPECT_EQ(pthread_mutex_destroy(&mutex), 0);
181 }
182
183 /**
184 * @tc.number : pthread_cond_clockwait_0010
185 * @tc.desc : Test multiple cases of pthread_cond_clockwait
186 * @tc.level : Level 2
187 */
pthread_cond_clockwait_0010(void)188 void pthread_cond_clockwait_0010(void)
189 {
190 pthread_condattr_t a;
191 EXPECT_EQ(pthread_condattr_init(&a), 0);
192 pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
193 EXPECT_EQ(pthread_cond_init(&cond, &a), 0);
194 pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
195 struct timespec ts = {0};
196
197 ts.tv_nsec += SLEEP_10_MS*MS_PER_S*MS_PER_S;
198 if (ts.tv_nsec >= MS_PER_S*MS_PER_S*MS_PER_S) {
199 ts.tv_nsec -= MS_PER_S*MS_PER_S*MS_PER_S;
200 ts.tv_sec += 1;
201 }
202 clockid_t clock_id = CLOCK_REALTIME;
203 EXPECT_EQ(pthread_cond_clockwait(&cond, &mutex, clock_id, &ts), ETIMEDOUT);
204 clock_id = CLOCK_MONOTONIC;
205 EXPECT_EQ(pthread_cond_clockwait(&cond, &mutex, clock_id, &ts), ETIMEDOUT);
206 clock_id = CLOCK_PROCESS_CPUTIME_ID;
207 EXPECT_EQ(pthread_cond_clockwait(&cond, &mutex, clock_id, &ts), EINVAL);
208 EXPECT_EQ(pthread_mutex_unlock(&mutex), 0);
209 EXPECT_EQ(pthread_cond_destroy(&cond), 0);
210 EXPECT_EQ(pthread_mutex_destroy(&mutex), 0);
211 }
212
213 /**
214 * @tc.number : pthread_cond_timedwait_Time_0010
215 * @tc.desc : Whether the interface is normal when the time is not set
216 * @tc.level : Level 0
217 */
pthread_cond_timedwait_Time_0010(void)218 void pthread_cond_timedwait_Time_0010(void)
219 {
220 pthread_cond_t cond;
221 pthread_mutex_t mutex;
222 struct timespec ts;
223 EXPECT_EQ(pthread_cond_init(&cond, 0), 0);
224 EXPECT_EQ(pthread_mutex_init(&mutex, 0), 0);
225
226 EXPECT_EQ(pthread_cond_timedwait_monotonic_np(&cond, &mutex, &ts), ETIMEDOUT);
227
228 EXPECT_EQ(pthread_mutex_unlock(&mutex), 0);
229 EXPECT_EQ(pthread_cond_destroy(&cond), 0);
230 EXPECT_EQ(pthread_mutex_destroy(&mutex), 0);
231 }
232
ClockWaitTimedwait1(void *arg)233 static void *ClockWaitTimedwait1(void *arg)
234 {
235 Msleep(SLEEP_50_MS);
236 EXPECT_EQ(pthread_mutex_lock(&c_mtx1), 0);
237 EXPECT_EQ(pthread_mutex_unlock(&c_mtx1), 0);
238 EXPECT_EQ(pthread_cond_signal(&c_cond1), 0);
239 return arg;
240 }
241
ClockWaitTimedwait2(void *arg)242 static void *ClockWaitTimedwait2(void *arg)
243 {
244 const unsigned int nsecPerSec = NSEC_PER_SEC;
245 const unsigned int nsecPer100Ms = NSEC_PER_100MS;
246 struct timespec ts = {0};
247 EXPECT_EQ(pthread_mutex_lock(&c_mtx1), 0);
248
249 clock_gettime(CLOCK_REALTIME, &ts);
250 ts.tv_sec = ts.tv_sec + (ts.tv_nsec + nsecPer100Ms) / nsecPerSec;
251 ts.tv_nsec = (ts.tv_nsec + nsecPer100Ms) % nsecPerSec;
252
253 clockid_t clock_id = CLOCK_REALTIME;
254 EXPECT_EQ(pthread_cond_clockwait(&c_cond1, &c_mtx1, clock_id, &ts), 0);
255 EXPECT_EQ(pthread_mutex_unlock(&c_mtx1), 0);
256 return arg;
257 }
258
259 /**
260 * @tc.number : clockwait_timedwait_0010
261 * @tc.desc : Test the case of pthread_cond_clockwait timewait by CLOCK_REALTIME
262 * @tc.level : Level 1
263 */
clockwait_timedwait_0010(void)264 void clockwait_timedwait_0010(void)
265 {
266 pthread_t tid1;
267 pthread_t tid2;
268 EXPECT_EQ(pthread_create(&tid1, NULL, ClockWaitTimedwait1, NULL), 0);
269 EXPECT_EQ(pthread_create(&tid2, NULL, ClockWaitTimedwait2, NULL), 0);
270 Msleep(SLEEP_100_MS);
271 pthread_join(tid1, NULL);
272 pthread_join(tid2, NULL);
273 EXPECT_EQ(pthread_cond_destroy(&c_cond1), 0);
274 EXPECT_EQ(pthread_mutex_destroy(&c_mtx1), 0);
275 }
276
ClockWaitTimeOut(void *arg)277 static void *ClockWaitTimeOut(void *arg)
278 {
279 struct timespec ts = {0};
280 struct timespec tsNow = {0};
281 EXPECT_EQ(pthread_mutex_lock(&c_mtx2), 0);
282
283 clockid_t clock_id = CLOCK_REALTIME;
284 GetDelayedTimeByClockid(&ts, SLEEP_100_MS, clock_id);
285 EXPECT_EQ(pthread_cond_clockwait(&c_cond2, &c_mtx2, clock_id, &ts), ETIMEDOUT);
286 clock_gettime(CLOCK_REALTIME, &tsNow);
287
288 int timeDiff = GetTimeDiff(tsNow, ts);
289 EXPECT_GE(timeDiff, 0);
290 EXPECT_LE(timeDiff, SLEEP_20_MS);
291
292 EXPECT_EQ(pthread_mutex_unlock(&c_mtx2), 0);
293 return arg;
294 }
295
296 /**
297 * @tc.number : clockwait_timedwait_0020
298 * @tc.desc : Test the case of pthread_cond_clockwait timeout by CLOCK_REALTIME
299 * @tc.level : Level 1
300 */
clockwait_timedwait_0020(void)301 void clockwait_timedwait_0020(void)
302 {
303 pthread_t tid;
304 EXPECT_EQ(pthread_create(&tid, NULL, ClockWaitTimeOut, NULL), 0);
305 Msleep(SLEEP_200_MS);
306 pthread_join(tid, NULL);
307 EXPECT_EQ(pthread_cond_destroy(&c_cond2), 0);
308 EXPECT_EQ(pthread_mutex_destroy(&c_mtx2), 0);
309 }
310
ClockWaitTimedwait3(void *arg)311 static void *ClockWaitTimedwait3(void *arg)
312 {
313 Msleep(SLEEP_50_MS);
314 EXPECT_EQ(pthread_mutex_lock(&c_mtx3), 0);
315 EXPECT_EQ(pthread_mutex_unlock(&c_mtx3), 0);
316 EXPECT_EQ(pthread_cond_signal(&c_cond3), 0);
317 return arg;
318 }
319
ClockWaitTimedwait4(void *arg)320 static void *ClockWaitTimedwait4(void *arg)
321 {
322 const unsigned int nsecPerSec = NSEC_PER_SEC;
323 const unsigned int nsecPer100Ms = NSEC_PER_100MS;
324 struct timespec ts = {0};
325 EXPECT_EQ(pthread_mutex_lock(&c_mtx3), 0);
326
327 clock_gettime(CLOCK_MONOTONIC, &ts);
328 ts.tv_sec = ts.tv_sec + (ts.tv_nsec + nsecPer100Ms) / nsecPerSec;
329 ts.tv_nsec = (ts.tv_nsec + nsecPer100Ms) % nsecPerSec;
330
331 clockid_t clock_id = CLOCK_MONOTONIC;
332 EXPECT_EQ(pthread_cond_clockwait(&c_cond3, &c_mtx3, clock_id, &ts), 0);
333 EXPECT_EQ(pthread_mutex_unlock(&c_mtx3), 0);
334 return arg;
335 }
336
337 /**
338 * @tc.number : clockwait_timedwait_0030
339 * @tc.desc : Test the case of pthread_cond_clockwait timewait by CLOCK_MONOTONIC
340 * @tc.level : Level 1
341 */
clockwait_timedwait_0030(void)342 void clockwait_timedwait_0030(void)
343 {
344 pthread_t tid1;
345 pthread_t tid2;
346 EXPECT_EQ(pthread_create(&tid1, NULL, ClockWaitTimedwait3, NULL), 0);
347 EXPECT_EQ(pthread_create(&tid2, NULL, ClockWaitTimedwait4, NULL), 0);
348 Msleep(SLEEP_100_MS);
349 pthread_join(tid1, NULL);
350 pthread_join(tid2, NULL);
351 EXPECT_EQ(pthread_cond_destroy(&c_cond3), 0);
352 EXPECT_EQ(pthread_mutex_destroy(&c_mtx3), 0);
353 }
354
ClockWaitTimeOut2(void *arg)355 static void *ClockWaitTimeOut2(void *arg)
356 {
357 struct timespec ts = {0};
358 struct timespec tsNow = {0};
359 EXPECT_EQ(pthread_mutex_lock(&c_mtx10), 0);
360
361 clockid_t clock_id = CLOCK_MONOTONIC;
362 GetDelayedTimeByClockid(&ts, SLEEP_100_MS, clock_id);
363 EXPECT_EQ(pthread_cond_clockwait(&c_cond2, &c_mtx10, clock_id, &ts), ETIMEDOUT);
364 clock_gettime(CLOCK_MONOTONIC, &tsNow);
365
366 int timeDiff = GetTimeDiff(tsNow, ts);
367 EXPECT_GE(timeDiff, 0);
368 EXPECT_LE(timeDiff, SLEEP_20_MS);
369
370 EXPECT_EQ(pthread_mutex_unlock(&c_mtx10), 0);
371 return arg;
372 }
373
374 /**
375 * @tc.number : clockwait_timedwait_0040
376 * @tc.desc : Test the case of pthread_cond_clockwait timeout by CLOCK_MONOTONIC
377 * @tc.level : Level 1
378 */
clockwait_timedwait_0040(void)379 void clockwait_timedwait_0040(void)
380 {
381 pthread_t tid;
382 EXPECT_EQ(pthread_create(&tid, NULL, ClockWaitTimeOut2, NULL), 0);
383 Msleep(SLEEP_200_MS);
384 pthread_join(tid, NULL);
385 EXPECT_EQ(pthread_cond_destroy(&c_cond2), 0);
386 EXPECT_EQ(pthread_mutex_destroy(&c_mtx10), 0);
387 }
388
ClockWaitTimeMismatch(void *arg)389 static void *ClockWaitTimeMismatch(void *arg)
390 {
391 const unsigned int nsecPerSec = NSEC_PER_SEC;
392 const unsigned int nsecPer100Ms = NSEC_PER_100MS;
393 struct timespec ts = {0};
394 EXPECT_EQ(pthread_mutex_lock(&c_mtx4), 0);
395
396 clock_gettime(CLOCK_MONOTONIC, &ts);
397 ts.tv_sec = ts.tv_sec + (ts.tv_nsec + nsecPer100Ms) / nsecPerSec;
398 ts.tv_nsec = (ts.tv_nsec + nsecPer100Ms) % nsecPerSec;
399
400 clockid_t clock_id = CLOCK_REALTIME;
401 EXPECT_EQ(pthread_cond_clockwait(&c_cond4, &c_mtx4, clock_id, &ts), ETIMEDOUT);
402
403 EXPECT_EQ(pthread_mutex_unlock(&c_mtx4), 0);
404 return arg;
405 }
406
ClockWaitTimeMismatch1(void *arg)407 static void *ClockWaitTimeMismatch1(void *arg)
408 {
409 Msleep(SLEEP_50_MS);
410 EXPECT_EQ(pthread_mutex_lock(&c_mtx5), 0);
411 EXPECT_EQ(pthread_mutex_unlock(&c_mtx5), 0);
412 EXPECT_EQ(pthread_cond_signal(&c_cond5), 0);
413 return arg;
414 }
415
ClockWaitTimeMismatch2(void *arg)416 static void *ClockWaitTimeMismatch2(void *arg)
417 {
418 const unsigned int nsecPerSec = NSEC_PER_SEC;
419 const unsigned int nsecPer100Ms = NSEC_PER_100MS;
420 struct timespec ts = {0};
421 EXPECT_EQ(pthread_mutex_lock(&c_mtx5), 0);
422
423 clock_gettime(CLOCK_REALTIME, &ts);
424 ts.tv_sec = ts.tv_sec + (ts.tv_nsec + nsecPer100Ms) / nsecPerSec;
425 ts.tv_nsec = (ts.tv_nsec + nsecPer100Ms) % nsecPerSec;
426
427 clockid_t clock_id = CLOCK_REALTIME;
428 EXPECT_EQ(pthread_cond_clockwait(&c_cond5, &c_mtx5, clock_id, &ts), 0);
429
430 EXPECT_EQ(pthread_mutex_unlock(&c_mtx5), 0);
431 return arg;
432 }
433
434 /**
435 * @tc.number : clockwait_timedwait_0050
436 * @tc.desc : Test the case of pthread_cond_clockwait time mismatch
437 * @tc.level : Level 2
438 */
clockwait_timedwait_0050(void)439 void clockwait_timedwait_0050(void)
440 {
441 pthread_t tid;
442 pthread_t tid1;
443 pthread_t tid2;
444 EXPECT_EQ(pthread_create(&tid1, NULL, ClockWaitTimeMismatch1, NULL), 0);
445 EXPECT_EQ(pthread_create(&tid2, NULL, ClockWaitTimeMismatch2, NULL), 0);
446 EXPECT_EQ(pthread_create(&tid, NULL, ClockWaitTimeMismatch, NULL), 0);
447 Msleep(SLEEP_200_MS);
448 pthread_join(tid1, NULL);
449 pthread_join(tid2, NULL);
450 pthread_join(tid, NULL);
451 EXPECT_EQ(pthread_cond_destroy(&c_cond4), 0);
452 EXPECT_EQ(pthread_mutex_destroy(&c_mtx4), 0);
453 EXPECT_EQ(pthread_cond_destroy(&c_cond5), 0);
454 EXPECT_EQ(pthread_mutex_destroy(&c_mtx5), 0);
455 }
456
ClockWaitTimeMismatch3(void *arg)457 static void *ClockWaitTimeMismatch3(void *arg)
458 {
459 struct timespec ts = {0};
460 struct timespec tsNow = {0};
461 EXPECT_EQ(pthread_mutex_lock(&c_mtx6), 0);
462 Msleep(SLEEP_20_MS);
463 GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_REALTIME);
464 clock_gettime(CLOCK_REALTIME, &tsNow);
465 tsNow.tv_sec += 1;
466 EXPECT_EQ(clock_settime(CLOCK_REALTIME, &tsNow), 0);
467 EXPECT_EQ(pthread_cond_clockwait(&c_cond6, &c_mtx6, CLOCK_REALTIME, &ts), ETIMEDOUT);
468
469 clock_gettime(CLOCK_REALTIME, &tsNow);
470 tsNow.tv_sec -= 1;
471 clock_settime(CLOCK_REALTIME, &tsNow);
472 EXPECT_EQ(pthread_mutex_unlock(&c_mtx6), 0);
473
474 return arg;
475 }
476
477 /**
478 * @tc.number : clockwait_timedwait_0060
479 * @tc.desc : A case for testing the CLOCK_REALTIME feature of pthread_cond_clockwait.
480 * @tc.level : Level 1
481 */
clockwait_timedwait_0060(void)482 void clockwait_timedwait_0060(void)
483 {
484 pthread_t tid;
485 EXPECT_EQ(pthread_create(&tid, NULL, ClockWaitTimeMismatch3, NULL), 0);
486 Msleep(SLEEP_200_MS);
487 pthread_join(tid, NULL);
488 EXPECT_EQ(pthread_cond_destroy(&c_cond6), 0);
489 EXPECT_EQ(pthread_mutex_destroy(&c_mtx6), 0);
490 }
491
ClockWaitTimeMismatch4(void *arg)492 static void *ClockWaitTimeMismatch4(void *arg)
493 {
494 Msleep(SLEEP_50_MS);
495 EXPECT_EQ(pthread_mutex_lock(&c_mtx6), 0);
496 EXPECT_EQ(pthread_mutex_unlock(&c_mtx6), 0);
497 EXPECT_EQ(pthread_cond_signal(&c_cond6), 0);
498 return arg;
499 }
500
ClockWaitTimeMismatch5(void *arg)501 static void *ClockWaitTimeMismatch5(void *arg)
502 {
503 struct timespec ts = {0};
504 EXPECT_EQ(pthread_mutex_lock(&c_mtx6), 0);
505 Msleep(SLEEP_20_MS);
506 GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_MONOTONIC);
507 EXPECT_EQ(pthread_cond_clockwait(&c_cond6, &c_mtx6, CLOCK_MONOTONIC, &ts), 0);
508 EXPECT_EQ(pthread_mutex_unlock(&c_mtx6), 0);
509 return arg;
510 }
511
512 /**
513 * @tc.number : clockwait_timedwait_0070
514 * @tc.desc : A case for testing the CLOCK_MONOTONIC feature of pthread_cond_clockwait.
515 * @tc.level : Level 1
516 */
clockwait_timedwait_0070(void)517 void clockwait_timedwait_0070(void)
518 {
519 struct timespec tsNow = {0};
520 pthread_t tid1;
521 pthread_t tid2;
522
523 EXPECT_EQ(pthread_create(&tid1, NULL, ClockWaitTimeMismatch4, NULL), 0);
524 EXPECT_EQ(pthread_create(&tid2, NULL, ClockWaitTimeMismatch5, NULL), 0);
525 Msleep(SLEEP_100_MS);
526 clock_gettime(CLOCK_MONOTONIC, &tsNow);
527 tsNow.tv_sec += 1;
528 EXPECT_EQ(clock_settime(CLOCK_REALTIME, &tsNow), 0);
529 pthread_join(tid1, NULL);
530 pthread_join(tid2, NULL);
531 EXPECT_EQ(pthread_cond_destroy(&c_cond6), 0);
532 EXPECT_EQ(pthread_mutex_destroy(&c_mtx6), 0);
533 }
534
PthreadCondMonotonicTimeWait1(void *arg)535 static void *PthreadCondMonotonicTimeWait1(void *arg)
536 {
537 Msleep(SLEEP_50_MS);
538 EXPECT_EQ(pthread_mutex_lock(&m_mtx1), 0);
539 EXPECT_EQ(pthread_mutex_unlock(&m_mtx1), 0);
540 EXPECT_EQ(pthread_cond_signal(&m_cond1), 0);
541 return arg;
542 }
543
PthreadCondMonotonicTimeWait2(void *arg)544 static void *PthreadCondMonotonicTimeWait2(void *arg)
545 {
546 const unsigned int nsecPerSec = NSEC_PER_SEC;
547 const unsigned int nsecPer100Ms = NSEC_PER_100MS;
548 struct timespec ts = {0};
549 EXPECT_EQ(pthread_mutex_lock(&m_mtx1), 0);
550
551 clock_gettime(CLOCK_MONOTONIC, &ts);
552 ts.tv_sec = ts.tv_sec + (ts.tv_nsec + nsecPer100Ms) / nsecPerSec;
553 ts.tv_nsec = (ts.tv_nsec + nsecPer100Ms) % nsecPerSec;
554
555 EXPECT_EQ(pthread_cond_timedwait_monotonic_np(&m_cond1, &m_mtx1, &ts), 0);
556 EXPECT_EQ(pthread_mutex_unlock(&m_mtx1), 0);
557 return arg;
558 }
559
560 /**
561 * @tc.number : monotonic_timewait_0010
562 * @tc.desc : Test the case of pthread_cond_timedwait_monotonic_np timewait
563 * @tc.level : Level 1
564 */
monotonic_timewait_0010(void)565 void monotonic_timewait_0010(void)
566 {
567 pthread_t tid1;
568 pthread_t tid2;
569
570 EXPECT_EQ(pthread_create(&tid1, NULL, PthreadCondMonotonicTimeWait1, NULL), 0);
571 EXPECT_EQ(pthread_create(&tid2, NULL, PthreadCondMonotonicTimeWait2, NULL), 0);
572
573 Msleep(SLEEP_100_MS);
574 pthread_join(tid1, NULL);
575 pthread_join(tid2, NULL);
576 EXPECT_EQ(pthread_cond_destroy(&m_cond1), 0);
577 EXPECT_EQ(pthread_mutex_destroy(&m_mtx1), 0);
578 }
579
PthreadCondMonotonicTimeOut(void *arg)580 static void *PthreadCondMonotonicTimeOut(void *arg)
581 {
582 struct timespec ts = {0};
583 struct timespec tsNow = {0};
584 EXPECT_EQ(pthread_mutex_lock(&m_mtx2), 0);
585
586 GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_MONOTONIC);
587 EXPECT_EQ(pthread_cond_timedwait_monotonic_np(&m_cond2, &m_mtx2, &ts), ETIMEDOUT);
588 clock_gettime(CLOCK_MONOTONIC, &tsNow);
589
590 int timeDiff = GetTimeDiff(tsNow, ts);
591 EXPECT_GE(timeDiff, 0);
592 EXPECT_LE(timeDiff, SLEEP_20_MS);
593
594 EXPECT_EQ(pthread_mutex_unlock(&m_mtx2), 0);
595 return arg;
596 }
597
598 /**
599 * @tc.number : monotonic_timewait_0020
600 * @tc.desc : Test the case of pthread_cond_timedwait_monotonic_np timeout
601 * @tc.level : Level 1
602 */
monotonic_timewait_0020(void)603 void monotonic_timewait_0020(void)
604 {
605 pthread_t tid;
606 EXPECT_EQ(pthread_create(&tid, NULL, PthreadCondMonotonicTimeOut, NULL), 0);
607 Msleep(SLEEP_200_MS);
608 pthread_join(tid, NULL);
609 EXPECT_EQ(pthread_cond_destroy(&m_cond2), 0);
610 EXPECT_EQ(pthread_mutex_destroy(&m_mtx2), 0);
611 }
612
PthreadCondMonotonicTimeEinval(void *arg)613 static void *PthreadCondMonotonicTimeEinval(void *arg)
614 {
615 const long einvalNsec = NSEC_PER_SEC;
616 struct timespec ts = {0};
617 EXPECT_EQ(pthread_mutex_lock(&m_mtx3), 0);
618
619 ts.tv_sec = 1;
620 ts.tv_nsec = einvalNsec;
621 EXPECT_EQ(pthread_cond_timedwait_monotonic_np(&m_cond3, &m_mtx3, &ts), EINVAL);
622
623 EXPECT_EQ(pthread_mutex_unlock(&m_mtx3), 0);
624 return arg;
625 }
626
627 /**
628 * @tc.number : monotonic_timewait_0030
629 * @tc.desc : Test the case of pthread_cond_timedwait_monotonic_np EINVAL
630 * @tc.level : Level 2
631 */
monotonic_timewait_0030(void)632 void monotonic_timewait_0030(void)
633 {
634 pthread_t tid;
635
636 EXPECT_EQ(pthread_create(&tid, NULL, PthreadCondMonotonicTimeEinval, NULL), 0);
637
638 Msleep(SLEEP_200_MS);
639 pthread_join(tid, NULL);
640 EXPECT_EQ(pthread_cond_destroy(&m_cond3), 0);
641 EXPECT_EQ(pthread_mutex_destroy(&m_mtx3), 0);
642 }
643
PthreadCondUnsignedTimeWait1(void *arg)644 static void *PthreadCondUnsignedTimeWait1(void *arg)
645 {
646 Msleep(SLEEP_50_MS);
647 EXPECT_EQ(pthread_mutex_lock(&u_mtx1), 0);
648 EXPECT_EQ(pthread_mutex_unlock(&u_mtx1), 0);
649 EXPECT_EQ(pthread_cond_signal(&u_cond1), 0);
650 return arg;
651 }
652
PthreadCondUnsignedTimeWait2(void *arg)653 static void *PthreadCondUnsignedTimeWait2(void *arg)
654 {
655 unsigned int ms = SLEEP_100_MS;
656 struct timespec ts = {0};
657
658 GetDelayedTimeByClockid(&ts, ms, CLOCK_MONOTONIC);
659 EXPECT_EQ(pthread_mutex_lock(&u_mtx1), 0);
660 EXPECT_EQ(pthread_cond_timeout_np(&u_cond1, &u_mtx1, ms), 0);
661 EXPECT_EQ(pthread_mutex_unlock(&u_mtx1), 0);
662 return arg;
663 }
664
665 /**
666 * @tc.number : timeoutnp_timewait_0010
667 * @tc.desc : Test the case of pthread_cond_timeout_np timewait
668 * @tc.level : Level 1
669 */
timeoutnp_timewait_0010(void)670 void timeoutnp_timewait_0010(void)
671 {
672 pthread_t tid1;
673 pthread_t tid2;
674
675 EXPECT_EQ(pthread_create(&tid1, NULL, PthreadCondUnsignedTimeWait1, NULL), 0);
676 EXPECT_EQ(pthread_create(&tid2, NULL, PthreadCondUnsignedTimeWait2, NULL), 0);
677
678 Msleep(SLEEP_100_MS);
679 pthread_join(tid1, NULL);
680 pthread_join(tid2, NULL);
681 EXPECT_EQ(pthread_cond_destroy(&u_cond1), 0);
682 EXPECT_EQ(pthread_mutex_destroy(&u_mtx1), 0);
683 }
684
PthreadCondUnsignedTimeOut(void *arg)685 static void *PthreadCondUnsignedTimeOut(void *arg)
686 {
687 unsigned int ms = SLEEP_100_MS;
688 struct timespec ts = {0};
689 struct timespec tsNow = {0};
690 EXPECT_EQ(pthread_mutex_lock(&u_mtx2), 0);
691
692 GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_MONOTONIC);
693 EXPECT_EQ(pthread_cond_timeout_np(&u_cond2, &u_mtx2, ms), ETIMEDOUT);
694 clock_gettime(CLOCK_MONOTONIC, &tsNow);
695
696 int timeDiff = GetTimeDiff(tsNow, ts);
697 EXPECT_GE(timeDiff, 0);
698 EXPECT_LE(timeDiff, SLEEP_20_MS);
699
700 EXPECT_EQ(pthread_mutex_unlock(&u_mtx2), 0);
701 return arg;
702 }
703
704 /**
705 * @tc.number : timeoutnp_timewait_0020
706 * @tc.desc : Test the case of pthread_cond_timeout_np timeout
707 * @tc.level : Level 1
708 */
timeoutnp_timewait_0020(void)709 void timeoutnp_timewait_0020(void)
710 {
711 pthread_t tid;
712 EXPECT_EQ(pthread_create(&tid, NULL, PthreadCondUnsignedTimeOut, NULL), 0);
713 Msleep(SLEEP_200_MS);
714 pthread_join(tid, NULL);
715 EXPECT_EQ(pthread_cond_destroy(&u_cond2), 0);
716 EXPECT_EQ(pthread_mutex_destroy(&u_mtx2), 0);
717 }
718
719 TEST_FUN G_Fun_Array[] = {
720 pthread_cond_timedwait_0010,
721 pthread_cond_timedwait_0020,
722 pthread_cond_timedwait_time64_0010,
723 pthread_cond_timedwait_monotonic_np_0010,
724 pthread_cond_timedwait_monotonic_np_0020,
725 pthread_cond_timeout_np_0010,
726 pthread_cond_clockwait_0010,
727 pthread_cond_timedwait_Time_0010,
728 clockwait_timedwait_0010,
729 clockwait_timedwait_0020,
730 clockwait_timedwait_0030,
731 clockwait_timedwait_0040,
732 clockwait_timedwait_0050,
733 monotonic_timewait_0010,
734 monotonic_timewait_0020,
735 monotonic_timewait_0030,
736 timeoutnp_timewait_0010,
737 timeoutnp_timewait_0020,
738 clockwait_timedwait_0060,
739 clockwait_timedwait_0070,
740 };
741
main(void)742 int main(void)
743 {
744 int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
745 for (int pos = 0; pos < num; ++pos) {
746 G_Fun_Array[pos]();
747 }
748 return t_status;
749 }
750