From 03d0ad83b8f752b3a09247cfbdc865adf7650bf7 Mon Sep 17 00:00:00 2001 From: Philip Kovacs Date: Sat, 2 Dec 2006 23:07:21 +0000 Subject: [PATCH] test_timed_thread: acquire mutex before calculating future time git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@803 7f574dfc-610e-0410-a909-a81674777703 --- src/timed_thread.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/timed_thread.c b/src/timed_thread.c index 9768cf2b..7a910d66 100644 --- a/src/timed_thread.c +++ b/src/timed_thread.c @@ -122,7 +122,9 @@ timed_thread_unlock (timed_thread* p_timed_thread) } -/* waits required interval for termination signal and returns 1 if got it, 0 otherwise */ +/* thread waits interval_usecs for runnable_cond to be signaled. returns 1 if signaled, + * -1 on error, and 0 otherwise. caller should call timed_thread_exit() on any non-zero + * return value. */ int timed_thread_test (timed_thread* p_timed_thread) { @@ -131,6 +133,10 @@ timed_thread_test (timed_thread* p_timed_thread) assert (p_timed_thread != NULL); + /* acquire runnable_cond mutex */ + if (pthread_mutex_lock (&p_timed_thread->runnable_mutex)) + return (-1); /* could not acquire runnable_cond mutex, so tell caller to exit thread */ + /* get the absolute time in the future we stop waiting for condition to signal */ clock_gettime (CLOCK_REALTIME, &abstime); /* seconds portion of the microseconds interval */ @@ -141,10 +147,6 @@ timed_thread_test (timed_thread* p_timed_thread) abstime.tv_sec += reltime.tv_sec; abstime.tv_nsec += reltime.tv_nsec; - /* acquire runnable_cond mutex */ - if (pthread_mutex_lock (&p_timed_thread->runnable_mutex)) - return (-1); /* could not acquire runnable_cond mutex, so tell caller to exit 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,