diff --git a/src/main.c b/src/main.c index 93dc85f5..0f3d37c9 100644 --- a/src/main.c +++ b/src/main.c @@ -200,7 +200,7 @@ gtimercmp(gtimer_t *a, gtimer_t *b) return -1; if(a->gti_expire.tv_nsec > b->gti_expire.tv_nsec) return 1; - return -1; + return 0; } /** @@ -221,6 +221,8 @@ gtimer_arm_abs2 LIST_INSERT_SORTED(>imers, gti, gti_link, gtimercmp); + //tvhdebug("gtimer", "%p @ %ld.%09ld", gti, when->tv_sec, when->tv_nsec); + if (LIST_FIRST(>imers) == gti) pthread_cond_signal(>imer_cond); // force timer re-check } @@ -269,6 +271,7 @@ void gtimer_disarm(gtimer_t *gti) { if(gti->gti_callback) { + //tvhdebug("gtimer", "%p disarm", gti); LIST_REMOVE(gti, gti_link); gti->gti_callback = NULL; } @@ -361,7 +364,15 @@ mainloop(void) // TODO: there is a risk that if timers re-insert themselves to // the top of the list with a 0 offset we could loop indefinitely +#if 0 + tvhdebug("gtimer", "now %ld.%09ld", ts.tv_sec, ts.tv_nsec); + LIST_FOREACH(gti, >imers, gti_link) + tvhdebug("gtimer", " gti %p expire %ld.%08ld", + gti, gti->gti_expire.tv_sec, gti->gti_expire.tv_nsec); +#endif + while((gti = LIST_FIRST(>imers)) != NULL) { + if ((gti->gti_expire.tv_sec > ts.tv_sec) || ((gti->gti_expire.tv_sec == ts.tv_sec) && (gti->gti_expire.tv_nsec > ts.tv_nsec))) { @@ -370,6 +381,7 @@ mainloop(void) } cb = gti->gti_callback; + //tvhdebug("gtimer", "%p callback", gti); LIST_REMOVE(gti, gti_link); gti->gti_callback = NULL; @@ -384,6 +396,7 @@ mainloop(void) } /* Wait */ + //tvhdebug("gtimer", "wait till %ld.%09ld", ts.tv_sec, ts.tv_nsec); pthread_cond_timedwait(>imer_cond, &global_lock, &ts); pthread_mutex_unlock(&global_lock); }