!475 挑单 同步pthread_cond修改到 2.2分支

Merge pull request !475 from wangchen/cond
This commit is contained in:
openharmony_ci 2021-12-20 10:40:42 +00:00 committed by Gitee
commit a0b126246e
1 changed files with 25 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,
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__);
}