add pipe to wake up the softsynth thread

This adds a pipe to wake up the softsynth thread, in case we receive a
signal while it is in a select.  Thanks to Chris Brannon.
This commit is contained in:
William Hubbs 2009-06-24 21:16:30 -05:00
commit 474580b08b
4 changed files with 23 additions and 1 deletions

View file

@ -42,6 +42,7 @@ const int defaultVolume = 5;
char *defaultVoice = NULL;
int debug = 0;
int self_pipe_fds[2];
volatile int stopped = 0;
volatile int should_run = 1;
espeak_AUDIO_OUTPUT audio_mode;
@ -123,6 +124,12 @@ int main(int argc, char **argv)
sigaddset(&sigset, SIGTERM);
sigprocmask(SIG_BLOCK, &sigset, NULL);
/* set up the pipe used to wake the reader. */
if(pipe(self_pipe_fds) < 0) {
perror("Unable to create pipe");
return 5;
}
/* Spawn our queue-processing thread. */
err = pthread_create(&queue_thread_id, NULL, &queue_runner, &s);
if (err != 0) {

View file

@ -85,6 +85,9 @@ extern void unlock_audio_mutex(void);
extern volatile int should_run;
extern volatile int stopped;
extern volatile int runner_must_stop;
extern int self_pipe_fds[2];
#define PIPE_READ_FD (self_pipe_fds[0])
#define PIPE_WRITE_FD (self_pipe_fds[1])
extern espeak_AUDIO_OUTPUT audio_mode;
#endif

View file

@ -19,6 +19,9 @@
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#define STOP_MSG "s"
#include "espeakup.h"
@ -57,5 +60,7 @@ void *signal_thread(void *arg)
break;
}
}
/* Tell the reader to stop, if it is in a select() call. */
write(PIPE_WRITE_FD, STOP_MSG, strlen(STOP_MSG));
return NULL;
}

View file

@ -130,6 +130,8 @@ void *softsynth_thread(void *arg)
ssize_t length;
char buf[maxBufferSize];
char *cp;
int terminalFD = PIPE_READ_FD;
int greatestFD;
/* open the softsynth. */
softFD = open("/dev/softsynth", O_RDWR | O_NONBLOCK);
@ -138,11 +140,16 @@ void *softsynth_thread(void *arg)
should_run = 0;
}
if (terminalFD > softFD)
greatestFD = terminalFD;
else
greatestFD = softFD;
while (should_run) {
FD_ZERO(&set);
FD_SET(softFD, &set);
FD_SET(terminalFD, &set);
if (select(softFD + 1, &set, NULL, NULL, NULL) < 0) {
if (select(greatestFD + 1, &set, NULL, NULL, NULL) < 0) {
if (errno == EINTR)
continue;
perror("Select failed");