From eb3cad47d9aaf1993823d42f806a478a930eb001 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 27 Aug 2019 15:37:49 +0300 Subject: [PATCH 1/2] posix: pthread: pthread_cond_timedwait should accept absolute deadline Instead, it was coded as if it accepted a relative timeout. Normative reference: http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_cond_timedwait.html Fixes: #17812 Signed-off-by: Paul Sokolovsky --- include/posix/pthread.h | 2 +- lib/posix/pthread_cond.c | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/posix/pthread.h b/include/posix/pthread.h index 19846b93c7741..cd96f26ac7c2f 100644 --- a/include/posix/pthread.h +++ b/include/posix/pthread.h @@ -124,7 +124,7 @@ int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut); * See IEEE 1003.1 */ int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, - const struct timespec *to); + const struct timespec *abstime); /** * @brief POSIX threading compatibility API diff --git a/lib/posix/pthread_cond.c b/lib/posix/pthread_cond.c index 7bde01ee95702..d6e839247a90d 100644 --- a/lib/posix/pthread_cond.c +++ b/lib/posix/pthread_cond.c @@ -9,6 +9,8 @@ #include #include +s64_t timespec_to_timeoutms(const struct timespec *abstime); + static int cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut, int timeout) { __ASSERT(mut->lock_count == 1U, ""); @@ -73,8 +75,9 @@ int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mut) } int pthread_cond_timedwait(pthread_cond_t *cv, pthread_mutex_t *mut, - const struct timespec *to) + const struct timespec *abstime) { - return cond_wait(cv, mut, _ts_to_ms(to)); + s32_t timeout = (s32_t)timespec_to_timeoutms(abstime); + return cond_wait(cv, mut, timeout); } From 537a6f50de9b5703c49cb95e75a8d7df3bdeb81b Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Tue, 27 Aug 2019 15:48:29 +0300 Subject: [PATCH 2/2] posix: pthread: pthread_mutex_timedlock should accept absolute deadline It was coded as if it accepts relative timeout. Normative reference: http://pubs.opengroup.org/onlinepubs/9699919799/functions/pthread_mutex_timedlock.html Signed-off-by: Paul Sokolovsky --- include/posix/pthread.h | 2 +- lib/posix/pthread_mutex.c | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/include/posix/pthread.h b/include/posix/pthread.h index cd96f26ac7c2f..fc75eaa7c4d5a 100644 --- a/include/posix/pthread.h +++ b/include/posix/pthread.h @@ -224,7 +224,7 @@ int pthread_mutex_unlock(pthread_mutex_t *m); */ int pthread_mutex_timedlock(pthread_mutex_t *m, - const struct timespec *to); + const struct timespec *abstime); /** * @brief POSIX threading compatibility API diff --git a/lib/posix/pthread_mutex.c b/lib/posix/pthread_mutex.c index 414ad349acc87..531092b7de15e 100644 --- a/lib/posix/pthread_mutex.c +++ b/lib/posix/pthread_mutex.c @@ -9,6 +9,8 @@ #include #include +s64_t timespec_to_timeoutms(const struct timespec *abstime); + #define MUTEX_MAX_REC_LOCK 32767 /* @@ -73,9 +75,10 @@ int pthread_mutex_trylock(pthread_mutex_t *m) * See IEEE 1003.1 */ int pthread_mutex_timedlock(pthread_mutex_t *m, - const struct timespec *to) + const struct timespec *abstime) { - return acquire_mutex(m, _ts_to_ms(to)); + s32_t timeout = (s32_t)timespec_to_timeoutms(abstime); + return acquire_mutex(m, timeout); } /**