[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/1] Add module_semaphore_free() to destroy semaphore...
From: |
sub |
Subject: |
[PATCH 1/1] Add module_semaphore_free() to destroy semaphore... |
Date: |
Wed, 7 Dec 2011 19:35:56 +0400 |
Add module_semaphore_free() to destroy semaphore, and release allocated memory
(see module_semaphore_init()).
Add module_semaphore_count_get() to get a value of the semaphores counter.
Modify module_semaphore_init(): now it return NULL, when memory allocation
fails.
Modify all modules that use semaphore.
---
src/modules/cicero.c | 13 ++++++++++-
src/modules/dummy.c | 13 +++++++++++-
src/modules/espeak.c | 26 +++++++++++++++++++++---
src/modules/festival.c | 14 ++++++++++--
src/modules/flite.c | 12 ++++++++++-
src/modules/generic.c | 13 +++++++++++-
src/modules/ibmtts.c | 38 +++++++++++++++++++++++++++++++++--
src/modules/ivona.c | 13 +++++++++++-
src/modules/module_utils.c | 46 ++++++++++++++++++++++++++++++++++++++-----
src/modules/pico.c | 8 +++---
10 files changed, 170 insertions(+), 26 deletions(-)
diff --git a/src/modules/cicero.c b/src/modules/cicero.c
index 1ad1b8f..6c730c3 100644
--- a/src/modules/cicero.c
+++ b/src/modules/cicero.c
@@ -44,7 +44,7 @@ DECLARE_DEBUG()
static int cicero_speaking = 0;
static pthread_t cicero_speaking_thread;
-static sem_t *cicero_semaphore;
+static sem_t *cicero_semaphore = NULL;
static char **cicero_message;
static SPDMessageType cicero_message_type;
@@ -206,6 +206,13 @@ int module_init(char **status_info)
*cicero_message = NULL;
cicero_semaphore = module_semaphore_init();
+ if (NULL == cicero_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
+ return -1;
+ }
DBG("Cicero: creating new thread for cicero_tracking\n");
cicero_speaking = 0;
@@ -295,7 +302,9 @@ int module_close(void)
if (module_terminate_thread(cicero_speaking_thread) != 0)
return -1;
-
+
+ module_semaphore_free(cicero_semaphore);
+ cicero_semaphore = NULL;
return 0;
}
diff --git a/src/modules/dummy.c b/src/modules/dummy.c
index 056e2a9..ff34c4c 100644
--- a/src/modules/dummy.c
+++ b/src/modules/dummy.c
@@ -45,7 +45,7 @@ static int dummy_speaking = 0;
static pthread_t dummy_speak_thread;
static pid_t dummy_pid;
-static sem_t *dummy_semaphore;
+static sem_t *dummy_semaphore = NULL;
/* Internal functions prototypes */
static void *_dummy_speak(void *);
@@ -69,6 +69,14 @@ int module_init(char **status_info)
*status_info = NULL;
dummy_semaphore = module_semaphore_init();
+ if (NULL == dummy_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
+ return -1;
+ }
+
DBG("Dummy: creating new thread for dummy_speak\n");
dummy_speaking = 0;
@@ -153,6 +161,9 @@ int module_close(void)
if (module_terminate_thread(dummy_speak_thread) != 0)
return -1;
+
+ module_semaphore_free(dummy_semaphore);
+ dummy_semaphore = NULL;
return 0;
}
diff --git a/src/modules/espeak.c b/src/modules/espeak.c
index dab8ce3..efb9926 100644
--- a/src/modules/espeak.c
+++ b/src/modules/espeak.c
@@ -87,9 +87,9 @@ static pthread_mutex_t espeak_state_mutex;
static pthread_t espeak_play_thread;
static pthread_t espeak_stop_or_pause_thread;
-static sem_t *espeak_stop_or_pause_semaphore;
+static sem_t *espeak_stop_or_pause_semaphore = NULL;
static pthread_mutex_t espeak_stop_or_pause_suspended_mutex;
-static sem_t *espeak_play_semaphore;
+static sem_t *espeak_play_semaphore = NULL;
static pthread_mutex_t espeak_play_suspended_mutex;
static gboolean espeak_close_requested = FALSE;
@@ -293,6 +293,14 @@ int module_init(char **status_info)
DBG("Espeak: Creating new thread for stop or pause.");
espeak_stop_or_pause_semaphore = module_semaphore_init();
+ if (NULL == espeak_stop_or_pause_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
+ return FATAL_ERROR;
+ }
+
ret =
pthread_create(&espeak_stop_or_pause_thread, NULL,
_espeak_stop_or_pause, NULL);
@@ -304,6 +312,14 @@ int module_init(char **status_info)
}
espeak_play_semaphore = module_semaphore_init();
+ if (NULL == espeak_play_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
+ return FATAL_ERROR;
+ }
+
DBG("Espeak: Creating new thread for playback.");
ret = pthread_create(&espeak_play_thread, NULL, _espeak_play, NULL);
if (ret != OK) {
@@ -502,8 +518,10 @@ int module_close(void)
pthread_mutex_destroy(&espeak_stop_or_pause_suspended_mutex);
pthread_mutex_destroy(&playback_queue_mutex);
pthread_cond_destroy(&playback_queue_condition);
- sem_destroy(espeak_play_semaphore);
- sem_destroy(espeak_stop_or_pause_semaphore);
+ module_semaphore_free(espeak_play_semaphore);
+ espeak_play_semaphore = NULL;
+ module_semaphore_free(espeak_stop_or_pause_semaphore);
+ espeak_stop_or_pause_semaphore = NULL;
return 0;
}
diff --git a/src/modules/festival.c b/src/modules/festival.c
index 6264108..b44d080 100644
--- a/src/modules/festival.c
+++ b/src/modules/festival.c
@@ -40,7 +40,7 @@ DECLARE_DEBUG()
/* Thread and process control */
static pthread_t festival_speak_thread;
-static sem_t *festival_semaphore;
+static sem_t *festival_semaphore = NULL;
static int festival_speaking = 0;
static int festival_pause_requested = 0;
@@ -286,9 +286,14 @@ int module_init(char **status_info)
with festival in a separate thread (to be faster in communication
with Speech Dispatcher) */
festival_semaphore = module_semaphore_init();
- if (festival_semaphore == NULL)
+ if (NULL == festival_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
return -1;
- DBG("Festival: creating new thread for festival_speak\n");
+ }
+DBG("Festival: creating new thread for festival_speak\n");
festival_speaking = 0;
ret =
pthread_create(&festival_speak_thread, NULL, _festival_speak, NULL);
@@ -476,6 +481,9 @@ int module_close(void)
/* TODO: Solve this */
// DBG("Removing junk files in tmp/");
// system("rm -f /tmp/est* 2> /dev/null");
+
+ module_semaphore_free(festival_semaphore );
+ festival_semaphore = NULL;
return 0;
}
diff --git a/src/modules/flite.c b/src/modules/flite.c
index 1619e9c..e07c031 100644
--- a/src/modules/flite.c
+++ b/src/modules/flite.c
@@ -43,7 +43,7 @@ DECLARE_DEBUG();
static int flite_speaking = 0;
static pthread_t flite_speak_thread;
-static sem_t *flite_semaphore;
+static sem_t *flite_semaphore = NULL;
static char **flite_message;
static SPDMessageType flite_message_type;
@@ -126,6 +126,14 @@ int module_init(char **status_info)
*flite_message = NULL;
flite_semaphore = module_semaphore_init();
+ if (NULL == flite_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
+ return -1;
+ }
+
DBG("Flite: creating new thread for flite_speak\n");
flite_speaking = 0;
@@ -229,6 +237,8 @@ int module_close(void)
return -1;
g_free(flite_voice);
+ module_semaphore_free(flite_semaphore);
+ flite_semaphore = NULL;
return 0;
}
diff --git a/src/modules/generic.c b/src/modules/generic.c
index 8639f80..9da4f54 100644
--- a/src/modules/generic.c
+++ b/src/modules/generic.c
@@ -42,7 +42,7 @@ static int generic_speaking = 0;
static pthread_t generic_speak_thread;
static pid_t generic_pid;
-static sem_t *generic_semaphore;
+static sem_t *generic_semaphore = NULL;
static char **generic_message;
static SPDMessageType generic_message_type;
@@ -150,6 +150,14 @@ int module_init(char **status_info)
generic_message = g_malloc(sizeof(char *));
generic_semaphore = module_semaphore_init();
+ if (NULL == generic_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
+ return -1;
+ }
+
DBG("Generic: creating new thread for generic_speak\n");
generic_speaking = 0;
@@ -273,6 +281,9 @@ int module_close(void)
if (module_terminate_thread(generic_speak_thread) != 0)
return -1;
+
+ module_semaphore_free(generic_semaphore);
+ generic_semaphore = NULL;
return 0;
}
diff --git a/src/modules/ibmtts.c b/src/modules/ibmtts.c
index 1adfbb1..409b62d 100644
--- a/src/modules/ibmtts.c
+++ b/src/modules/ibmtts.c
@@ -160,9 +160,9 @@ static pthread_t ibmtts_synth_thread;
static pthread_t ibmtts_play_thread;
static pthread_t ibmtts_stop_or_pause_thread;
-static sem_t *ibmtts_synth_semaphore;
-static sem_t *ibmtts_play_semaphore;
-static sem_t *ibmtts_stop_or_pause_semaphore;
+static sem_t *ibmtts_synth_semaphore = NULL;
+static sem_t *ibmtts_play_semaphore = NULL;
+static sem_t *ibmtts_stop_or_pause_semaphore = NULL;
static pthread_mutex_t ibmtts_synth_suspended_mutex;
static pthread_mutex_t ibmtts_play_suspended_mutex;
@@ -494,6 +494,14 @@ int module_init(char **status_info)
DBG("Ibmtts: Creating new thread for stop or pause.");
ibmtts_stop_or_pause_semaphore = module_semaphore_init();
+ if (NULL == ibmtts_stop_or_pause_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
+ return FATAL_ERROR;
+ }
+
ret =
pthread_create(&ibmtts_stop_or_pause_thread, NULL,
_ibmtts_stop_or_pause, NULL);
@@ -510,6 +518,14 @@ int module_init(char **status_info)
DBG("Ibmtts: Creating new thread for playback.");
ibmtts_play_semaphore = module_semaphore_init();
+ if (NULL == ibmtts_play_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
+ return FATAL_ERROR;
+ }
+
ret = pthread_create(&ibmtts_play_thread, NULL, _ibmtts_play, NULL);
if (0 != ret) {
DBG("Ibmtts: play thread creation failed.");
@@ -523,6 +539,15 @@ int module_init(char **status_info)
DBG("Ibmtts: Creating new thread for IBM TTS synthesis.");
ibmtts_synth_semaphore = module_semaphore_init();
+ if (NULL == ibmtts_synth_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
+ return FATAL_ERROR;
+ }
+
+
ret = pthread_create(&ibmtts_synth_thread, NULL, _ibmtts_synth, NULL);
if (0 != ret) {
DBG("Ibmtts: synthesis thread creation failed.");
@@ -701,6 +726,13 @@ int module_close(void)
}
free_voice_list();
+
+ module_semaphore_free(ibmtts_synth_semaphore);
+ ibmtts_synth_semaphore = NULL;
+ module_semaphore_free(ibmtts_play_semaphore);
+ ibmtts_play_semaphore = NULL;
+ module_semaphore_free(ibmtts_stop_or_pause_semaphore);
+ ibmtts_stop_or_pause_semaphore = NULL;
return 0;
}
diff --git a/src/modules/ivona.c b/src/modules/ivona.c
index a4050ff..c3c41f0 100644
--- a/src/modules/ivona.c
+++ b/src/modules/ivona.c
@@ -51,7 +51,7 @@ DECLARE_DEBUG();
static int ivona_speaking = 0;
static pthread_t ivona_speak_thread;
-static sem_t *ivona_semaphore;
+static sem_t *ivona_semaphore = NULL;
static char **ivona_message;
static SPDMessageType ivona_message_type;
@@ -146,6 +146,14 @@ module_init(char **status_info)
*ivona_message = NULL;
ivona_semaphore = module_semaphore_init();
+ if (NULL == ivona_semaphore)
+ {
+ *status_info = g_strdup(MODULE_NAME
+ ": Failed to initialize semaphore!");
+ DBG(*status_info);
+ return -1;
+ }
+
DBG("Ivona: creating new thread for ivona_speak\n");
ivona_speaking = 0;
@@ -255,6 +263,9 @@ module_close(void)
DBG("Terminating threads");
if (module_terminate_thread(ivona_speak_thread) != 0)
return -1;
+
+ module_semaphore_free(ivona_semaphore);
+ ivona_semaphore = NULL;
return 0;
}
diff --git a/src/modules/module_utils.c b/src/modules/module_utils.c
index 6368011..57792a1 100644
--- a/src/modules/module_utils.c
+++ b/src/modules/module_utils.c
@@ -34,6 +34,7 @@
#include "module_utils.h"
static char* module_audio_pars[10];
+static int module_semaphore_count = 0;
extern char* module_index_mark;
@@ -57,7 +58,7 @@ do_message(SPDMessageType msgtype)
cur_line = NULL;
n = 0;
ret = spd_getline(&cur_line, &n, stdin);
- nlines++;
+ ++nlines;
if (ret == -1) return g_strdup("401 ERROR INTERNAL");
if (!strcmp(cur_line, "..\n")){
@@ -909,23 +910,56 @@ module_terminate_thread(pthread_t thread)
return 0;
}
+int
+module_semaphore_count_get(void)
+{
+ return module_semaphore_count;
+}
+
sem_t*
-module_semaphore_init()
+module_semaphore_init(void)
{
sem_t *semaphore;
int ret;
- semaphore = (sem_t *) g_malloc(sizeof(sem_t));
- if (semaphore == NULL) return NULL;
+ semaphore = (sem_t *) g_try_malloc(sizeof(sem_t));
+ if (NULL == semaphore) return NULL;
ret = sem_init(semaphore, 0, 0);
- if (ret != 0){
- DBG("Semaphore initialization failed");
+ if (0 == ret)
+ {
+ ++module_semaphore_count;
+ } else {
+ DBG("Semaphore initialization failed.");
g_free(semaphore);
semaphore = NULL;
}
return semaphore;
}
+void
+module_semaphore_free(sem_t *semaphore)
+{
+ if (NULL == semaphore)
+ {
+ DBG("Cannot destroy the semaphore, because it is NULL.");
+ return;
+ }
+
+ --module_semaphore_count;
+ if (module_semaphore_count < 0)
+ {
+ DBG("Invalid count of semaphores: %d", module_semaphore_count);
+ return;
+ }
+
+ if (0 > sem_destroy(semaphore))
+ {
+ DBG("Cannot destroy the semaphore: %s", strerror(errno));
+ }
+
+ g_free(semaphore);
+}
+
char *
module_recode_to_iso(char *data, int bytes, char *language, char *fallback)
{
diff --git a/src/modules/pico.c b/src/modules/pico.c
index 2e72e12..e520d74 100644
--- a/src/modules/pico.c
+++ b/src/modules/pico.c
@@ -106,8 +106,8 @@ static const SPDVoice *pico_voices_list[] = {
};
static GThread *pico_play_thread;
-static sem_t *pico_play_semaphore;
-static sem_t *pico_idle_semaphore;
+static sem_t *pico_play_semaphore = NULL;
+static sem_t *pico_idle_semaphore = NULL;
enum states { STATE_IDLE, STATE_PLAY, STATE_PAUSE, STATE_STOP, STATE_CLOSE };
static enum states pico_state;
@@ -623,9 +623,9 @@ int module_close(void)
picoSystem = NULL;
}
- g_free(pico_idle_semaphore);
- g_free(pico_play_semaphore);
+ module_semaphore_free(pico_idle_semaphore);
pico_idle_semaphore = NULL;
+ module_semaphore_free(pico_play_semaphore);
pico_play_semaphore = NULL;
return 0;
--
1.7.4.1
- [PATCH 1/1] Add module_semaphore_free() to destroy semaphore...,
sub <=
- [PATCH 1/1] Add module_semaphore_free() to destroy semaphore..., Trevor Saunders, 2011/12/07
- [PATCH 1/1] Add module_semaphore_free() to destroy semaphore..., Andrei Kholodnyi, 2011/12/07
- [PATCH 1/1] Add module_semaphore_free() to destroy semaphore..., Trevor Saunders, 2011/12/07
- [PATCH 1/1] Add module_semaphore_free() to destroy semaphore..., Andrei Kholodnyi, 2011/12/08
- [PATCH 1/1] Add module_semaphore_free() to destroy semaphore..., sub, 2011/12/08
- [PATCH 1/1] Add module_semaphore_free() to destroy semaphore..., Trevor Saunders, 2011/12/08
- [PATCH 1/1] Add module_semaphore_free() to destroy semaphore..., sub, 2011/12/09
- [PATCH 1/1] Add module_semaphore_free() to destroy semaphore..., Trevor Saunders, 2011/12/09