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