audacious thread timing the correct way -- texeci's to follow
authorPhilip Kovacs <pkovacs@users.sourceforge.net>
Tue, 14 Nov 2006 02:04:27 +0000 (02:04 +0000)
committerPhilip Kovacs <pkovacs@users.sourceforge.net>
Tue, 14 Nov 2006 02:04:27 +0000 (02:04 +0000)
git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@766 7f574dfc-610e-0410-a909-a81674777703

src/audacious.c
src/conky.h

index ee28fe9..fae63a2 100644 (file)
@@ -21,6 +21,7 @@
  * --------------------------------------------------------------------------- */
 
 #include <pthread.h>
+#include <errno.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -69,9 +70,7 @@ int create_audacious_thread(void)
     pthread_mutex_init(&info.audacious.item_mutex, NULL);
     pthread_mutex_init(&info.audacious.runnable_mutex, NULL);
     /* Init runnable condition for worker thread */
-    pthread_mutex_lock(&info.audacious.runnable_mutex);
-    info.audacious.runnable=1;
-    pthread_mutex_unlock(&info.audacious.runnable_mutex);
+    pthread_cond_init(&info.audacious.runnable_cond, NULL);
     if (pthread_create(&info.audacious.thread, &info.audacious.thread_attr, audacious_thread_func, NULL))
         return(-1);
 
@@ -90,16 +89,19 @@ int destroy_audacious_thread(void)
         return(0);
 
     /* Signal audacious thread to terminate */
-    pthread_mutex_lock(&info.audacious.runnable_mutex);
-    info.audacious.runnable=0;
-    pthread_mutex_unlock(&info.audacious.runnable_mutex);
+    pthread_mutex_lock (&info.audacious.runnable_mutex);
+    pthread_cond_signal (&info.audacious.runnable_cond);
+    pthread_mutex_unlock (&info.audacious.runnable_mutex);
+
     /* Destroy thread attribute and wait for thread */
     pthread_attr_destroy(&info.audacious.thread_attr);
     if (pthread_join(info.audacious.thread, NULL))
         return(-1);
-    /* Destroy mutexes */
+
+    /* Destroy mutexes and cond */
     pthread_mutex_destroy(&info.audacious.item_mutex);
     pthread_mutex_destroy(&info.audacious.runnable_mutex);
+    pthread_cond_destroy(&info.audacious.runnable_cond);
 
     info.audacious.thread=(pthread_t)0;
     return 0;
@@ -110,22 +112,18 @@ int destroy_audacious_thread(void)
  * --------------------------------------------------- */ 
 void *audacious_thread_func(void *pvoid)
 {
-    int runnable;
+    int runnable=1;
     static audacious_t items;
     gint session,playpos,frames,length;
     gint rate,freq,chans;
     gchar *psong,*pfilename;
+    struct timespec abstime;
 
     pvoid=(void *)pvoid;  /* avoid warning */
     session=0;
     psong=NULL;
     pfilename=NULL;
 
-    /* Grab the runnable signal.  Should be non-zero here or we do nothing. */
-    pthread_mutex_lock(&info.audacious.runnable_mutex);
-    runnable=info.audacious.runnable;
-    pthread_mutex_unlock(&info.audacious.runnable_mutex );
-
     /* Loop until the main thread sets the runnable signal to 0. */
     while(runnable) {
 
@@ -207,12 +205,19 @@ void *audacious_thread_func(void *pvoid)
         memcpy(&audacious_items,items,sizeof(items));
         pthread_mutex_unlock(&info.audacious.item_mutex);
 
-        /* Grab the runnable signal for next loop. */
-        pthread_mutex_lock(&info.audacious.runnable_mutex);
-        runnable=info.audacious.runnable;
-        pthread_mutex_unlock(&info.audacious.runnable_mutex);
+        /* Get absolute time 1 sec in the future. */
+       clock_gettime (CLOCK_REALTIME, &abstime);
+       abstime.tv_sec += 1;
+
+       /* Wait for a second before looping or until signalled to stop. */
+       if (pthread_cond_timedwait (&info.audacious.runnable_cond, 
+                                    &info.audacious.runnable_mutex, 
+                                    &abstime) != ETIMEDOUT)
+       {
+           runnable=0;
+           (void) pthread_mutex_unlock (&info.audacious.runnable_mutex);
+       }
 
-        sleep(1);
     }
 
     pthread_exit(NULL);
index dd157b9..e19bb3b 100644 (file)
@@ -172,11 +172,11 @@ struct xmms2_s {
 #ifdef AUDACIOUS
 struct audacious_s {
        audacious_t items;              /* e.g. items[AUDACIOUS_STATUS] */
-       int runnable;                   /* used to signal worker thread to stop */
        pthread_t thread;               /* worker thread */
        pthread_attr_t thread_attr;     /* thread attributes */
        pthread_mutex_t item_mutex;     /* mutex for item array */
-       pthread_mutex_t runnable_mutex; /* mutex for runnable flag */
+       pthread_mutex_t runnable_mutex; /* mutex for runnable */
+       pthread_cond_t runnable_cond;   /* cond for runnable */
 };
 #endif