speechd-discuss
[Top][All Lists]
Advanced

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

[PATCH 3/3] Abstract unnamed semaphore


From: Boris Dušek
Subject: [PATCH 3/3] Abstract unnamed semaphore
Date: Tue, 24 Jul 2012 22:08:33 +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        |   14 +++++++-
 src/clients/say/say.c      |   15 ++++----
 src/common/Makefile.am     |    2 +-
 src/common/spd_sem.c       |   81 ++++++++++++++++++++++++++++++++++++++++++++
 src/modules/cicero.c       |   10 +++---
 src/modules/dummy.c        |   10 +++---
 src/modules/espeak.c       |   34 +++++++++---------
 src/modules/festival.c     |   12 +++---
 src/modules/flite.c        |   12 +++---
 src/modules/generic.c      |   12 +++---
 src/modules/ibmtts.c       |   50 +++++++++++++-------------
 src/modules/ivona.c        |   12 +++---
 src/modules/module_utils.c |    6 ++--
 src/modules/module_utils.h |    5 ++-
 src/modules/pico.c         |   28 +++++++-------
 15 files changed, 199 insertions(+), 104 deletions(-)
 create mode 100644 src/common/spd_sem.c

diff --git a/include/spd_utils.h b/include/spd_utils.h
index f07d13a..e5a79e6 100644
--- a/include/spd_utils.h
+++ b/include/spd_utils.h
@@ -2,7 +2,7 @@
  * spd_utils.h - prototypes for utility functions used
  * in the Speech Dispatcher server and modules.
  *
- * 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 General Public License as published by
@@ -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..979326f 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
@@ -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..0d7528c
--- /dev/null
+++ b/src/common/spd_sem.c
@@ -0,0 +1,81 @@
+/*
+ * 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 DBG
+#define DBG(...)
+#endif
+
+#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);
+}
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 28fd5e4..16c893b 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..5d2904f 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
@@ -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..2c9e591 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
@@ -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..eb8eb8c 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
@@ -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..e6ec08f 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
@@ -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..c1a23ca 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
@@ -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..7b8b131 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
@@ -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..7b7335e 100644
--- a/src/modules/module_utils.c
+++ b/src/modules/module_utils.c
@@ -1,7 +1,7 @@
 /*
  * module_utils.c - Module utilities
  *           Functions to help writing output modules for Speech Dispatcher
- * Copyright (C) 2003,2006, 2007 Brailcom, o.p.s.
+ * Copyright (C) 2003,2006, 2007, 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 the Free
@@ -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..10082ad 100644
--- a/src/modules/module_utils.h
+++ b/src/modules/module_utils.h
@@ -1,7 +1,7 @@
 /*
  * module_utils.h - Module utilities
  *           Functions to help writing output modules for Speech Dispatcher
- * Copyright (C) 2003, 2004, 2007 Brailcom, o.p.s.
+ * Copyright (C) 2003, 2004, 2007, 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 the Free
@@ -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..d6494ad 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
@@ -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)




reply via email to

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