[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v2 3/3] Abstract unnamed semaphore
From: |
Boris Dušek |
Subject: |
[PATCH v2 3/3] Abstract unnamed semaphore |
Date: |
Sun, 29 Jul 2012 16:37:02 +0200 |
From: Boris Dus?ek <address@hidden>
To: address@hidden
Some systems lack unnamed semaphores. So wrap our use of unnamed
semaphores in thin wrappers around sem_* function to be able to add
different implementations of these wrappers on systems that need it.
---
include/spd_utils.h | 15 ++++++++++
src/clients/say/say.c | 19 +++++-------
src/common/Makefile.am | 2 +-
src/common/spd_sem.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++
src/modules/cicero.c | 10 +++----
src/modules/dummy.c | 12 ++++----
src/modules/espeak.c | 34 +++++++++++-----------
src/modules/festival.c | 14 ++++-----
src/modules/flite.c | 15 +++++-----
src/modules/generic.c | 14 ++++-----
src/modules/ibmtts.c | 52 ++++++++++++++++-----------------
src/modules/ivona.c | 15 +++++-----
src/modules/pico.c | 30 +++++++++----------
13 files changed, 196 insertions(+), 112 deletions(-)
create mode 100644 src/common/spd_sem.c
diff --git a/include/spd_utils.h b/include/spd_utils.h
index 165f7c0..7d37197 100644
--- a/include/spd_utils.h
+++ b/include/spd_utils.h
@@ -25,10 +25,25 @@
#include <stdio.h>
#include <stddef.h>
#include <sys/types.h>
+#include <semaphore.h>
ssize_t spd_getline(char **lineptr, size_t * n, FILE * f);
ssize_t safe_read(int fd, void *buf, size_t count);
ssize_t safe_write(int fd, const void *buf, size_t count);
+/**
+ * spd_sem_t and functions below behave like a POSIX unnamed semaphore
+ */
+struct spd_sem_s {
+ sem_t _sem;
+};
+typedef struct spd_sem_s spd_sem_t;
+
+void spd_sem_init(spd_sem_t *sem, unsigned value);
+void spd_sem_destroy(spd_sem_t *sem);
+void spd_sem_wait(spd_sem_t *sem);
+int spd_sem_trywait(spd_sem_t *sem);
+void spd_sem_post(spd_sem_t *sem);
+
#endif /* SPD_UTILS_H */
diff --git a/src/clients/say/say.c b/src/clients/say/say.c
index f92b845..caa60da 100644
--- a/src/clients/say/say.c
+++ b/src/clients/say/say.c
@@ -2,7 +2,7 @@
/*
* say.c - Super-simple Speech Dispatcher client
*
- * Copyright (C) 2001, 2002, 2003, 2007 Brailcom, o.p.s.
+ * Copyright (C) 2001, 2002, 2003, 2007, 2012 Brailcom, o.p.s.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
@@ -30,7 +30,6 @@
#include <string.h>
#include <stdlib.h>
#include <assert.h>
-#include <semaphore.h>
#include <errno.h>
#include <getopt.h>
@@ -45,15 +44,16 @@
#include "options.h"
#include <i18n.h>
+#include <spd_utils.h>
#define MAX_LINELEN 16384
-sem_t semaphore;
+spd_sem_t semaphore;
/* Callback for Speech Dispatcher notifications */
void end_of_speech(size_t msg_id, size_t client_id, SPDNotificationType type)
{
- sem_post(&semaphore);
+ spd_sem_post(&semaphore);
}
int main(int argc, char **argv)
@@ -253,12 +253,7 @@ int main(int argc, char **argv)
printf("Invalid sound_icon!\n");
if (wait_till_end) {
- ret = sem_init(&semaphore, 0, 0);
- if (ret < 0) {
- fprintf(stderr, "Can't initialize semaphore: %s",
- strerror(errno));
- return 0;
- }
+ spd_sem_init(&semaphore, 0);
/* Notify when the message is canceled or the speech terminates
*/
conn->callback_end = end_of_speech;
@@ -279,7 +274,7 @@ int main(int argc, char **argv)
} else {
spd_say(conn, spd_priority, line);
if (wait_till_end)
- sem_wait(&semaphore);
+ spd_sem_wait(&semaphore);
}
}
free(line);
@@ -298,7 +293,7 @@ int main(int argc, char **argv)
/* Wait till the callback is called */
if (wait_till_end)
- sem_wait(&semaphore);
+ spd_sem_wait(&semaphore);
}
}
diff --git a/src/common/Makefile.am b/src/common/Makefile.am
index 58da525..3f96ecf 100644
--- a/src/common/Makefile.am
+++ b/src/common/Makefile.am
@@ -5,5 +5,5 @@ libcommon_la_CFLAGS = $(ERROR_CFLAGS) $(GLIB_CFLAGS) \
-DGETTEXT_PACKAGE=\"$(GETTEXT_PACKAGE)\" -DLOCALEDIR=\"$(localedir)\"
libcommon_la_CPPFLAGS = "-I$(top_srcdir)/include/" $(GLIB_CFLAGS)
libcommon_la_LIBADD = $(GLIB_LIBS)
-libcommon_la_SOURCES = fdsetconv.c spd_getline.c i18n.c spd_safeio.c
+libcommon_la_SOURCES = fdsetconv.c spd_getline.c i18n.c spd_safeio.c spd_sem.c
diff --git a/src/common/spd_sem.c b/src/common/spd_sem.c
new file mode 100644
index 0000000..6d4cb72
--- /dev/null
+++ b/src/common/spd_sem.c
@@ -0,0 +1,76 @@
+/*
+ * spd_sem.c - portable implementations of POSIX unnamed semaphores
+ *
+ * Copyright (C) 2012 Brailcom, o.p.s.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this package; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <pthread.h>
+#include <spd_utils.h>
+
+#ifndef SPD_FATAL
+#define SPD_FATAL(...) exit(EXIT_FAILURE)
+#endif
+#define SPD_CHECK_PARAM(param) do { if (!param) SPD_FATAL(__func__ ": " #param
" == NULL"); } while(0)
+
+#include <semaphore.h>
+
+void spd_sem_init(spd_sem_t *sem, unsigned count)
+{
+ SPD_CHECK_PARAM(sem);
+ if (0 != sem_init(&sem->_sem, 0, count))
+ SPD_FATAL("sem_init returned non-zero: %s", strerror(errno));
+}
+
+void
+spd_sem_destroy(spd_sem_t *sem)
+{
+ SPD_CHECK_PARAM(sem);
+ if (0 != sem_destroy(&sem->_sem))
+ SPD_FATAL("sem_destroy returned non-zero: %s", strerror(errno));
+}
+
+void
+spd_sem_wait(spd_sem_t *sem)
+{
+ SPD_CHECK_PARAM(sem);
+ if (0 != sem_wait(&sem->_sem))
+ SPD_FATAL("sem_wait returned non-zero: %s", strerror(errno));
+}
+
+int
+spd_sem_trywait(spd_sem_t *sem)
+{
+ SPD_CHECK_PARAM(sem);
+ int ret = sem_trywait(&sem->_sem);
+ if ((0 != ret) && (EAGAIN != errno))
+ SPD_FATAL("sem_trywait returned non-zero and it was not because
the semaphore was already locked: %s", strerror(errno));
+ return ret;
+}
+
+void
+spd_sem_post(spd_sem_t *sem)
+{
+ SPD_CHECK_PARAM(sem);
+ if (0 != sem_post(&sem->_sem))
+ SPD_FATAL("sem_post returned non-zero: %s", strerror(errno));
+}
diff --git a/src/modules/cicero.c b/src/modules/cicero.c
index 5f3abe7..0202965 100644
--- a/src/modules/cicero.c
+++ b/src/modules/cicero.c
@@ -46,7 +46,7 @@ DECLARE_DEBUG()
static int cicero_speaking = 0;
static pthread_t cicero_speaking_thread;
-static sem_t cicero_semaphore;
+static spd_sem_t cicero_semaphore;
static char **cicero_message;
static SPDMessageType cicero_message_type;
@@ -207,7 +207,7 @@ int module_init(char **status_info)
cicero_message = g_malloc(sizeof(char *));
*cicero_message = NULL;
- sem_init(&cicero_semaphore , 0, 0);
+ spd_sem_init(&cicero_semaphore, 0);
DBG("Cicero: creating new thread for cicero_tracking\n");
cicero_speaking = 0;
@@ -259,7 +259,7 @@ int module_speak(gchar * data, size_t bytes, SPDMessageType
msgtype)
/* Send semaphore signal to the speaking thread */
cicero_speaking = 1;
- sem_post(&cicero_semaphore);
+ spd_sem_post(&cicero_semaphore);
DBG("Cicero: leaving module_speak() normaly\n\r");
return bytes;
@@ -298,7 +298,7 @@ int module_close(void)
if (module_terminate_thread(cicero_speaking_thread) != 0)
return -1;
- sem_destroy(&cicero_semaphore );
+ spd_sem_destroy(&cicero_semaphore);
return 0;
}
@@ -317,7 +317,7 @@ void *_cicero_speak(void *nothing)
DBG("cicero: speaking thread starting.......\n");
set_speaking_thread_parameters();
while (1) {
- sem_wait(&cicero_semaphore);
+ spd_sem_wait(&cicero_semaphore);
DBG("Semaphore on\n");
len = strlen(*cicero_message);
cicero_stop = 0;
diff --git a/src/modules/dummy.c b/src/modules/dummy.c
index 28fd5e4..c3cb2c5 100644
--- a/src/modules/dummy.c
+++ b/src/modules/dummy.c
@@ -30,9 +30,9 @@
#endif
#include <glib.h>
-#include <semaphore.h>
#include <speechd_types.h>
+#include <spd_utils.h>
#include "module_utils.h"
@@ -46,7 +46,7 @@ static int dummy_speaking = 0;
static pthread_t dummy_speak_thread;
static pid_t dummy_pid;
-static sem_t dummy_semaphore;
+static spd_sem_t dummy_semaphore;
/* Internal functions prototypes */
static void *_dummy_speak(void *);
@@ -69,7 +69,7 @@ int module_init(char **status_info)
*status_info = NULL;
- sem_init(&dummy_semaphore, 0, 0);
+ spd_sem_init(&dummy_semaphore, 0);
DBG("Dummy: creating new thread for dummy_speak\n");
dummy_speaking = 0;
@@ -109,7 +109,7 @@ int module_speak(gchar * data, size_t bytes, SPDMessageType
msgtype)
/* Send semaphore signal to the speaking thread */
dummy_speaking = 1;
- sem_post(&dummy_semaphore);
+ spd_sem_post(&dummy_semaphore);
DBG("Dummy: leaving write() normaly\n\r");
return bytes;
@@ -155,7 +155,7 @@ int module_close(void)
if (module_terminate_thread(dummy_speak_thread) != 0)
return -1;
- sem_destroy(&dummy_semaphore );
+ spd_sem_destroy(&dummy_semaphore);
return 0;
}
@@ -171,7 +171,7 @@ void *_dummy_speak(void *nothing)
set_speaking_thread_parameters();
while (1) {
- sem_wait(&dummy_semaphore);
+ spd_sem_wait(&dummy_semaphore);
DBG("Semaphore on\n");
module_report_event_begin();
diff --git a/src/modules/espeak.c b/src/modules/espeak.c
index 1457121..75bff84 100644
--- a/src/modules/espeak.c
+++ b/src/modules/espeak.c
@@ -2,7 +2,7 @@
/*
* espeak.c - Speech Dispatcher backend for espeak
*
- * Copyright (C) 2007 Brailcom, o.p.s.
+ * Copyright (C) 2007, 2012 Brailcom, o.p.s.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
@@ -34,7 +34,6 @@
/* System includes. */
#include <string.h>
#include <glib.h>
-#include <semaphore.h>
/* espeak header file */
#include <espeak/speak_lib.h>
@@ -46,6 +45,7 @@
#include "spd_audio.h"
#include <speechd_types.h>
#include "module_utils.h"
+#include <spd_utils.h>
/* > */
@@ -88,9 +88,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 spd_sem_t espeak_stop_or_pause_semaphore;
static pthread_mutex_t espeak_stop_or_pause_suspended_mutex;
-static sem_t espeak_play_semaphore;
+static spd_sem_t espeak_play_semaphore;
static pthread_mutex_t espeak_play_suspended_mutex;
static gboolean espeak_close_requested = FALSE;
@@ -293,7 +293,7 @@ int module_init(char **status_info)
pthread_mutex_init(&espeak_state_mutex, NULL);
DBG("Espeak: Creating new thread for stop or pause.");
- sem_init(&espeak_stop_or_pause_semaphore, 0, 0);
+ spd_sem_init(espeak_stop_or_pause_semaphore, 0);
ret =
pthread_create(&espeak_stop_or_pause_thread, NULL,
@@ -305,7 +305,7 @@ int module_init(char **status_info)
return FATAL_ERROR;
}
- sem_init(&espeak_play_semaphore, 0, 0);
+ spd_sem_init(espeak_play_semaphore, 0);
DBG("Espeak: Creating new thread for playback.");
ret = pthread_create(&espeak_play_thread, NULL, _espeak_play, NULL);
@@ -446,7 +446,7 @@ int module_stop(void)
DBG("Espeak: stopping...");
espeak_stop_requested = TRUE;
/* Wake the stop_or_pause thread. */
- sem_post(&espeak_stop_or_pause_semaphore);
+ spd_sem_post(&espeak_stop_or_pause_semaphore);
} else {
DBG("Espeak: Cannot stop now.");
}
@@ -479,8 +479,8 @@ int module_close(void)
pthread_cond_broadcast(&playback_queue_condition);
pthread_mutex_unlock(&playback_queue_mutex);
- sem_post(&espeak_play_semaphore);
- sem_post(&espeak_stop_or_pause_semaphore);
+ spd_sem_post(&espeak_play_semaphore);
+ spd_sem_post(&espeak_stop_or_pause_semaphore);
/* Give threads a chance to quit on their own terms. */
g_usleep(25000);
@@ -505,8 +505,8 @@ 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);
+ spd_sem_destroy(&espeak_play_semaphore);
+ spd_sem_destroy(&espeak_stop_or_pause_semaphore);
return 0;
}
@@ -543,10 +543,10 @@ static void *_espeak_stop_or_pause(void *nothing)
while (!espeak_close_requested) {
/* If semaphore not set, set suspended lock and suspend until
it is signaled. */
- if (0 != sem_trywait(&espeak_stop_or_pause_semaphore)) {
+ if (0 != spd_sem_trywait(&espeak_stop_or_pause_semaphore)) {
pthread_mutex_lock
(&espeak_stop_or_pause_suspended_mutex);
- sem_wait(&espeak_stop_or_pause_semaphore);
+ spd_sem_wait(&espeak_stop_or_pause_semaphore);
pthread_mutex_unlock
(&espeak_stop_or_pause_suspended_mutex);
}
@@ -807,7 +807,7 @@ static int synth_callback(short *wav, int numsamples,
espeak_EVENT * events)
espeak_state = BEFORE_PLAY;
espeak_add_flag_to_playback_queue(ESPEAK_QET_BEGIN);
/* Wake up playback thread */
- sem_post(&espeak_play_semaphore);
+ spd_sem_post(&espeak_play_semaphore);
}
pthread_mutex_unlock(&espeak_state_mutex);
@@ -1044,9 +1044,9 @@ static void *_espeak_play(void *nothing)
while (!espeak_close_requested) {
/* If semaphore not set, set suspended lock and suspend until
it is signaled. */
- if (0 != sem_trywait(&espeak_play_semaphore)) {
+ if (0 != spd_sem_trywait(&espeak_play_semaphore)) {
pthread_mutex_lock(&espeak_play_suspended_mutex);
- sem_wait(&espeak_play_semaphore);
+ spd_sem_wait(&espeak_play_semaphore);
pthread_mutex_unlock(&espeak_play_suspended_mutex);
}
DBG("Espeak: Playback semaphore on.");
@@ -1088,7 +1088,7 @@ static void *_espeak_play(void *nothing)
espeak_stop_requested = TRUE;
espeak_pause_state =
ESPEAK_PAUSE_MARK_REPORTED;
- sem_post
+ spd_sem_post
(&espeak_stop_or_pause_semaphore);
finished = TRUE;
}
diff --git a/src/modules/festival.c b/src/modules/festival.c
index d6edb2f..7169368 100644
--- a/src/modules/festival.c
+++ b/src/modules/festival.c
@@ -1,7 +1,7 @@
/*
* festival.c - Speech Dispatcher backend for Festival
*
- * Copyright (C) 2003, 2007 Brailcom, o.p.s.
+ * Copyright (C) 2003, 2007, 2012 Brailcom, o.p.s.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
@@ -26,9 +26,9 @@
#endif
#include <stdio.h>
-#include <semaphore.h>
#include <speechd_types.h>
+#include <spd_utils.h>
#include "fdsetconv.h"
#include "festival_client.h"
@@ -41,7 +41,7 @@ DECLARE_DEBUG()
/* Thread and process control */
static pthread_t festival_speak_thread;
-static sem_t festival_semaphore;
+static spd_sem_t festival_semaphore;
static int festival_speaking = 0;
static int festival_pause_requested = 0;
@@ -287,7 +287,7 @@ int module_init(char **status_info)
with festival in a separate thread (to be faster in communication
with Speech Dispatcher) */
- sem_init(&festival_semaphore, 0, 0);
+ spd_sem_init(&festival_semaphore, 0);
DBG("Festival: creating new thread for festival_speak\n");
festival_speaking = 0;
@@ -399,7 +399,7 @@ int module_speak(char *data, size_t bytes, SPDMessageType
msgtype)
/* Send semaphore signal to the speaking thread */
festival_speaking = 1;
- sem_post(&festival_semaphore);
+ spd_sem_post(&festival_semaphore);
DBG("Festival: leaving write() normaly\n\r");
return bytes;
@@ -478,7 +478,7 @@ int module_close(void)
// DBG("Removing junk files in tmp/");
// system("rm -f /tmp/est* 2> /dev/null");
- sem_destroy(&festival_semaphore);
+ spd_sem_destroy(&festival_semaphore);
return 0;
}
@@ -613,7 +613,7 @@ void *_festival_speak(void *nothing)
while (1) {
sem_wait:
- sem_wait(&festival_semaphore);
+ spd_sem_wait(&festival_semaphore);
DBG("Semaphore on, speaking\n");
festival_stop = 0;
diff --git a/src/modules/flite.c b/src/modules/flite.c
index dd7d462..db66528 100644
--- a/src/modules/flite.c
+++ b/src/modules/flite.c
@@ -2,7 +2,7 @@
/*
* flite.c - Speech Dispatcher backend for Flite (Festival Lite)
*
- * Copyright (C) 2001, 2002, 2003, 2007 Brailcom, o.p.s.
+ * Copyright (C) 2001, 2002, 2003, 2007, 2012 Brailcom, o.p.s.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
@@ -26,12 +26,11 @@
#include <config.h>
#endif
-#include <semaphore.h>
-
#include <flite/flite.h>
#include "spd_audio.h"
#include <speechd_types.h>
+#include <spd_utils.h>
#include "module_utils.h"
@@ -45,7 +44,7 @@ DECLARE_DEBUG();
static int flite_speaking = 0;
static pthread_t flite_speak_thread;
-static sem_t flite_semaphore;
+static spd_sem_t flite_semaphore;
static char **flite_message;
static SPDMessageType flite_message_type;
@@ -127,7 +126,7 @@ int module_init(char **status_info)
flite_message = g_malloc(sizeof(char *));
*flite_message = NULL;
- sem_init(&flite_semaphore, 0, 0);
+ spd_sem_init(&flite_semaphore, 0);
DBG("Flite: creating new thread for flite_speak\n");
flite_speaking = 0;
@@ -179,7 +178,7 @@ int module_speak(gchar * data, size_t bytes, SPDMessageType
msgtype)
/* Send semaphore signal to the speaking thread */
flite_speaking = 1;
- sem_post(&flite_semaphore);
+ spd_sem_post(&flite_semaphore);
DBG("Flite: leaving write() normally\n\r");
return bytes;
@@ -231,7 +230,7 @@ int module_close(void)
return -1;
g_free(flite_voice);
- sem_destroy(&flite_semaphore);
+ spd_sem_destroy(&flite_semaphore);
return 0;
}
@@ -276,7 +275,7 @@ void *_flite_speak(void *nothing)
set_speaking_thread_parameters();
while (1) {
- sem_wait(&flite_semaphore);
+ spd_sem_wait(&flite_semaphore);
DBG("Semaphore on\n");
flite_stop = 0;
diff --git a/src/modules/generic.c b/src/modules/generic.c
index 09de945..a7caaa7 100644
--- a/src/modules/generic.c
+++ b/src/modules/generic.c
@@ -2,7 +2,7 @@
/*
* generic.c - Speech Dispatcher generic output module
*
- * Copyright (C) 2001, 2002, 2003, 2007 Brailcom, o.p.s.
+ * Copyright (C) 2001, 2002, 2003, 2007, 2012 Brailcom, o.p.s.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
@@ -27,9 +27,9 @@
#endif
#include <glib.h>
-#include <semaphore.h>
#include <speechd_types.h>
+#include <spd_utils.h>
#include "module_utils.h"
@@ -43,7 +43,7 @@ static int generic_speaking = 0;
static pthread_t generic_speak_thread;
static pid_t generic_pid;
-static sem_t generic_semaphore;
+static spd_sem_t generic_semaphore;
static char **generic_message;
static SPDMessageType generic_message_type;
@@ -151,7 +151,7 @@ int module_init(char **status_info)
generic_message = g_malloc(sizeof(char *));
- sem_init(&generic_semaphore, 0, 0);
+ spd_sem_init(&generic_semaphore, 0);
DBG("Generic: creating new thread for generic_speak\n");
generic_speaking = 0;
@@ -229,7 +229,7 @@ int module_speak(gchar * data, size_t bytes, SPDMessageType
msgtype)
/* Send semaphore signal to the speaking thread */
generic_speaking = 1;
- sem_post(&generic_semaphore);
+ spd_sem_post(&generic_semaphore);
DBG("Generic: leaving write() normaly\n\r");
return bytes;
@@ -276,7 +276,7 @@ int module_close(void)
if (module_terminate_thread(generic_speak_thread) != 0)
return -1;
- sem_destroy(&generic_semaphore);
+ spd_sem_destroy(&generic_semaphore);
return 0;
}
@@ -324,7 +324,7 @@ void *_generic_speak(void *nothing)
set_speaking_thread_parameters();
while (1) {
- sem_wait(&generic_semaphore);
+ spd_sem_wait(&generic_semaphore);
DBG("Semaphore on\n");
ret = pipe(module_pipe.pc);
diff --git a/src/modules/ibmtts.c b/src/modules/ibmtts.c
index b615c3b..a1fc11a 100644
--- a/src/modules/ibmtts.c
+++ b/src/modules/ibmtts.c
@@ -2,7 +2,7 @@
/*
* ibmtts.c - Speech Dispatcher backend for IBM TTS
*
- * Copyright (C) 2006, 2007 Brailcom, o.p.s.
+ * Copyright (C) 2006, 2007, 2012 Brailcom, o.p.s.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -51,7 +51,6 @@
/* System includes. */
#include <string.h>
#include <glib.h>
-#include <semaphore.h>
/* IBM Eloquence Command Interface.
Won't exist unless IBM TTS or IBM TTS SDK is installed. */
@@ -60,6 +59,7 @@
/* Speech Dispatcher includes. */
#include "spd_audio.h"
#include <speechd_types.h>
+#include <spd_utils.h>
#include "module_utils.h"
typedef enum { IBMTTS_FALSE, IBMTTS_TRUE } TIbmttsBool;
@@ -161,9 +161,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 spd_sem_t ibmtts_synth_semaphore;
+static spd_sem_t ibmtts_play_semaphore;
+static spd_sem_t ibmtts_stop_or_pause_semaphore;
static pthread_mutex_t ibmtts_synth_suspended_mutex;
static pthread_mutex_t ibmtts_play_suspended_mutex;
@@ -494,7 +494,7 @@ int module_init(char **status_info)
*ibmtts_message = NULL;
DBG("Ibmtts: Creating new thread for stop or pause.");
- sem_init(&ibmtts_stop_or_pause_semaphore, 0, 0);
+ spd_sem_init(&ibmtts_stop_or_pause_semaphore, 0);
ret =
pthread_create(&ibmtts_stop_or_pause_thread, NULL,
@@ -511,7 +511,7 @@ int module_init(char **status_info)
}
DBG("Ibmtts: Creating new thread for playback.");
- sem_init(&ibmtts_play_semaphore, 0, 0);
+ spd_sem_init(&ibmtts_play_semaphore, 0);
ret = pthread_create(&ibmtts_play_thread, NULL, _ibmtts_play, NULL);
if (0 != ret) {
@@ -525,7 +525,7 @@ int module_init(char **status_info)
}
DBG("Ibmtts: Creating new thread for IBM TTS synthesis.");
- sem_init(&ibmtts_synth_semaphore, 0, 0);
+ spd_sem_init(&ibmtts_synth_semaphore, 0);
ret = pthread_create(&ibmtts_synth_thread, NULL, _ibmtts_synth, NULL);
if (0 != ret) {
@@ -615,7 +615,7 @@ int module_speak(gchar * data, size_t bytes, SPDMessageType
msgtype)
}
/* Send semaphore signal to the synthesis thread */
- sem_post(&ibmtts_synth_semaphore);
+ spd_sem_post(&ibmtts_synth_semaphore);
DBG("Ibmtts: Leaving module_speak() normally.");
return TRUE;
@@ -635,7 +635,7 @@ int module_stop(void)
ibmtts_stop_play_requested = IBMTTS_TRUE;
/* Wake the stop_or_pause thread. */
- sem_post(&ibmtts_stop_or_pause_semaphore);
+ spd_sem_post(&ibmtts_stop_or_pause_semaphore);
}
return OK;
@@ -657,7 +657,7 @@ size_t module_pause(void)
ibmtts_pause_requested = IBMTTS_TRUE;
/* Wake the stop_or_pause thread. */
- sem_post(&ibmtts_stop_or_pause_semaphore);
+ spd_sem_post(&ibmtts_stop_or_pause_semaphore);
return OK;
}
@@ -686,9 +686,9 @@ int module_close(void)
/* Request each thread exit and wait until it exits. */
DBG("Ibmtts: Terminating threads");
ibmtts_thread_exit_requested = IBMTTS_TRUE;
- sem_post(&ibmtts_synth_semaphore);
- sem_post(&ibmtts_play_semaphore);
- sem_post(&ibmtts_stop_or_pause_semaphore);
+ spd_sem_post(&ibmtts_synth_semaphore);
+ spd_sem_post(&ibmtts_play_semaphore);
+ spd_sem_post(&ibmtts_stop_or_pause_semaphore);
if (0 != pthread_join(ibmtts_synth_thread, NULL))
return -1;
if (0 != pthread_join(ibmtts_play_thread, NULL))
@@ -705,9 +705,9 @@ int module_close(void)
}
free_voice_list();
- sem_destroy(&ibmtts_synth_semaphore);
- sem_destroy(&ibmtts_play_semaphore);
- sem_destroy(&ibmtts_stop_or_pause_semaphore);
+ spd_sem_destroy(&ibmtts_synth_semaphore);
+ spd_sem_destroy(&ibmtts_play_semaphore);
+ spd_sem_destroy(&ibmtts_stop_or_pause_semaphore);
return 0;
}
@@ -780,10 +780,10 @@ static void *_ibmtts_stop_or_pause(void *nothing)
while (!ibmtts_thread_exit_requested) {
/* If semaphore not set, set suspended lock and suspend until
it is signaled. */
- if (0 != sem_trywait(&ibmtts_stop_or_pause_semaphore)) {
+ if (0 != spd_sem_trywait(&ibmtts_stop_or_pause_semaphore)) {
pthread_mutex_lock
(&ibmtts_stop_or_pause_suspended_mutex);
- sem_wait(&ibmtts_stop_or_pause_semaphore);
+ spd_sem_wait(&ibmtts_stop_or_pause_semaphore);
pthread_mutex_unlock
(&ibmtts_stop_or_pause_suspended_mutex);
if (ibmtts_thread_exit_requested)
@@ -924,9 +924,9 @@ static void *_ibmtts_synth(void *nothing)
while (!ibmtts_thread_exit_requested) {
/* If semaphore not set, set suspended lock and suspend until
it is signaled. */
- if (0 != sem_trywait(&ibmtts_synth_semaphore)) {
+ if (0 != spd_sem_trywait(&ibmtts_synth_semaphore)) {
pthread_mutex_lock(&ibmtts_synth_suspended_mutex);
- sem_wait(&ibmtts_synth_semaphore);
+ spd_sem_wait(&ibmtts_synth_semaphore);
pthread_mutex_unlock(&ibmtts_synth_suspended_mutex);
if (ibmtts_thread_exit_requested)
break;
@@ -962,7 +962,7 @@ static void *_ibmtts_synth(void *nothing)
/* Wake up the audio playback thread, if not
already awake. */
if (!is_thread_busy
(&ibmtts_play_suspended_mutex))
- sem_post(&ibmtts_play_semaphore);
+ spd_sem_post(&ibmtts_play_semaphore);
continue;
} else
eciSetParam(eciHandle, eciTextMode,
@@ -1374,7 +1374,7 @@ static enum ECICallbackReturn eciCallback(ECIHand hEngine,
ret = ibmtts_add_audio_to_playback_queue(audio_chunk, lparam);
/* Wake up the audio playback thread, if not already awake. */
if (!is_thread_busy(&ibmtts_play_suspended_mutex))
- sem_post(&ibmtts_play_semaphore);
+ spd_sem_post(&ibmtts_play_semaphore);
return eciDataProcessed;
break;
case eciIndexReply:
@@ -1387,7 +1387,7 @@ static enum ECICallbackReturn eciCallback(ECIHand hEngine,
}
/* Wake up the audio playback thread, if not already awake. */
if (!is_thread_busy(&ibmtts_play_suspended_mutex))
- sem_post(&ibmtts_play_semaphore);
+ spd_sem_post(&ibmtts_play_semaphore);
return eciDataProcessed;
break;
default:
@@ -1539,9 +1539,9 @@ static void *_ibmtts_play(void *nothing)
while (!ibmtts_thread_exit_requested) {
/* If semaphore not set, set suspended lock and suspend until
it is signaled. */
- if (0 != sem_trywait(&ibmtts_play_semaphore)) {
+ if (0 != spd_sem_trywait(&ibmtts_play_semaphore)) {
pthread_mutex_lock(&ibmtts_play_suspended_mutex);
- sem_wait(&ibmtts_play_semaphore);
+ spd_sem_wait(&ibmtts_play_semaphore);
pthread_mutex_unlock(&ibmtts_play_suspended_mutex);
}
/* DBG("Ibmtts: Playback semaphore on."); */
diff --git a/src/modules/ivona.c b/src/modules/ivona.c
index 65a3fe0..0632ebc 100644
--- a/src/modules/ivona.c
+++ b/src/modules/ivona.c
@@ -2,7 +2,7 @@
/*
* ivona.c - Speech Dispatcher backend for Ivona (IVO Software)
*
- * Copyright (C) 2001, 2002, 2003, 2007 Brailcom, o.p.s.
+ * Copyright (C) 2001, 2002, 2003, 2007, 2012 Brailcom, o.p.s.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
@@ -29,12 +29,11 @@
#include <config.h>
#endif
-#include <semaphore.h>
-
#include <libdumbtts.h>
#include "spd_audio.h"
#include <speechd_types.h>
+#include <spd_utils.h>
#include "module_utils.h"
#include "ivona_client.h"
@@ -53,7 +52,7 @@ DECLARE_DEBUG();
static int ivona_speaking = 0;
static pthread_t ivona_speak_thread;
-static sem_t ivona_semaphore;
+static spd_sem_t ivona_semaphore;
static char **ivona_message;
static SPDMessageType ivona_message_type;
@@ -147,7 +146,7 @@ module_init(char **status_info)
ivona_message = g_malloc (sizeof (char*));
*ivona_message = NULL;
- sem_init(&ivona_semaphore, 0, 0);
+ spd_sem_init(&ivona_semaphore, 0);
DBG("Ivona: creating new thread for ivona_speak\n");
ivona_speaking = 0;
@@ -206,7 +205,7 @@ module_speak(gchar *data, size_t bytes, SPDMessageType
msgtype)
/* Send semaphore signal to the speaking thread */
ivona_speaking = 1;
- sem_post(&ivona_semaphore);
+ spd_sem_post(&ivona_semaphore);
DBG("Ivona: leaving write() normally\n\r");
return bytes;
@@ -258,7 +257,7 @@ module_close(void)
if (module_terminate_thread(ivona_speak_thread) != 0)
return -1;
- sem_destroy(&ivona_semaphore);
+ spd_sem_destroy(&ivona_semaphore);
return 0;
}
@@ -426,7 +425,7 @@ _ivona_speak(void* nothing)
set_speaking_thread_parameters();
while(1){
- sem_wait(&ivona_semaphore);
+ spd_sem_wait(&ivona_semaphore);
DBG("Semaphore on\n");
ivona_stop = 0;
diff --git a/src/modules/pico.c b/src/modules/pico.c
index 9df4622..2112a3c 100644
--- a/src/modules/pico.c
+++ b/src/modules/pico.c
@@ -3,7 +3,7 @@
*
* A SVOX pico output module
*
- * Copyright (C) 2010 Brailcom, o.p.s.
+ * Copyright (C) 2010, 2012 Brailcom, o.p.s.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
@@ -32,12 +32,12 @@
#include <string.h>
#include <glib.h>
-#include <semaphore.h>
#include <picoapi.h>
#include "spd_audio.h"
#include <speechd_types.h>
+#include <spd_utils.h>
#include "module_utils.h"
#define MODULE_NAME "pico"
@@ -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 spd_sem_t pico_play_semaphore;
+static spd_sem_t pico_idle_semaphore;
enum states { STATE_IDLE, STATE_PLAY, STATE_PAUSE, STATE_STOP, STATE_CLOSE };
static enum states pico_state;
@@ -246,7 +246,7 @@ static gpointer pico_play_func(gpointer nothing)
while (g_atomic_int_get(&pico_state) != STATE_CLOSE) {
- sem_wait(&pico_play_semaphore);
+ spd_sem_wait(&pico_play_semaphore);
if (g_atomic_int_get(&pico_state) != STATE_PLAY)
continue;
@@ -265,14 +265,14 @@ static gpointer pico_play_func(gpointer nothing)
if (g_atomic_int_get(&pico_state) == STATE_STOP) {
module_report_event_stop();
g_atomic_int_set(&pico_state, STATE_IDLE);
- sem_post(&pico_idle_semaphore);
+ spd_sem_post(&pico_idle_semaphore);
}
if (g_atomic_int_get(&pico_state) == STATE_PAUSE) {
module_report_event_pause();
g_atomic_int_set(&pico_state, STATE_IDLE);
- sem_post(&pico_idle_semaphore);
+ spd_sem_post(&pico_idle_semaphore);
}
DBG(MODULE_NAME ": state %d", pico_state);
@@ -397,8 +397,8 @@ int module_init(char **status_info)
if (!g_thread_supported())
g_thread_init(NULL);
- sem_init(&pico_play_semaphore, 0, 0);
- sem_init(&pico_idle_semaphore, 0, 0);
+ spd_sem_init(&pico_play_semaphore, 0);
+ spd_sem_init(&pico_idle_semaphore, 0);
if ((pico_play_thread = g_thread_create((GThreadFunc) pico_play_func,
NULL, TRUE, &error)) == NULL) {
@@ -552,7 +552,7 @@ int module_speak(char *data, size_t bytes, SPDMessageType
msgtype)
}
*/
g_atomic_int_set(&pico_state, STATE_PLAY);
- sem_post(&pico_play_semaphore);
+ spd_sem_post(&pico_play_semaphore);
return bytes;
}
@@ -567,7 +567,7 @@ int module_stop(void)
}
g_atomic_int_set(&pico_state, STATE_STOP);
- sem_wait(&pico_idle_semaphore);
+ spd_sem_wait(&pico_idle_semaphore);
/* reset Pico engine. */
if ((ret = pico_resetEngine(picoEngine, PICO_RESET_SOFT))) {
@@ -591,7 +591,7 @@ size_t module_pause(void)
}
g_atomic_int_set(&pico_state, STATE_PAUSE);
- sem_wait(&pico_idle_semaphore);
+ spd_sem_wait(&pico_idle_semaphore);
/* reset Pico engine. */
if ((ret = pico_resetEngine(picoEngine, PICO_RESET_SOFT))) {
@@ -608,7 +608,7 @@ int module_close(void)
{
g_atomic_int_set(&pico_state, STATE_CLOSE);
- sem_post(&pico_play_semaphore);
+ spd_sem_post(&pico_play_semaphore);
g_thread_join(pico_play_thread);
@@ -617,8 +617,8 @@ int module_close(void)
picoSystem = NULL;
}
- sem_destroy(&pico_idle_semaphore);
- sem_destroy(&pico_play_semaphore);
+ spd_sem_destroy(&pico_idle_semaphore);
+ spd_sem_destroy(&pico_play_semaphore);
return 0;
}
--
1.7.9.6 (Apple Git-31.1)
- [PATCH 3/3] Abstract unnamed semaphore, (continued)
[PATCH 1/3] Remove references to TEMP_FAILURE_RETRY, Trevor Saunders, 2012/07/25
[PATCH v2 0/3] Portability improvements, Boris Dušek, 2012/07/29