Extract timeout calculation

This commit is contained in:
Nils O. Selåsdal
2012-12-06 19:13:02 +01:00
parent 16e1ac52ec
commit 29fd133ff2
+20 -11
View File
@@ -5,6 +5,23 @@
#include <sys/time.h>
#include <ucore/ucore_threadqueue.h>
/**
* @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;
}