Protect the stopped variable with a mutex.

An oversight.  Should have done this in the initial commit.
volatile does not imply atomic.
This commit is contained in:
Christopher Brannon 2009-06-21 12:46:43 -05:00 committed by William Hubbs
commit 91b8960b6a
4 changed files with 38 additions and 0 deletions

17
alsa.c
View file

@ -25,10 +25,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <pthread.h>
#define ALSA_PCM_NEW_HW_PARAMS_API
#include <alsa/asoundlib.h>
#include "espeakup.h"
static pthread_mutex_t audio_mutex = PTHREAD_MUTEX_INITIALIZER;
void lock_audio_mutex(void)
{
pthread_mutex_lock(&audio_mutex);
}
void unlock_audio_mutex(void)
{
pthread_mutex_unlock(&audio_mutex);
}
int minimum(int x, int y)
{
if (x <= y)
@ -57,11 +70,15 @@ static int alsa_play_callback(short *audio, int numsamples,
snd_pcm_prepare(handle);
while (numsamples > 0) {
lock_audio_mutex();
if (stopped) {
snd_pcm_drop(handle);
stopped = 0;
unlock_audio_mutex();
return 1;
}
unlock_audio_mutex();
avail = snd_pcm_avail_update(handle);
if (avail <= 0)
continue;

View file

@ -5,3 +5,18 @@ int init_audio(void)
audio_callback = NULL;
return 0;
}
/*
* lock_audio_mutex and unlock_audio_mutex are no-ops if we use native
* sound support. The stopped variable is never read; no need to protect it.
*/
void lock_audio_mutex(void)
{
return;
}
void unlock_audio_mutex(void)
{
return;
}

View file

@ -79,6 +79,8 @@ extern void close_softsynth(void);
extern void main_loop(struct synth_t *s);
extern void *queue_runner(void *arg);
extern int init_audio(void);
extern void lock_audio_mutex(void);
extern void unlock_audio_mutex(void);
extern volatile int stopped;
extern espeak_AUDIO_OUTPUT audio_mode;

View file

@ -114,7 +114,9 @@ espeak_ERROR set_volume(struct synth_t * s, int vol, enum adjust_t adj)
espeak_ERROR stop_speech(void)
{
lock_audio_mutex();
stopped = 1;
unlock_audio_mutex();
return (espeak_Cancel());
}
@ -122,7 +124,9 @@ espeak_ERROR speak_text(struct synth_t * s)
{
espeak_ERROR rc;
lock_audio_mutex();
stopped = 0;
unlock_audio_mutex();
rc = espeak_Synth(s->buf, s->len + 1, 0, POS_CHARACTER, 0, 0, NULL,
NULL);
return rc;