summaryrefslogtreecommitdiff
path: root/sd
diff options
context:
space:
mode:
authorSiqi <me@siqi.fr>2013-07-26 12:53:19 +0200
committerSiqi <me@siqi.fr>2013-07-26 12:54:48 +0200
commitaf1ce3de97a6c1d583f82558ee18054b7917b03d (patch)
tree7ffb2a0de2e524133859f966454de139f4afc89a /sd
parent02931dc8c7ca01c536a12e213b3d728632d5d4c0 (diff)
add threaded pool support for avahi backward compatibility
As suggested by Avahi maintainer, we should ship threadedpool support with the build since it's not availble to avahi priori to 0.6.3 This keeps the avahi support backward compatible. Change-Id: I70db9d5b3be3fba744e40c9bb183307819b7df4e
Diffstat (limited to 'sd')
-rw-r--r--sd/Library_sd.mk8
-rw-r--r--sd/source/ui/remotecontrol/AvahiNetworkService.cxx2
-rw-r--r--sd/source/ui/remotecontrol/avahi-common/thread-watch.c191
-rw-r--r--sd/source/ui/remotecontrol/avahi-common/thread-watch.h82
4 files changed, 282 insertions, 1 deletions
diff --git a/sd/Library_sd.mk b/sd/Library_sd.mk
index 807926fa6994..848442355fbd 100644
--- a/sd/Library_sd.mk
+++ b/sd/Library_sd.mk
@@ -553,6 +553,14 @@ ifeq ($(ENABLE_AVAHI),TRUE)
$(eval $(call gb_Library_add_exception_objects,sd,\
sd/source/ui/remotecontrol/AvahiNetworkService \
))
+
+$(eval $(call gb_Library_add_cobjects,sd,\
+ sd/source/ui/remotecontrol/avahi-common/thread-watch \
+))
+
+$(eval $(call gb_Library_add_libs,sd,\
+ -lpthread \
+))
endif
$(eval $(call gb_Library_add_exception_objects,sd,\
diff --git a/sd/source/ui/remotecontrol/AvahiNetworkService.cxx b/sd/source/ui/remotecontrol/AvahiNetworkService.cxx
index 5cb52de68749..6043d49b2fbb 100644
--- a/sd/source/ui/remotecontrol/AvahiNetworkService.cxx
+++ b/sd/source/ui/remotecontrol/AvahiNetworkService.cxx
@@ -10,7 +10,7 @@
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>
#include <avahi-common/timeval.h>
-#include <avahi-common/thread-watch.h>
+#include "avahi-common/thread-watch.h"
#include "AvahiNetworkService.hxx"
#include "ZeroconfService.hxx"
diff --git a/sd/source/ui/remotecontrol/avahi-common/thread-watch.c b/sd/source/ui/remotecontrol/avahi-common/thread-watch.c
new file mode 100644
index 000000000000..4bb277f53ee0
--- /dev/null
+++ b/sd/source/ui/remotecontrol/avahi-common/thread-watch.c
@@ -0,0 +1,191 @@
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi 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 Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi 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 Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <sys/poll.h>
+#include <assert.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <pthread.h>
+#include <signal.h>
+
+#include <avahi-common/llist.h>
+#include <avahi-common/malloc.h>
+#include <avahi-common/error.h>
+#include <avahi-common/timeval.h>
+#include <avahi-common/simple-watch.h>
+
+// Only introduced in 0.6.13. Place it here as advised by avahi maintainer
+#include "thread-watch.h"
+
+struct AvahiThreadedPoll {
+ AvahiSimplePoll *simple_poll;
+ pthread_t thread_id;
+ pthread_mutex_t mutex;
+ int thread_running;
+ int retval;
+};
+
+static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
+ pthread_mutex_t *mutex = userdata;
+ int r;
+
+ /* Before entering poll() we unlock the mutex, so that
+ * avahi_simple_poll_quit() can succeed from another thread. */
+
+ pthread_mutex_unlock(mutex);
+ r = poll(ufds, nfds, timeout);
+ pthread_mutex_lock(mutex);
+
+ return r;
+}
+
+static void* thread(void *userdata){
+ AvahiThreadedPoll *p = userdata;
+ sigset_t mask;
+
+ /* Make sure that signals are delivered to the main thread */
+ sigfillset(&mask);
+ pthread_sigmask(SIG_BLOCK, &mask, NULL);
+
+ pthread_mutex_lock(&p->mutex);
+ p->retval = avahi_simple_poll_loop(p->simple_poll);
+ pthread_mutex_unlock(&p->mutex);
+
+ return NULL;
+}
+
+AvahiThreadedPoll *avahi_threaded_poll_new(void) {
+ AvahiThreadedPoll *p;
+
+ if (!(p = avahi_new(AvahiThreadedPoll, 1)))
+ goto fail; /* OOM */
+
+ if (!(p->simple_poll = avahi_simple_poll_new()))
+ goto fail;
+
+ pthread_mutex_init(&p->mutex, NULL);
+
+ avahi_simple_poll_set_func(p->simple_poll, poll_func, &p->mutex);
+
+ p->thread_running = 0;
+
+ return p;
+
+fail:
+ if (p) {
+ if (p->simple_poll) {
+ avahi_simple_poll_free(p->simple_poll);
+ pthread_mutex_destroy(&p->mutex);
+ }
+
+ avahi_free(p);
+ }
+
+ return NULL;
+}
+
+void avahi_threaded_poll_free(AvahiThreadedPoll *p) {
+ assert(p);
+
+ /* Make sure that this function is not called from the helper thread */
+ assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
+
+ if (p->thread_running)
+ avahi_threaded_poll_stop(p);
+
+ if (p->simple_poll)
+ avahi_simple_poll_free(p->simple_poll);
+
+ pthread_mutex_destroy(&p->mutex);
+ avahi_free(p);
+}
+
+const AvahiPoll* avahi_threaded_poll_get(AvahiThreadedPoll *p) {
+ assert(p);
+
+ return avahi_simple_poll_get(p->simple_poll);
+}
+
+int avahi_threaded_poll_start(AvahiThreadedPoll *p) {
+ assert(p);
+
+ assert(!p->thread_running);
+
+ if (pthread_create(&p->thread_id, NULL, thread, p) < 0)
+ return -1;
+
+ p->thread_running = 1;
+
+ return 0;
+}
+
+int avahi_threaded_poll_stop(AvahiThreadedPoll *p) {
+ assert(p);
+
+ if (!p->thread_running)
+ return -1;
+
+ /* Make sure that this function is not called from the helper thread */
+ assert(!pthread_equal(pthread_self(), p->thread_id));
+
+ pthread_mutex_lock(&p->mutex);
+ avahi_simple_poll_quit(p->simple_poll);
+ pthread_mutex_unlock(&p->mutex);
+
+ pthread_join(p->thread_id, NULL);
+ p->thread_running = 0;
+
+ return p->retval;
+}
+
+void avahi_threaded_poll_quit(AvahiThreadedPoll *p) {
+ assert(p);
+
+ /* Make sure that this function is called from the helper thread */
+ assert(pthread_equal(pthread_self(), p->thread_id));
+
+ avahi_simple_poll_quit(p->simple_poll);
+}
+
+void avahi_threaded_poll_lock(AvahiThreadedPoll *p) {
+ assert(p);
+
+ /* Make sure that this function is not called from the helper thread */
+ assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
+
+ pthread_mutex_lock(&p->mutex);
+}
+
+void avahi_threaded_poll_unlock(AvahiThreadedPoll *p) {
+ assert(p);
+
+ /* Make sure that this function is not called from the helper thread */
+ assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
+
+ pthread_mutex_unlock(&p->mutex);
+} \ No newline at end of file
diff --git a/sd/source/ui/remotecontrol/avahi-common/thread-watch.h b/sd/source/ui/remotecontrol/avahi-common/thread-watch.h
new file mode 100644
index 000000000000..0f0a12d490bc
--- /dev/null
+++ b/sd/source/ui/remotecontrol/avahi-common/thread-watch.h
@@ -0,0 +1,82 @@
+#ifndef foothreadedwatchhfoo
+#define foothreadedwatchhfoo
+
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi 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 Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi 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 Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+/** \file thread-watch.h Threaded poll() based main loop implementation */
+
+#include <sys/poll.h>
+#include <avahi-common/cdecl.h>
+#include <avahi-common/watch.h>
+
+AVAHI_C_DECL_BEGIN
+
+/** A main loop object that runs an AvahiSimplePoll in its own thread. \since 0.6.4 */
+typedef struct AvahiThreadedPoll AvahiThreadedPoll;
+
+/** Create a new event loop object. This will allocate the internal
+ * AvahiSimplePoll, but will not start the helper thread. \since 0.6.4 */
+AvahiThreadedPoll *avahi_threaded_poll_new(void);
+
+/** Free an event loop object. This will stop the associated event loop
+ * thread (if it is running). \since 0.6.4 */
+void avahi_threaded_poll_free(AvahiThreadedPoll *p);
+
+/** Return the abstracted poll API object for this event loop
+ * object. The will return the same pointer each time it is
+ * called. \since 0.6.4 */
+const AvahiPoll* avahi_threaded_poll_get(AvahiThreadedPoll *p);
+
+/** Start the event loop helper thread. After the thread has started
+ * you must make sure to access the event loop object
+ * (AvahiThreadedPoll, AvahiPoll and all its associated objects)
+ * synchronized, i.e. with proper locking. You may want to use
+ * avahi_threaded_poll_lock()/avahi_threaded_poll_unlock() for this,
+ * which will lock the the entire event loop. Please note that event
+ * loop callback functions are called from the event loop helper thread
+ * with that lock held, i.e. avahi_threaded_poll_lock() calls are not
+ * required from event callbacks. \since 0.6.4 */
+int avahi_threaded_poll_start(AvahiThreadedPoll *p);
+
+/** Request that the event loop quits and the associated thread
+ stops. Call this from outside the helper thread if you want to shut
+ it down. \since 0.6.4 */
+int avahi_threaded_poll_stop(AvahiThreadedPoll *p);
+
+/** Request that the event loop quits and the associated thread
+ stops. Call this from inside the helper thread if you want to shut it
+ down. \since 0.6.4 */
+void avahi_threaded_poll_quit(AvahiThreadedPoll *p);
+
+/** Lock the main loop object. Use this if you want to access the event
+ * loop objects (such as creating a new event source) from anything
+ * else but the event loop helper thread, i.e. from anything else but event
+ * loop callbacks \since 0.6.4 */
+void avahi_threaded_poll_lock(AvahiThreadedPoll *p);
+
+/** Unlock the event loop object, use this as counterpart to
+ * avahi_threaded_poll_lock() \since 0.6.4 */
+void avahi_threaded_poll_unlock(AvahiThreadedPoll *p);
+
+AVAHI_C_DECL_END
+
+#endif \ No newline at end of file