1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
/***
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 <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 "llist.h"
#include "malloc.h"
#include "timeval.h"
#include "simple-watch.h"
#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);
}
|