Clean up time handling functions

use milliseconds all over the place
This commit is contained in:
Moritz Bitsch 2018-07-30 11:54:41 +02:00
parent 436c24ef25
commit 1cae7a8b27
3 changed files with 13 additions and 11 deletions

View file

@ -17,4 +17,7 @@ pkg_check_modules(GLIB2 REQUIRED glib-2.0)
target_include_directories(${PROJECT_NAME} PUBLIC ${GLIB2_INCLUDE_DIRS}) target_include_directories(${PROJECT_NAME} PUBLIC ${GLIB2_INCLUDE_DIRS})
target_link_libraries(${PROJECT_NAME} ${GLIB2_LIBRARIES}) target_link_libraries(${PROJECT_NAME} ${GLIB2_LIBRARIES})
target_link_libraries(${PROJECT_NAME} m)
target_include_directories(${PROJECT_NAME} PUBLIC .) target_include_directories(${PROJECT_NAME} PUBLIC .)

17
coio.c
View file

@ -18,6 +18,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <math.h>
#include <stdio.h> #include <stdio.h>
#include "coioimpl.h" #include "coioimpl.h"
@ -61,7 +62,7 @@ _process_events()
if (now >= t->timeout) { if (now >= t->timeout) {
ms = 0; ms = 0;
} else { } else {
ms = (t->timeout - now) / 1000000; ms = (t->timeout - now);
} }
} }
/* TODO:do I/O polling instead of usleep */ /* TODO:do I/O polling instead of usleep */
@ -152,7 +153,7 @@ coio_timeout(CoioTask* task, int ms)
CoioTask* t; CoioTask* t;
if (ms >= 0) { if (ms >= 0) {
task->timeout = coio_now() + (ms * 1000000); task->timeout = coio_now() + ms;
for (t = coio_sleeping.head; t != NULL && t->timeout && t->timeout < task->timeout; t = t->next); for (t = coio_sleeping.head; t != NULL && t->timeout && t->timeout < task->timeout; t = t->next);
} else { } else {
task->timeout = 0; task->timeout = 0;
@ -190,7 +191,7 @@ coio_delay(int ms)
uvlong when; uvlong when;
when = coio_timeout(coio_current, ms); when = coio_timeout(coio_current, ms);
coio_transfer(); coio_transfer();
return (coio_now() - when) / 1000000; return (coio_now() - when);
} }
void void
@ -252,21 +253,19 @@ coio_now()
{ {
#if defined(__APPLE__) #if defined(__APPLE__)
clock_serv_t cclock; clock_serv_t cclock;
mach_timespec_t mts; mach_timespec_t ts;
host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock);
clock_get_time(cclock, &mts); clock_get_time(cclock, &ts);
mach_port_deallocate(mach_task_self(), cclock); mach_port_deallocate(mach_task_self(), cclock);
return (uvlong) mts.tv_sec * 1000 * 1000 * 1000 + mts.tv_nsec;
#else #else
struct timespec ts; struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
return -1; return -1;
return (uvlong) ts.tv_sec * 1000 * 1000 * 1000 + ts.tv_nsec;
#endif #endif
return (uvlong) ts.tv_sec * 1000 + round(ts.tv_nsec / 1000000.0);
} }
void void

View file

@ -47,7 +47,7 @@ static gboolean coio_source_prepare(GSource* source, gint* timeout_)
if (now >= t->timeout) { if (now >= t->timeout) {
ms = 0; ms = 0;
} else { } else {
ms = (t->timeout - now) / 1000000; ms = (t->timeout - now);
} }
} }