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