From 820b85ed21419c2e43e5f4257922361512f7dcae Mon Sep 17 00:00:00 2001 From: Yury Popov Date: Sun, 27 Nov 2022 15:09:07 +0300 Subject: [PATCH] fifo, event: fix thread management --- src/libespeak-ng/event.c | 2 +- src/libespeak-ng/fifo.c | 15 ++++++++++++++- src/libespeak-ng/speech.c | 7 +++---- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/libespeak-ng/event.c b/src/libespeak-ng/event.c index 5561fdbc..7f04edc2 100644 --- a/src/libespeak-ng/event.c +++ b/src/libespeak-ng/event.c @@ -46,7 +46,7 @@ static bool my_stop_is_acknowledged = false; static bool my_terminate_is_required = 0; // my_thread: polls the audio duration and compares it to the duration of the first event. static pthread_t my_thread; -static bool thread_inited; +static bool thread_inited = false; static t_espeak_callback *my_callback = NULL; static bool my_event_is_running = false; diff --git a/src/libespeak-ng/fifo.c b/src/libespeak-ng/fifo.c index aaa9eca0..0befcbb6 100644 --- a/src/libespeak-ng/fifo.c +++ b/src/libespeak-ng/fifo.c @@ -63,6 +63,7 @@ static espeak_ng_STATUS push(t_espeak_command *the_command); static t_espeak_command *pop(void); static void init(int process_parameters); static int node_counter = 0; +static bool thread_inited = false; enum { MAX_NODE_COUNTER = 400, @@ -89,6 +90,7 @@ void fifo_init() (void *)NULL)) { assert(0); } + thread_inited = true; pthread_attr_destroy(&a_attrib); @@ -131,6 +133,9 @@ espeak_ng_STATUS fifo_add_command(t_espeak_command *the_command) espeak_ng_STATUS fifo_add_commands(t_espeak_command *command1, t_espeak_command *command2) { espeak_ng_STATUS status; + if (!thread_inited) { + return ENS_NOT_INITIALIZED; + } if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK) return status; @@ -166,6 +171,7 @@ espeak_ng_STATUS fifo_add_commands(t_espeak_command *command1, t_espeak_command espeak_ng_STATUS fifo_stop() { + if (!thread_inited) return ENS_OK; espeak_ng_STATUS status; if ((status = pthread_mutex_lock(&my_mutex)) != ENS_OK) return status; @@ -193,7 +199,11 @@ espeak_ng_STATUS fifo_stop() int fifo_is_busy() { - return my_command_is_running; + if (!thread_inited) return false; + pthread_mutex_lock(&my_mutex); + bool running = my_command_is_running; + pthread_mutex_unlock(&my_mutex); + return running; } static int sleep_until_start_request_or_inactivity() @@ -438,12 +448,15 @@ static void init(int process_parameters) void fifo_terminate() { + if (!thread_inited) return; + pthread_mutex_lock(&my_mutex); my_terminate_is_required = true; pthread_mutex_unlock(&my_mutex); pthread_cond_signal(&my_cond_start_is_required); pthread_join(my_thread, NULL); my_terminate_is_required = false; + thread_inited = false; pthread_mutex_destroy(&my_mutex); pthread_cond_destroy(&my_cond_start_is_required); diff --git a/src/libespeak-ng/speech.c b/src/libespeak-ng/speech.c index eb427e97..4babf780 100644 --- a/src/libespeak-ng/speech.c +++ b/src/libespeak-ng/speech.c @@ -273,6 +273,9 @@ ESPEAK_NG_API espeak_ng_STATUS espeak_ng_InitializeOutput(espeak_ng_OUTPUT_MODE my_audio = create_audio_device_object(device, "eSpeak", "Text-to-Speech"); #endif +#if USE_ASYNC + if ((my_mode & ENOUTPUT_MODE_SYNCHRONOUS) == 0) fifo_init(); +#endif // Don't allow buffer be smaller than safe minimum if (buffer_length < min_buffer_length) @@ -393,10 +396,6 @@ ESPEAK_NG_API espeak_ng_STATUS espeak_ng_Initialize(espeak_ng_ERROR_CONTEXT *con SetParameter(espeakPUNCTUATION, option_punctuation, 0); SetParameter(espeakWORDGAP, 0, 0); -#if USE_ASYNC - fifo_init(); -#endif - option_phonemes = 0; option_phoneme_events = 0;