1570af302Sopenharmony_ci/* 2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4570af302Sopenharmony_ci * you may not use this file except in compliance with the License. 5570af302Sopenharmony_ci * You may obtain a copy of the License at 6570af302Sopenharmony_ci * 7570af302Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8570af302Sopenharmony_ci * 9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12570af302Sopenharmony_ci * See the License for the specific language governing permissions and 13570af302Sopenharmony_ci * limitations under the License. 14570af302Sopenharmony_ci */ 15570af302Sopenharmony_ci#include <errno.h> 16570af302Sopenharmony_ci#include <pthread.h> 17570af302Sopenharmony_ci#include "pthread_util.h" 18570af302Sopenharmony_ci 19570af302Sopenharmony_ciextern int __pthread_cond_timedwait_time64(pthread_cond_t *__restrict, 20570af302Sopenharmony_ci pthread_mutex_t *__restrict, const struct timespec *__restrict); 21570af302Sopenharmony_ci 22570af302Sopenharmony_ci// pthread_cond_clockwait 23570af302Sopenharmony_cistatic pthread_mutex_t c_mtx1 = PTHREAD_MUTEX_INITIALIZER; 24570af302Sopenharmony_cistatic pthread_cond_t c_cond1 = PTHREAD_COND_INITIALIZER; 25570af302Sopenharmony_cistatic pthread_mutex_t c_mtx2 = PTHREAD_MUTEX_INITIALIZER; 26570af302Sopenharmony_cistatic pthread_mutex_t c_mtx10 = PTHREAD_MUTEX_INITIALIZER; 27570af302Sopenharmony_cistatic pthread_cond_t c_cond2 = PTHREAD_COND_INITIALIZER; 28570af302Sopenharmony_cistatic pthread_mutex_t c_mtx3 = PTHREAD_MUTEX_INITIALIZER; 29570af302Sopenharmony_cistatic pthread_cond_t c_cond3 = PTHREAD_COND_INITIALIZER; 30570af302Sopenharmony_cistatic pthread_mutex_t c_mtx4 = PTHREAD_MUTEX_INITIALIZER; 31570af302Sopenharmony_cistatic pthread_cond_t c_cond4 = PTHREAD_COND_INITIALIZER; 32570af302Sopenharmony_cistatic pthread_mutex_t c_mtx5 = PTHREAD_MUTEX_INITIALIZER; 33570af302Sopenharmony_cistatic pthread_cond_t c_cond5 = PTHREAD_COND_INITIALIZER; 34570af302Sopenharmony_cistatic pthread_mutex_t c_mtx6 = PTHREAD_MUTEX_INITIALIZER; 35570af302Sopenharmony_cistatic pthread_cond_t c_cond6 = PTHREAD_COND_INITIALIZER; 36570af302Sopenharmony_ci// pthread_cond_timedwait_monotonic_np 37570af302Sopenharmony_cistatic pthread_mutex_t m_mtx1 = PTHREAD_MUTEX_INITIALIZER; 38570af302Sopenharmony_cistatic pthread_cond_t m_cond1 = PTHREAD_COND_INITIALIZER; 39570af302Sopenharmony_cistatic pthread_mutex_t m_mtx2 = PTHREAD_MUTEX_INITIALIZER; 40570af302Sopenharmony_cistatic pthread_cond_t m_cond2 = PTHREAD_COND_INITIALIZER; 41570af302Sopenharmony_cistatic pthread_mutex_t m_mtx3 = PTHREAD_MUTEX_INITIALIZER; 42570af302Sopenharmony_cistatic pthread_cond_t m_cond3 = PTHREAD_COND_INITIALIZER; 43570af302Sopenharmony_ci// pthread_cond_timeout_np 44570af302Sopenharmony_cistatic pthread_mutex_t u_mtx1 = PTHREAD_MUTEX_INITIALIZER; 45570af302Sopenharmony_cistatic pthread_cond_t u_cond1 = PTHREAD_COND_INITIALIZER; 46570af302Sopenharmony_cistatic pthread_mutex_t u_mtx2 = PTHREAD_MUTEX_INITIALIZER; 47570af302Sopenharmony_cistatic pthread_cond_t u_cond2 = PTHREAD_COND_INITIALIZER; 48570af302Sopenharmony_ci 49570af302Sopenharmony_ci/** 50570af302Sopenharmony_ci * @tc.number : pthread_cond_timedwait_0010 51570af302Sopenharmony_ci * @tc.desc : Test whether the pthread_cond_timedwait is normal 52570af302Sopenharmony_ci * @tc.level : Level 0 53570af302Sopenharmony_ci */ 54570af302Sopenharmony_civoid pthread_cond_timedwait_0010(void) 55570af302Sopenharmony_ci{ 56570af302Sopenharmony_ci pthread_condattr_t a; 57570af302Sopenharmony_ci EXPECT_EQ(pthread_condattr_init(&a), 0); 58570af302Sopenharmony_ci pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 59570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_init(&cond, &a), 0); 60570af302Sopenharmony_ci pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 61570af302Sopenharmony_ci struct timespec ts = {0}; 62570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&mutex), 0); 63570af302Sopenharmony_ci EXPECT_EQ(clock_gettime(CLOCK_REALTIME, &ts), 0); 64570af302Sopenharmony_ci 65570af302Sopenharmony_ci ts.tv_nsec += SLEEP_10_MS*MS_PER_S*MS_PER_S; 66570af302Sopenharmony_ci if (ts.tv_nsec >= MS_PER_S*MS_PER_S*MS_PER_S) { 67570af302Sopenharmony_ci ts.tv_nsec -= MS_PER_S*MS_PER_S*MS_PER_S; 68570af302Sopenharmony_ci ts.tv_sec += 1; 69570af302Sopenharmony_ci } 70570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timedwait(&cond, &mutex, &ts), ETIMEDOUT); 71570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); 72570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&cond), 0); 73570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); 74570af302Sopenharmony_ci} 75570af302Sopenharmony_ci 76570af302Sopenharmony_ci/** 77570af302Sopenharmony_ci * @tc.number : pthread_cond_timedwait_0020 78570af302Sopenharmony_ci * @tc.desc : Test for pthread_cond_timedwait exceptions 79570af302Sopenharmony_ci * @tc.level : Level 2 80570af302Sopenharmony_ci */ 81570af302Sopenharmony_civoid pthread_cond_timedwait_0020(void) 82570af302Sopenharmony_ci{ 83570af302Sopenharmony_ci pthread_condattr_t a; 84570af302Sopenharmony_ci EXPECT_EQ(pthread_condattr_init(&a), 0); 85570af302Sopenharmony_ci pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 86570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_init(&cond, &a), 0); 87570af302Sopenharmony_ci pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 88570af302Sopenharmony_ci struct timespec ts = {0}; 89570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&mutex), 0); 90570af302Sopenharmony_ci EXPECT_EQ(clock_gettime(CLOCK_REALTIME, &ts), 0); 91570af302Sopenharmony_ci 92570af302Sopenharmony_ci ts.tv_nsec = NSEC_PER_SEC; 93570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timedwait(&cond, &mutex, &ts), EINVAL); 94570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); 95570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&cond), 0); 96570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); 97570af302Sopenharmony_ci} 98570af302Sopenharmony_ci 99570af302Sopenharmony_ci/** 100570af302Sopenharmony_ci * @tc.number : pthread_cond_timedwait_time64_0010 101570af302Sopenharmony_ci * @tc.desc : Test whether the pthread_cond_timedwait is normal 102570af302Sopenharmony_ci * @tc.level : Level 0 103570af302Sopenharmony_ci */ 104570af302Sopenharmony_civoid pthread_cond_timedwait_time64_0010(void) 105570af302Sopenharmony_ci{ 106570af302Sopenharmony_ci pthread_condattr_t a; 107570af302Sopenharmony_ci EXPECT_EQ(pthread_condattr_init(&a), 0); 108570af302Sopenharmony_ci pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 109570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_init(&cond, &a), 0); 110570af302Sopenharmony_ci pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 111570af302Sopenharmony_ci struct timespec ts = {0}; 112570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&mutex), 0); 113570af302Sopenharmony_ci EXPECT_EQ(clock_gettime(CLOCK_REALTIME, &ts), 0); 114570af302Sopenharmony_ci 115570af302Sopenharmony_ci ts.tv_nsec += SLEEP_10_MS*MS_PER_S*MS_PER_S; 116570af302Sopenharmony_ci if (ts.tv_nsec >= MS_PER_S*MS_PER_S*MS_PER_S) { 117570af302Sopenharmony_ci ts.tv_nsec -= MS_PER_S*MS_PER_S*MS_PER_S; 118570af302Sopenharmony_ci ts.tv_sec += 1; 119570af302Sopenharmony_ci } 120570af302Sopenharmony_ci EXPECT_EQ(__pthread_cond_timedwait_time64(&cond, &mutex, &ts), ETIMEDOUT); 121570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); 122570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&cond), 0); 123570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); 124570af302Sopenharmony_ci} 125570af302Sopenharmony_ci 126570af302Sopenharmony_ci/** 127570af302Sopenharmony_ci * @tc.number : pthread_cond_timedwait_monotonic_np_0010 128570af302Sopenharmony_ci * @tc.desc : Test whether the pthread_cond_timedwait_monotonic_np is normal 129570af302Sopenharmony_ci * @tc.level : Level 0 130570af302Sopenharmony_ci */ 131570af302Sopenharmony_civoid pthread_cond_timedwait_monotonic_np_0010(void) 132570af302Sopenharmony_ci{ 133570af302Sopenharmony_ci pthread_cond_t cond; 134570af302Sopenharmony_ci pthread_mutex_t mutex; 135570af302Sopenharmony_ci struct timespec ts = {0}; 136570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_init(&cond, 0), 0); 137570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_init(&mutex, 0), 0); 138570af302Sopenharmony_ci 139570af302Sopenharmony_ci EXPECT_EQ(clock_gettime(CLOCK_MONOTONIC, &ts), 0); 140570af302Sopenharmony_ci ts.tv_nsec += SLEEP_10_MS*MS_PER_S*MS_PER_S; 141570af302Sopenharmony_ci if (ts.tv_nsec >= MS_PER_S*MS_PER_S*MS_PER_S) { 142570af302Sopenharmony_ci ts.tv_nsec -= MS_PER_S*MS_PER_S*MS_PER_S; 143570af302Sopenharmony_ci ts.tv_sec += 1; 144570af302Sopenharmony_ci } 145570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timedwait_monotonic_np(&cond, &mutex, &ts), ETIMEDOUT); 146570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); 147570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&cond), 0); 148570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); 149570af302Sopenharmony_ci} 150570af302Sopenharmony_ci 151570af302Sopenharmony_ci/** 152570af302Sopenharmony_ci * @tc.number : pthread_cond_timedwait_monotonic_np_0020 153570af302Sopenharmony_ci * @tc.desc : Test whether the pthread_cond_timedwait_monotonic_np is invalid 154570af302Sopenharmony_ci * @tc.level : Level 2 155570af302Sopenharmony_ci */ 156570af302Sopenharmony_civoid pthread_cond_timedwait_monotonic_np_0020(void) 157570af302Sopenharmony_ci{ 158570af302Sopenharmony_ci pthread_cond_t *cond = (pthread_cond_t *)NULL; 159570af302Sopenharmony_ci pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 160570af302Sopenharmony_ci struct timespec ts = {0}; 161570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timedwait_monotonic_np(cond, &mutex, &ts), EINVAL); 162570af302Sopenharmony_ci} 163570af302Sopenharmony_ci 164570af302Sopenharmony_ci/** 165570af302Sopenharmony_ci * @tc.number : pthread_cond_timeout_np_0010 166570af302Sopenharmony_ci * @tc.desc : Test whether the pthread_cond_timeout_np is normal 167570af302Sopenharmony_ci * @tc.level : Level 0 168570af302Sopenharmony_ci */ 169570af302Sopenharmony_civoid pthread_cond_timeout_np_0010(void) 170570af302Sopenharmony_ci{ 171570af302Sopenharmony_ci unsigned int ms = SLEEP_100_MS; 172570af302Sopenharmony_ci pthread_condattr_t a; 173570af302Sopenharmony_ci EXPECT_EQ(pthread_condattr_init(&a), 0); 174570af302Sopenharmony_ci pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 175570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_init(&cond, &a), 0); 176570af302Sopenharmony_ci pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 177570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timeout_np(&cond, &mutex, ms), ETIMEDOUT); 178570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); 179570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&cond), 0); 180570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); 181570af302Sopenharmony_ci} 182570af302Sopenharmony_ci 183570af302Sopenharmony_ci/** 184570af302Sopenharmony_ci * @tc.number : pthread_cond_clockwait_0010 185570af302Sopenharmony_ci * @tc.desc : Test multiple cases of pthread_cond_clockwait 186570af302Sopenharmony_ci * @tc.level : Level 2 187570af302Sopenharmony_ci */ 188570af302Sopenharmony_civoid pthread_cond_clockwait_0010(void) 189570af302Sopenharmony_ci{ 190570af302Sopenharmony_ci pthread_condattr_t a; 191570af302Sopenharmony_ci EXPECT_EQ(pthread_condattr_init(&a), 0); 192570af302Sopenharmony_ci pthread_cond_t cond = PTHREAD_COND_INITIALIZER; 193570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_init(&cond, &a), 0); 194570af302Sopenharmony_ci pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; 195570af302Sopenharmony_ci struct timespec ts = {0}; 196570af302Sopenharmony_ci 197570af302Sopenharmony_ci ts.tv_nsec += SLEEP_10_MS*MS_PER_S*MS_PER_S; 198570af302Sopenharmony_ci if (ts.tv_nsec >= MS_PER_S*MS_PER_S*MS_PER_S) { 199570af302Sopenharmony_ci ts.tv_nsec -= MS_PER_S*MS_PER_S*MS_PER_S; 200570af302Sopenharmony_ci ts.tv_sec += 1; 201570af302Sopenharmony_ci } 202570af302Sopenharmony_ci clockid_t clock_id = CLOCK_REALTIME; 203570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&cond, &mutex, clock_id, &ts), ETIMEDOUT); 204570af302Sopenharmony_ci clock_id = CLOCK_MONOTONIC; 205570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&cond, &mutex, clock_id, &ts), ETIMEDOUT); 206570af302Sopenharmony_ci clock_id = CLOCK_PROCESS_CPUTIME_ID; 207570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&cond, &mutex, clock_id, &ts), EINVAL); 208570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); 209570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&cond), 0); 210570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); 211570af302Sopenharmony_ci} 212570af302Sopenharmony_ci 213570af302Sopenharmony_ci/** 214570af302Sopenharmony_ci * @tc.number : pthread_cond_timedwait_Time_0010 215570af302Sopenharmony_ci * @tc.desc : Whether the interface is normal when the time is not set 216570af302Sopenharmony_ci * @tc.level : Level 0 217570af302Sopenharmony_ci */ 218570af302Sopenharmony_civoid pthread_cond_timedwait_Time_0010(void) 219570af302Sopenharmony_ci{ 220570af302Sopenharmony_ci pthread_cond_t cond; 221570af302Sopenharmony_ci pthread_mutex_t mutex; 222570af302Sopenharmony_ci struct timespec ts; 223570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_init(&cond, 0), 0); 224570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_init(&mutex, 0), 0); 225570af302Sopenharmony_ci 226570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timedwait_monotonic_np(&cond, &mutex, &ts), ETIMEDOUT); 227570af302Sopenharmony_ci 228570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&mutex), 0); 229570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&cond), 0); 230570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&mutex), 0); 231570af302Sopenharmony_ci} 232570af302Sopenharmony_ci 233570af302Sopenharmony_cistatic void *ClockWaitTimedwait1(void *arg) 234570af302Sopenharmony_ci{ 235570af302Sopenharmony_ci Msleep(SLEEP_50_MS); 236570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx1), 0); 237570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx1), 0); 238570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_signal(&c_cond1), 0); 239570af302Sopenharmony_ci return arg; 240570af302Sopenharmony_ci} 241570af302Sopenharmony_ci 242570af302Sopenharmony_cistatic void *ClockWaitTimedwait2(void *arg) 243570af302Sopenharmony_ci{ 244570af302Sopenharmony_ci const unsigned int nsecPerSec = NSEC_PER_SEC; 245570af302Sopenharmony_ci const unsigned int nsecPer100Ms = NSEC_PER_100MS; 246570af302Sopenharmony_ci struct timespec ts = {0}; 247570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx1), 0); 248570af302Sopenharmony_ci 249570af302Sopenharmony_ci clock_gettime(CLOCK_REALTIME, &ts); 250570af302Sopenharmony_ci ts.tv_sec = ts.tv_sec + (ts.tv_nsec + nsecPer100Ms) / nsecPerSec; 251570af302Sopenharmony_ci ts.tv_nsec = (ts.tv_nsec + nsecPer100Ms) % nsecPerSec; 252570af302Sopenharmony_ci 253570af302Sopenharmony_ci clockid_t clock_id = CLOCK_REALTIME; 254570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&c_cond1, &c_mtx1, clock_id, &ts), 0); 255570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx1), 0); 256570af302Sopenharmony_ci return arg; 257570af302Sopenharmony_ci} 258570af302Sopenharmony_ci 259570af302Sopenharmony_ci/** 260570af302Sopenharmony_ci * @tc.number : clockwait_timedwait_0010 261570af302Sopenharmony_ci * @tc.desc : Test the case of pthread_cond_clockwait timewait by CLOCK_REALTIME 262570af302Sopenharmony_ci * @tc.level : Level 1 263570af302Sopenharmony_ci */ 264570af302Sopenharmony_civoid clockwait_timedwait_0010(void) 265570af302Sopenharmony_ci{ 266570af302Sopenharmony_ci pthread_t tid1; 267570af302Sopenharmony_ci pthread_t tid2; 268570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid1, NULL, ClockWaitTimedwait1, NULL), 0); 269570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid2, NULL, ClockWaitTimedwait2, NULL), 0); 270570af302Sopenharmony_ci Msleep(SLEEP_100_MS); 271570af302Sopenharmony_ci pthread_join(tid1, NULL); 272570af302Sopenharmony_ci pthread_join(tid2, NULL); 273570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&c_cond1), 0); 274570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&c_mtx1), 0); 275570af302Sopenharmony_ci} 276570af302Sopenharmony_ci 277570af302Sopenharmony_cistatic void *ClockWaitTimeOut(void *arg) 278570af302Sopenharmony_ci{ 279570af302Sopenharmony_ci struct timespec ts = {0}; 280570af302Sopenharmony_ci struct timespec tsNow = {0}; 281570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx2), 0); 282570af302Sopenharmony_ci 283570af302Sopenharmony_ci clockid_t clock_id = CLOCK_REALTIME; 284570af302Sopenharmony_ci GetDelayedTimeByClockid(&ts, SLEEP_100_MS, clock_id); 285570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&c_cond2, &c_mtx2, clock_id, &ts), ETIMEDOUT); 286570af302Sopenharmony_ci clock_gettime(CLOCK_REALTIME, &tsNow); 287570af302Sopenharmony_ci 288570af302Sopenharmony_ci int timeDiff = GetTimeDiff(tsNow, ts); 289570af302Sopenharmony_ci EXPECT_GE(timeDiff, 0); 290570af302Sopenharmony_ci EXPECT_LE(timeDiff, SLEEP_20_MS); 291570af302Sopenharmony_ci 292570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx2), 0); 293570af302Sopenharmony_ci return arg; 294570af302Sopenharmony_ci} 295570af302Sopenharmony_ci 296570af302Sopenharmony_ci/** 297570af302Sopenharmony_ci * @tc.number : clockwait_timedwait_0020 298570af302Sopenharmony_ci * @tc.desc : Test the case of pthread_cond_clockwait timeout by CLOCK_REALTIME 299570af302Sopenharmony_ci * @tc.level : Level 1 300570af302Sopenharmony_ci */ 301570af302Sopenharmony_civoid clockwait_timedwait_0020(void) 302570af302Sopenharmony_ci{ 303570af302Sopenharmony_ci pthread_t tid; 304570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid, NULL, ClockWaitTimeOut, NULL), 0); 305570af302Sopenharmony_ci Msleep(SLEEP_200_MS); 306570af302Sopenharmony_ci pthread_join(tid, NULL); 307570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&c_cond2), 0); 308570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&c_mtx2), 0); 309570af302Sopenharmony_ci} 310570af302Sopenharmony_ci 311570af302Sopenharmony_cistatic void *ClockWaitTimedwait3(void *arg) 312570af302Sopenharmony_ci{ 313570af302Sopenharmony_ci Msleep(SLEEP_50_MS); 314570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx3), 0); 315570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx3), 0); 316570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_signal(&c_cond3), 0); 317570af302Sopenharmony_ci return arg; 318570af302Sopenharmony_ci} 319570af302Sopenharmony_ci 320570af302Sopenharmony_cistatic void *ClockWaitTimedwait4(void *arg) 321570af302Sopenharmony_ci{ 322570af302Sopenharmony_ci const unsigned int nsecPerSec = NSEC_PER_SEC; 323570af302Sopenharmony_ci const unsigned int nsecPer100Ms = NSEC_PER_100MS; 324570af302Sopenharmony_ci struct timespec ts = {0}; 325570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx3), 0); 326570af302Sopenharmony_ci 327570af302Sopenharmony_ci clock_gettime(CLOCK_MONOTONIC, &ts); 328570af302Sopenharmony_ci ts.tv_sec = ts.tv_sec + (ts.tv_nsec + nsecPer100Ms) / nsecPerSec; 329570af302Sopenharmony_ci ts.tv_nsec = (ts.tv_nsec + nsecPer100Ms) % nsecPerSec; 330570af302Sopenharmony_ci 331570af302Sopenharmony_ci clockid_t clock_id = CLOCK_MONOTONIC; 332570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&c_cond3, &c_mtx3, clock_id, &ts), 0); 333570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx3), 0); 334570af302Sopenharmony_ci return arg; 335570af302Sopenharmony_ci} 336570af302Sopenharmony_ci 337570af302Sopenharmony_ci/** 338570af302Sopenharmony_ci * @tc.number : clockwait_timedwait_0030 339570af302Sopenharmony_ci * @tc.desc : Test the case of pthread_cond_clockwait timewait by CLOCK_MONOTONIC 340570af302Sopenharmony_ci * @tc.level : Level 1 341570af302Sopenharmony_ci */ 342570af302Sopenharmony_civoid clockwait_timedwait_0030(void) 343570af302Sopenharmony_ci{ 344570af302Sopenharmony_ci pthread_t tid1; 345570af302Sopenharmony_ci pthread_t tid2; 346570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid1, NULL, ClockWaitTimedwait3, NULL), 0); 347570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid2, NULL, ClockWaitTimedwait4, NULL), 0); 348570af302Sopenharmony_ci Msleep(SLEEP_100_MS); 349570af302Sopenharmony_ci pthread_join(tid1, NULL); 350570af302Sopenharmony_ci pthread_join(tid2, NULL); 351570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&c_cond3), 0); 352570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&c_mtx3), 0); 353570af302Sopenharmony_ci} 354570af302Sopenharmony_ci 355570af302Sopenharmony_cistatic void *ClockWaitTimeOut2(void *arg) 356570af302Sopenharmony_ci{ 357570af302Sopenharmony_ci struct timespec ts = {0}; 358570af302Sopenharmony_ci struct timespec tsNow = {0}; 359570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx10), 0); 360570af302Sopenharmony_ci 361570af302Sopenharmony_ci clockid_t clock_id = CLOCK_MONOTONIC; 362570af302Sopenharmony_ci GetDelayedTimeByClockid(&ts, SLEEP_100_MS, clock_id); 363570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&c_cond2, &c_mtx10, clock_id, &ts), ETIMEDOUT); 364570af302Sopenharmony_ci clock_gettime(CLOCK_MONOTONIC, &tsNow); 365570af302Sopenharmony_ci 366570af302Sopenharmony_ci int timeDiff = GetTimeDiff(tsNow, ts); 367570af302Sopenharmony_ci EXPECT_GE(timeDiff, 0); 368570af302Sopenharmony_ci EXPECT_LE(timeDiff, SLEEP_20_MS); 369570af302Sopenharmony_ci 370570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx10), 0); 371570af302Sopenharmony_ci return arg; 372570af302Sopenharmony_ci} 373570af302Sopenharmony_ci 374570af302Sopenharmony_ci/** 375570af302Sopenharmony_ci * @tc.number : clockwait_timedwait_0040 376570af302Sopenharmony_ci * @tc.desc : Test the case of pthread_cond_clockwait timeout by CLOCK_MONOTONIC 377570af302Sopenharmony_ci * @tc.level : Level 1 378570af302Sopenharmony_ci */ 379570af302Sopenharmony_civoid clockwait_timedwait_0040(void) 380570af302Sopenharmony_ci{ 381570af302Sopenharmony_ci pthread_t tid; 382570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid, NULL, ClockWaitTimeOut2, NULL), 0); 383570af302Sopenharmony_ci Msleep(SLEEP_200_MS); 384570af302Sopenharmony_ci pthread_join(tid, NULL); 385570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&c_cond2), 0); 386570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&c_mtx10), 0); 387570af302Sopenharmony_ci} 388570af302Sopenharmony_ci 389570af302Sopenharmony_cistatic void *ClockWaitTimeMismatch(void *arg) 390570af302Sopenharmony_ci{ 391570af302Sopenharmony_ci const unsigned int nsecPerSec = NSEC_PER_SEC; 392570af302Sopenharmony_ci const unsigned int nsecPer100Ms = NSEC_PER_100MS; 393570af302Sopenharmony_ci struct timespec ts = {0}; 394570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx4), 0); 395570af302Sopenharmony_ci 396570af302Sopenharmony_ci clock_gettime(CLOCK_MONOTONIC, &ts); 397570af302Sopenharmony_ci ts.tv_sec = ts.tv_sec + (ts.tv_nsec + nsecPer100Ms) / nsecPerSec; 398570af302Sopenharmony_ci ts.tv_nsec = (ts.tv_nsec + nsecPer100Ms) % nsecPerSec; 399570af302Sopenharmony_ci 400570af302Sopenharmony_ci clockid_t clock_id = CLOCK_REALTIME; 401570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&c_cond4, &c_mtx4, clock_id, &ts), ETIMEDOUT); 402570af302Sopenharmony_ci 403570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx4), 0); 404570af302Sopenharmony_ci return arg; 405570af302Sopenharmony_ci} 406570af302Sopenharmony_ci 407570af302Sopenharmony_cistatic void *ClockWaitTimeMismatch1(void *arg) 408570af302Sopenharmony_ci{ 409570af302Sopenharmony_ci Msleep(SLEEP_50_MS); 410570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx5), 0); 411570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx5), 0); 412570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_signal(&c_cond5), 0); 413570af302Sopenharmony_ci return arg; 414570af302Sopenharmony_ci} 415570af302Sopenharmony_ci 416570af302Sopenharmony_cistatic void *ClockWaitTimeMismatch2(void *arg) 417570af302Sopenharmony_ci{ 418570af302Sopenharmony_ci const unsigned int nsecPerSec = NSEC_PER_SEC; 419570af302Sopenharmony_ci const unsigned int nsecPer100Ms = NSEC_PER_100MS; 420570af302Sopenharmony_ci struct timespec ts = {0}; 421570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx5), 0); 422570af302Sopenharmony_ci 423570af302Sopenharmony_ci clock_gettime(CLOCK_REALTIME, &ts); 424570af302Sopenharmony_ci ts.tv_sec = ts.tv_sec + (ts.tv_nsec + nsecPer100Ms) / nsecPerSec; 425570af302Sopenharmony_ci ts.tv_nsec = (ts.tv_nsec + nsecPer100Ms) % nsecPerSec; 426570af302Sopenharmony_ci 427570af302Sopenharmony_ci clockid_t clock_id = CLOCK_REALTIME; 428570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&c_cond5, &c_mtx5, clock_id, &ts), 0); 429570af302Sopenharmony_ci 430570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx5), 0); 431570af302Sopenharmony_ci return arg; 432570af302Sopenharmony_ci} 433570af302Sopenharmony_ci 434570af302Sopenharmony_ci/** 435570af302Sopenharmony_ci * @tc.number : clockwait_timedwait_0050 436570af302Sopenharmony_ci * @tc.desc : Test the case of pthread_cond_clockwait time mismatch 437570af302Sopenharmony_ci * @tc.level : Level 2 438570af302Sopenharmony_ci */ 439570af302Sopenharmony_civoid clockwait_timedwait_0050(void) 440570af302Sopenharmony_ci{ 441570af302Sopenharmony_ci pthread_t tid; 442570af302Sopenharmony_ci pthread_t tid1; 443570af302Sopenharmony_ci pthread_t tid2; 444570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid1, NULL, ClockWaitTimeMismatch1, NULL), 0); 445570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid2, NULL, ClockWaitTimeMismatch2, NULL), 0); 446570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid, NULL, ClockWaitTimeMismatch, NULL), 0); 447570af302Sopenharmony_ci Msleep(SLEEP_200_MS); 448570af302Sopenharmony_ci pthread_join(tid1, NULL); 449570af302Sopenharmony_ci pthread_join(tid2, NULL); 450570af302Sopenharmony_ci pthread_join(tid, NULL); 451570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&c_cond4), 0); 452570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&c_mtx4), 0); 453570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&c_cond5), 0); 454570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&c_mtx5), 0); 455570af302Sopenharmony_ci} 456570af302Sopenharmony_ci 457570af302Sopenharmony_cistatic void *ClockWaitTimeMismatch3(void *arg) 458570af302Sopenharmony_ci{ 459570af302Sopenharmony_ci struct timespec ts = {0}; 460570af302Sopenharmony_ci struct timespec tsNow = {0}; 461570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx6), 0); 462570af302Sopenharmony_ci Msleep(SLEEP_20_MS); 463570af302Sopenharmony_ci GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_REALTIME); 464570af302Sopenharmony_ci clock_gettime(CLOCK_REALTIME, &tsNow); 465570af302Sopenharmony_ci tsNow.tv_sec += 1; 466570af302Sopenharmony_ci EXPECT_EQ(clock_settime(CLOCK_REALTIME, &tsNow), 0); 467570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&c_cond6, &c_mtx6, CLOCK_REALTIME, &ts), ETIMEDOUT); 468570af302Sopenharmony_ci 469570af302Sopenharmony_ci clock_gettime(CLOCK_REALTIME, &tsNow); 470570af302Sopenharmony_ci tsNow.tv_sec -= 1; 471570af302Sopenharmony_ci clock_settime(CLOCK_REALTIME, &tsNow); 472570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx6), 0); 473570af302Sopenharmony_ci 474570af302Sopenharmony_ci return arg; 475570af302Sopenharmony_ci} 476570af302Sopenharmony_ci 477570af302Sopenharmony_ci/** 478570af302Sopenharmony_ci * @tc.number : clockwait_timedwait_0060 479570af302Sopenharmony_ci * @tc.desc : A case for testing the CLOCK_REALTIME feature of pthread_cond_clockwait. 480570af302Sopenharmony_ci * @tc.level : Level 1 481570af302Sopenharmony_ci */ 482570af302Sopenharmony_civoid clockwait_timedwait_0060(void) 483570af302Sopenharmony_ci{ 484570af302Sopenharmony_ci pthread_t tid; 485570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid, NULL, ClockWaitTimeMismatch3, NULL), 0); 486570af302Sopenharmony_ci Msleep(SLEEP_200_MS); 487570af302Sopenharmony_ci pthread_join(tid, NULL); 488570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&c_cond6), 0); 489570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&c_mtx6), 0); 490570af302Sopenharmony_ci} 491570af302Sopenharmony_ci 492570af302Sopenharmony_cistatic void *ClockWaitTimeMismatch4(void *arg) 493570af302Sopenharmony_ci{ 494570af302Sopenharmony_ci Msleep(SLEEP_50_MS); 495570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx6), 0); 496570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx6), 0); 497570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_signal(&c_cond6), 0); 498570af302Sopenharmony_ci return arg; 499570af302Sopenharmony_ci} 500570af302Sopenharmony_ci 501570af302Sopenharmony_cistatic void *ClockWaitTimeMismatch5(void *arg) 502570af302Sopenharmony_ci{ 503570af302Sopenharmony_ci struct timespec ts = {0}; 504570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&c_mtx6), 0); 505570af302Sopenharmony_ci Msleep(SLEEP_20_MS); 506570af302Sopenharmony_ci GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_MONOTONIC); 507570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_clockwait(&c_cond6, &c_mtx6, CLOCK_MONOTONIC, &ts), 0); 508570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&c_mtx6), 0); 509570af302Sopenharmony_ci return arg; 510570af302Sopenharmony_ci} 511570af302Sopenharmony_ci 512570af302Sopenharmony_ci/** 513570af302Sopenharmony_ci * @tc.number : clockwait_timedwait_0070 514570af302Sopenharmony_ci * @tc.desc : A case for testing the CLOCK_MONOTONIC feature of pthread_cond_clockwait. 515570af302Sopenharmony_ci * @tc.level : Level 1 516570af302Sopenharmony_ci */ 517570af302Sopenharmony_civoid clockwait_timedwait_0070(void) 518570af302Sopenharmony_ci{ 519570af302Sopenharmony_ci struct timespec tsNow = {0}; 520570af302Sopenharmony_ci pthread_t tid1; 521570af302Sopenharmony_ci pthread_t tid2; 522570af302Sopenharmony_ci 523570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid1, NULL, ClockWaitTimeMismatch4, NULL), 0); 524570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid2, NULL, ClockWaitTimeMismatch5, NULL), 0); 525570af302Sopenharmony_ci Msleep(SLEEP_100_MS); 526570af302Sopenharmony_ci clock_gettime(CLOCK_MONOTONIC, &tsNow); 527570af302Sopenharmony_ci tsNow.tv_sec += 1; 528570af302Sopenharmony_ci EXPECT_EQ(clock_settime(CLOCK_REALTIME, &tsNow), 0); 529570af302Sopenharmony_ci pthread_join(tid1, NULL); 530570af302Sopenharmony_ci pthread_join(tid2, NULL); 531570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&c_cond6), 0); 532570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&c_mtx6), 0); 533570af302Sopenharmony_ci} 534570af302Sopenharmony_ci 535570af302Sopenharmony_cistatic void *PthreadCondMonotonicTimeWait1(void *arg) 536570af302Sopenharmony_ci{ 537570af302Sopenharmony_ci Msleep(SLEEP_50_MS); 538570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&m_mtx1), 0); 539570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&m_mtx1), 0); 540570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_signal(&m_cond1), 0); 541570af302Sopenharmony_ci return arg; 542570af302Sopenharmony_ci} 543570af302Sopenharmony_ci 544570af302Sopenharmony_cistatic void *PthreadCondMonotonicTimeWait2(void *arg) 545570af302Sopenharmony_ci{ 546570af302Sopenharmony_ci const unsigned int nsecPerSec = NSEC_PER_SEC; 547570af302Sopenharmony_ci const unsigned int nsecPer100Ms = NSEC_PER_100MS; 548570af302Sopenharmony_ci struct timespec ts = {0}; 549570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&m_mtx1), 0); 550570af302Sopenharmony_ci 551570af302Sopenharmony_ci clock_gettime(CLOCK_MONOTONIC, &ts); 552570af302Sopenharmony_ci ts.tv_sec = ts.tv_sec + (ts.tv_nsec + nsecPer100Ms) / nsecPerSec; 553570af302Sopenharmony_ci ts.tv_nsec = (ts.tv_nsec + nsecPer100Ms) % nsecPerSec; 554570af302Sopenharmony_ci 555570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timedwait_monotonic_np(&m_cond1, &m_mtx1, &ts), 0); 556570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&m_mtx1), 0); 557570af302Sopenharmony_ci return arg; 558570af302Sopenharmony_ci} 559570af302Sopenharmony_ci 560570af302Sopenharmony_ci/** 561570af302Sopenharmony_ci * @tc.number : monotonic_timewait_0010 562570af302Sopenharmony_ci * @tc.desc : Test the case of pthread_cond_timedwait_monotonic_np timewait 563570af302Sopenharmony_ci * @tc.level : Level 1 564570af302Sopenharmony_ci */ 565570af302Sopenharmony_civoid monotonic_timewait_0010(void) 566570af302Sopenharmony_ci{ 567570af302Sopenharmony_ci pthread_t tid1; 568570af302Sopenharmony_ci pthread_t tid2; 569570af302Sopenharmony_ci 570570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid1, NULL, PthreadCondMonotonicTimeWait1, NULL), 0); 571570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid2, NULL, PthreadCondMonotonicTimeWait2, NULL), 0); 572570af302Sopenharmony_ci 573570af302Sopenharmony_ci Msleep(SLEEP_100_MS); 574570af302Sopenharmony_ci pthread_join(tid1, NULL); 575570af302Sopenharmony_ci pthread_join(tid2, NULL); 576570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&m_cond1), 0); 577570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&m_mtx1), 0); 578570af302Sopenharmony_ci} 579570af302Sopenharmony_ci 580570af302Sopenharmony_cistatic void *PthreadCondMonotonicTimeOut(void *arg) 581570af302Sopenharmony_ci{ 582570af302Sopenharmony_ci struct timespec ts = {0}; 583570af302Sopenharmony_ci struct timespec tsNow = {0}; 584570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&m_mtx2), 0); 585570af302Sopenharmony_ci 586570af302Sopenharmony_ci GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_MONOTONIC); 587570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timedwait_monotonic_np(&m_cond2, &m_mtx2, &ts), ETIMEDOUT); 588570af302Sopenharmony_ci clock_gettime(CLOCK_MONOTONIC, &tsNow); 589570af302Sopenharmony_ci 590570af302Sopenharmony_ci int timeDiff = GetTimeDiff(tsNow, ts); 591570af302Sopenharmony_ci EXPECT_GE(timeDiff, 0); 592570af302Sopenharmony_ci EXPECT_LE(timeDiff, SLEEP_20_MS); 593570af302Sopenharmony_ci 594570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&m_mtx2), 0); 595570af302Sopenharmony_ci return arg; 596570af302Sopenharmony_ci} 597570af302Sopenharmony_ci 598570af302Sopenharmony_ci/** 599570af302Sopenharmony_ci * @tc.number : monotonic_timewait_0020 600570af302Sopenharmony_ci * @tc.desc : Test the case of pthread_cond_timedwait_monotonic_np timeout 601570af302Sopenharmony_ci * @tc.level : Level 1 602570af302Sopenharmony_ci */ 603570af302Sopenharmony_civoid monotonic_timewait_0020(void) 604570af302Sopenharmony_ci{ 605570af302Sopenharmony_ci pthread_t tid; 606570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid, NULL, PthreadCondMonotonicTimeOut, NULL), 0); 607570af302Sopenharmony_ci Msleep(SLEEP_200_MS); 608570af302Sopenharmony_ci pthread_join(tid, NULL); 609570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&m_cond2), 0); 610570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&m_mtx2), 0); 611570af302Sopenharmony_ci} 612570af302Sopenharmony_ci 613570af302Sopenharmony_cistatic void *PthreadCondMonotonicTimeEinval(void *arg) 614570af302Sopenharmony_ci{ 615570af302Sopenharmony_ci const long einvalNsec = NSEC_PER_SEC; 616570af302Sopenharmony_ci struct timespec ts = {0}; 617570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&m_mtx3), 0); 618570af302Sopenharmony_ci 619570af302Sopenharmony_ci ts.tv_sec = 1; 620570af302Sopenharmony_ci ts.tv_nsec = einvalNsec; 621570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timedwait_monotonic_np(&m_cond3, &m_mtx3, &ts), EINVAL); 622570af302Sopenharmony_ci 623570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&m_mtx3), 0); 624570af302Sopenharmony_ci return arg; 625570af302Sopenharmony_ci} 626570af302Sopenharmony_ci 627570af302Sopenharmony_ci/** 628570af302Sopenharmony_ci * @tc.number : monotonic_timewait_0030 629570af302Sopenharmony_ci * @tc.desc : Test the case of pthread_cond_timedwait_monotonic_np EINVAL 630570af302Sopenharmony_ci * @tc.level : Level 2 631570af302Sopenharmony_ci */ 632570af302Sopenharmony_civoid monotonic_timewait_0030(void) 633570af302Sopenharmony_ci{ 634570af302Sopenharmony_ci pthread_t tid; 635570af302Sopenharmony_ci 636570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid, NULL, PthreadCondMonotonicTimeEinval, NULL), 0); 637570af302Sopenharmony_ci 638570af302Sopenharmony_ci Msleep(SLEEP_200_MS); 639570af302Sopenharmony_ci pthread_join(tid, NULL); 640570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&m_cond3), 0); 641570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&m_mtx3), 0); 642570af302Sopenharmony_ci} 643570af302Sopenharmony_ci 644570af302Sopenharmony_cistatic void *PthreadCondUnsignedTimeWait1(void *arg) 645570af302Sopenharmony_ci{ 646570af302Sopenharmony_ci Msleep(SLEEP_50_MS); 647570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&u_mtx1), 0); 648570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&u_mtx1), 0); 649570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_signal(&u_cond1), 0); 650570af302Sopenharmony_ci return arg; 651570af302Sopenharmony_ci} 652570af302Sopenharmony_ci 653570af302Sopenharmony_cistatic void *PthreadCondUnsignedTimeWait2(void *arg) 654570af302Sopenharmony_ci{ 655570af302Sopenharmony_ci unsigned int ms = SLEEP_100_MS; 656570af302Sopenharmony_ci struct timespec ts = {0}; 657570af302Sopenharmony_ci 658570af302Sopenharmony_ci GetDelayedTimeByClockid(&ts, ms, CLOCK_MONOTONIC); 659570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&u_mtx1), 0); 660570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timeout_np(&u_cond1, &u_mtx1, ms), 0); 661570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&u_mtx1), 0); 662570af302Sopenharmony_ci return arg; 663570af302Sopenharmony_ci} 664570af302Sopenharmony_ci 665570af302Sopenharmony_ci/** 666570af302Sopenharmony_ci * @tc.number : timeoutnp_timewait_0010 667570af302Sopenharmony_ci * @tc.desc : Test the case of pthread_cond_timeout_np timewait 668570af302Sopenharmony_ci * @tc.level : Level 1 669570af302Sopenharmony_ci */ 670570af302Sopenharmony_civoid timeoutnp_timewait_0010(void) 671570af302Sopenharmony_ci{ 672570af302Sopenharmony_ci pthread_t tid1; 673570af302Sopenharmony_ci pthread_t tid2; 674570af302Sopenharmony_ci 675570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid1, NULL, PthreadCondUnsignedTimeWait1, NULL), 0); 676570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid2, NULL, PthreadCondUnsignedTimeWait2, NULL), 0); 677570af302Sopenharmony_ci 678570af302Sopenharmony_ci Msleep(SLEEP_100_MS); 679570af302Sopenharmony_ci pthread_join(tid1, NULL); 680570af302Sopenharmony_ci pthread_join(tid2, NULL); 681570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&u_cond1), 0); 682570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&u_mtx1), 0); 683570af302Sopenharmony_ci} 684570af302Sopenharmony_ci 685570af302Sopenharmony_cistatic void *PthreadCondUnsignedTimeOut(void *arg) 686570af302Sopenharmony_ci{ 687570af302Sopenharmony_ci unsigned int ms = SLEEP_100_MS; 688570af302Sopenharmony_ci struct timespec ts = {0}; 689570af302Sopenharmony_ci struct timespec tsNow = {0}; 690570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_lock(&u_mtx2), 0); 691570af302Sopenharmony_ci 692570af302Sopenharmony_ci GetDelayedTimeByClockid(&ts, SLEEP_100_MS, CLOCK_MONOTONIC); 693570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_timeout_np(&u_cond2, &u_mtx2, ms), ETIMEDOUT); 694570af302Sopenharmony_ci clock_gettime(CLOCK_MONOTONIC, &tsNow); 695570af302Sopenharmony_ci 696570af302Sopenharmony_ci int timeDiff = GetTimeDiff(tsNow, ts); 697570af302Sopenharmony_ci EXPECT_GE(timeDiff, 0); 698570af302Sopenharmony_ci EXPECT_LE(timeDiff, SLEEP_20_MS); 699570af302Sopenharmony_ci 700570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_unlock(&u_mtx2), 0); 701570af302Sopenharmony_ci return arg; 702570af302Sopenharmony_ci} 703570af302Sopenharmony_ci 704570af302Sopenharmony_ci/** 705570af302Sopenharmony_ci * @tc.number : timeoutnp_timewait_0020 706570af302Sopenharmony_ci * @tc.desc : Test the case of pthread_cond_timeout_np timeout 707570af302Sopenharmony_ci * @tc.level : Level 1 708570af302Sopenharmony_ci */ 709570af302Sopenharmony_civoid timeoutnp_timewait_0020(void) 710570af302Sopenharmony_ci{ 711570af302Sopenharmony_ci pthread_t tid; 712570af302Sopenharmony_ci EXPECT_EQ(pthread_create(&tid, NULL, PthreadCondUnsignedTimeOut, NULL), 0); 713570af302Sopenharmony_ci Msleep(SLEEP_200_MS); 714570af302Sopenharmony_ci pthread_join(tid, NULL); 715570af302Sopenharmony_ci EXPECT_EQ(pthread_cond_destroy(&u_cond2), 0); 716570af302Sopenharmony_ci EXPECT_EQ(pthread_mutex_destroy(&u_mtx2), 0); 717570af302Sopenharmony_ci} 718570af302Sopenharmony_ci 719570af302Sopenharmony_ciTEST_FUN G_Fun_Array[] = { 720570af302Sopenharmony_ci pthread_cond_timedwait_0010, 721570af302Sopenharmony_ci pthread_cond_timedwait_0020, 722570af302Sopenharmony_ci pthread_cond_timedwait_time64_0010, 723570af302Sopenharmony_ci pthread_cond_timedwait_monotonic_np_0010, 724570af302Sopenharmony_ci pthread_cond_timedwait_monotonic_np_0020, 725570af302Sopenharmony_ci pthread_cond_timeout_np_0010, 726570af302Sopenharmony_ci pthread_cond_clockwait_0010, 727570af302Sopenharmony_ci pthread_cond_timedwait_Time_0010, 728570af302Sopenharmony_ci clockwait_timedwait_0010, 729570af302Sopenharmony_ci clockwait_timedwait_0020, 730570af302Sopenharmony_ci clockwait_timedwait_0030, 731570af302Sopenharmony_ci clockwait_timedwait_0040, 732570af302Sopenharmony_ci clockwait_timedwait_0050, 733570af302Sopenharmony_ci monotonic_timewait_0010, 734570af302Sopenharmony_ci monotonic_timewait_0020, 735570af302Sopenharmony_ci monotonic_timewait_0030, 736570af302Sopenharmony_ci timeoutnp_timewait_0010, 737570af302Sopenharmony_ci timeoutnp_timewait_0020, 738570af302Sopenharmony_ci clockwait_timedwait_0060, 739570af302Sopenharmony_ci clockwait_timedwait_0070, 740570af302Sopenharmony_ci}; 741570af302Sopenharmony_ci 742570af302Sopenharmony_ciint main(void) 743570af302Sopenharmony_ci{ 744570af302Sopenharmony_ci int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN); 745570af302Sopenharmony_ci for (int pos = 0; pos < num; ++pos) { 746570af302Sopenharmony_ci G_Fun_Array[pos](); 747570af302Sopenharmony_ci } 748570af302Sopenharmony_ci return t_status; 749570af302Sopenharmony_ci} 750