fix: pthread_cond_timedwait接口入参不规范

【背景】pthread_cond_timedwait函数的入参时间应该是绝对时间

【修改方案】
1,修改为相对时间
2,每次等待事件前清理

【影响】
对现有的产品编译不会有影响。

re #I4MVUO
Signed-off-by: wangchen <253227059@qq.com>
This commit is contained in:
wangchen 2021-12-16 10:21:49 +00:00
parent 915e5b9517
commit 08e3025ec5
1 changed files with 24 additions and 13 deletions

View File

@ -202,18 +202,22 @@ STATIC INT32 ProcessReturnVal(pthread_cond_t *cond, INT32 val)
} }
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
const struct timespec *absTime) const struct timespec *ts)
{ {
UINT32 absTicks;
INT32 ret; INT32 ret;
UINT32 absTicks;
const UINT32 nsPerTick = OS_SYS_NS_PER_SECOND / LOSCFG_BASE_CORE_TICK_PER_SECOND;
struct timespec tp;
UINT64 nseconds;
UINT64 currTime;
if ((cond == NULL) || (mutex == NULL) || (absTime == NULL)) { if ((cond == NULL) || (mutex == NULL) || (ts == NULL)) {
return EINVAL; return EINVAL;
} }
if (CondInitCheck(cond)) { if (CondInitCheck(cond)) {
ret = pthread_cond_init(cond, NULL); ret = pthread_cond_init(cond, NULL);
if (ret != ENOERR) { if (ret != 0) {
return ret; return ret;
} }
} }
@ -222,22 +226,29 @@ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex,
cond->count++; cond->count++;
(VOID)pthread_mutex_unlock(cond->mutex); (VOID)pthread_mutex_unlock(cond->mutex);
if ((absTime->tv_sec == 0) && (absTime->tv_nsec == 0)) { if (!ValidTimeSpec(ts)) {
return ETIMEDOUT;
}
if (!ValidTimeSpec(absTime)) {
return EINVAL; return EINVAL;
} }
absTicks = OsTimeSpec2Tick(absTime); clock_gettime(CLOCK_REALTIME, &tp);
if (pthread_mutex_unlock(mutex) != ENOERR) { currTime = (UINT64)tp.tv_sec * OS_SYS_NS_PER_SECOND + tp.tv_nsec;
nseconds = (UINT64)ts->tv_sec * OS_SYS_NS_PER_SECOND + ts->tv_nsec;
if (currTime >= nseconds) {
return ETIMEDOUT;
}
absTicks = ((nseconds - currTime) + nsPerTick - 1) / nsPerTick + 1;
if (absTicks >= UINT32_MAX) {
return EINVAL;
}
if (pthread_mutex_unlock(mutex) != 0) {
PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__); PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__);
} }
ret = (INT32)LOS_EventRead(&(cond->event), 0x0f, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, absTicks); (VOID)LOS_EventClear(&(cond->event), 0);
(VOID)LOS_EventRead(&(cond->event), 0x0f, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, absTicks);
if (pthread_mutex_lock(mutex) != ENOERR) { if (pthread_mutex_lock(mutex) != 0) {
PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__); PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__);
} }