From 836ec0a47f3b852e9a95f3792fb3f72ea3c875c3 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Tue, 29 May 2018 03:57:40 +0300 Subject: pthread: Implement monotonic/relative wait funcs --- src/libpthread.c | 44 ++++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/src/libpthread.c b/src/libpthread.c index f640c45..0488365 100644 --- a/src/libpthread.c +++ b/src/libpthread.c @@ -120,20 +120,6 @@ bionic___pthread_cleanup_pop(void *c, int execute) assert(0 && "implement"); } -int -bionic_pthread_cond_timedwait_relative_np(bionic_cond_t *cond, bionic_mutex_t *mutex, const struct timespec *reltime) -{ - assert(0 && "implement"); - return 0; -} - -int -bionic_pthread_cond_timedwait_monotonic_np(bionic_cond_t *cond, bionic_mutex_t *mutex, const struct timespec *abstime) -{ - assert(0 && "implement"); - return 0; -} - int bionic_sem_destroy(bionic_sem_t *sem) { @@ -452,3 +438,33 @@ bionic_pthread_cond_timedwait(bionic_cond_t *cond, bionic_mutex_t *mutex, const INIT_IF_NOT_MAPPED(mutex, default_pthread_mutex_init); return pthread_cond_timedwait(cond->glibc, mutex->glibc, abs_timeout); } + +int +bionic_pthread_cond_timedwait_relative_np(bionic_cond_t *cond, bionic_mutex_t *mutex, const struct timespec *reltime) +{ + assert(cond && mutex && reltime); + struct timespec tv; + clock_gettime(CLOCK_REALTIME, &tv); + tv.tv_sec += reltime->tv_sec; + tv.tv_nsec += reltime->tv_nsec; + if (tv.tv_nsec >= 1000000000) { + ++tv.tv_sec; + tv.tv_nsec -= 1000000000; + } + return bionic_pthread_cond_timedwait(cond, mutex, &tv); +} + +int +bionic_pthread_cond_timedwait_monotonic_np(bionic_cond_t *cond, bionic_mutex_t *mutex, const struct timespec *abstime) +{ + assert(cond && mutex && abstime); + struct timespec tv; + clock_gettime(CLOCK_MONOTONIC, &tv); + tv.tv_sec += abstime->tv_sec; + tv.tv_nsec += abstime->tv_nsec; + if (tv.tv_nsec >= 1000000000) { + ++tv.tv_sec; + tv.tv_nsec -= 1000000000; + } + return bionic_pthread_cond_timedwait(cond, mutex, &tv); +} -- cgit v1.2.3-70-g09d2