]> git.proxmox.com Git - qemu.git/blame - main-loop.c
main loop: use msec-based timeout in glib_select_fill
[qemu.git] / main-loop.c
CommitLineData
d3b12f5d
PB
1/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
d3b12f5d 24
0ec024f6 25#include "qemu-common.h"
d3b12f5d 26#include "qemu-timer.h"
0ec024f6
SW
27#include "slirp/slirp.h"
28#include "main-loop.h"
d3b12f5d
PB
29
30#ifndef _WIN32
31
0ec024f6
SW
32#include "compatfd.h"
33
d3b12f5d
PB
34static int io_thread_fd = -1;
35
36void qemu_notify_event(void)
37{
38 /* Write 8 bytes to be compatible with eventfd. */
39 static const uint64_t val = 1;
40 ssize_t ret;
41
42 if (io_thread_fd == -1) {
43 return;
44 }
45 do {
46 ret = write(io_thread_fd, &val, sizeof(val));
47 } while (ret < 0 && errno == EINTR);
48
49 /* EAGAIN is fine, a read must be pending. */
50 if (ret < 0 && errno != EAGAIN) {
51 fprintf(stderr, "qemu_notify_event: write() failed: %s\n",
52 strerror(errno));
53 exit(1);
54 }
55}
56
57static void qemu_event_read(void *opaque)
58{
59 int fd = (intptr_t)opaque;
60 ssize_t len;
61 char buffer[512];
62
63 /* Drain the notify pipe. For eventfd, only 8 bytes will be read. */
64 do {
65 len = read(fd, buffer, sizeof(buffer));
66 } while ((len == -1 && errno == EINTR) || len == sizeof(buffer));
67}
68
69static int qemu_event_init(void)
70{
71 int err;
72 int fds[2];
73
74 err = qemu_eventfd(fds);
75 if (err == -1) {
76 return -errno;
77 }
78 err = fcntl_setfl(fds[0], O_NONBLOCK);
79 if (err < 0) {
80 goto fail;
81 }
82 err = fcntl_setfl(fds[1], O_NONBLOCK);
83 if (err < 0) {
84 goto fail;
85 }
86 qemu_set_fd_handler2(fds[0], NULL, qemu_event_read, NULL,
87 (void *)(intptr_t)fds[0]);
88
89 io_thread_fd = fds[1];
90 return 0;
91
92fail:
93 close(fds[0]);
94 close(fds[1]);
95 return err;
96}
97
98/* If we have signalfd, we mask out the signals we want to handle and then
99 * use signalfd to listen for them. We rely on whatever the current signal
100 * handler is to dispatch the signals when we receive them.
101 */
102static void sigfd_handler(void *opaque)
103{
104 int fd = (intptr_t)opaque;
105 struct qemu_signalfd_siginfo info;
106 struct sigaction action;
107 ssize_t len;
108
109 while (1) {
110 do {
111 len = read(fd, &info, sizeof(info));
112 } while (len == -1 && errno == EINTR);
113
114 if (len == -1 && errno == EAGAIN) {
115 break;
116 }
117
118 if (len != sizeof(info)) {
119 printf("read from sigfd returned %zd: %m\n", len);
120 return;
121 }
122
123 sigaction(info.ssi_signo, NULL, &action);
124 if ((action.sa_flags & SA_SIGINFO) && action.sa_sigaction) {
125 action.sa_sigaction(info.ssi_signo,
126 (siginfo_t *)&info, NULL);
127 } else if (action.sa_handler) {
128 action.sa_handler(info.ssi_signo);
129 }
130 }
131}
132
133static int qemu_signal_init(void)
134{
135 int sigfd;
136 sigset_t set;
137
138 /*
139 * SIG_IPI must be blocked in the main thread and must not be caught
140 * by sigwait() in the signal thread. Otherwise, the cpu thread will
141 * not catch it reliably.
142 */
143 sigemptyset(&set);
144 sigaddset(&set, SIG_IPI);
d3b12f5d
PB
145 sigaddset(&set, SIGIO);
146 sigaddset(&set, SIGALRM);
147 sigaddset(&set, SIGBUS);
148 pthread_sigmask(SIG_BLOCK, &set, NULL);
149
4aa7534d 150 sigdelset(&set, SIG_IPI);
d3b12f5d
PB
151 sigfd = qemu_signalfd(&set);
152 if (sigfd == -1) {
153 fprintf(stderr, "failed to create signalfd\n");
154 return -errno;
155 }
156
157 fcntl_setfl(sigfd, O_NONBLOCK);
158
159 qemu_set_fd_handler2(sigfd, NULL, sigfd_handler, NULL,
160 (void *)(intptr_t)sigfd);
161
162 return 0;
163}
164
165#else /* _WIN32 */
166
ee77dfb2 167HANDLE qemu_event_handle = NULL;
d3b12f5d
PB
168
169static void dummy_event_handler(void *opaque)
170{
171}
172
173static int qemu_event_init(void)
174{
175 qemu_event_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
176 if (!qemu_event_handle) {
177 fprintf(stderr, "Failed CreateEvent: %ld\n", GetLastError());
178 return -1;
179 }
180 qemu_add_wait_object(qemu_event_handle, dummy_event_handler, NULL);
181 return 0;
182}
183
184void qemu_notify_event(void)
185{
ee77dfb2
MR
186 if (!qemu_event_handle) {
187 return;
188 }
d3b12f5d
PB
189 if (!SetEvent(qemu_event_handle)) {
190 fprintf(stderr, "qemu_notify_event: SetEvent failed: %ld\n",
191 GetLastError());
192 exit(1);
193 }
194}
195
196static int qemu_signal_init(void)
197{
198 return 0;
199}
200#endif
201
d34e8f6e 202int main_loop_init(void)
d3b12f5d
PB
203{
204 int ret;
205
206 qemu_mutex_lock_iothread();
207 ret = qemu_signal_init();
208 if (ret) {
209 return ret;
210 }
211
212 /* Note eventfd must be drained before signalfd handlers run */
213 ret = qemu_event_init();
214 if (ret) {
215 return ret;
216 }
217
218 return 0;
219}
220
221
222static GPollFD poll_fds[1024 * 2]; /* this is probably overkill */
223static int n_poll_fds;
224static int max_priority;
225
226static void glib_select_fill(int *max_fd, fd_set *rfds, fd_set *wfds,
4dae83ae 227 fd_set *xfds, int *cur_timeout)
d3b12f5d
PB
228{
229 GMainContext *context = g_main_context_default();
230 int i;
4dae83ae 231 int timeout = 0;
d3b12f5d
PB
232
233 g_main_context_prepare(context, &max_priority);
234
235 n_poll_fds = g_main_context_query(context, max_priority, &timeout,
236 poll_fds, ARRAY_SIZE(poll_fds));
237 g_assert(n_poll_fds <= ARRAY_SIZE(poll_fds));
238
239 for (i = 0; i < n_poll_fds; i++) {
240 GPollFD *p = &poll_fds[i];
241
242 if ((p->events & G_IO_IN)) {
243 FD_SET(p->fd, rfds);
244 *max_fd = MAX(*max_fd, p->fd);
245 }
246 if ((p->events & G_IO_OUT)) {
247 FD_SET(p->fd, wfds);
248 *max_fd = MAX(*max_fd, p->fd);
249 }
250 if ((p->events & G_IO_ERR)) {
251 FD_SET(p->fd, xfds);
252 *max_fd = MAX(*max_fd, p->fd);
253 }
254 }
255
4dae83ae
PB
256 if (timeout >= 0 && timeout < *cur_timeout) {
257 *cur_timeout = timeout;
d3b12f5d
PB
258 }
259}
260
261static void glib_select_poll(fd_set *rfds, fd_set *wfds, fd_set *xfds,
262 bool err)
263{
264 GMainContext *context = g_main_context_default();
265
266 if (!err) {
267 int i;
268
269 for (i = 0; i < n_poll_fds; i++) {
270 GPollFD *p = &poll_fds[i];
271
272 if ((p->events & G_IO_IN) && FD_ISSET(p->fd, rfds)) {
273 p->revents |= G_IO_IN;
274 }
275 if ((p->events & G_IO_OUT) && FD_ISSET(p->fd, wfds)) {
276 p->revents |= G_IO_OUT;
277 }
278 if ((p->events & G_IO_ERR) && FD_ISSET(p->fd, xfds)) {
279 p->revents |= G_IO_ERR;
280 }
281 }
282 }
283
284 if (g_main_context_check(context, max_priority, poll_fds, n_poll_fds)) {
285 g_main_context_dispatch(context);
286 }
287}
288
289#ifdef _WIN32
290/***********************************************************/
291/* Polling handling */
292
293typedef struct PollingEntry {
294 PollingFunc *func;
295 void *opaque;
296 struct PollingEntry *next;
297} PollingEntry;
298
299static PollingEntry *first_polling_entry;
300
301int qemu_add_polling_cb(PollingFunc *func, void *opaque)
302{
303 PollingEntry **ppe, *pe;
304 pe = g_malloc0(sizeof(PollingEntry));
305 pe->func = func;
306 pe->opaque = opaque;
307 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next);
308 *ppe = pe;
309 return 0;
310}
311
312void qemu_del_polling_cb(PollingFunc *func, void *opaque)
313{
314 PollingEntry **ppe, *pe;
315 for(ppe = &first_polling_entry; *ppe != NULL; ppe = &(*ppe)->next) {
316 pe = *ppe;
317 if (pe->func == func && pe->opaque == opaque) {
318 *ppe = pe->next;
319 g_free(pe);
320 break;
321 }
322 }
323}
324
325/***********************************************************/
326/* Wait objects support */
327typedef struct WaitObjects {
328 int num;
329 HANDLE events[MAXIMUM_WAIT_OBJECTS + 1];
330 WaitObjectFunc *func[MAXIMUM_WAIT_OBJECTS + 1];
331 void *opaque[MAXIMUM_WAIT_OBJECTS + 1];
332} WaitObjects;
333
334static WaitObjects wait_objects = {0};
335
336int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
337{
338 WaitObjects *w = &wait_objects;
339 if (w->num >= MAXIMUM_WAIT_OBJECTS) {
340 return -1;
341 }
342 w->events[w->num] = handle;
343 w->func[w->num] = func;
344 w->opaque[w->num] = opaque;
345 w->num++;
346 return 0;
347}
348
349void qemu_del_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
350{
351 int i, found;
352 WaitObjects *w = &wait_objects;
353
354 found = 0;
355 for (i = 0; i < w->num; i++) {
356 if (w->events[i] == handle) {
357 found = 1;
358 }
359 if (found) {
360 w->events[i] = w->events[i + 1];
361 w->func[i] = w->func[i + 1];
362 w->opaque[i] = w->opaque[i + 1];
363 }
364 }
365 if (found) {
366 w->num--;
367 }
368}
369
370static void os_host_main_loop_wait(int *timeout)
371{
372 int ret, ret2, i;
373 PollingEntry *pe;
374
375 /* XXX: need to suppress polling by better using win32 events */
376 ret = 0;
377 for (pe = first_polling_entry; pe != NULL; pe = pe->next) {
378 ret |= pe->func(pe->opaque);
379 }
380 if (ret == 0) {
381 int err;
382 WaitObjects *w = &wait_objects;
383
384 qemu_mutex_unlock_iothread();
385 ret = WaitForMultipleObjects(w->num, w->events, FALSE, *timeout);
386 qemu_mutex_lock_iothread();
387 if (WAIT_OBJECT_0 + 0 <= ret && ret <= WAIT_OBJECT_0 + w->num - 1) {
388 if (w->func[ret - WAIT_OBJECT_0]) {
389 w->func[ret - WAIT_OBJECT_0](w->opaque[ret - WAIT_OBJECT_0]);
390 }
391
392 /* Check for additional signaled events */
393 for (i = (ret - WAIT_OBJECT_0 + 1); i < w->num; i++) {
394 /* Check if event is signaled */
395 ret2 = WaitForSingleObject(w->events[i], 0);
396 if (ret2 == WAIT_OBJECT_0) {
397 if (w->func[i]) {
398 w->func[i](w->opaque[i]);
399 }
400 } else if (ret2 != WAIT_TIMEOUT) {
401 err = GetLastError();
402 fprintf(stderr, "WaitForSingleObject error %d %d\n", i, err);
403 }
404 }
405 } else if (ret != WAIT_TIMEOUT) {
406 err = GetLastError();
407 fprintf(stderr, "WaitForMultipleObjects error %d %d\n", ret, err);
408 }
409 }
410
411 *timeout = 0;
412}
413#else
414static inline void os_host_main_loop_wait(int *timeout)
415{
416}
417#endif
418
419int main_loop_wait(int nonblocking)
420{
421 fd_set rfds, wfds, xfds;
422 int ret, nfds;
423 struct timeval tv;
424 int timeout;
425
426 if (nonblocking) {
427 timeout = 0;
428 } else {
429 timeout = qemu_calculate_timeout();
430 qemu_bh_update_timeout(&timeout);
431 }
432
d3b12f5d
PB
433 /* poll any events */
434 /* XXX: separate device handlers from system ones */
435 nfds = -1;
436 FD_ZERO(&rfds);
437 FD_ZERO(&wfds);
438 FD_ZERO(&xfds);
439
440#ifdef CONFIG_SLIRP
441 slirp_select_fill(&nfds, &rfds, &wfds, &xfds);
442#endif
443 qemu_iohandler_fill(&nfds, &rfds, &wfds, &xfds);
4dae83ae
PB
444
445 glib_select_fill(&nfds, &rfds, &wfds, &xfds, &timeout);
446 os_host_main_loop_wait(&timeout);
447
448 tv.tv_sec = timeout / 1000;
449 tv.tv_usec = (timeout % 1000) * 1000;
d3b12f5d
PB
450
451 if (timeout > 0) {
452 qemu_mutex_unlock_iothread();
453 }
454
455 ret = select(nfds + 1, &rfds, &wfds, &xfds, &tv);
456
457 if (timeout > 0) {
458 qemu_mutex_lock_iothread();
459 }
460
461 glib_select_poll(&rfds, &wfds, &xfds, (ret < 0));
462 qemu_iohandler_poll(&rfds, &wfds, &xfds, ret);
463#ifdef CONFIG_SLIRP
464 slirp_select_poll(&rfds, &wfds, &xfds, (ret < 0));
465#endif
466
467 qemu_run_all_timers();
468
469 /* Check bottom-halves last in case any of the earlier events triggered
470 them. */
471 qemu_bh_poll();
472
473 return ret;
474}