[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 07/10] Abstract unnamed semaphore interface and add OS X implemen
From: |
Boris Dušek |
Subject: |
[PATCH 07/10] Abstract unnamed semaphore interface and add OS X implementation |
Date: |
Mon, 23 Jul 2012 15:24:18 +0200 |
From: Boris Dus?ek <address@hidden>
To: address@hidden
---
include/spd_utils.h | 12 +++
src/clients/say/say.c | 13 ++-
src/common/Makefile.am | 2 +-
src/common/spd_sem.c | 224 ++++++++++++++++++++++++++++++++++++++++++++
src/modules/cicero.c | 10 +-
src/modules/dummy.c | 10 +-
src/modules/espeak.c | 32 +++---
src/modules/festival.c | 10 +-
src/modules/flite.c | 10 +-
src/modules/generic.c | 10 +-
src/modules/ibmtts.c | 48 +++++-----
src/modules/ivona.c | 10 +-
src/modules/module_utils.c | 4 +-
src/modules/module_utils.h | 3 +-
src/modules/pico.c | 26 +++---
15 files changed, 331 insertions(+), 93 deletions(-)
create mode 100644 src/common/spd_sem.c
diff --git a/include/spd_utils.h b/include/spd_utils.h
index f07d13a..fe927ec 100644
--- a/include/spd_utils.h
+++ b/include/spd_utils.h
@@ -27,4 +27,16 @@
#include <sys/types.h>
ssize_t spd_getline(char **lineptr, size_t * n, FILE * f);
+
+/**
+ * spd_sem_t and functions below behave like a POSIX unnamed semaphore
+ */
+struct spd_sem_s;
+typedef struct spd_sem_s spd_sem_t;
+spd_sem_t* spd_sem_create(unsigned value);
+int spd_sem_destroy(spd_sem_t *sem);
+int spd_sem_wait(spd_sem_t *sem);
+int spd_sem_trywait(spd_sem_t *sem);
+int 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..6db0c1f 100644
--- a/src/clients/say/say.c
+++ b/src/clients/say/say.c
@@ -45,15 +45,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,8 +254,8 @@ int main(int argc, char **argv)
printf("Invalid sound_icon!\n");
if (wait_till_end) {
- ret = sem_init(&semaphore, 0, 0);
- if (ret < 0) {
+ semaphore = spd_sem_create(0);
+ if (0 == semaphore) {
fprintf(stderr, "Can't initialize semaphore: %s",
strerror(errno));
return 0;
@@ -279,7 +280,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 +299,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 139ba32..eeb6fc0 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
+libcommon_la_SOURCES = fdsetconv.c spd_getline.c i18n.c spd_sem.c
diff --git a/src/common/spd_sem.c b/src/common/spd_sem.c
new file mode 100644
index 0000000..d589ba6
--- /dev/null
+++ b/src/common/spd_sem.c
@@ -0,0 +1,224 @@
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <pthread.h>
+#include <spd_utils.h>
+#ifndef DBG
+#define DBG(...)
+#endif
+
+#ifdef __APPLE__
+/* Implement unnamed semaphore on Mac OS X using 1 condition variable and 1
mutex */
+struct spd_sem_s {
+ pthread_mutex_t count_mutex;
+ pthread_cond_t nonzero;
+ int count;
+};
+
+/* Implementation note: the code below more or less follows this pattern:
+ *
+ * if (unsuccessful to acquire resource 1) {
+ * log error;
+ * set return value;
+ * } else {
+ * continue recursively with acquiring needed resources;
+ * release resource 1 (and log error if unsuccessful);
+ * }
+ * return;
+ */
+
+spd_sem_t*
+spd_sem_create(unsigned count)
+{
+ spd_sem_t *sem;
+ int err;
+
+ sem = (spd_sem_t *) malloc(sizeof(spd_sem_t));
+ if (sem == NULL) {
+ DBG("Could not allocate memory for semaphore");
+ errno = ENOMEM;
+ } else {
+ if (0 != (err = pthread_mutex_init(&sem->count_mutex, 0))) {
+ DBG("Semaphore's mutex initialization failed: %s",
strerror(err));
+ errno = err;
+ } else {
+ if (0 != (err = pthread_cond_init(&sem->nonzero, 0))) {
+ DBG("Semaphore's condition variable
initialization failed: %s", strerror(err));
+ errno = err;
+ } else {
+ /* commit and escape rollback with return */
+ sem->count = count;
+ return sem;
+ }
+ if (0 != (err =
pthread_mutex_destroy(&sem->count_mutex))) {
+ DBG("Semaphore's mutex could not be destroyed:
%s", strerror(err));
+ /* let's keep errno of the first error that
occured */
+ }
+ }
+ free(sem);
+ }
+ return NULL;
+}
+
+int
+spd_sem_destroy(spd_sem_t *sem)
+{
+ int ret = 0, err;
+ if (sem == NULL) {
+ DBG("Logic error: sem is NULL");
+ ret = -1;
+ errno = EINVAL;
+ } else {
+ if (0 != (err = pthread_mutex_destroy(&sem->count_mutex))) {
+ DBG("Semaphore's mutex could not be destroyed: %s",
strerror(err));
+ errno = err;
+ ret = -1;
+ }
+ if (0 != (err = pthread_cond_destroy(&sem->nonzero))) {
+ DBG("Semaphore's mutex could not be destroyed: %s",
strerror(err));
+ errno = err;
+ ret = -1;
+ }
+ free(sem);
+ }
+ return ret;
+}
+
+int
+spd_sem_wait(spd_sem_t *sem)
+{
+ int ret = -1, err;
+ if (0 != (err = pthread_mutex_lock(&sem->count_mutex))) {
+ DBG("Could not lock semaphore's mutex: %s", strerror(err));
+ errno = err;
+ } else {
+ /* err is 0 here */
+ while (sem->count == 0) {
+ if (0 != (err = pthread_cond_wait(&sem->nonzero,
&sem->count_mutex))) {
+ DBG("Could not wait for condition variable:
%s", strerror(err));
+ errno = err;
+ ret = -1;
+ }
+ }
+ if (0 != err) {
+ DBG("Could not wait on condition variable: %s",
strerror(err));
+ } else {
+ --sem->count;
+ ret = 0;
+ }
+ if (0 != (err = pthread_mutex_unlock(&sem->count_mutex))) {
+ errno = err;
+ ret = -1;
+ DBG("Could not unlock mutex: %s", strerror(err));
+ }
+ }
+ return ret;
+}
+
+int
+spd_sem_trywait(spd_sem_t *sem)
+{
+ int ret = -1, err;
+ if (sem->count > 0) {
+ if (0 != (err = pthread_mutex_trylock(&sem->count_mutex))) {
+ if (EBUSY == err) {
+ errno = EAGAIN;
+ } else {
+ DBG("Could not lock semaphore's mutex: %s",
strerror(err));
+ errno = err;
+ }
+ } else {
+ if (sem->count > 0) {
+ --sem->count;
+ ret = 0;
+ } else {
+ errno = EAGAIN;
+ }
+ if (0 != (err =
pthread_mutex_unlock(&sem->count_mutex))) {
+ DBG("Could not unlock semaphores's mutex: %s",
strerror(err));
+ }
+ }
+ } else {
+ errno = EAGAIN;
+ }
+ return ret;
+}
+
+int
+spd_sem_post(spd_sem_t *sem)
+{
+ int ret = -1, err;
+ if (0 != (err = pthread_mutex_lock(&sem->count_mutex))) {
+ DBG("Could not lock semaphore's mutex: %s", strerror(err));
+ errno = err;
+ } else {
+ ++sem->count;
+ ret = 0;
+ if (sem->count -1 == 0) {
+ /* someone was waiting for us */
+ if (0 != (err = pthread_cond_signal(&sem->nonzero))) {
+ DBG("Could not signal semaphore's condition
variable: %s", strerror(err));
+ errno = err;
+ ret = -1;
+ }
+ }
+ if (0 != (err = pthread_mutex_unlock(&sem->count_mutex))) {
+ DBG("Could not unlock semaphore's mutex");
+ }
+ }
+ return ret;
+}
+
+#else
+/* Do not change behavior on other OSes - use native unnamed semaphores there
*/
+#include <semaphore.h>
+
+struct spd_sem_s {
+ sem_t sem;
+};
+
+spd_sem_t*
+spd_sem_create(unsigned count)
+{
+ spd_sem_t *sem;
+ int err;
+
+ sem = (spd_sem_t *) malloc(sizeof(spd_sem_t));
+ if (sem == NULL) {
+ DBG("Could not allocate memory for semaphore");
+ errno = ENOMEM;
+ } else if (-1 == (err = sem_init(&sem->sem, 0, count))) {
+ free(sem);
+ sem = NULL;
+ }
+
+ return sem;
+}
+
+int
+spd_sem_destroy(spd_sem_t *sem)
+{
+ int ret = sem_destroy(&sem->sem);
+ free(sem);
+ return ret;
+}
+
+int
+spd_sem_wait(spd_sem_t *sem)
+{
+ return sem_wait(&sem->sem);
+}
+
+int
+spd_sem_trywait(spd_sem_t *sem)
+{
+ return sem_trywait(&sem->sem);
+}
+
+int
+spd_sem_post(spd_sem_t *sem)
+{
+ return sem_post(&sem->sem);
+}
+#endif
diff --git a/src/modules/cicero.c b/src/modules/cicero.c
index 7f8c4c9..a31338e 100644
--- a/src/modules/cicero.c
+++ b/src/modules/cicero.c
@@ -45,7 +45,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;
@@ -206,7 +206,7 @@ int module_init(char **status_info)
cicero_message = g_malloc(sizeof(char *));
*cicero_message = NULL;
- sem_init(&cicero_semaphore , 0, 0);
+ cicero_semaphore = spd_sem_create(0);
DBG("Cicero: creating new thread for cicero_tracking\n");
cicero_speaking = 0;
@@ -258,7 +258,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;
@@ -297,7 +297,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;
}
@@ -316,7 +316,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 c66223f..666dc38 100644
--- a/src/modules/dummy.c
+++ b/src/modules/dummy.c
@@ -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);
+ dummy_semaphore = spd_sem_create(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..5a324c2 100644
--- a/src/modules/espeak.c
+++ b/src/modules/espeak.c
@@ -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);
+ espeak_stop_or_pause_semaphore = spd_sem_create(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);
+ espeak_play_semaphore = spd_sem_create(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,8 +1088,8 @@ static void *_espeak_play(void *nothing)
espeak_stop_requested = TRUE;
espeak_pause_state =
ESPEAK_PAUSE_MARK_REPORTED;
- sem_post
- (&espeak_stop_or_pause_semaphore);
+ spd_sem_post
+ (espeak_stop_or_pause_semaphore);
finished = TRUE;
}
pthread_mutex_unlock(&espeak_state_mutex);
diff --git a/src/modules/festival.c b/src/modules/festival.c
index d6edb2f..f2b9fd2 100644
--- a/src/modules/festival.c
+++ b/src/modules/festival.c
@@ -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);
+ festival_semaphore = spd_sem_create(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..eff30f4 100644
--- a/src/modules/flite.c
+++ b/src/modules/flite.c
@@ -45,7 +45,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 +127,7 @@ int module_init(char **status_info)
flite_message = g_malloc(sizeof(char *));
*flite_message = NULL;
- sem_init(&flite_semaphore, 0, 0);
+ flite_semaphore = spd_sem_create(0);
DBG("Flite: creating new thread for flite_speak\n");
flite_speaking = 0;
@@ -179,7 +179,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 +231,7 @@ int module_close(void)
return -1;
g_free(flite_voice);
- sem_destroy(&flite_semaphore);
+ spd_sem_destroy(flite_semaphore);
return 0;
}
@@ -276,7 +276,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..c3d1dfa 100644
--- a/src/modules/generic.c
+++ b/src/modules/generic.c
@@ -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);
+ generic_semaphore = spd_sem_create(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..cfaed5d 100644
--- a/src/modules/ibmtts.c
+++ b/src/modules/ibmtts.c
@@ -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);
+ ibmtts_stop_or_pause_semaphore = spd_sem_create(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);
+ ibmtts_play_semaphore = spd_sem_create(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);
+ ibmtts_synth_semaphore = spd_sem_create(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..d37a796 100644
--- a/src/modules/ivona.c
+++ b/src/modules/ivona.c
@@ -53,7 +53,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 +147,7 @@ module_init(char **status_info)
ivona_message = g_malloc (sizeof (char*));
*ivona_message = NULL;
- sem_init(&ivona_semaphore, 0, 0);
+ ivona_semaphore = spd_sem_create(0);
DBG("Ivona: creating new thread for ivona_speak\n");
ivona_speaking = 0;
@@ -206,7 +206,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 +258,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 +426,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/module_utils.c b/src/modules/module_utils.c
index de81c79..c70b305 100644
--- a/src/modules/module_utils.c
+++ b/src/modules/module_utils.c
@@ -605,7 +605,7 @@ module_strip_punctuation_default(char *buf)
}
void
-module_speak_thread_wfork(sem_t *semaphore, pid_t *process_pid,
+module_speak_thread_wfork(spd_sem_t *semaphore, pid_t *process_pid,
TChildFunction child_function,
TParentFunction parent_function,
int *speaking_flag, char **message, const
SPDMessageType *msgtype,
@@ -619,7 +619,7 @@ module_speak_thread_wfork(sem_t *semaphore, pid_t
*process_pid,
set_speaking_thread_parameters();
while(1){
- sem_wait(semaphore);
+ spd_sem_wait(semaphore);
DBG("Semaphore on\n");
ret = pipe(module_pipe.pc);
diff --git a/src/modules/module_utils.h b/src/modules/module_utils.h
index 031692f..fac6e21 100644
--- a/src/modules/module_utils.h
+++ b/src/modules/module_utils.h
@@ -45,6 +45,7 @@
#include <speechd_types.h>
#include "spd_audio.h"
+#include <spd_utils.h>
int log_level;
@@ -209,7 +210,7 @@ typedef size_t(*TParentFunction) (TModuleDoublePipe dpipe,
const char *message,
const size_t maxlen, const char *dividers,
int *pause_requested);
-void module_speak_thread_wfork(sem_t * semaphore, pid_t * process_pid,
+void module_speak_thread_wfork(spd_sem_t * semaphore, pid_t * process_pid,
TChildFunction child_function,
TParentFunction parent_function,
int *speaking_flag, char **message,
diff --git a/src/modules/pico.c b/src/modules/pico.c
index 9df4622..e8d6e50 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 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);
+ pico_play_semaphore = spd_sem_create(0);
+ pico_idle_semaphore = spd_sem_create(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.7.5 (Apple Git-26)
- [PATCH 10/10] Native SSM output module for OS X, (continued)
- [PATCH 01/10] Remove references to TEMP_FAILURE_RETRY, Boris Dušek, 2012/07/23
- [PATCH 02/10] Do not exit on localization init fail, Boris Dušek, 2012/07/23
- [PATCH 03/10] Fix termination of threads in spd_close, Boris Dušek, 2012/07/23
- [PATCH 04/10] Cleanup execution of commands in dummy, Boris Dušek, 2012/07/23
- [PATCH 05/10] Fix audio module dynamic opening on OS X, Boris Dušek, 2012/07/23
- [PATCH 06/10] Use afplay and libao for playing sound on OS X in dummy, Boris Dušek, 2012/07/23
- [PATCH 07/10] Abstract unnamed semaphore interface and add OS X implementation,
Boris Dušek <=
- [PATCH 08/10] Use launchd on OS X for launching the server, Boris Dušek, 2012/07/23
- [PATCH 09/10] Installation instructions for OS X, Boris Dušek, 2012/07/23
- [PATCH 10/10] Native SSM output module for OS X, Boris Dušek, 2012/07/23