diff --git a/src/common.c b/src/common.c index 1b5c5157..beb33b74 100644 --- a/src/common.c +++ b/src/common.c @@ -234,7 +234,7 @@ void update_stuff() if (NEED(INFO_MPD)) { if (!mpd_timed_thread) { init_mpd_stats(&info); - mpd_timed_thread = timed_thread_create((void*)update_mpd, (void*) NULL, update_interval * 1000000); + mpd_timed_thread = timed_thread_create((void*)update_mpd, (void*) NULL, 500000); if (!mpd_timed_thread) { ERR("Failed to create MPD timed thread"); } diff --git a/src/timed_thread.c b/src/timed_thread.c index a234ddbe..c6c5a781 100644 --- a/src/timed_thread.c +++ b/src/timed_thread.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -109,6 +110,10 @@ timed_thread_create (void *(*start_routine)(void*), void *arg, unsigned int inte /* remaining microseconds convert to nanoseconds */ p_timed_thread->interval_time.tv_nsec = (long)((interval_usecs % 1000000) * 1000); + /*printf ("interval_time.tv_sec = %li, .tv_nsec = %li\n", + p_timed_thread->interval_time.tv_sec, + p_timed_thread->interval_time.tv_nsec);*/ + return p_timed_thread; } @@ -180,9 +185,22 @@ timed_thread_test (timed_thread* p_timed_thread) assert (p_timed_thread != NULL); if (now (&wait_time)) return (-1); + /*printf ("PRE:wait_time.tv_secs = %li, .tv_nsecs = %li\n", wait_time.tv_sec, wait_time.tv_nsec);*/ /* now add in the wait interval */ - wait_time.tv_sec += p_timed_thread->interval_time.tv_sec; - wait_time.tv_nsec += p_timed_thread->interval_time.tv_nsec; + if (1000000000-wait_time.tv_nsec < p_timed_thread->interval_time.tv_nsec) + { + /* adjust for impending overflow of wait_time.tv_nsec */ + wait_time.tv_sec += p_timed_thread->interval_time.tv_sec; + wait_time.tv_sec += (p_timed_thread->interval_time.tv_nsec / 1000000000) + 1; + wait_time.tv_nsec = labs (wait_time.tv_nsec - p_timed_thread->interval_time.tv_nsec); + /*printf ("001:wait_time.tv_secs = %li, .tv_nsecs = %li\n", wait_time.tv_sec, wait_time.tv_nsec);*/ + } + else + { + wait_time.tv_sec += p_timed_thread->interval_time.tv_sec; + wait_time.tv_nsec += p_timed_thread->interval_time.tv_nsec; + /*printf ("002:wait_time.tv_secs = %li, .tv_nsecs = %li\n", wait_time.tv_sec, wait_time.tv_nsec);*/ + } /* acquire runnable_cond mutex */ if (pthread_mutex_lock (&p_timed_thread->runnable_mutex))