From 8b1728d946828badec6b3d1383d5729eca19d746 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Fri, 24 Feb 2012 15:47:07 -0600 Subject: [PATCH] Use a pthread mutex around the volume settings Using the `volatile` qualifier in multithreading code is never the right answer. Mutexes should be used as was attempted with the audio buffer code. Here, we implement a new mutex for the volume and fix_volume globals, and grab a lock on it when necessary, which is for both reads and writes. --- hairtunes.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/hairtunes.c b/hairtunes.c index a9abfcb7..ecd99211 100644 --- a/hairtunes.c +++ b/hairtunes.c @@ -97,9 +97,10 @@ static void rtp_request_resend(seq_t first, seq_t last); static void ab_resync(void); // interthread variables - // stdin->decoder -volatile double volume = 1.0; -volatile long fix_volume = 0x10000; +// stdin->decoder +static double volume = 1.0; +static int fix_volume = 0x10000; +static pthread_mutex_t vol_mutex = PTHREAD_MUTEX_INITIALIZER; typedef struct audio_buffer_entry { // decoded audio packets int ready; @@ -220,8 +221,10 @@ int hairtunes_init(char *pAeskey, char *pAesiv, char *fmtpstr, int pCtrlPort, in assert(f<=0); if (debug) fprintf(stderr, "VOL: %lf\n", f); + pthread_mutex_lock(&vol_mutex); volume = pow(10.0,0.05*f); fix_volume = 65536.0 * volume; + pthread_mutex_unlock(&vol_mutex); continue; } if (!strcmp(line, "exit\n")) { @@ -702,6 +705,7 @@ static int stuff_buffer(double playback_rate, short *inptr, short *outptr) { stuffsamp = rand() % (frame_size - 1); } + pthread_mutex_lock(&vol_mutex); for (i=0; i