]> git.meshlink.io Git - catta/blob - avahi-common/thread-watch.c
call _stop() instead of _quit() in _free(), to avoid hitting an assert()
[catta] / avahi-common / thread-watch.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/poll.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <pthread.h>
34 #include <signal.h>
35
36 #include "llist.h"
37 #include "malloc.h"
38 #include "timeval.h"
39 #include "simple-watch.h"
40 #include "thread-watch.h"
41
42 struct AvahiThreadedPoll {
43     AvahiSimplePoll *simple_poll;
44     pthread_t thread_id;
45     pthread_mutex_t mutex;
46     int thread_running;
47     int retval;
48 };
49
50 static int poll_func(struct pollfd *ufds, unsigned int nfds, int timeout, void *userdata) {
51     pthread_mutex_t *mutex = userdata;
52     int r;
53
54     /* Before entering poll() we unlock the mutex, so that
55      * avahi_simple_poll_quit() can succeed from another thread. */
56     
57     pthread_mutex_unlock(mutex);
58     r = poll(ufds, nfds, timeout);
59     pthread_mutex_lock(mutex);
60
61     return r;
62 }
63
64 static void* thread(void *userdata){
65     AvahiThreadedPoll *p = userdata;
66     sigset_t mask;
67
68     /* Make sure that signals are delivered to the main thread */
69     sigfillset(&mask);
70     pthread_sigmask(SIG_BLOCK, &mask, NULL);
71     
72     pthread_mutex_lock(&p->mutex);
73     p->retval = avahi_simple_poll_loop(p->simple_poll);
74     pthread_mutex_unlock(&p->mutex);
75     
76     return NULL;
77 }
78
79 AvahiThreadedPoll *avahi_threaded_poll_new(void) {
80     AvahiThreadedPoll *p;
81
82     if (!(p = avahi_new(AvahiThreadedPoll, 1)))
83         goto fail; /* OOM */
84     
85     if (!(p->simple_poll = avahi_simple_poll_new()))
86         goto fail;
87         
88     pthread_mutex_init(&p->mutex, NULL);
89
90     avahi_simple_poll_set_func(p->simple_poll, poll_func, &p->mutex);
91     
92     p->thread_running = 0;
93
94     return p;
95
96 fail:
97     if (p) {
98         if (p->simple_poll) {
99             avahi_simple_poll_free(p->simple_poll);
100             pthread_mutex_destroy(&p->mutex);
101         }
102
103         avahi_free(p);
104     }
105
106     return NULL;
107 }
108
109 void avahi_threaded_poll_free(AvahiThreadedPoll *p) {
110     assert(p);
111
112     /* Make sure that this function is not called from the helper thread */
113     assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
114
115     if (p->thread_running)
116         avahi_threaded_poll_stop(p);
117
118     if (p->simple_poll)
119         avahi_simple_poll_free(p->simple_poll);
120
121     pthread_mutex_destroy(&p->mutex);
122     avahi_free(p);
123 }
124
125 const AvahiPoll* avahi_threaded_poll_get(AvahiThreadedPoll *p) {
126     assert(p);
127
128     return avahi_simple_poll_get(p->simple_poll);
129 }
130
131 int avahi_threaded_poll_start(AvahiThreadedPoll *p) {
132     assert(p);
133
134     assert(!p->thread_running);
135
136     if (pthread_create(&p->thread_id, NULL, thread, p) < 0)
137         return -1;
138
139     return 0;
140 }
141
142 int avahi_threaded_poll_stop(AvahiThreadedPoll *p) {
143     assert(p);
144
145     if (!p->thread_running)
146         return -1;
147
148     /* Make sure that this function is not called from the helper thread */
149     assert(!pthread_equal(pthread_self(), p->thread_id));
150
151     pthread_mutex_lock(&p->mutex);
152     avahi_simple_poll_quit(p->simple_poll);
153     pthread_mutex_unlock(&p->mutex);
154     
155     pthread_join(p->thread_id, NULL);
156     p->thread_running = 0;
157
158     return p->retval;
159 }
160
161 void avahi_threaded_poll_quit(AvahiThreadedPoll *p) {
162     assert(p);
163
164     /* Make sure that this function is called from the helper thread */
165     assert(pthread_equal(pthread_self(), p->thread_id));
166
167     avahi_simple_poll_quit(p->simple_poll);
168 }
169
170 void avahi_threaded_poll_lock(AvahiThreadedPoll *p) {
171     assert(p);
172
173     /* Make sure that this function is not called from the helper thread */
174     assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
175     
176     pthread_mutex_lock(&p->mutex);
177 }
178
179 void avahi_threaded_poll_unlock(AvahiThreadedPoll *p) {
180     assert(p);
181
182     /* Make sure that this function is not called from the helper thread */
183     assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
184     
185     pthread_mutex_unlock(&p->mutex);
186 }