1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-26 12:27:52 +00:00

* Improve timed_thread timing

git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@1249 7f574dfc-610e-0410-a909-a81674777703
This commit is contained in:
Brenden Matthews 2008-09-11 22:27:19 +00:00
parent f968843514
commit 8bb6b479a8
2 changed files with 34 additions and 31 deletions

View File

@ -3,6 +3,7 @@
2008-09-11
* Maybe fix missing include bug
(http://bugs.gentoo.org/show_bug.cgi?id=235233)
* Improve timed_thread timing
2008-09-10
* Improved hddtemp support

View File

@ -46,6 +46,7 @@ struct _timed_thread {
void *(*start_routine)(void *); /* thread function to run */
void *arg; /* thread function argument */
struct timespec interval_time; /* interval_usecs as a struct timespec */
struct timespec wait_time; /* absolute future time next timed_thread_test will wait until */
};
/* linked list of created threads */
@ -108,6 +109,11 @@ timed_thread *timed_thread_create(void *start_routine(void *), void *arg,
p_timed_thread->start_routine = start_routine;
p_timed_thread->arg = arg;
/* set wait time to current time */
if (now(&p_timed_thread->wait_time)) {
return NULL;
}
/* seconds portion of the microseconds interval */
p_timed_thread->interval_time.tv_sec = (time_t) (interval_usecs / 1000000);
/* remaining microseconds convert to nanoseconds */
@ -180,33 +186,11 @@ int timed_thread_unlock(timed_thread *p_timed_thread)
* caller should call timed_thread_exit() on any non-zero return value. */
int timed_thread_test(timed_thread *p_timed_thread)
{
struct timespec wait_time;
struct timespec now_time;
int rc;
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); */
/* add in the wait interval */
if (1000000000 - wait_time.tv_nsec
<= p_timed_thread->interval_time.tv_nsec) {
/* perform nsec->sec carry operation */
wait_time.tv_sec += p_timed_thread->interval_time.tv_sec + 1;
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 carry needed, 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); */
}
/* acquire runnable_cond mutex */
if (pthread_mutex_lock(&p_timed_thread->runnable_mutex)) {
/* could not acquire runnable_cond mutex,
@ -216,10 +200,28 @@ int timed_thread_test(timed_thread *p_timed_thread)
/* release mutex and wait until future time for runnable_cond to signal */
rc = pthread_cond_timedwait(&p_timed_thread->runnable_cond,
&p_timed_thread->runnable_mutex, &wait_time);
&p_timed_thread->runnable_mutex, &p_timed_thread->wait_time);
/* mutex re-acquired, so release it */
pthread_mutex_unlock(&p_timed_thread->runnable_mutex);
if (now(&now_time)) {
return -1;
}
/* absolute future time for next pass */
p_timed_thread->wait_time.tv_sec += p_timed_thread->interval_time.tv_sec;
p_timed_thread->wait_time.tv_nsec += p_timed_thread->interval_time.tv_nsec;
p_timed_thread->wait_time.tv_sec += p_timed_thread->wait_time.tv_nsec / 1000000000;
p_timed_thread->wait_time.tv_nsec = p_timed_thread->wait_time.tv_nsec % 1000000000;
/* ensure our future wait time is sane */
if (p_timed_thread->wait_time.tv_sec > (now_time.tv_sec + p_timed_thread->interval_time.tv_sec) || p_timed_thread->wait_time.tv_sec < now_time.tv_sec) {
p_timed_thread->wait_time.tv_sec = now_time.tv_sec + p_timed_thread->interval_time.tv_sec;
p_timed_thread->wait_time.tv_nsec = now_time.tv_nsec + p_timed_thread->interval_time.tv_nsec;
p_timed_thread->wait_time.tv_sec += p_timed_thread->wait_time.tv_nsec / 1000000000;
p_timed_thread->wait_time.tv_nsec = p_timed_thread->wait_time.tv_nsec % 1000000000;
}
if (rc == 0) {
/* runnable_cond was signaled, so tell caller to exit thread */
return 1;