diff --git a/src/timed_thread.c b/src/timed_thread.c index c6c5a781..81f481e5 100644 --- a/src/timed_thread.c +++ b/src/timed_thread.c @@ -25,7 +25,6 @@ #include #include #include -#include #include #include #include @@ -110,10 +109,11 @@ 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", + /* + printf ("interval_time.tv_sec = %li, .tv_nsec = %li\n", p_timed_thread->interval_time.tv_sec, - p_timed_thread->interval_time.tv_nsec);*/ - + p_timed_thread->interval_time.tv_nsec); + */ return p_timed_thread; } @@ -186,17 +186,18 @@ timed_thread_test (timed_thread* p_timed_thread) 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 */ - if (1000000000-wait_time.tv_nsec < p_timed_thread->interval_time.tv_nsec) + + /* add in the wait interval */ + 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); + wait_time.tv_sec += p_timed_thread->interval_time.tv_sec + 1; + wait_time.tv_nsec = wait_time.tv_nsec - (1000000000 - 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 { + /* no overflow will occur, just add respective components */ 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);*/