From 474580b08b32d713ffb6f23c486685f1927e12c0 Mon Sep 17 00:00:00 2001 From: William Hubbs Date: Wed, 24 Jun 2009 21:16:30 -0500 Subject: [PATCH] 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. --- espeakup.c | 7 +++++++ espeakup.h | 3 +++ signal.c | 5 +++++ softsynth.c | 9 ++++++++- 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/espeakup.c b/espeakup.c index 7c8c257..5928bc4 100644 --- a/espeakup.c +++ b/espeakup.c @@ -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) { diff --git a/espeakup.h b/espeakup.h index fd22467..b6ecb4c 100644 --- a/espeakup.h +++ b/espeakup.h @@ -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 diff --git a/signal.c b/signal.c index 89d8a93..6fdf6bc 100644 --- a/signal.c +++ b/signal.c @@ -19,6 +19,9 @@ #include #include +#include +#include +#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; } diff --git a/softsynth.c b/softsynth.c index 06d9cc6..9e700c5 100644 --- a/softsynth.c +++ b/softsynth.c @@ -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");