From 29fd133ff298a68eea531bcb497b14f9b44ef837 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Thu, 6 Dec 2012 19:13:02 +0100 Subject: [PATCH] Extract timeout calculation --- src/ucore_threadqueue.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/ucore_threadqueue.c b/src/ucore_threadqueue.c index 44c665d..b553fc4 100644 --- a/src/ucore_threadqueue.c +++ b/src/ucore_threadqueue.c @@ -5,6 +5,23 @@ #include #include +/** + * @param timeout a timeout duration + * @param result the absolute time from now when the timeout expires + */ +void uc_get_absolute_time(struct timespec *result, const struct timespec *timeout) +{ + struct timeval now; + //TODO investigate using clock_gettime() instead of gettimeofday + gettimeofday(&now, NULL); + result->tv_sec = now.tv_sec + timeout->tv_sec; + result->tv_nsec = (now.tv_usec * 1000) + timeout->tv_nsec; + + if (result->tv_nsec >= 1000000000) { + result->tv_sec++; + result->tv_nsec -= 1000000000; + } +} int uc_thread_queue_init(struct uc_threadqueue *queue, long max_elements) { @@ -91,16 +108,9 @@ int uc_thread_queue_get(struct uc_threadqueue *queue, const struct timespec *tim if (queue == NULL || msg == NULL) { return EINVAL; } - if (timeout) { - struct timeval now; - gettimeofday(&now, NULL); - abstimeout.tv_sec = now.tv_sec + timeout->tv_sec; - abstimeout.tv_nsec = (now.tv_usec * 1000) + timeout->tv_nsec; - if (abstimeout.tv_nsec >= 1000000000) { - abstimeout.tv_sec++; - abstimeout.tv_nsec -= 1000000000; - } + if (timeout) { + uc_get_absolute_time(&abstimeout, timeout); } pthread_mutex_lock(&queue->mutex); @@ -207,7 +217,6 @@ int uc_thread_queue_destroy(struct uc_threadqueue *queue, uc_thread_queue_walk_f pthread_cond_destroy(&queue->write_cond); return rc; - } @@ -218,6 +227,6 @@ long uc_thread_queue_length(struct uc_threadqueue *queue) pthread_mutex_lock(&queue->mutex); length = queue->num_elements; pthread_mutex_unlock(&queue->mutex); - return length; + return length; }