speechd-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/1] move exit from module_close to main


From: Andrei Kholodnyi
Subject: [PATCH 1/1] move exit from module_close to main
Date: Wed, 17 Nov 2010 21:53:05 +0100

There is no need to have exit in each module_close of synth
the right place for it is in main function
now module_close simply returns status

Also now module_init is called in main after we get INIT command
and not before

some small clean ups were made in main
---
 doc/speech-dispatcher.texi |   11 ++----
 src/modules/cicero.c       |    9 ++---
 src/modules/dummy.c        |    8 ++--
 src/modules/espeak.c       |    6 ++--
 src/modules/festival.c     |    6 ++--
 src/modules/flite.c        |    8 ++--
 src/modules/generic.c      |    8 ++--
 src/modules/ibmtts.c       |   12 +++---
 src/modules/ivona.c        |    8 ++--
 src/modules/module_main.c  |   86 +++++++++++++++++++++++++------------------
 src/modules/module_utils.c |    2 +-
 src/modules/module_utils.h |    2 +-
 src/modules/pico.c         |    4 ++-
 13 files changed, 90 insertions(+), 80 deletions(-)

diff --git a/doc/speech-dispatcher.texi b/doc/speech-dispatcher.texi
index 9c907f0..a3d2e9b 100644
--- a/doc/speech-dispatcher.texi
+++ b/doc/speech-dispatcher.texi
@@ -3315,7 +3315,7 @@ and the value returned by @code{module_speaking()}.
 @deffn {Module Utils function} void do_quit()
 @findex do_quit
 Prints the farewell message to the standard output, according
-to the protocol. Then it calls @code{module_close(0)}.
+to the protocol. Then it calls @code{module_close()}.
 @end deffn
 
 @node Functions for use when talking to synthesizer, Multi-process output 
modules, Functions used by module_main.c, Module Utils Functions and Macros
@@ -3368,8 +3368,8 @@ way to do this is to emit the events from another thread.
 
 @end deffn
 
- at deffn {Output module function}  {int module_close(int status)} (void)
- at findex module_close()
+ at deffn {Output module function} int module_close(void)
+ at findex module_close
 
 This function is called when Speech Dispatcher terminates.  The output
 module should terminate all threads and processes, free all resources,
@@ -3378,11 +3378,6 @@ Speech Dispatcher terminates and exit(0) will do the 
work for you.  It's
 perfectly ok for Speech Dispatcher to load, unload or reload output modules
 in the middle of its run.
 
-If the parameter @code{status} is non-zero, the process should
-terminate with this error value.
-
-This function never returns, instead, it should call exit() to terminate
-the whole process.
 @end deffn
 
 @node Multi-process output modules, Memory Handling Functions, Functions for 
use when talking to synthesizer, Module Utils Functions and Macros
diff --git a/src/modules/cicero.c b/src/modules/cicero.c
index eea2e6b..ddce0db 100644
--- a/src/modules/cicero.c
+++ b/src/modules/cicero.c
@@ -300,8 +300,8 @@ module_pause(void)
     return 0;
 }
 
-void
-module_close(int status)
+int
+module_close(void)
 {
   DBG("cicero: close()\n");
   if(cicero_speaking)
@@ -310,10 +310,9 @@ module_close(int status)
     }
 
     if (module_terminate_thread(cicero_speaking_thread) != 0)
-        exit(1);
+        return(-1);
 
-    /*    g_free(cicero_voice); */
-    exit(status);
+    return(0);
 }
 
 /* Internal functions */
diff --git a/src/modules/dummy.c b/src/modules/dummy.c
index 1835d77..a6977e9 100644
--- a/src/modules/dummy.c
+++ b/src/modules/dummy.c
@@ -157,8 +157,8 @@ module_is_speaking(void)
     return NULL ; 
 }
 
-void
-module_close(int status)
+int
+module_close(void)
 {
     DBG("dummy: close()\n");
 
@@ -167,9 +167,9 @@ module_close(int status)
     }
 
     if (module_terminate_thread(dummy_speak_thread) != 0)
-        exit(1);
+        return(-1);
 
-    exit(status);
+    return(0);
 }
 
 
diff --git a/src/modules/espeak.c b/src/modules/espeak.c
index ce0ce12..86ac041 100644
--- a/src/modules/espeak.c
+++ b/src/modules/espeak.c
@@ -457,8 +457,8 @@ module_pause(void)
        return OK;
 }
 
-void
-module_close(int status)
+int
+module_close(void)
 {
        DBG("Espeak: close().");
 
@@ -504,7 +504,7 @@ module_close(int status)
        sem_destroy(espeak_play_semaphore);
        sem_destroy(espeak_stop_or_pause_semaphore);
 
-       exit(status);
+       return(0);
 }
 
 /* > */
diff --git a/src/modules/festival.c b/src/modules/festival.c
index 9b1b871..94fef6b 100644
--- a/src/modules/festival.c
+++ b/src/modules/festival.c
@@ -452,8 +452,8 @@ module_pause(void)
     }
 }
 
-void
-module_close(int status)
+int
+module_close(void)
 {
     
     DBG("festival: close()\n");
@@ -481,7 +481,7 @@ module_close(int status)
     if (module_audio_id)
        spd_audio_close(module_audio_id);
 
-    exit(status);
+    return(0);
 }
 
 
diff --git a/src/modules/flite.c b/src/modules/flite.c
index 31ff828..c1fd55c 100644
--- a/src/modules/flite.c
+++ b/src/modules/flite.c
@@ -228,8 +228,8 @@ module_pause(void)
     }
 }
 
-void
-module_close(int status)
+int
+module_close(void)
 {
 
     DBG("flite: close()\n");
@@ -241,14 +241,14 @@ module_close(int status)
 
     DBG("Terminating threads");
     if (module_terminate_thread(flite_speak_thread) != 0)
-        exit(1);
+        return(-1);
 
     g_free(flite_voice);
 
     DBG("Closing audio output");
     spd_audio_close(module_audio_id);
 
-    exit(status);
+    return(0);
 }
 
 /* Internal functions */
diff --git a/src/modules/generic.c b/src/modules/generic.c
index a362a64..175e58c 100644
--- a/src/modules/generic.c
+++ b/src/modules/generic.c
@@ -276,8 +276,8 @@ module_is_speaking(void)
     return NULL ; 
 }
 
-void
-module_close(int status)
+int
+module_close(void)
 {
     DBG("generic: close()\n");
 
@@ -286,12 +286,12 @@ module_close(int status)
     }
 
     if (module_terminate_thread(generic_speak_thread) != 0)
-        exit(1);
+        return(-1);
 
     if (module_audio_id)
         spd_audio_close(module_audio_id);
 
-    exit(status);
+    return(0);
 }
 
 
diff --git a/src/modules/ibmtts.c b/src/modules/ibmtts.c
index c56e986..1503488 100644
--- a/src/modules/ibmtts.c
+++ b/src/modules/ibmtts.c
@@ -655,8 +655,8 @@ module_pause(void)
     return OK;
 }
 
-void
-module_close(int status)
+int
+module_close(void)
 {
 
     DBG("Ibmtts: close().");
@@ -687,11 +687,11 @@ module_close(int status)
     sem_post(ibmtts_play_semaphore);
     sem_post(ibmtts_stop_or_pause_semaphore);
     if (0 != pthread_join(ibmtts_synth_thread, NULL))
-        exit(1);
+        return(-1);
     if (0 != pthread_join(ibmtts_play_thread, NULL))
-        exit(1);
+        return(-1);
     if (0 != pthread_join(ibmtts_stop_or_pause_thread, NULL))
-        exit(1);
+        return(-1);
 
     ibmtts_clear_playback_queue();
 
@@ -703,7 +703,7 @@ module_close(int status)
 
     free_voice_list();
     
-    exit(status);
+    return(0);
 }
 
 /* Internal functions */
diff --git a/src/modules/ivona.c b/src/modules/ivona.c
index 25ce763..7f80b08 100644
--- a/src/modules/ivona.c
+++ b/src/modules/ivona.c
@@ -250,8 +250,8 @@ module_pause(void)
     }
 }
 
-void
-module_close(int status)
+int
+module_close(void)
 {
 
     DBG("ivona: close()\n");
@@ -263,13 +263,13 @@ module_close(int status)
 
     DBG("Terminating threads");
     if (module_terminate_thread(ivona_speak_thread) != 0)
-        exit(1);
+        return(-1);
 
 
     DBG("Closing audio output");
     spd_audio_close(module_audio_id);
 
-    exit(status);
+    return(0);
 }
 
 /* Internal functions */
diff --git a/src/modules/module_main.c b/src/modules/module_main.c
index 99f4b04..b56dd30 100644
--- a/src/modules/module_main.c
+++ b/src/modules/module_main.c
@@ -41,7 +41,8 @@ if (!strcmp(cmd_buf, #command"\n")){ \
  pthread_mutex_lock(&module_stdout_mutex); \
  if (printf("%s\n", msg = (char*) function()) < 0){ \
      DBG("Broken pipe, exiting...\n"); \
-     module_close(2); \
+     ret = 2; \
+     break; \
  } \
  fflush(stdout); \
  pthread_mutex_unlock(&module_stdout_mutex);\
@@ -54,7 +55,8 @@ if (!strncmp(cmd_buf, #command, strlen(#command))){   \
  pthread_mutex_lock(&module_stdout_mutex); \
  if (printf("%s\n", msg = (char*) function(cmd_buf)) < 0){ \
      DBG("Broken pipe, exiting...\n"); \
-     module_close(2); \
+     ret = 2; \
+     break; \
  } \
  fflush(stdout); \
  pthread_mutex_unlock(&module_stdout_mutex);\
@@ -71,9 +73,8 @@ main(int argc, char *argv[])
 {
     char *cmd_buf;
     int ret;
-    int ret_init;
     size_t n;
-    char *configfilename;
+    char *configfilename = NULL;
     char *status_info = NULL;
 
     g_thread_init(NULL);
@@ -83,12 +84,13 @@ main(int argc, char *argv[])
 
     if (argc >= 2){
         configfilename = g_strdup(argv[1]);
-    }else{
-        configfilename = NULL;
     }
 
     ret = module_load();
-    if (ret == -1) module_close(1);
+    if (ret == -1) {
+       module_close();
+       exit(1);
+    }
 
     if (configfilename != NULL){
         /* Add the LAST option */
@@ -99,7 +101,8 @@ main(int argc, char *argv[])
         if (configfile){
             if (dotconf_command_loop(configfile) == 0){
                 DBG("Error reading config file\n");
-                module_close(1);
+                module_close();
+               exit(1);
             }
            dotconf_cleanup(configfile);
            DBG("Configuration (pre) has been read from \"%s\"\n", 
configfilename);    
@@ -111,41 +114,45 @@ main(int argc, char *argv[])
     }else{
         DBG("No config file specified, using defaults...\n");        
     }
-    
-    ret_init = module_init(&status_info);
-
-    if (status_info == NULL) {
-        status_info = g_strdup("unknown, was not set by module");
-    }
 
     cmd_buf = NULL;  n=0;
     ret = spd_getline(&cmd_buf, &n, stdin);
     if (ret == -1){
        DBG("Broken pipe when reading INIT, exiting... \n");
-       module_close(2); 
+       module_close();
+       exit(2);
     }
 
-    if (!strcmp(cmd_buf, "INIT\n")){
-       if (ret_init != 0){
-           printf("399-%s\n", status_info);
-           ret = printf("%s\n", "399 ERR CANT INIT MODULE");
-           g_free(status_info);
-           return -1;
-       }
-
-       printf("299-%s\n", status_info);
-       ret = printf("%s\n", "299 OK LOADED SUCCESSFULLY");
-
-       if (ret < 0){ 
-           DBG("Broken pipe, exiting...\n");
-            module_close(2); 
-       }
-       fflush(stdout);
-    }else{
+    if (strcmp(cmd_buf, "INIT\n")){
        DBG("ERROR: Wrong communication from module client: didn't call 
INIT\n");
-       module_close(3);
+       module_close();
+       exit(3);
     }
 
+    ret = module_init(&status_info);
+
+    if (status_info == NULL) {
+        status_info = g_strdup("unknown, was not set by module");
+    }
+
+    if (ret != 0){
+       printf("399-%s\n", status_info);
+       printf("%s\n", "399 ERR CANT INIT MODULE");
+       g_free(status_info);
+       module_close();
+       exit(1);
+    }
+
+    printf("299-%s\n", status_info);
+    ret = printf("%s\n", "299 OK LOADED SUCCESSFULLY");
+
+    if (ret < 0){
+       DBG("Broken pipe, exiting...\n");
+        module_close();
+        exit(2);
+    }
+    fflush(stdout);
+
     g_free(status_info);
     g_free(cmd_buf);
 
@@ -154,7 +161,8 @@ main(int argc, char *argv[])
         ret = spd_getline(&cmd_buf, &n, stdin);
         if (ret == -1){
             DBG("Broken pipe, exiting... \n");
-            module_close(2); 
+           ret = 2;
+            break;
         }
 
        DBG("CMD: <%s>", cmd_buf);
@@ -170,14 +178,20 @@ main(int argc, char *argv[])
         else PROCESS_CMD(AUDIO, do_audio) 
         else PROCESS_CMD(LOGLEVEL, do_loglevel)
        else PROCESS_CMD_W_ARGS(DEBUG, do_debug)
-        else PROCESS_CMD_NRP(QUIT, do_quit) 
+        else if (!strcmp(cmd_buf,"QUIT""\n")) {
+            do_quit();
+             exit(0);
+             }
         else{
           printf("300 ERR UNKNOWN COMMAND\n"); 
           fflush(stdout);
         }
        
        g_free(cmd_buf);
-    } 
+    }
+
+    module_close();
+    exit(ret);
 }
 
 #undef PROCESS_CMD
diff --git a/src/modules/module_utils.c b/src/modules/module_utils.c
index 9fe907e..583af5f 100644
--- a/src/modules/module_utils.c
+++ b/src/modules/module_utils.c
@@ -450,7 +450,7 @@ do_quit(void)
 {
     printf("210 OK QUIT\n");    
     fflush(stdout);
-    module_close(0);
+    module_close();
     return;
 }
 
diff --git a/src/modules/module_utils.h b/src/modules/module_utils.h
index e13d74b..289b6a1 100644
--- a/src/modules/module_utils.h
+++ b/src/modules/module_utils.h
@@ -130,7 +130,7 @@ int     module_stop         (void);
 SPDVoice**     module_get_voices   (void);
 size_t  module_pause        (void);
 char*     module_is_speaking  (void);
-void    module_close        (int status);
+int    module_close        (void);
 
 #define UPDATE_PARAMETER(value, setter) \
   if (msg_settings_old.value != msg_settings.value) \
diff --git a/src/modules/pico.c b/src/modules/pico.c
index bc18b27..0394502 100644
--- a/src/modules/pico.c
+++ b/src/modules/pico.c
@@ -605,7 +605,7 @@ size_t module_pause(void)
        return 0;
 }
 
-void module_close(int status)
+int module_close(void)
 {
 
        g_atomic_int_set(&pico_state, STATE_CLOSE);
@@ -627,4 +627,6 @@ void module_close(int status)
        g_free(pico_play_semaphore);
        pico_idle_semaphore = NULL;
        pico_play_semaphore = NULL;
+
+       return(0);
 }
-- 
1.6.0.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]