]> git.meshlink.io Git - catta/blob - avahi-common/thread-watch.c
4f236081d9c55f2cdfa52ca9cce5bffead6683a7
[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     p->thread_running = 1;
140
141     return 0;
142 }
143
144 int avahi_threaded_poll_stop(AvahiThreadedPoll *p) {
145     assert(p);
146
147     if (!p->thread_running)
148         return -1;
149
150     /* Make sure that this function is not called from the helper thread */
151     assert(!pthread_equal(pthread_self(), p->thread_id));
152
153     pthread_mutex_lock(&p->mutex);
154     avahi_simple_poll_quit(p->simple_poll);
155     pthread_mutex_unlock(&p->mutex);
156
157     pthread_join(p->thread_id, NULL);
158     p->thread_running = 0;
159
160     return p->retval;
161 }
162
163 void avahi_threaded_poll_quit(AvahiThreadedPoll *p) {
164     assert(p);
165
166     /* Make sure that this function is called from the helper thread */
167     assert(pthread_equal(pthread_self(), p->thread_id));
168
169     avahi_simple_poll_quit(p->simple_poll);
170 }
171
172 void avahi_threaded_poll_lock(AvahiThreadedPoll *p) {
173     assert(p);
174
175     /* Make sure that this function is not called from the helper thread */
176     assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
177
178     pthread_mutex_lock(&p->mutex);
179 }
180
181 void avahi_threaded_poll_unlock(AvahiThreadedPoll *p) {
182     assert(p);
183
184     /* Make sure that this function is not called from the helper thread */
185     assert(!p->thread_running || !pthread_equal(pthread_self(), p->thread_id));
186
187     pthread_mutex_unlock(&p->mutex);
188 }