]> code.delx.au - pulseaudio/blob - src/polyp/mainloop-signal.c
* rename "latency correction" to "write index correction"
[pulseaudio] / src / polyp / mainloop-signal.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
10
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; 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 <stdio.h>
27 #include <assert.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34
35 #ifdef HAVE_WINDOWS_H
36 #include <windows.h>
37 #endif
38
39 #include <polypcore/util.h>
40 #include <polypcore/xmalloc.h>
41 #include <polypcore/log.h>
42 #include <polypcore/gccmacro.h>
43
44 #include "mainloop-signal.h"
45
46 struct pa_signal_event {
47 int sig;
48 #ifdef HAVE_SIGACTION
49 struct sigaction saved_sigaction;
50 #else
51 void (*saved_handler)(int sig);
52 #endif
53 void (*callback) (pa_mainloop_api*a, pa_signal_event *e, int sig, void *userdata);
54 void *userdata;
55 void (*destroy_callback) (pa_mainloop_api*a, pa_signal_event*e, void *userdata);
56 pa_signal_event *previous, *next;
57 };
58
59 static pa_mainloop_api *api = NULL;
60 static int signal_pipe[2] = { -1, -1 };
61 static pa_io_event* io_event = NULL;
62 static pa_defer_event *defer_event = NULL;
63 static pa_signal_event *signals = NULL;
64
65 #ifdef OS_IS_WIN32
66 static unsigned int waiting_signals = 0;
67 static CRITICAL_SECTION crit;
68 #endif
69
70 static void signal_handler(int sig) {
71 #ifndef HAVE_SIGACTION
72 signal(sig, signal_handler);
73 #endif
74 write(signal_pipe[1], &sig, sizeof(sig));
75
76 #ifdef OS_IS_WIN32
77 EnterCriticalSection(&crit);
78 waiting_signals++;
79 LeaveCriticalSection(&crit);
80 #endif
81 }
82
83 static void dispatch(pa_mainloop_api*a, int sig) {
84 pa_signal_event*s;
85
86 for (s = signals; s; s = s->next)
87 if (s->sig == sig) {
88 assert(s->callback);
89 s->callback(a, s, sig, s->userdata);
90 break;
91 }
92 }
93
94 #ifdef OS_IS_WIN32
95 static void defer(pa_mainloop_api*a, PA_GCC_UNUSED pa_defer_event*e, PA_GCC_UNUSED void *userdata) {
96 ssize_t r;
97 int sig;
98 unsigned int sigs;
99
100 EnterCriticalSection(&crit);
101 sigs = waiting_signals;
102 waiting_signals = 0;
103 LeaveCriticalSection(&crit);
104
105 while (sigs) {
106 if ((r = read(signal_pipe[0], &sig, sizeof(sig))) < 0) {
107 pa_log(__FILE__": read(): %s", strerror(errno));
108 return;
109 }
110
111 if (r != sizeof(sig)) {
112 pa_log(__FILE__": short read()");
113 return;
114 }
115
116 dispatch(a, sig);
117
118 sigs--;
119 }
120 }
121 #endif
122
123 static void callback(pa_mainloop_api*a, pa_io_event*e, int fd, pa_io_event_flags_t f, PA_GCC_UNUSED void *userdata) {
124 ssize_t r;
125 int sig;
126 assert(a && e && f == PA_IO_EVENT_INPUT && e == io_event && fd == signal_pipe[0]);
127
128
129 if ((r = read(signal_pipe[0], &sig, sizeof(sig))) < 0) {
130 if (errno == EAGAIN)
131 return;
132
133 pa_log(__FILE__": read(): %s", strerror(errno));
134 return;
135 }
136
137 if (r != sizeof(sig)) {
138 pa_log(__FILE__": short read()");
139 return;
140 }
141
142 dispatch(a, sig);
143 }
144
145 int pa_signal_init(pa_mainloop_api *a) {
146 assert(!api && a && signal_pipe[0] == -1 && signal_pipe[1] == -1 && !io_event && !defer_event);
147
148 #ifdef OS_IS_WIN32
149 if (_pipe(signal_pipe, 200, _O_BINARY) < 0) {
150 #else
151 if (pipe(signal_pipe) < 0) {
152 #endif
153 pa_log(__FILE__": pipe() failed: %s", strerror(errno));
154 return -1;
155 }
156
157 pa_make_nonblock_fd(signal_pipe[0]);
158 pa_make_nonblock_fd(signal_pipe[1]);
159 pa_fd_set_cloexec(signal_pipe[0], 1);
160 pa_fd_set_cloexec(signal_pipe[1], 1);
161
162 api = a;
163
164 #ifndef OS_IS_WIN32
165 io_event = api->io_new(api, signal_pipe[0], PA_IO_EVENT_INPUT, callback, NULL);
166 assert(io_event);
167 #else
168 defer_event = api->defer_new(api, defer, NULL);
169 assert(defer_event);
170
171 InitializeCriticalSection(&crit);
172 #endif
173
174 return 0;
175 }
176
177 void pa_signal_done(void) {
178 assert(api && signal_pipe[0] >= 0 && signal_pipe[1] >= 0 && (io_event || defer_event));
179
180 while (signals)
181 pa_signal_free(signals);
182
183 #ifndef OS_IS_WIN32
184 api->io_free(io_event);
185 io_event = NULL;
186 #else
187 api->defer_free(defer_event);
188 defer_event = NULL;
189
190 DeleteCriticalSection(&crit);
191 #endif
192
193 close(signal_pipe[0]);
194 close(signal_pipe[1]);
195 signal_pipe[0] = signal_pipe[1] = -1;
196
197 api = NULL;
198 }
199
200 pa_signal_event* pa_signal_new(int sig, void (*_callback) (pa_mainloop_api *api, pa_signal_event*e, int sig, void *userdata), void *userdata) {
201 pa_signal_event *e = NULL;
202
203 #ifdef HAVE_SIGACTION
204 struct sigaction sa;
205 #endif
206
207 assert(sig > 0 && _callback);
208
209 for (e = signals; e; e = e->next)
210 if (e->sig == sig)
211 goto fail;
212
213 e = pa_xmalloc(sizeof(pa_signal_event));
214 e->sig = sig;
215 e->callback = _callback;
216 e->userdata = userdata;
217 e->destroy_callback = NULL;
218
219 #ifdef HAVE_SIGACTION
220 memset(&sa, 0, sizeof(sa));
221 sa.sa_handler = signal_handler;
222 sigemptyset(&sa.sa_mask);
223 sa.sa_flags = SA_RESTART;
224
225 if (sigaction(sig, &sa, &e->saved_sigaction) < 0)
226 #else
227 if ((e->saved_handler = signal(sig, signal_handler)) == SIG_ERR)
228 #endif
229 goto fail;
230
231 e->previous = NULL;
232 e->next = signals;
233 signals = e;
234
235 return e;
236 fail:
237 if (e)
238 pa_xfree(e);
239 return NULL;
240 }
241
242 void pa_signal_free(pa_signal_event *e) {
243 assert(e);
244
245 if (e->next)
246 e->next->previous = e->previous;
247 if (e->previous)
248 e->previous->next = e->next;
249 else
250 signals = e->next;
251
252 #ifdef HAVE_SIGACTION
253 sigaction(e->sig, &e->saved_sigaction, NULL);
254 #else
255 signal(e->sig, e->saved_handler);
256 #endif
257
258 if (e->destroy_callback)
259 e->destroy_callback(api, e, e->userdata);
260
261 pa_xfree(e);
262 }
263
264 void pa_signal_set_destroy(pa_signal_event *e, void (*_callback) (pa_mainloop_api *api, pa_signal_event*e, void *userdata)) {
265 assert(e);
266 e->destroy_callback = _callback;
267 }