diff --git a/thread/thread.c b/thread/thread.c index c6c6505..6c6db81 100644 --- a/thread/thread.c +++ b/thread/thread.c @@ -513,7 +513,11 @@ void thread_mutex_unlock_c(mutex_t *mutex, int line, char *file) void thread_cond_create_c(cond_t *cond, int line, char *file) { - pthread_cond_init(&cond->sys_cond, NULL); + pthread_condattr_t attr; + pthread_condattr_init(&attr); + pthread_condattr_setclock(&attr, CLOCK_MONOTONIC); + pthread_cond_init(&cond->sys_cond, &attr); + pthread_condattr_destroy(&attr); pthread_mutex_init(&cond->cond_mutex, NULL); } @@ -537,8 +541,15 @@ void thread_cond_timedwait_c(cond_t *cond, int millis, int line, char *file) { struct timespec time; - time.tv_sec = millis/1000; - time.tv_nsec = (millis - time.tv_sec*1000)*1000000; + clock_gettime(CLOCK_MONOTONIC, &time); + + time.tv_nsec += millis*1000000; + + if (time.tv_nsec >= 1000000000) { + unsigned long int extra = time.tv_nsec / 1000000000; + time.tv_nsec -= extra * 1000000000; + time.tv_sec += extra; + } pthread_mutex_lock(&cond->cond_mutex); pthread_cond_timedwait(&cond->sys_cond, &cond->cond_mutex, &time); diff --git a/thread/thread.h b/thread/thread.h index e9b1e9b..51aa34e 100644 --- a/thread/thread.h +++ b/thread/thread.h @@ -120,7 +120,7 @@ typedef mutex_t spin_t; #define thread_cond_signal(x) thread_cond_signal_c(x,__LINE__,__FILE__) #define thread_cond_broadcast(x) thread_cond_broadcast_c(x,__LINE__,__FILE__) #define thread_cond_wait(x) thread_cond_wait_c(x,__LINE__,__FILE__) -#define thread_cond_timedwait(x,t) thread_cond_wait_c(x,t,__LINE__,__FILE__) +#define thread_cond_timedwait(x,t) thread_cond_timedwait_c(x,t,__LINE__,__FILE__) #define thread_rwlock_create(x) thread_rwlock_create_c(x,__LINE__,__FILE__) #define thread_rwlock_rlock(x) thread_rwlock_rlock_c(x,__LINE__,__FILE__) #define thread_rwlock_wlock(x) thread_rwlock_wlock_c(x,__LINE__,__FILE__)