From a598b64da0a120100ea298725d627b7cfc9bf9b6 Mon Sep 17 00:00:00 2001 From: wangchen <253227059@qq.com> Date: Mon, 20 Dec 2021 07:22:21 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20pthread=5Fcond=5Ftimedwait=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E5=85=A5=E5=8F=82=E4=B8=8D=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 【背景】pthread_cond_timedwait函数的入参时间应该是绝对时间 【修改方案】 1,修改为相对时间 2,每次等待事件前清理 【影响】 对现有的产品编译不会有影响。 re #I4NB5C Signed-off-by: wangchen <253227059@qq.com> --- kal/posix/src/pthread_cond.c | 38 ++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/kal/posix/src/pthread_cond.c b/kal/posix/src/pthread_cond.c index 9f8cec9a..a08e1584 100644 --- a/kal/posix/src/pthread_cond.c +++ b/kal/posix/src/pthread_cond.c @@ -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, - const struct timespec *absTime) + const struct timespec *ts) { - UINT32 absTicks; INT32 ret; + UINT64 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; } if (CondInitCheck(cond)) { ret = pthread_cond_init(cond, NULL); - if (ret != ENOERR) { + if (ret != 0) { return ret; } } @@ -222,22 +226,30 @@ int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, cond->count++; (VOID)pthread_mutex_unlock(cond->mutex); - if ((absTime->tv_sec == 0) && (absTime->tv_nsec == 0)) { - return ETIMEDOUT; - } - - if (!ValidTimeSpec(absTime)) { + if (!ValidTimeSpec(ts)) { return EINVAL; } - absTicks = OsTimeSpec2Tick(absTime); - if (pthread_mutex_unlock(mutex) != ENOERR) { + clock_gettime(CLOCK_REALTIME, &tp); + 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__); } - ret = (INT32)LOS_EventRead(&(cond->event), 0x0f, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, absTicks); + (VOID)LOS_EventClear(&(cond->event), 0); + + ret = (INT32)LOS_EventRead(&(cond->event), 0x0f, LOS_WAITMODE_OR | LOS_WAITMODE_CLR, (UINT32)absTicks); - if (pthread_mutex_lock(mutex) != ENOERR) { + if (pthread_mutex_lock(mutex) != 0) { PRINT_ERR("%s: %d failed\n", __FUNCTION__, __LINE__); }